diff --git a/src/main/java/net/minestom/server/instance/palette/Palette.java b/src/main/java/net/minestom/server/instance/palette/Palette.java index 10982435d..7e9bc8dfa 100644 --- a/src/main/java/net/minestom/server/instance/palette/Palette.java +++ b/src/main/java/net/minestom/server/instance/palette/Palette.java @@ -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); + } } diff --git a/src/main/java/net/minestom/server/instance/palette/PaletteImpl.java b/src/main/java/net/minestom/server/instance/palette/PaletteImpl.java index 8964bc23e..3ed104c13 100644 --- a/src/main/java/net/minestom/server/instance/palette/PaletteImpl.java +++ b/src/main/java/net/minestom/server/instance/palette/PaletteImpl.java @@ -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; diff --git a/src/test/java/net/minestom/server/instance/PaletteTest.java b/src/test/java/net/minestom/server/instance/PaletteTest.java index 0917a522b..844f8c0bf 100644 --- a/src/test/java/net/minestom/server/instance/PaletteTest.java +++ b/src/test/java/net/minestom/server/instance/PaletteTest.java @@ -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));