Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bits overflowing when using global palette with bit storage #632

Merged
merged 1 commit into from
Dec 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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