From b80902ec100df275b217cea01e469b97648096d5 Mon Sep 17 00:00:00 2001 From: Gerrygames Date: Wed, 22 Apr 2020 21:42:11 +0200 Subject: [PATCH] convert height maps, fix 1.16 chunk reading --- .../api/type/types/version/ChunkSectionType1_16.java | 3 ++- .../protocol1_16to1_15_2/packets/WorldPackets.java | 10 ++++++++++ .../us/myles/ViaVersion/util/CompactArrayUtil.java | 2 +- 3 files changed, 13 insertions(+), 2 deletions(-) diff --git a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/ChunkSectionType1_16.java b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/ChunkSectionType1_16.java index 28a8ee2f8..f06c51e0f 100644 --- a/common/src/main/java/us/myles/ViaVersion/api/type/types/version/ChunkSectionType1_16.java +++ b/common/src/main/java/us/myles/ViaVersion/api/type/types/version/ChunkSectionType1_16.java @@ -34,7 +34,8 @@ public class ChunkSectionType1_16 extends Type { // Read blocks long[] blockData = new long[Type.VAR_INT.read(buffer)]; if (blockData.length > 0) { - int expectedLength = (int) Math.ceil(ChunkSection.SIZE * bitsPerBlock / 64.0); + char valuesPerLong = (char) (64 / bitsPerBlock); + int expectedLength = (ChunkSection.SIZE + valuesPerLong - 1) / valuesPerLong; if (blockData.length != expectedLength) { throw new IllegalStateException("Block data length (" + blockData.length + ") does not match expected length (" + expectedLength + ")! bitsPerBlock=" + bitsPerBlock + ", originalBitsPerBlock=" + originalBitsPerBlock); } diff --git a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/WorldPackets.java b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/WorldPackets.java index d57fa6c3a..9d183c50d 100644 --- a/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/WorldPackets.java +++ b/common/src/main/java/us/myles/ViaVersion/protocols/protocol1_16to1_15_2/packets/WorldPackets.java @@ -2,6 +2,7 @@ package us.myles.ViaVersion.protocols.protocol1_16to1_15_2.packets; import com.github.steveice10.opennbt.tag.builtin.CompoundTag; import com.github.steveice10.opennbt.tag.builtin.IntArrayTag; +import com.github.steveice10.opennbt.tag.builtin.LongArrayTag; import com.github.steveice10.opennbt.tag.builtin.StringTag; import com.github.steveice10.opennbt.tag.builtin.Tag; import us.myles.ViaVersion.api.minecraft.chunks.Chunk; @@ -16,6 +17,7 @@ import us.myles.ViaVersion.protocols.protocol1_15to1_14_4.types.Chunk1_15Type; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.Protocol1_16To1_15_2; import us.myles.ViaVersion.protocols.protocol1_16to1_15_2.types.Chunk1_16Type; import us.myles.ViaVersion.protocols.protocol1_9_3to1_9_1_2.storage.ClientWorld; +import us.myles.ViaVersion.util.CompactArrayUtil; import java.util.UUID; @@ -82,6 +84,14 @@ public class WorldPackets { blockEntity.put(skullOwnerTag); } } + + CompoundTag heightMaps = chunk.getHeightMap(); + for (String key : heightMaps.keySet()) { + LongArrayTag heightMap = heightMaps.get(key); + int[] heightMapData = new int[256]; + CompactArrayUtil.iterateCompactArray(9, heightMapData.length, heightMap.getValue(), (i, v) -> heightMapData[i] = v); + heightMap.setValue(CompactArrayUtil.createCompactArrayWithPadding(9, heightMapData.length, i -> heightMapData[i])); + } }); } }); diff --git a/common/src/main/java/us/myles/ViaVersion/util/CompactArrayUtil.java b/common/src/main/java/us/myles/ViaVersion/util/CompactArrayUtil.java index 59d139e54..5bd476bc4 100644 --- a/common/src/main/java/us/myles/ViaVersion/util/CompactArrayUtil.java +++ b/common/src/main/java/us/myles/ViaVersion/util/CompactArrayUtil.java @@ -37,7 +37,7 @@ public class CompactArrayUtil { long divideMul = Integer.toUnsignedLong(MAGIC[magicIndex]); long divideAdd = Integer.toUnsignedLong(MAGIC[magicIndex + 1]); int divideShift = MAGIC[magicIndex + 2]; - int size = (4096 + valuesPerLong - 1) / valuesPerLong; + int size = (entries + valuesPerLong - 1) / valuesPerLong; long[] data = new long[size];