Update legacy palette handling as well

This commit is contained in:
Nassim Jahnke 2022-07-24 19:03:24 +02:00
parent c2ee558235
commit 56cc711c01
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
5 changed files with 41 additions and 81 deletions

View File

@ -36,7 +36,7 @@ public class LongArrayType extends Type<long[]> {
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<long[]> {
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);
}
}
}

View File

@ -41,8 +41,10 @@ public class ChunkSectionType1_13 extends Type<ChunkSection> {
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<ChunkSection> {
}
// 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<ChunkSection> {
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<ChunkSection> {
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);
}
}

View File

@ -41,8 +41,10 @@ public class ChunkSectionType1_16 extends Type<ChunkSection> {
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<ChunkSection> {
}
// 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<ChunkSection> {
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<ChunkSection> {
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);
}
}

View File

@ -40,10 +40,6 @@ public class ChunkSectionType1_9 extends Type<ChunkSection> {
// 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<ChunkSection> {
}
// 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<ChunkSection> {
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<ChunkSection> {
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);
}
}

View File

@ -45,18 +45,12 @@ public final class PaletteType1_18 extends Type<DataPalette> {
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<DataPalette> {
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<DataPalette> {
}
// 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<DataPalette> {
@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<DataPalette> {
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));
}
}