Fix reading of cursed Hypixel block palettes

Fixes #3037
This commit is contained in:
Nassim Jahnke 2022-07-24 18:42:25 +02:00
parent c8a010c390
commit c2ee558235
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B

View File

@ -51,7 +51,12 @@ public final class PaletteType1_18 extends Type<DataPalette> {
//TODO Create proper singleton palette Object
palette = new DataPaletteImpl(type.size(), 1);
palette.addId(Type.VAR_INT.readPrimitive(buffer));
Type.VAR_INT.readPrimitive(buffer); // 0 values length
// 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();
}
return palette;
}
@ -74,18 +79,17 @@ public final class PaletteType1_18 extends Type<DataPalette> {
// Read values
final int valuesLength = Type.VAR_INT.readPrimitive(buffer);
if (valuesLength > 0) {
final int valuesPerLong = (char) (64 / bitsPerValue);
final int expectedLength = (type.size() + valuesPerLong - 1) / valuesPerLong;
if (valuesLength != expectedLength) {
throw new IllegalStateException("Palette data length (" + valuesLength + ") does not match expected length (" + expectedLength + ")! bitsPerValue=" + bitsPerValue + ", originalBitsPerValue=" + originalBitsPerValue);
}
final long[] values = new long[valuesLength];
for (int i = 0; i < values.length; i++) {
for (int i = 0; i < valuesLength; i++) {
values[i] = buffer.readLong();
}
CompactArrayUtil.iterateCompactArrayWithPadding(bitsPerValue, type.size(), values,
bitsPerValue == globalPaletteBits ? palette::setIdAt : palette::setPaletteIndexAt);
final int valuesPerLong = (char) (64 / bitsPerValue);
final int expectedLength = (type.size() + valuesPerLong - 1) / valuesPerLong;
if (valuesLength == expectedLength) { // Thanks, Hypixel
CompactArrayUtil.iterateCompactArrayWithPadding(bitsPerValue, type.size(), values,
bitsPerValue == globalPaletteBits ? palette::setIdAt : palette::setPaletteIndexAt);
}
}
return palette;
}