Handle negative bits per value in palette

This commit is contained in:
Nassim Jahnke 2022-07-23 13:56:02 +02:00
parent 53c54485eb
commit c8a010c390
No known key found for this signature in database
GPG Key ID: 6BE3B555EBC5982B
4 changed files with 23 additions and 17 deletions

View File

@ -26,6 +26,7 @@ import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import com.viaversion.viaversion.api.type.Type;
import io.netty.buffer.ByteBuf;
import org.checkerframework.checker.nullness.qual.Nullable;
public class FlatItemType extends BaseItemType {
public FlatItemType() {
@ -33,7 +34,7 @@ public class FlatItemType extends BaseItemType {
}
@Override
public Item read(ByteBuf buffer) throws Exception {
public @Nullable Item read(ByteBuf buffer) throws Exception {
short id = buffer.readShort();
if (id < 0) {
return null;
@ -47,7 +48,7 @@ public class FlatItemType extends BaseItemType {
}
@Override
public void write(ByteBuf buffer, Item object) throws Exception {
public void write(ByteBuf buffer, @Nullable Item object) throws Exception {
if (object == null) {
buffer.writeShort(-1);
} else {

View File

@ -25,6 +25,7 @@ package com.viaversion.viaversion.api.type.types.minecraft;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import io.netty.buffer.ByteBuf;
import org.checkerframework.checker.nullness.qual.Nullable;
public class FlatVarIntItemType extends BaseItemType {
public FlatVarIntItemType() {
@ -32,7 +33,7 @@ public class FlatVarIntItemType extends BaseItemType {
}
@Override
public Item read(ByteBuf buffer) throws Exception {
public @Nullable Item read(ByteBuf buffer) throws Exception {
boolean present = buffer.readBoolean();
if (!present) {
return null;
@ -46,7 +47,7 @@ public class FlatVarIntItemType extends BaseItemType {
}
@Override
public void write(ByteBuf buffer, Item object) throws Exception {
public void write(ByteBuf buffer, @Nullable Item object) throws Exception {
if (object == null) {
buffer.writeBoolean(false);
} else {

View File

@ -25,6 +25,7 @@ package com.viaversion.viaversion.api.type.types.minecraft;
import com.viaversion.viaversion.api.minecraft.item.DataItem;
import com.viaversion.viaversion.api.minecraft.item.Item;
import io.netty.buffer.ByteBuf;
import org.checkerframework.checker.nullness.qual.Nullable;
public class ItemType extends BaseItemType {
public ItemType() {
@ -32,7 +33,7 @@ public class ItemType extends BaseItemType {
}
@Override
public Item read(ByteBuf buffer) throws Exception {
public @Nullable Item read(ByteBuf buffer) throws Exception {
short id = buffer.readShort();
if (id < 0) {
return null;
@ -47,7 +48,7 @@ public class ItemType extends BaseItemType {
}
@Override
public void write(ByteBuf buffer, Item object) throws Exception {
public void write(ByteBuf buffer, @Nullable Item object) throws Exception {
if (object == null) {
buffer.writeShort(-1);
} else {

View File

@ -42,12 +42,8 @@ public final class PaletteType1_18 extends Type<DataPalette> {
@Override
public DataPalette read(final ByteBuf buffer) throws Exception {
int bitsPerValue = buffer.readByte();
final int originalBitsPerValue = bitsPerValue;
if (bitsPerValue > type.highestBitsPerValue()) {
bitsPerValue = globalPaletteBits;
}
final int originalBitsPerValue = buffer.readByte();
int bitsPerValue = originalBitsPerValue;
// Read palette
final DataPaletteImpl palette;
@ -59,6 +55,12 @@ public final class PaletteType1_18 extends Type<DataPalette> {
return palette;
}
if (bitsPerValue < 0 || bitsPerValue > type.highestBitsPerValue()) {
bitsPerValue = globalPaletteBits;
} else if (type == PaletteType.BLOCKS && bitsPerValue < 4) {
bitsPerValue = 4; // Linear block palette values are always 4 bits
}
if (bitsPerValue != globalPaletteBits) {
final int paletteLength = Type.VAR_INT.readPrimitive(buffer);
palette = new DataPaletteImpl(type.size(), paletteLength);
@ -70,14 +72,15 @@ public final class PaletteType1_18 extends Type<DataPalette> {
}
// Read values
final long[] values = new long[Type.VAR_INT.readPrimitive(buffer)];
if (values.length > 0) {
final char valuesPerLong = (char) (64 / bitsPerValue);
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 (values.length != expectedLength) {
throw new IllegalStateException("Palette data length (" + values.length + ") does not match expected length (" + expectedLength + ")! bitsPerValue=" + bitsPerValue + ", originalBitsPerValue=" + originalBitsPerValue);
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++) {
values[i] = buffer.readLong();
}