From 9689c89010bdd3bd79ddb0e8d7dc371a3cb37277 Mon Sep 17 00:00:00 2001 From: themode Date: Mon, 3 Jan 2022 22:07:13 +0100 Subject: [PATCH] Add Palette#fill --- .../server/instance/palette/Palette.java | 2 ++ .../server/instance/palette/PaletteImpl.java | 26 +++++++++++++++++++ .../minestom/server/instance/PaletteTest.java | 22 ++++++++++++++++ 3 files changed, 50 insertions(+) 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 3726fe00a..10982435d 100644 --- a/src/main/java/net/minestom/server/instance/palette/Palette.java +++ b/src/main/java/net/minestom/server/instance/palette/Palette.java @@ -25,6 +25,8 @@ public sealed interface Palette extends Writeable permits PaletteImpl { void set(int x, int y, int z, int value); + void fill(int value); + /** * Returns the number of entries in this palette. */ 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 fca721d0e..8c74d8ff6 100644 --- a/src/main/java/net/minestom/server/instance/palette/PaletteImpl.java +++ b/src/main/java/net/minestom/server/instance/palette/PaletteImpl.java @@ -7,6 +7,8 @@ import net.minestom.server.instance.Chunk; import net.minestom.server.utils.binary.BinaryWriter; import org.jetbrains.annotations.NotNull; +import java.util.Arrays; + final class PaletteImpl implements Palette, Cloneable { private static final int[] MAGIC_MASKS; private static final int[] VALUES_PER_LONG; @@ -125,6 +127,30 @@ final class PaletteImpl implements Palette, Cloneable { } } + @Override + public void fill(int value) { + final boolean placedAir = value == 0; + if (!placedAir) value = getPaletteIndex(value); + final int bitsPerEntry = this.bitsPerEntry; + final int valuesPerLong = VALUES_PER_LONG[bitsPerEntry]; + long[] values = this.values; + if (values.length == 0) { + if (placedAir) { + // Section is empty and method is trying to place an air block, stop unnecessary computation + return; + } + // Initialize the section + this.values = values = new long[(size + valuesPerLong - 1) / valuesPerLong]; + } + + long block = 0; + for (int i = 0; i < valuesPerLong; i++) { + block |= (long) value << i * bitsPerEntry; + } + Arrays.fill(values, block); + this.count = maxSize(); + } + @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 4fa1aa42e..66c60c6b4 100644 --- a/src/test/java/net/minestom/server/instance/PaletteTest.java +++ b/src/test/java/net/minestom/server/instance/PaletteTest.java @@ -77,6 +77,28 @@ public class PaletteTest { assertEquals(4, palette.get(0, 0, 3)); } + + @Test + public void fill() { + var palettes = testPalettes(); + for (Palette palette : palettes) { + assertEquals(0, palette.size()); + palette.set(0, 0, 0, 5); + assertEquals(1, palette.size()); + assertEquals(5, palette.get(0, 0, 0)); + palette.fill(6); + assertEquals(6, palette.get(0, 0, 0)); + assertEquals(palette.maxSize(), palette.size()); + for (int y = 0; y < palette.dimension(); y++) { + for (int x = 0; x < palette.dimension(); x++) { + for (int z = 0; z < palette.dimension(); z++) { + assertEquals(6, palette.get(x, y, z)); + } + } + } + } + } + @Test public void dimension() { assertThrows(Exception.class, () -> Palette.newPalette(0, 5, 3, 1));