mirror of
https://github.com/Minestom/Minestom.git
synced 2025-02-03 22:12:20 +01:00
Add Palette#replace and Palette#replaceAll
This commit is contained in:
parent
0148ad9aaf
commit
0e70613fb7
@ -3,6 +3,8 @@ package net.minestom.server.instance.palette;
|
||||
import net.minestom.server.utils.binary.Writeable;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.function.IntUnaryOperator;
|
||||
|
||||
/**
|
||||
* Represents a palette used to store blocks and biomes.
|
||||
* <p>
|
||||
@ -31,6 +33,10 @@ public sealed interface Palette extends Writeable permits PaletteImpl {
|
||||
|
||||
void setAll(@NotNull EntrySupplier supplier);
|
||||
|
||||
void replace(int x, int y, int z, @NotNull IntUnaryOperator operator);
|
||||
|
||||
void replaceAll(@NotNull EntryFunction function);
|
||||
|
||||
/**
|
||||
* Returns the number of entries in this palette.
|
||||
*/
|
||||
@ -70,4 +76,9 @@ public sealed interface Palette extends Writeable permits PaletteImpl {
|
||||
interface EntryConsumer {
|
||||
void accept(int x, int y, int z, int value);
|
||||
}
|
||||
|
||||
@FunctionalInterface
|
||||
interface EntryFunction {
|
||||
int apply(int x, int y, int z, int value);
|
||||
}
|
||||
}
|
||||
|
@ -7,6 +7,7 @@ import net.minestom.server.utils.binary.BinaryWriter;
|
||||
import org.jetbrains.annotations.NotNull;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.function.IntUnaryOperator;
|
||||
|
||||
final class PaletteImpl implements Palette, Cloneable {
|
||||
private static final int[] MAGIC_MASKS;
|
||||
@ -179,6 +180,24 @@ final class PaletteImpl implements Palette, Cloneable {
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void replace(int x, int y, int z, @NotNull IntUnaryOperator operator) {
|
||||
// TODO optimize
|
||||
set(x, y, z, operator.applyAsInt(get(x, y, z)));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void replaceAll(@NotNull EntryFunction function) {
|
||||
// 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, function.apply(x, y, z, get(x, y, z)));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int size() {
|
||||
return count;
|
||||
|
@ -154,9 +154,31 @@ public class PaletteTest {
|
||||
count.set(0);
|
||||
palette.getAll((x, y, z, value) -> assertEquals(count.incrementAndGet(), value));
|
||||
assertEquals(count.get(), palette.size());
|
||||
|
||||
// Replacing
|
||||
count.set(0);
|
||||
palette.replaceAll((x, y, z, value) -> {
|
||||
assertEquals(count.incrementAndGet(), value);
|
||||
return count.get();
|
||||
});
|
||||
assertEquals(count.get(), palette.size());
|
||||
|
||||
count.set(0);
|
||||
palette.getAll((x, y, z, value) -> assertEquals(count.incrementAndGet(), value));
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void replace() {
|
||||
var palette = Palette.blocks();
|
||||
palette.set(0, 0, 0, 1);
|
||||
palette.replace(0, 0, 0, operand -> {
|
||||
assertEquals(1, operand);
|
||||
return 2;
|
||||
});
|
||||
assertEquals(2, palette.get(0, 0, 0));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void dimension() {
|
||||
assertThrows(Exception.class, () -> Palette.newPalette(-4, 5, 3, 1));
|
||||
|
Loading…
Reference in New Issue
Block a user