From bfe63893774c0c5d82a84f43cfd2afb07455f5ae Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Thu, 16 Sep 2021 11:35:05 +0200 Subject: [PATCH] Write biomes to palette --- .../types/version/ChunkSectionType1_18.java | 9 +--- .../packets/WorldPackets.java | 42 +++++++++++------- .../types/Chunk1_18Type.java | 43 ++++++++++--------- settings.gradle.kts | 1 + 4 files changed, 50 insertions(+), 45 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/ChunkSectionType1_18.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/version/ChunkSectionType1_18.java index 8415b2004..900a6190f 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/ChunkSectionType1_18.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/version/ChunkSectionType1_18.java @@ -101,13 +101,6 @@ public class ChunkSectionType1_18 extends Type { } private void writePalette(final ByteBuf buffer, final DataPalette palette) { - // Make sure this works for 1.17 chunk sections //TODO ? - if (palette.size() == 0) { - buffer.writeByte(GLOBAL_PALETTE); - Type.VAR_INT.writePrimitive(buffer, 0); - return; - } - int bitsPerValue = 0; while (palette.size() > 1 << bitsPerValue) { bitsPerValue += 1; @@ -121,7 +114,7 @@ public class ChunkSectionType1_18 extends Type { if (bitsPerValue == 0) { // Write single value - Type.VAR_INT.writePrimitive(buffer, palette.entry(0)); //TODO right? + Type.VAR_INT.writePrimitive(buffer, palette.entry(0)); Type.VAR_INT.writePrimitive(buffer, 0); // Empty values length return; } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/packets/WorldPackets.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/packets/WorldPackets.java index 862faf909..1a18e04c8 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/packets/WorldPackets.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/packets/WorldPackets.java @@ -38,13 +38,16 @@ import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.BlockEntityIds; import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.Protocol1_18To1_17_1; import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.storage.ChunkLightStorage; import com.viaversion.viaversion.protocols.protocol1_18to1_17_1.types.Chunk1_18Type; -import org.checkerframework.checker.nullness.qual.Nullable; import java.util.ArrayList; import java.util.List; public final class WorldPackets { + private static final int WIDTH_BITS = 2; + private static final int HORIZONTAL_MASK = 3; + private static final int BIOMES_PER_CHUNK = 4 * 4 * 4; + public static void register(final Protocol1_18To1_17_1 protocol) { protocol.registerClientbound(ClientboundPackets1_17_1.BLOCK_ENTITY_DATA, new PacketRemapper() { @Override @@ -61,8 +64,8 @@ public final class WorldPackets { @Override public void registerMap() { handler(wrapper -> { - final int chunkX = wrapper.read(Type.VAR_INT); - final int chunkZ = wrapper.read(Type.VAR_INT); + final int chunkX = wrapper.passthrough(Type.VAR_INT); + final int chunkZ = wrapper.passthrough(Type.VAR_INT); if (wrapper.user().get(ChunkLightStorage.class).isLoaded(chunkX, chunkZ)) { // Light packets updating already sent chunks are the same as before return; @@ -130,28 +133,35 @@ public final class WorldPackets { final int[] biomeData = oldChunk.getBiomeData(); final ChunkSection[] sections = oldChunk.getSections(); for (int i = 0; i < sections.length; i++) { - final ChunkSection section = sections[i]; + ChunkSection section = sections[i]; if (section == null) { // There's no section mask anymore - final ChunkSectionImpl emptySection = new ChunkSectionImpl(false); - sections[i] = emptySection; - emptySection.setNonAirBlocksCount(0); - emptySection.addPalette(new DataPaletteImpl(PaletteType.BIOMES)); - continue; + section = new ChunkSectionImpl(); + sections[i] = section; + section.setNonAirBlocksCount(0); + + final DataPaletteImpl blockPalette = new DataPaletteImpl(PaletteType.BLOCKS); + blockPalette.addEntry(0); + section.addPalette(blockPalette); } // Fill biome palette + //TODO Use single value palette if given the possibility final DataPaletteImpl biomePalette = new DataPaletteImpl(PaletteType.BIOMES); - //TODO - for (int x = 0; x < 16; x++) { - for (int y = 0; y < 16; y++) { - for (int z = 0; z < 16; z++) { - biomePalette.setValue(x, y, z, 0); + section.addPalette(biomePalette); + for (int biomeIndex = i * BIOMES_PER_CHUNK; biomeIndex < (i * BIOMES_PER_CHUNK) + BIOMES_PER_CHUNK; biomeIndex++) { + final int biome = biomeData[biomeIndex]; + final int minX = (biomeIndex & HORIZONTAL_MASK) << 2; + final int minY = ((biomeIndex >> WIDTH_BITS + WIDTH_BITS) << 2) & 15; + final int minZ = (biomeIndex >> WIDTH_BITS & HORIZONTAL_MASK) << 2; + for (int x = minX; x < minX + 4; x++) { + for (int y = minY; y < minY + 4; y++) { + for (int z = minZ; z < minZ + 4; z++) { + biomePalette.setValue(x, y, z, biome); + } } } } - //TODO - section.addPalette(biomePalette); } final Chunk chunk = new Chunk1_18(oldChunk.getX(), oldChunk.getZ(), oldChunk.getSections(), oldChunk.getHeightMap(), blockEntities); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/types/Chunk1_18Type.java b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/types/Chunk1_18Type.java index 644a61651..57599bba8 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/types/Chunk1_18Type.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/protocol1_18to1_17_1/types/Chunk1_18Type.java @@ -42,28 +42,28 @@ public final class Chunk1_18Type extends Type { } @Override - public Chunk read(final ByteBuf input) throws Exception { - final int chunkX = input.readInt(); - final int chunkZ = input.readInt(); - final CompoundTag heightMap = Type.NBT.read(input); + public Chunk read(final ByteBuf buffer) throws Exception { + final int chunkX = buffer.readInt(); + final int chunkZ = buffer.readInt(); + final CompoundTag heightMap = Type.NBT.read(buffer); - Type.VAR_INT.readPrimitive(input); // Data size in bytes + Type.VAR_INT.readPrimitive(buffer); // Data size in bytes // Read sections final ChunkSection[] sections = new ChunkSection[ySectionCount]; for (int i = 0; i < ySectionCount; i++) { - sections[i] = Types1_18.CHUNK_SECTION.read(input); + sections[i] = Types1_18.CHUNK_SECTION.read(buffer); } - final int blockEntitiesLength = Type.VAR_INT.readPrimitive(input); + final int blockEntitiesLength = Type.VAR_INT.readPrimitive(buffer); final List blockEntities = new ArrayList<>(blockEntitiesLength); for (int i = 0; i < blockEntitiesLength; i++) { - blockEntities.add(Types1_18.BLOCK_ENTITY.read(input)); + blockEntities.add(Types1_18.BLOCK_ENTITY.read(buffer)); } // Read all the remaining bytes (workaround for #681) - if (input.readableBytes() > 0) { - final byte[] array = Type.REMAINING_BYTES.read(input); + if (buffer.readableBytes() > 0) { + final byte[] array = Type.REMAINING_BYTES.read(buffer); if (Via.getManager().isDebug()) { Via.getPlatform().getLogger().warning("Found " + array.length + " more bytes than expected while reading the chunk: " + chunkX + "/" + chunkZ); } @@ -73,26 +73,27 @@ public final class Chunk1_18Type extends Type { } @Override - public void write(final ByteBuf output, final Chunk chunk) throws Exception { - output.writeInt(chunk.getX()); - output.writeInt(chunk.getZ()); + public void write(final ByteBuf buffer, final Chunk chunk) throws Exception { + buffer.writeInt(chunk.getX()); + buffer.writeInt(chunk.getZ()); - Type.NBT.write(output, chunk.getHeightMap()); + Type.NBT.write(buffer, chunk.getHeightMap()); - final ByteBuf buf = output.alloc().buffer(); + final ByteBuf sectionBuffer = buffer.alloc().buffer(); try { for (final ChunkSection section : chunk.getSections()) { - Types1_18.CHUNK_SECTION.write(buf, section); + Types1_18.CHUNK_SECTION.write(sectionBuffer, section); } - buf.readerIndex(0); - Type.VAR_INT.writePrimitive(output, buf.readableBytes()); - output.writeBytes(buf); + sectionBuffer.readerIndex(0); + Type.VAR_INT.writePrimitive(buffer, sectionBuffer.readableBytes()); + buffer.writeBytes(sectionBuffer); } finally { - buf.release(); // release buffer + sectionBuffer.release(); // release buffer } + Type.VAR_INT.writePrimitive(buffer, chunk.blockEntities().size()); for (final BlockEntity blockEntity : chunk.blockEntities()) { - Types1_18.BLOCK_ENTITY.write(output, blockEntity); + Types1_18.BLOCK_ENTITY.write(buffer, blockEntity); } } diff --git a/settings.gradle.kts b/settings.gradle.kts index 995a746ed..3e411d3f2 100644 --- a/settings.gradle.kts +++ b/settings.gradle.kts @@ -4,6 +4,7 @@ enableFeaturePreview("VERSION_CATALOGS") dependencyResolutionManagement { // configures repositories for all projects repositories { + mavenLocal() maven("https://repo.viaversion.com") maven("https://papermc.io/repo/repository/maven-public/") maven("https://oss.sonatype.org/content/repositories/snapshots/")