Add Palette#getAll and Palette#setAll

This commit is contained in:
themode 2022-01-04 03:22:31 +01:00 committed by TheMode
parent 9327dfaccf
commit 0662a3c604
3 changed files with 47 additions and 0 deletions

View File

@ -23,10 +23,14 @@ public sealed interface Palette extends Writeable permits PaletteImpl {
int get(int x, int y, int z);
void getAll(@NotNull EntryConsumer consumer);
void set(int x, int y, int z, int value);
void fill(int value);
void setAll(@NotNull EntrySupplier supplier);
/**
* Returns the number of entries in this palette.
*/
@ -56,4 +60,14 @@ public sealed interface Palette extends Writeable permits PaletteImpl {
int dimension();
@NotNull Palette clone();
@FunctionalInterface
interface EntrySupplier {
int get(int x, int y, int z);
}
@FunctionalInterface
interface EntryConsumer {
void accept(int x, int y, int z, int value);
}
}

View File

@ -81,6 +81,18 @@ final class PaletteImpl implements Palette, Cloneable {
return hasPalette ? paletteToValueList.getInt(value) : value;
}
@Override
public void getAll(@NotNull EntryConsumer consumer) {
// TODO optimize
for (int x = 0; x < dimension; x++) {
for (int y = 0; y < dimension; y++) {
for (int z = 0; z < dimension; z++) {
consumer.accept(x, y, z, get(x, y, z));
}
}
}
}
@Override
public void set(int x, int y, int z, int value) {
if (x < 0 || y < 0 || z < 0) {
@ -150,6 +162,18 @@ final class PaletteImpl implements Palette, Cloneable {
this.count = placedAir ? 0 : maxSize();
}
@Override
public void setAll(@NotNull EntrySupplier supplier) {
// TODO optimize
for (int x = 0; x < dimension; x++) {
for (int y = 0; y < dimension; y++) {
for (int z = 0; z < dimension; z++) {
set(x, y, z, supplier.get(x, y, z));
}
}
}
}
@Override
public int size() {
return count;

View File

@ -109,6 +109,15 @@ public class PaletteTest {
}
}
@Test
public void bulkAll() {
var palettes = testPalettes();
for (Palette palette : palettes) {
palette.setAll((x, y, z) -> x + y + z);
palette.getAll((x, y, z, value) -> assertEquals(x + y + z, value));
}
}
@Test
public void dimension() {
assertThrows(Exception.class, () -> Palette.newPalette(-4, 5, 3, 1));