Skip to content

Commit

Permalink
Support 1.20.2
Browse files Browse the repository at this point in the history
  • Loading branch information
Ampflower committed Sep 21, 2023
1 parent 2551be0 commit a4e1006
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 5 deletions.
2 changes: 1 addition & 1 deletion gradle.properties
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ modrinthId=G9eJHDO2
# Minecraft
minecraftVersion=1.20.1
minecraftRequired=1.19.3
minecraftCompatible=1.19.3,1.19.4,1.20,1.20.1
minecraftCompatible=1.19.3,1.19.4,1.20,1.20.1,1.20.2
yarnMappings=1.20.1+build.5
loaderVersion=0.14.17
fabricApiVersion=0.83.1+1.20.1
Expand Down
48 changes: 46 additions & 2 deletions src/main/java/gay/ampflower/polysit/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import eu.pb4.polymer.core.api.entity.PolymerEntityUtils;
import net.fabricmc.fabric.api.command.v2.CommandRegistrationCallback;
import net.fabricmc.fabric.api.event.player.UseBlockCallback;
import net.minecraft.SharedConstants;
import net.minecraft.block.*;
import net.minecraft.block.enums.BedPart;
import net.minecraft.block.enums.BlockHalf;
Expand Down Expand Up @@ -45,8 +46,51 @@ public class Main {
private static final Logger logger = LogUtils.getLogger();

public static final double HORIZONTAL_CENTER_OFFSET = 0.5D;
public static final double VERTICAL_SLAB_OFFSET = 0.35D;
public static final double VERTICAL_FENCE_OFFSET = 0.75D;
public static final double UPDATE_HEIGHT_OFFSET = 0.25D;
public static final double VERTICAL_SLAB_OFFSET;
public static final double VERTICAL_FENCE_OFFSET;

static final String VERSION_TAG_NAME = "polysit:runtimeVersion";
static final int RUNTIME_VERSION;
private static final double[] OFFSET_DELTA = { 0, UPDATE_HEIGHT_OFFSET };

static {
final var currentVersion = SharedConstants.getGameVersion().getSaveVersion().getId();

// No need to pollute the class fields.
final double verticalSlabOffset = 0.35D;
final double verticalFenceOffset = 0.75D;
final int updateChangingOffset = 3572; // 1.20.2-pre.1; <=23w35a are no-boots

// Adjusts offset for >1.20.2-rc.1.
if (currentVersion >= updateChangingOffset) {
VERTICAL_SLAB_OFFSET = verticalSlabOffset + UPDATE_HEIGHT_OFFSET;
VERTICAL_FENCE_OFFSET = verticalFenceOffset + UPDATE_HEIGHT_OFFSET;
RUNTIME_VERSION = 1;
} else {
VERTICAL_SLAB_OFFSET = verticalSlabOffset;
VERTICAL_FENCE_OFFSET = verticalFenceOffset;
RUNTIME_VERSION = 0;
}
}

static double delta(int runtimeVersion) {
if (runtimeVersion > RUNTIME_VERSION) {
return -sum(OFFSET_DELTA, RUNTIME_VERSION + 1, runtimeVersion);
}
if (runtimeVersion < RUNTIME_VERSION) {
return sum(OFFSET_DELTA, runtimeVersion + 1, RUNTIME_VERSION);
}
return 0D;
}

private static double sum(double[] array, int from, int to) {
double sum = 0D;
for (; from <= to; from++) {
sum += array[from];
}
return sum;
}

/** Seat entity type. Disallows manual summoning, makes fire immune. */
public static EntityType<SeatEntity> SEAT = registerEntity("polysit:seat",
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/gay/ampflower/polysit/SeatEntity.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,18 @@ protected void initDataTracker() {

@Override
protected void readCustomDataFromNbt(NbtCompound nbt) {

// Avoids setting position on entity init
final var version = nbt.getInt(Main.VERSION_TAG_NAME);
if (version != Main.RUNTIME_VERSION) {
this.setPos(this.getX(), this.getY() + Main.delta(version), this.getZ());
// Required to suppress the packet
this.resetPosition();
}
}

@Override
protected void writeCustomDataToNbt(NbtCompound nbt) {

nbt.putInt(Main.VERSION_TAG_NAME, Main.RUNTIME_VERSION);
}

/** Only save if being ridden. */
Expand Down

0 comments on commit a4e1006

Please sign in to comment.