Skip to content

Commit

Permalink
Merge pull request #632 from booky10/fix/bits-overflow-global-palette
Browse files Browse the repository at this point in the history
Fix bits overflowing when using global palette with bit storage
  • Loading branch information
retrooper authored Dec 21, 2023
2 parents f013520 + 1a2d39d commit c15b88e
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,24 @@ 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);
}

@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;
Expand Down

0 comments on commit c15b88e

Please sign in to comment.