diff --git a/api/src/main/java/com/github/retrooper/packetevents/protocol/world/chunk/palette/DataPalette.java b/api/src/main/java/com/github/retrooper/packetevents/protocol/world/chunk/palette/DataPalette.java index f03fd62776..e5e5881866 100644 --- a/api/src/main/java/com/github/retrooper/packetevents/protocol/world/chunk/palette/DataPalette.java +++ b/api/src/main/java/com/github/retrooper/packetevents/protocol/world/chunk/palette/DataPalette.java @@ -27,7 +27,9 @@ import org.jetbrains.annotations.Nullable; public class DataPalette { - public static final int GLOBAL_PALETTE_BITS_PER_ENTRY = 14; + + // this is the amount of bits required to store the biggest state id number + public static final int GLOBAL_PALETTE_BITS_PER_ENTRY = 15; public @NotNull Palette palette; public BaseStorage storage; @@ -187,4 +189,4 @@ private static Palette createPalette(int bitsPerEntry, PaletteType paletteType) private static int index(int x, int y, int z) { return y << 8 | z << 4 | x; } -} \ No newline at end of file +} diff --git a/api/src/main/java/com/github/retrooper/packetevents/protocol/world/chunk/storage/BitStorage.java b/api/src/main/java/com/github/retrooper/packetevents/protocol/world/chunk/storage/BitStorage.java index 17f6817451..4ae40d587e 100644 --- a/api/src/main/java/com/github/retrooper/packetevents/protocol/world/chunk/storage/BitStorage.java +++ b/api/src/main/java/com/github/retrooper/packetevents/protocol/world/chunk/storage/BitStorage.java @@ -107,6 +107,10 @@ public int getSize() { @Override public int get(int index) { + if (index < 0 || index > this.size - 1L) { + throw new IllegalStateException("Illegal index: " + index + " < 0 || " + index + " > " + this.size + " - 1"); + } + int cellIndex = cellIndex(index); int bitIndex = bitIndex(index, cellIndex); return (int) (this.data[cellIndex] >> bitIndex & this.maxValue); @@ -114,6 +118,13 @@ public int get(int index) { @Override public void set(int index, int value) { + if (index < 0 || index > this.size - 1L) { + throw new IllegalStateException("Illegal index: " + index + " < 0 || " + index + " > " + this.size + " - 1"); + } + if (value < 0 || value > this.maxValue) { + throw new IllegalStateException("Illegal value: " + value + " < 0 || " + value + " > " + this.maxValue); + } + int cellIndex = cellIndex(index); int bitIndex = bitIndex(index, cellIndex); this.data[cellIndex] = this.data[cellIndex] & ~(this.maxValue << bitIndex) | ((long) value & this.maxValue) << bitIndex;