Skip to content

Commit

Permalink
fix: version bump to 4 with minestom 1.20.2 nbt changes
Browse files Browse the repository at this point in the history
  • Loading branch information
mworzala committed Nov 30, 2023
1 parent c5bf761 commit b36043a
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 9 deletions.
6 changes: 4 additions & 2 deletions build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,9 @@ repositories {
}

dependencies {
compileOnly(libs.minestom)
val minestom = libs.minestomSnapshot

compileOnly(minestom)
implementation(libs.zstd)
// Fastutil is only included because minestom already uses it, otherwise it is a crazy dependency
// for how it is used in this project.
Expand All @@ -27,7 +29,7 @@ dependencies {

testImplementation(platform("org.junit:junit-bom:5.9.1"))
testImplementation("org.junit.jupiter:junit-jupiter")
testImplementation(libs.minestom)
testImplementation(minestom)
}

java {
Expand Down
2 changes: 2 additions & 0 deletions gradle/libs.versions.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@ metadata.format.version = "1.1"

[versions]
minestom = "9f3ee89506"
minestomSnapshot = "1_20_2-909719b369"
zstd = "1.5.5-3"
fastutil = "8.5.12"

nexuspublish = "1.3.0"

[libraries]
minestom = { group = "dev.hollowcube", name = "minestom-ce", version.ref = "minestom" }
minestomSnapshot = { group = "dev.hollowcube", name = "minestom-ce-snapshots", version.ref = "minestomSnapshot" }
zstd = { group = "com.github.luben", name = "zstd-jni", version.ref = "zstd" }
fastutil = { group = "it.unimi.dsi", name = "fastutil", version.ref = "fastutil" }

Expand Down
38 changes: 36 additions & 2 deletions src/main/java/net/hollowcube/polar/PolarReader.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,12 @@
import net.minestom.server.utils.validate.Check;
import org.jetbrains.annotations.Contract;
import org.jetbrains.annotations.NotNull;
import org.jglrxavpok.hephaistos.nbt.CompressedProcesser;
import org.jglrxavpok.hephaistos.nbt.NBT;
import org.jglrxavpok.hephaistos.nbt.NBTCompound;
import org.jglrxavpok.hephaistos.nbt.NBTReader;

import java.io.InputStream;
import java.nio.ByteBuffer;

import static net.minestom.server.network.NetworkBuffer.*;
Expand Down Expand Up @@ -119,8 +123,13 @@ private PolarReader() {}
var id = buffer.readOptional(STRING);

NBTCompound nbt = null;
if (version <= PolarWorld.VERSION_USERDATA_OPT_BLOCK_ENT_NBT || buffer.read(BOOLEAN))
nbt = (NBTCompound) buffer.read(NBT);
if (version <= PolarWorld.VERSION_USERDATA_OPT_BLOCK_ENT_NBT || buffer.read(BOOLEAN)) {
if (version <= PolarWorld.VERSION_MINESTOM_NBT_READ_BREAK) {
nbt = (NBTCompound) legacyReadNBT(buffer);
} else {
buffer.read(NBT);
}
}

return new PolarChunk.BlockEntity(
ChunkUtils.blockIndexToChunkPositionX(posIndex),
Expand Down Expand Up @@ -148,6 +157,31 @@ private static void validateVersion(int version) {
};
}

/**
* Minecraft (so Minestom) had a breaking change in NBT reading in 1.20.2. This method replicates the old
* behavior which we use for any Polar version less than {@link PolarWorld#VERSION_MINESTOM_NBT_READ_BREAK}.
*
* @see NetworkBuffer#NBT
*/
private static org.jglrxavpok.hephaistos.nbt.NBT legacyReadNBT(@NotNull NetworkBuffer buffer) {
try {
var nbtReader = new NBTReader(new InputStream() {
@Override
public int read() {
return buffer.read(BYTE) & 0xFF;
}
@Override
public int available() {
return buffer.readableBytes();
}
}, CompressedProcesser.NONE);

return nbtReader.read();
} catch (Exception e) {
throw new RuntimeException(e);
}
}

@Contract("false, _ -> fail")
private static void assertThat(boolean condition, @NotNull String message) {
if (!condition) throw new Error(message);
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/net/hollowcube/polar/PolarWorld.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
@SuppressWarnings("UnstableApiUsage")
public class PolarWorld {
public static final int MAGIC_NUMBER = 0x506F6C72; // `Polr`
public static final short LATEST_VERSION = 3;
public static final short LATEST_VERSION = 4;

static final short VERSION_UNIFIED_LIGHT = 1;
static final short VERSION_USERDATA_OPT_BLOCK_ENT_NBT = 2;
static final short VERSION_MINESTOM_NBT_READ_BREAK = 3;

public static CompressionType DEFAULT_COMPRESSION = CompressionType.ZSTD;

Expand Down
2 changes: 1 addition & 1 deletion src/test/java/net/hollowcube/polar/TestAnvilPolar.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ void testConvertAnvilWorld() throws Exception {

var result = PolarWriter.write(world);
System.out.println(result.length);
Files.write(Path.of("./src/test/resources/3.polar"), result);
Files.write(Path.of("./src/test/resources/4.polar"), result);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,11 @@ void testVersion3() {
runTest(3);
}

@Test
void testVersion4() {
runTest(4);
}

private static void runTest(int version) {
var is = TestReaderBackwardsCompatibility.class.getResourceAsStream("/backward/" + version + ".polar");
assertNotNull(is);
Expand Down
5 changes: 2 additions & 3 deletions src/test/java/net/hollowcube/polar/demo/DemoServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@
import net.minestom.server.MinecraftServer;
import net.minestom.server.coordinate.Pos;
import net.minestom.server.entity.GameMode;
import net.minestom.server.event.player.AsyncPlayerConfigurationEvent;
import net.minestom.server.event.player.PlayerChatEvent;
import net.minestom.server.event.player.PlayerLoginEvent;
import net.minestom.server.event.player.PlayerSpawnEvent;
import net.minestom.server.instance.LightingChunk;

import java.nio.file.Path;

Expand All @@ -22,7 +21,7 @@ public static void main(String[] args) throws Exception {
// instance.setChunkSupplier(LightingChunk::new);

MinecraftServer.getGlobalEventHandler()
.addListener(PlayerLoginEvent.class, event -> {
.addListener(AsyncPlayerConfigurationEvent.class, event -> {
event.setSpawningInstance(instance);
event.getPlayer().setRespawnPoint(new Pos(0, 100, 0));
})
Expand Down
Binary file added src/test/resources/backward/4.polar
Binary file not shown.

0 comments on commit b36043a

Please sign in to comment.