From 56cc711c0107ed7fb36f9daceec7080eefabb3d8 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sun, 24 Jul 2022 19:03:24 +0200 Subject: [PATCH] Update legacy palette handling as well --- .../api/type/types/LongArrayType.java | 4 +- .../types/version/ChunkSectionType1_13.java | 24 ++++------- .../types/version/ChunkSectionType1_16.java | 24 ++++------- .../types/version/ChunkSectionType1_9.java | 27 +++--------- .../type/types/version/PaletteType1_18.java | 43 ++++++------------- 5 files changed, 41 insertions(+), 81 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/LongArrayType.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/LongArrayType.java index fc30a74a5..40930a82c 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/LongArrayType.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/LongArrayType.java @@ -36,7 +36,7 @@ public class LongArrayType extends Type { int length = Type.VAR_INT.readPrimitive(buffer); long[] array = new long[length]; for (int i = 0; i < array.length; i++) { - array[i] = Type.LONG.readPrimitive(buffer); + array[i] = buffer.readLong(); } return array; } @@ -45,7 +45,7 @@ public class LongArrayType extends Type { public void write(ByteBuf buffer, long[] object) throws Exception { Type.VAR_INT.writePrimitive(buffer, object.length); for (long l : object) { - Type.LONG.writePrimitive(buffer, l); + buffer.writeLong(l); } } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/ChunkSectionType1_13.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/version/ChunkSectionType1_13.java index c8c8bb8bd..9f2fcf201 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/ChunkSectionType1_13.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/version/ChunkSectionType1_13.java @@ -41,8 +41,10 @@ public class ChunkSectionType1_13 extends Type { int bitsPerBlock = buffer.readUnsignedByte(); int originalBitsPerBlock = bitsPerBlock; - if (bitsPerBlock == 0 || bitsPerBlock > 8) { + if (bitsPerBlock > 8) { bitsPerBlock = GLOBAL_PALETTE; + } else if (bitsPerBlock < 4) { + bitsPerBlock = 4; } // Read palette @@ -58,18 +60,13 @@ public class ChunkSectionType1_13 extends Type { } // Read blocks - long[] blockData = new long[Type.VAR_INT.readPrimitive(buffer)]; + long[] blockData = Type.LONG_ARRAY_PRIMITIVE.read(buffer); if (blockData.length > 0) { int expectedLength = (int) Math.ceil(ChunkSection.SIZE * bitsPerBlock / 64.0); - if (blockData.length != expectedLength) { - throw new IllegalStateException("Block data length (" + blockData.length + ") does not match expected length (" + expectedLength + ")! bitsPerBlock=" + bitsPerBlock + ", originalBitsPerBlock=" + originalBitsPerBlock); + if (blockData.length == expectedLength) { + CompactArrayUtil.iterateCompactArray(bitsPerBlock, ChunkSection.SIZE, blockData, + bitsPerBlock == GLOBAL_PALETTE ? chunkSection::setFlatBlock : chunkSection::setPaletteIndex); } - - for (int i = 0; i < blockData.length; i++) { - blockData[i] = buffer.readLong(); - } - CompactArrayUtil.iterateCompactArray(bitsPerBlock, ChunkSection.SIZE, blockData, - bitsPerBlock == GLOBAL_PALETTE ? chunkSection::setFlatBlock : chunkSection::setPaletteIndex); } return chunkSection; @@ -88,7 +85,7 @@ public class ChunkSectionType1_13 extends Type { buffer.writeByte(bitsPerBlock); - // Write pallet (or not) + // Write palette if (bitsPerBlock != GLOBAL_PALETTE) { Type.VAR_INT.writePrimitive(buffer, chunkSection.getPaletteSize()); for (int i = 0; i < chunkSection.getPaletteSize(); i++) { @@ -98,9 +95,6 @@ public class ChunkSectionType1_13 extends Type { long[] data = CompactArrayUtil.createCompactArray(bitsPerBlock, ChunkSection.SIZE, bitsPerBlock == GLOBAL_PALETTE ? chunkSection::getFlatBlock : chunkSection::getPaletteIndex); - Type.VAR_INT.writePrimitive(buffer, data.length); - for (long l : data) { - buffer.writeLong(l); - } + Type.LONG_ARRAY_PRIMITIVE.write(buffer, data); } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/ChunkSectionType1_16.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/version/ChunkSectionType1_16.java index 437d9ebff..a2829fb68 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/ChunkSectionType1_16.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/version/ChunkSectionType1_16.java @@ -41,8 +41,10 @@ public class ChunkSectionType1_16 extends Type { int bitsPerBlock = buffer.readUnsignedByte(); int originalBitsPerBlock = bitsPerBlock; - if (bitsPerBlock == 0 || bitsPerBlock > 8) { + if (bitsPerBlock > 8) { bitsPerBlock = GLOBAL_PALETTE; + } else if (bitsPerBlock < 4) { + bitsPerBlock = 4; } // Read palette @@ -58,19 +60,14 @@ public class ChunkSectionType1_16 extends Type { } // Read blocks - long[] blockData = new long[Type.VAR_INT.readPrimitive(buffer)]; + long[] blockData = Type.LONG_ARRAY_PRIMITIVE.read(buffer); if (blockData.length > 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); + if (blockData.length == expectedLength) { + CompactArrayUtil.iterateCompactArrayWithPadding(bitsPerBlock, ChunkSection.SIZE, blockData, + bitsPerBlock == GLOBAL_PALETTE ? chunkSection::setFlatBlock : chunkSection::setPaletteIndex); } - - for (int i = 0; i < blockData.length; i++) { - blockData[i] = buffer.readLong(); - } - CompactArrayUtil.iterateCompactArrayWithPadding(bitsPerBlock, ChunkSection.SIZE, blockData, - bitsPerBlock == GLOBAL_PALETTE ? chunkSection::setFlatBlock : chunkSection::setPaletteIndex); } return chunkSection; @@ -89,7 +86,7 @@ public class ChunkSectionType1_16 extends Type { buffer.writeByte(bitsPerBlock); - // Write pallet (or not) + // Write palette if (bitsPerBlock != GLOBAL_PALETTE) { Type.VAR_INT.writePrimitive(buffer, chunkSection.getPaletteSize()); for (int i = 0; i < chunkSection.getPaletteSize(); i++) { @@ -99,9 +96,6 @@ public class ChunkSectionType1_16 extends Type { long[] data = CompactArrayUtil.createCompactArrayWithPadding(bitsPerBlock, ChunkSection.SIZE, bitsPerBlock == GLOBAL_PALETTE ? chunkSection::getFlatBlock : chunkSection::getPaletteIndex); - Type.VAR_INT.writePrimitive(buffer, data.length); - for (long l : data) { - buffer.writeLong(l); - } + Type.LONG_ARRAY_PRIMITIVE.write(buffer, data); } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/ChunkSectionType1_9.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/version/ChunkSectionType1_9.java index 74f2a8657..be59af951 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/ChunkSectionType1_9.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/version/ChunkSectionType1_9.java @@ -40,10 +40,6 @@ public class ChunkSectionType1_9 extends Type { // Reaad bits per block int bitsPerBlock = buffer.readUnsignedByte(); int originalBitsPerBlock = bitsPerBlock; - - if (bitsPerBlock == 0) { - bitsPerBlock = GLOBAL_PALETTE; - } if (bitsPerBlock < 4) { bitsPerBlock = 4; } @@ -63,19 +59,14 @@ public class ChunkSectionType1_9 extends Type { } // Read blocks - long[] blockData = new long[Type.VAR_INT.readPrimitive(buffer)]; + long[] blockData = Type.LONG_ARRAY_PRIMITIVE.read(buffer); if (blockData.length > 0) { int expectedLength = (int) Math.ceil(ChunkSection.SIZE * bitsPerBlock / 64.0); - if (blockData.length != expectedLength) { - throw new IllegalStateException("Block data length (" + blockData.length + ") does not match expected length (" + expectedLength + ")! bitsPerBlock=" + bitsPerBlock + ", originalBitsPerBlock=" + originalBitsPerBlock); + if (blockData.length == expectedLength) { + CompactArrayUtil.iterateCompactArray(bitsPerBlock, ChunkSection.SIZE, blockData, + bitsPerBlock == GLOBAL_PALETTE ? chunkSection::setFlatBlock + : chunkSection::setPaletteIndex); } - - for (int i = 0; i < blockData.length; i++) { - blockData[i] = buffer.readLong(); - } - CompactArrayUtil.iterateCompactArray(bitsPerBlock, ChunkSection.SIZE, blockData, - bitsPerBlock == GLOBAL_PALETTE ? chunkSection::setFlatBlock - : chunkSection::setPaletteIndex); } return chunkSection; @@ -92,10 +83,9 @@ public class ChunkSectionType1_9 extends Type { bitsPerBlock = GLOBAL_PALETTE; } - long maxEntryValue = (1L << bitsPerBlock) - 1; buffer.writeByte(bitsPerBlock); - // Write pallet (or not) + // Write palette if (bitsPerBlock != GLOBAL_PALETTE) { Type.VAR_INT.writePrimitive(buffer, chunkSection.getPaletteSize()); for (int i = 0; i < chunkSection.getPaletteSize(); i++) { @@ -107,9 +97,6 @@ public class ChunkSectionType1_9 extends Type { long[] data = CompactArrayUtil.createCompactArray(bitsPerBlock, ChunkSection.SIZE, bitsPerBlock == GLOBAL_PALETTE ? chunkSection::getFlatBlock : chunkSection::getPaletteIndex); - Type.VAR_INT.writePrimitive(buffer, data.length); - for (long l : data) { - buffer.writeLong(l); - } + Type.LONG_ARRAY_PRIMITIVE.write(buffer, data); } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/PaletteType1_18.java b/api/src/main/java/com/viaversion/viaversion/api/type/types/version/PaletteType1_18.java index 4565029d7..aede3e3a0 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/type/types/version/PaletteType1_18.java +++ b/api/src/main/java/com/viaversion/viaversion/api/type/types/version/PaletteType1_18.java @@ -45,18 +45,12 @@ public final class PaletteType1_18 extends Type { final int originalBitsPerValue = buffer.readByte(); int bitsPerValue = originalBitsPerValue; - // Read palette final DataPaletteImpl palette; if (bitsPerValue == 0) { - //TODO Create proper singleton palette Object + // Single value storage palette = new DataPaletteImpl(type.size(), 1); palette.addId(Type.VAR_INT.readPrimitive(buffer)); - - // Just eat it if not 0 - thanks, Hypixel - final int valuesLength = Type.VAR_INT.readPrimitive(buffer); - for (int i = 0; i < valuesLength; i++) { - buffer.readLong(); - } + Type.LONG_ARRAY_PRIMITIVE.read(buffer); // Just eat it if not empty - thanks, Hypixel return palette; } @@ -66,6 +60,7 @@ public final class PaletteType1_18 extends Type { bitsPerValue = 4; // Linear block palette values are always 4 bits } + // Read palette if (bitsPerValue != globalPaletteBits) { final int paletteLength = Type.VAR_INT.readPrimitive(buffer); palette = new DataPaletteImpl(type.size(), paletteLength); @@ -77,16 +72,11 @@ public final class PaletteType1_18 extends Type { } // Read values - final int valuesLength = Type.VAR_INT.readPrimitive(buffer); - if (valuesLength > 0) { - final long[] values = new long[valuesLength]; - for (int i = 0; i < valuesLength; i++) { - values[i] = buffer.readLong(); - } - + final long[] values = Type.LONG_ARRAY_PRIMITIVE.read(buffer); + if (values.length > 0) { final int valuesPerLong = (char) (64 / bitsPerValue); final int expectedLength = (type.size() + valuesPerLong - 1) / valuesPerLong; - if (valuesLength == expectedLength) { // Thanks, Hypixel + if (values.length == expectedLength) { // Thanks, Hypixel CompactArrayUtil.iterateCompactArrayWithPadding(bitsPerValue, type.size(), values, bitsPerValue == globalPaletteBits ? palette::setIdAt : palette::setPaletteIndexAt); } @@ -96,19 +86,18 @@ public final class PaletteType1_18 extends Type { @Override public void write(final ByteBuf buffer, final DataPalette palette) throws Exception { - if (palette.size() == 1) { + final int size = palette.size(); + if (size == 1) { // Single value palette buffer.writeByte(0); // 0 bit storage Type.VAR_INT.writePrimitive(buffer, palette.idByIndex(0)); Type.VAR_INT.writePrimitive(buffer, 0); // Empty values length return; - }/* else if (palette.size() == 0) { - Via.getPlatform().getLogger().warning("Palette size is 0!"); - }*/ + } // 1, 2, and 3 bit linear block palettes can't be read by the client final int min = type == PaletteType.BLOCKS ? 4 : 1; - int bitsPerValue = Math.max(min, MathUtil.ceilLog2(palette.size())); + int bitsPerValue = Math.max(min, MathUtil.ceilLog2(size)); if (bitsPerValue > type.highestBitsPerValue()) { bitsPerValue = globalPaletteBits; } @@ -116,17 +105,13 @@ public final class PaletteType1_18 extends Type { buffer.writeByte(bitsPerValue); if (bitsPerValue != globalPaletteBits) { - // Write pallete - Type.VAR_INT.writePrimitive(buffer, palette.size()); - for (int i = 0; i < palette.size(); i++) { + // Write palette + Type.VAR_INT.writePrimitive(buffer, size); + for (int i = 0; i < size; i++) { Type.VAR_INT.writePrimitive(buffer, palette.idByIndex(i)); } } - final long[] data = CompactArrayUtil.createCompactArrayWithPadding(bitsPerValue, type.size(), bitsPerValue == globalPaletteBits ? palette::idAt : palette::paletteIndexAt); - Type.VAR_INT.writePrimitive(buffer, data.length); - for (final long l : data) { - buffer.writeLong(l); - } + Type.LONG_ARRAY_PRIMITIVE.write(buffer, CompactArrayUtil.createCompactArrayWithPadding(bitsPerValue, type.size(), bitsPerValue == globalPaletteBits ? palette::idAt : palette::paletteIndexAt)); } }