Merge remote-tracking branch 'origin/master' into t3/port-studio-v4

This commit is contained in:
piexel committed 2026-11-25 10:12:16 +01:00
commit ef24f2e8b2
30 files changed
+599 -143

No files matched your search

@@ -28,6 +28,7 @@ import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Entity;
@@ -35,6 +36,7 @@ import org.bukkit.inventory.ItemStack;
import org.bukkit.plugin.Plugin;
import org.junit.Test;
import org.mockito.MockedStatic;
import org.mockito.Mockito;
import java.util.List;
import java.util.Map;
@@ -196,6 +198,68 @@ public class CraftEngineDataProviderTest {
}
}
@Test
public void decayingCustomLeavesAreMadePersistentAndKeepTheirCustomIdentity() {
BooleanProperty persistent = BooleanProperty.create("persistent", false);
BlockDefinition definition = mock(BlockDefinition.class);
ImmutableBlockState decaying = blockState(definition, "test:leaves[persistent=false]");
ImmutableBlockState kept = blockState(definition, "test:leaves[persistent=true]");
BlockData base = mock(BlockData.class);
BlockData keptBase = mock(BlockData.class);
doReturn(persistent).when(decaying).getProperty("persistent");
doReturn(false).when(decaying).get(persistent);
when(decaying.with(persistent, true)).thenReturn(kept);
try (MockedStatic<CraftEngineBlocks> blocks = mockStatic(CraftEngineBlocks.class)) {
blocks.when(() -> CraftEngineBlocks.getCustomBlockState(base)).thenReturn(decaying);
blocks.when(() -> CraftEngineBlocks.getBukkitBlockData(kept)).thenReturn(keptBase);
IrisCustomData result = (IrisCustomData) new CraftEngineDataProvider()
.withoutDecay(IrisCustomData.of(base, new Identifier("craftengine", "test/leaves[persistent=false]")));
assertSame(keptBase, result.getBase());
assertEquals(new Identifier("test", "leaves[persistent=true]"), result.getCustom());
}
}
@Test
public void persistentOrDecaylessCustomBlocksAreKeptAsTheyAre() {
BooleanProperty persistent = BooleanProperty.create("persistent", false);
BlockDefinition definition = mock(BlockDefinition.class);
ImmutableBlockState alreadyPersistent = blockState(definition, "test:leaves[persistent=true]");
ImmutableBlockState withoutProperty = blockState(definition, "test:log");
BlockData persistentData = mock(BlockData.class);
BlockData logData = mock(BlockData.class);
BlockData vanilla = mock(BlockData.class);
doReturn(persistent).when(alreadyPersistent).getProperty("persistent");
doReturn(true).when(alreadyPersistent).get(persistent);
try (MockedStatic<CraftEngineBlocks> blocks = mockStatic(CraftEngineBlocks.class)) {
blocks.when(() -> CraftEngineBlocks.getCustomBlockState(persistentData)).thenReturn(alreadyPersistent);
blocks.when(() -> CraftEngineBlocks.getCustomBlockState(logData)).thenReturn(withoutProperty);
CraftEngineDataProvider provider = new CraftEngineDataProvider();
assertSame(persistentData, provider.withoutDecay(persistentData));
assertSame(logData, provider.withoutDecay(logData));
assertSame(vanilla, provider.withoutDecay(vanilla));
}
}
@Test
public void customBlocksCarryTheTagsTheirSettingsListEvenWhenWrapped() {
ImmutableBlockState state = mock(ImmutableBlockState.class, Mockito.RETURNS_DEEP_STUBS);
BlockData base = mock(BlockData.class);
BlockData vanilla = mock(BlockData.class);
when(state.settings().tags()).thenReturn(Set.of(Key.of("minecraft:logs")));
try (MockedStatic<CraftEngineBlocks> blocks = mockStatic(CraftEngineBlocks.class)) {
blocks.when(() -> CraftEngineBlocks.getCustomBlockState(base)).thenReturn(state);
CraftEngineDataProvider provider = new CraftEngineDataProvider();
BlockData wrapped = IrisCustomData.of(base, new Identifier("craftengine", "test/log"));
assertTrue(provider.hasBlockTag(wrapped, NamespacedKey.minecraft("logs")));
assertFalse(provider.hasBlockTag(wrapped, NamespacedKey.minecraft("leaves")));
assertFalse(provider.hasBlockTag(vanilla, NamespacedKey.minecraft("logs")));
}
}
@Test
public void unknownOrInvalidBlockPropertiesFailBeforeNativeConversion() {
Identifier id = new Identifier("test", "lamp");
@@ -84,6 +84,14 @@ public class B {
return state != null && state.isTreeBlock();
}
public static boolean isLog(NativeBlockState state) {
return state != null && state.isLog();
}
public static boolean isLeaves(NativeBlockState state) {
return state != null && state.isLeaves();
}
public static boolean isFoliagePlantable(NativeBlockState state) {
return state != null && state.isFoliagePlantable();
}
@@ -62,6 +62,9 @@ public class IrisNoiseGenerator {
private static final long GENERATOR_SEED_SALT = 33_955_677L;
private static final int MINIMUM_FRACTURE_DEPTH = 4;
private static final int MAXIMUM_FRACTURE_NODES = 32;
// Fracture axes as CNG.reseededForAxis numbers them for a 2D read: x is axis 0, z is axis 1.
static final int X_AXIS = 0;
static final int Z_AXIS = 1;
private static final ThreadLocal<FractureSamples> FRACTURE_SAMPLES = ThreadLocal.withInitial(FractureSamples::new);
@Getter(AccessLevel.NONE)
@@ -144,6 +147,14 @@ public class IrisNoiseGenerator {
}
double getNoise(long superSeed, double xv, double zv, IrisData data, Engine engine) {
return getNoiseForAxis(superSeed, xv, zv, data, engine, X_AXIS);
}
/**
* This layer read as the fracture of one axis. Each axis samples this layer's generator reseeded for that axis at
* the same coordinates, so the x and z warps are uncorrelated instead of mirror images of each other.
*/
private double getNoiseForAxis(long superSeed, double xv, double zv, IrisData data, Engine engine, int axis) {
if (!enabled) {
return offsetY;
}
@@ -154,7 +165,7 @@ public class IrisNoiseGenerator {
samples.active = true;
try {
if (samples.prepare(this, superSeed, data, engine)) {
return samples.sample(xv, zv);
return samples.sample(xv, zv, axis);
}
} finally {
samples.clear();
@@ -169,13 +180,13 @@ public class IrisNoiseGenerator {
for (IrisNoiseGenerator i : fracture) {
if (i.isEnabled()) {
double fractureOffset = i.getOpacity() / 2D;
x += i.getNoise(superSeed + seed + g, xv, zv, data, engine) - fractureOffset;
z += i.getNoise(superSeed + seed + g, zv, xv, data, engine) - fractureOffset;
x += i.getNoiseForAxis(superSeed + seed + g, xv, zv, data, engine, X_AXIS) - fractureOffset;
z += i.getNoiseForAxis(superSeed + seed + g, xv, zv, data, engine, Z_AXIS) - fractureOffset;
}
g += 819;
}
return sample(getGenerator(superSeed, data, engine), x, z);
return sample(getGenerator(superSeed, data, engine).reseededForAxis(axis), x, z);
}
boolean supportsBatch() {
@@ -200,6 +211,10 @@ public class IrisNoiseGenerator {
}
NoiseLayerProgram compileProgram(long superSeed, IrisData data, Engine engine) {
return compileProgram(superSeed, data, engine, false);
}
private NoiseLayerProgram compileProgram(long superSeed, IrisData data, Engine engine, boolean readAsFracture) {
if (!enabled) {
return NoiseLayerProgram.disabled(this);
}
@@ -215,13 +230,14 @@ public class IrisNoiseGenerator {
int compiled = 0;
for (IrisNoiseGenerator child : fracture) {
if (child.isEnabled()) {
children[compiled] = child.compileProgram(superSeed + seed + salt, data, engine);
children[compiled] = child.compileProgram(superSeed + seed + salt, data, engine, true);
offsets[compiled++] = child.getOpacity() / 2D;
}
salt += 819;
}
CNG generator = Objects.requireNonNull(getGenerator(superSeed, data, engine), "Noise layer generator");
return NoiseLayerProgram.of(this, NoiseProgram.compile2D(generator), children, offsets);
NoiseProgram zAxisProgram = readAsFracture ? NoiseProgram.compile2D(generator.reseededForAxis(Z_AXIS)) : null;
return NoiseLayerProgram.of(this, NoiseProgram.compile2D(generator), zAxisProgram, children, offsets);
}
public KList<IrisNoiseGenerator> getAllComposites() {
@@ -357,20 +373,26 @@ public class IrisNoiseGenerator {
return false;
}
private double sample(double x, double z) {
/**
* Both axes of a fracture node read the same warped coordinates, since a reseeded generator shares the
* node's own fracture, so each level samples its generator once per axis.
*/
private double sample(double x, double z, int rootAxis) {
int leaf = size - 1;
double forward = nodes[leaf].sample(generators[leaf], x, z);
double reverse = nodes[leaf].sample(generators[leaf], z, x);
for (int index = leaf - 1; index >= 0; index--) {
double offset = nodes[index + 1].opacity / 2D;
double forwardX = x + (forward - offset);
double forwardZ = z + (reverse - offset);
forward = nodes[index].sample(generators[index], forwardX, forwardZ);
if (index > 0) {
reverse = nodes[index].sample(generators[index], forwardZ, forwardX);
}
if (leaf == 0) {
return nodes[0].sample(generators[0].reseededForAxis(rootAxis), x, z);
}
return forward;
double warpX = nodes[leaf].sample(generators[leaf], x, z);
double warpZ = nodes[leaf].sample(generators[leaf].reseededForAxis(Z_AXIS), x, z);
for (int index = leaf - 1; index > 0; index--) {
double offset = nodes[index + 1].opacity / 2D;
double warpedX = x + (warpX - offset);
double warpedZ = z + (warpZ - offset);
warpX = nodes[index].sample(generators[index], warpedX, warpedZ);
warpZ = nodes[index].sample(generators[index].reseededForAxis(Z_AXIS), warpedX, warpedZ);
}
double offset = nodes[1].opacity / 2D;
return nodes[0].sample(generators[0].reseededForAxis(rootAxis), x + (warpX - offset), z + (warpZ - offset));
}
private void clear() {
@@ -9,6 +9,7 @@ public final class NoiseLayerProgram {
private final boolean enabled;
private final double offsetY;
private final NoiseProgram program;
private final NoiseProgram zAxisProgram;
private final double zoom;
private final double opacity;
private final double offsetX;
@@ -21,10 +22,12 @@ public final class NoiseLayerProgram {
private final NoiseLayerProgram[] children;
private final double[] childOffsets;
private NoiseLayerProgram(IrisNoiseGenerator layer, NoiseProgram program, NoiseLayerProgram[] children, double[] childOffsets) {
private NoiseLayerProgram(IrisNoiseGenerator layer, NoiseProgram program, NoiseProgram zAxisProgram,
NoiseLayerProgram[] children, double[] childOffsets) {
this.enabled = program != null;
this.offsetY = layer.getOffsetY();
this.program = program;
this.zAxisProgram = zAxisProgram;
this.zoom = layer.getZoom();
this.opacity = layer.getOpacity();
this.offsetX = layer.getOffsetX();
@@ -39,11 +42,16 @@ public final class NoiseLayerProgram {
}
static NoiseLayerProgram disabled(IrisNoiseGenerator layer) {
return new NoiseLayerProgram(layer, null, new NoiseLayerProgram[0], new double[0]);
return new NoiseLayerProgram(layer, null, null, new NoiseLayerProgram[0], new double[0]);
}
static NoiseLayerProgram of(IrisNoiseGenerator layer, NoiseProgram program, NoiseLayerProgram[] children, double[] childOffsets) {
return new NoiseLayerProgram(layer, program, children, childOffsets);
/**
* zAxisProgram is the layer's generator reseeded for the z axis; only a layer read as a fracture needs it, and the
* root layer passes null.
*/
static NoiseLayerProgram of(IrisNoiseGenerator layer, NoiseProgram program, NoiseProgram zAxisProgram,
NoiseLayerProgram[] children, double[] childOffsets) {
return new NoiseLayerProgram(layer, program, zAxisProgram, children, childOffsets);
}
public void evaluate(TerrainBatchScratch scratch, double[] xs, double[] zs, int count, double[] out) {
@@ -53,143 +61,110 @@ public final class NoiseLayerProgram {
}
int mark = scratch.mark();
try {
fill(scratch, xs, zs, count, out);
double[] sampleX = scratch.buffer(count);
double[] sampleZ = scratch.buffer(count);
warpedSampleCoordinates(scratch, xs, zs, count, sampleX, sampleZ);
program.fill2D(scratch.noise(), sampleX, sampleZ, count, out);
applyPost(out, count);
} finally {
scratch.reset(mark);
}
}
private void fill(TerrainBatchScratch scratch, double[] xs, double[] zs, int count, double[] out) {
double[] sampleX = scratch.buffer(count);
double[] sampleZ = scratch.buffer(count);
if (children.length == 0) {
for (int index = 0; index < count; index++) {
sampleX[index] = (xs[index] / zoom) + offsetX;
sampleZ[index] = (zs[index] / zoom) + offsetZ;
}
} else {
System.arraycopy(xs, 0, sampleX, 0, count);
System.arraycopy(zs, 0, sampleZ, 0, count);
double[] forward = scratch.buffer(count);
double[] reverse = scratch.buffer(count);
for (int child = 0; child < children.length; child++) {
children[child].evaluatePair(scratch, xs, zs, count, forward, reverse);
double offset = childOffsets[child];
for (int index = 0; index < count; index++) {
sampleX[index] += forward[index] - offset;
sampleZ[index] += reverse[index] - offset;
}
}
for (int index = 0; index < count; index++) {
sampleX[index] = (sampleX[index] / zoom) + offsetX;
sampleZ[index] = (sampleZ[index] / zoom) + offsetZ;
}
}
program.fill2D(scratch.noise(), sampleX, sampleZ, count, out);
for (int index = 0; index < count; index++) {
out[index] = post(out[index]);
}
}
void evaluatePair(TerrainBatchScratch scratch, double[] xs, double[] zs, int count, double[] forward, double[] reverse) {
/** Fills this layer read as a fracture: the x warp from its generator, the z warp from its z-axis generator. */
void evaluateAxes(TerrainBatchScratch scratch, double[] xs, double[] zs, int count, double[] xWarp, double[] zWarp) {
if (!enabled) {
Arrays.fill(forward, 0, count, offsetY);
Arrays.fill(reverse, 0, count, offsetY);
Arrays.fill(xWarp, 0, count, offsetY);
Arrays.fill(zWarp, 0, count, offsetY);
return;
}
int mark = scratch.mark();
try {
fillPair(scratch, xs, zs, count, forward, reverse);
double[] sampleX = scratch.buffer(count);
double[] sampleZ = scratch.buffer(count);
warpedSampleCoordinates(scratch, xs, zs, count, sampleX, sampleZ);
program.fill2D(scratch.noise(), sampleX, sampleZ, count, xWarp);
zAxisProgram.fill2D(scratch.noise(), sampleX, sampleZ, count, zWarp);
applyPost(xWarp, count);
applyPost(zWarp, count);
} finally {
scratch.reset(mark);
}
}
private void fillPair(TerrainBatchScratch scratch, double[] xs, double[] zs, int count, double[] forward, double[] reverse) {
int pairs = count * 2;
double[] sampleX = scratch.buffer(pairs);
double[] sampleZ = scratch.buffer(pairs);
private void warpedSampleCoordinates(TerrainBatchScratch scratch, double[] xs, double[] zs, int count,
double[] sampleX, double[] sampleZ) {
System.arraycopy(xs, 0, sampleX, 0, count);
System.arraycopy(zs, 0, sampleZ, 0, count);
System.arraycopy(zs, 0, sampleX, count, count);
System.arraycopy(xs, 0, sampleZ, count, count);
if (children.length > 0) {
double[] childForward = scratch.buffer(count);
double[] childReverse = scratch.buffer(count);
double[] childX = scratch.buffer(count);
double[] childZ = scratch.buffer(count);
for (int child = 0; child < children.length; child++) {
children[child].evaluatePair(scratch, xs, zs, count, childForward, childReverse);
children[child].evaluateAxes(scratch, xs, zs, count, childX, childZ);
double offset = childOffsets[child];
for (int index = 0; index < count; index++) {
sampleX[index] += childForward[index] - offset;
sampleZ[index] += childReverse[index] - offset;
sampleX[count + index] += childReverse[index] - offset;
sampleZ[count + index] += childForward[index] - offset;
sampleX[index] += childX[index] - offset;
sampleZ[index] += childZ[index] - offset;
}
}
}
for (int index = 0; index < pairs; index++) {
for (int index = 0; index < count; index++) {
sampleX[index] = (sampleX[index] / zoom) + offsetX;
sampleZ[index] = (sampleZ[index] / zoom) + offsetZ;
}
double[] values = scratch.buffer(pairs);
program.fill2D(scratch.noise(), sampleX, sampleZ, pairs, values);
for (int index = 0; index < count; index++) {
forward[index] = post(values[index]);
reverse[index] = post(values[count + index]);
}
}
public double sample(TerrainBatchScratch scratch, double x, double z) {
if (!enabled) {
return offsetY;
}
double sampleX = x;
double sampleZ = z;
if (children.length > 0) {
int mark = scratch.mark();
try {
double[] pair = scratch.buffer(2);
for (int child = 0; child < children.length; child++) {
children[child].samplePair(scratch, x, z, pair);
double offset = childOffsets[child];
sampleX += pair[0] - offset;
sampleZ += pair[1] - offset;
}
} finally {
scratch.reset(mark);
}
int mark = scratch.mark();
try {
double[] sampleCoordinates = scratch.buffer(2);
warpedSampleCoordinate(scratch, x, z, sampleCoordinates);
return post(program.sample2D(sampleCoordinates[0], sampleCoordinates[1]));
} finally {
scratch.reset(mark);
}
return post(program.sample2D((sampleX / zoom) + offsetX, (sampleZ / zoom) + offsetZ));
}
private void samplePair(TerrainBatchScratch scratch, double x, double z, double[] out) {
private void sampleAxes(TerrainBatchScratch scratch, double x, double z, double[] out) {
if (!enabled) {
out[0] = offsetY;
out[1] = offsetY;
return;
}
double forwardX = x;
double forwardZ = z;
double reverseX = z;
double reverseZ = x;
int mark = scratch.mark();
try {
double[] sampleCoordinates = scratch.buffer(2);
warpedSampleCoordinate(scratch, x, z, sampleCoordinates);
out[0] = post(program.sample2D(sampleCoordinates[0], sampleCoordinates[1]));
out[1] = post(zAxisProgram.sample2D(sampleCoordinates[0], sampleCoordinates[1]));
} finally {
scratch.reset(mark);
}
}
private void warpedSampleCoordinate(TerrainBatchScratch scratch, double x, double z, double[] out) {
double sampleX = x;
double sampleZ = z;
if (children.length > 0) {
int mark = scratch.mark();
try {
double[] pair = scratch.buffer(2);
for (int child = 0; child < children.length; child++) {
children[child].samplePair(scratch, x, z, pair);
double offset = childOffsets[child];
forwardX += pair[0] - offset;
forwardZ += pair[1] - offset;
reverseX += pair[1] - offset;
reverseZ += pair[0] - offset;
}
} finally {
scratch.reset(mark);
double[] warp = scratch.buffer(2);
for (int child = 0; child < children.length; child++) {
children[child].sampleAxes(scratch, x, z, warp);
double offset = childOffsets[child];
sampleX += warp[0] - offset;
sampleZ += warp[1] - offset;
}
}
out[0] = post(program.sample2D((forwardX / zoom) + offsetX, (forwardZ / zoom) + offsetZ));
out[1] = post(program.sample2D((reverseX / zoom) + offsetX, (reverseZ / zoom) + offsetZ));
out[0] = (sampleX / zoom) + offsetX;
out[1] = (sampleZ / zoom) + offsetZ;
}
private void applyPost(double[] values, int count) {
for (int index = 0; index < count; index++) {
values[index] = post(values[index]);
}
}
private double post(double raw) {
@@ -14,6 +14,7 @@ import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
@@ -128,6 +129,25 @@ public abstract class ExternalDataProvider {
return false;
}
/**
* @param data The block to keep, possibly an {@link IrisCustomData} this provider returned from {@link #getBlockData}
* @return The block made to never decay, like custom leaves, or {@code data} itself when it is not one of the
* provider's decaying blocks
*/
@NotNull
public BlockData withoutDecay(@NotNull BlockData data) {
return data;
}
/**
* @param data The block to check, possibly an {@link IrisCustomData} this provider returned from {@link #getBlockData}
* @param tag A block tag, like {@code minecraft:logs}
* @return Whether the block is one of the provider's blocks and carries that tag
*/
public boolean hasBlockTag(@NotNull BlockData data, @NotNull NamespacedKey tag) {
return false;
}
/**
* Spawns a mob in the specified location using the given engine and entity identifier.
*
@@ -30,6 +30,7 @@ import art.arcane.volmlib.util.collection.KMap;
import art.arcane.iris.platform.bukkit.plugin.IrisService;
import org.bukkit.Bukkit;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Entity;
@@ -193,6 +194,26 @@ public class ExternalDataSVC implements IrisService {
return blockData;
}
/** The block made to never decay by the provider it belongs to, or {@code data} itself. */
public BlockData withoutDecay(BlockData data) {
for (ExternalDataProvider provider : activeProviders) {
BlockData kept = provider.withoutDecay(data);
if (kept == data) {
continue;
}
if (kept instanceof IrisCustomData custom && data instanceof IrisCustomData) {
return IrisCustomData.of(custom.getBase(), qualifyBlockId(provider, custom.getCustom()));
}
return kept;
}
return data;
}
/** Whether {@code data} is a provider's block carrying {@code tag}. */
public boolean hasBlockTag(BlockData data, NamespacedKey tag) {
return activeProviders.stream().anyMatch(provider -> provider.hasBlockTag(data, tag));
}
public Optional<BlockData> getBlockData(final Identifier key) {
Pair<Identifier, KMap<String, String>> pair;
try {
@@ -30,6 +30,7 @@ import net.momirealms.craftengine.core.util.Key;
import org.bukkit.Bukkit;
import org.bukkit.Chunk;
import org.bukkit.Location;
import org.bukkit.NamespacedKey;
import org.bukkit.block.Block;
import org.bukkit.block.data.BlockData;
import org.bukkit.entity.Entity;
@@ -57,6 +58,7 @@ public class CraftEngineDataProvider extends ExternalDataProvider implements Lis
BlockProperty.ofBoolean("randomPitch", false),
BlockProperty.ofDouble("pitch", 0, 0, 360f, false, true)
);
private static final String PERSISTENT_PROPERTY = "persistent";
private static final ContentRegistry EMPTY_CONTENT = new ContentRegistry(Set.of(), Set.of(), false);
private volatile ContentRegistry content = EMPTY_CONTENT;
@@ -131,6 +133,32 @@ public class CraftEngineDataProvider extends ExternalDataProvider implements Lis
return state == null || state.isEmpty() ? Optional.empty() : Optional.of(Identifier.fromString(state.toString()));
}
@Override
public @NotNull BlockData withoutDecay(@NotNull BlockData data) {
ImmutableBlockState state = customStateOf(data);
if (state == null) {
return data;
}
Property<Boolean> persistent = state.getProperty(PERSISTENT_PROPERTY);
if (persistent == null || persistent.valueClass() != Boolean.class || state.get(persistent)) {
return data;
}
ImmutableBlockState kept = state.with(persistent, true);
return IrisCustomData.of(CraftEngineBlocks.getBukkitBlockData(kept), Identifier.fromString(kept.toString()));
}
@Override
public boolean hasBlockTag(@NotNull BlockData data, @NotNull NamespacedKey tag) {
ImmutableBlockState state = customStateOf(data);
return state != null && state.settings().tags().contains(Key.of(tag.getNamespace(), tag.getKey()));
}
private static ImmutableBlockState customStateOf(BlockData data) {
BlockData nativeData = data instanceof IrisCustomData custom ? custom.getBase() : data;
ImmutableBlockState state = CraftEngineBlocks.getCustomBlockState(nativeData);
return state == null || state.isEmpty() ? null : state;
}
@Override
public boolean placeBlock(@NotNull Block block, @NotNull Identifier blockId) {
Pair<Identifier, KMap<String, String>> parsed = ExternalDataSVC.parseState(blockId);
@@ -143,7 +143,7 @@ public final class NoiseStyleCompiler implements StyleCompiler {
Field center = new Field.Const(-child.getOpacity() / 2.0);
String childPath = path + "/f" + index + "_" + child.getSeed();
warpX = sum(warpX, binary(Field.BinaryOp.ADD, layer(child, domain, emit, childPath + "x"), center));
warpZ = sum(warpZ, binary(Field.BinaryOp.ADD, layer(child, domain.swapped(), emit, childPath + "z"), center));
warpZ = sum(warpZ, binary(Field.BinaryOp.ADD, layer(child, domain, emit, childPath + "z"), center));
}
Domain sampled = domain.layer(positive(layer.getZoom(), "layer zoom"), layer.getOffsetX(), layer.getOffsetZ(), warpX, warpZ);
Field value = style(layer.getStyle(), layer.getOctaves(), sampled, emit, path);
@@ -208,7 +208,7 @@ public final class NoiseStyleCompiler implements StyleCompiler {
Displacement leafDisplacement = displacement;
if (warped(cng)) {
Field warpX = displacement(node(cng.getFracture(), displacement, emit, path + "/x"), cng.getFscale());
Field warpZ = displacement(node(cng.getFracture(), displacement.swapped(), emit, path + "/z"), cng.getFscale());
Field warpZ = displacement(node(cng.getFracture(), displacement, emit, path + "/z"), cng.getFscale());
leafDisplacement = displacement.warped(warpX, warpZ);
}
value = leaf(type, cng.getOct(), cng.getEffectiveScale() * multiplier, leafDisplacement, emit, path);
@@ -483,19 +483,11 @@ public final class NoiseStyleCompiler implements StyleCompiler {
sum(warpX, layerWarpX == null ? null : scaled(layerWarpX, zoom)),
sum(warpZ, layerWarpZ == null ? null : scaled(layerWarpZ, zoom)));
}
private Domain swapped() {
return new Domain(zoom, offsetZ, offsetX, warpZ, warpX);
}
}
private record Displacement(double offsetX, double offsetZ, Field warpX, Field warpZ) {
private static final Displacement NONE = new Displacement(0.0, 0.0, null, null);
private Displacement swapped() {
return new Displacement(offsetZ, offsetX, warpZ, warpX);
}
private Displacement warped(Field addedX, Field addedZ) {
return new Displacement(offsetX, offsetZ, sum(warpX, addedX), sum(warpZ, addedZ));
}
@@ -129,6 +129,16 @@ public final class MissingBlockState implements NativeBlockState {
return false;
}
@Override
public boolean isLog() {
return false;
}
@Override
public boolean isLeaves() {
return false;
}
@Override
public boolean isFoliagePlantable() {
return false;
@@ -100,8 +100,12 @@ public final class BukkitBlockResolution {
}
Identifier key = Identifier.fromString(ix);
Optional<BlockData> bd = IrisServices.get(ExternalDataSVC.class).getBlockData(key);
ExternalDataSVC external = IrisServices.get(ExternalDataSVC.class);
Optional<BlockData> bd = external.getBlockData(key);
debug("Loading block data " + key);
if (bd.isPresent() && shouldPreventLeafDecay()) {
return external.withoutDecay(bd.get());
}
return bd.orElse(null);
}
@@ -27,9 +27,11 @@ import art.arcane.volmlib.nativelib.terrain.BlockStateKey;
import art.arcane.volmlib.nativelib.terrain.NativeBlockState;
import art.arcane.iris.generation.block.IrisCustomData;
import art.arcane.iris.generation.geometry.IrisBlockVector;
import art.arcane.iris.spi.IrisServices;
import art.arcane.volmlib.util.collection.KMap;
import org.bukkit.Axis;
import org.bukkit.Bukkit;
import org.bukkit.Material;
import org.bukkit.Tag;
import org.bukkit.block.BlockFace;
import org.bukkit.block.data.BlockData;
@@ -75,6 +77,8 @@ public final class BukkitBlockState implements NativeBlockState {
private byte updatable;
private byte foliage;
private byte treeBlock;
private byte log;
private byte leaves;
private byte foliagePlantable;
private byte decorant;
private byte storage;
@@ -318,12 +322,41 @@ public final class BukkitBlockState implements NativeBlockState {
public boolean isTreeBlock() {
byte cached = treeBlock;
if (cached == UNKNOWN) {
cached = flag(Tag.LOGS.isTagged(data.getMaterial()) || Tag.LEAVES.isTagged(data.getMaterial()));
cached = flag(isLog() || isLeaves());
treeBlock = cached;
}
return cached == TRUE;
}
@Override
public boolean isLog() {
byte cached = log;
if (cached == UNKNOWN) {
cached = flag(hasTag(Tag.LOGS));
log = cached;
}
return cached == TRUE;
}
@Override
public boolean isLeaves() {
byte cached = leaves;
if (cached == UNKNOWN) {
cached = flag(hasTag(Tag.LEAVES));
leaves = cached;
}
return cached == TRUE;
}
// A plugin's custom block shows up as some other material, so the plugin is asked for its own tags too.
private boolean hasTag(Tag<Material> tag) {
if (tag.isTagged(data.getMaterial())) {
return true;
}
ExternalDataSVC external = IrisServices.getOrNull(ExternalDataSVC.class);
return external != null && external.hasBlockTag(data, tag.getKey());
}
@Override
public boolean isFoliagePlantable() {
byte cached = foliagePlantable;
@@ -141,8 +141,9 @@ final class ObjectBlockPass {
private boolean handleCustomData(NativeBlockState data, boolean air, TileData tile, String marker, int xx, int yy, int zz) {
// Short-circuit order matters for cost only: the mantle read is paid solely
// for vine blocks. Both operands are pure, so the value is unchanged.
boolean wouldReplace = !is(ObjectPlaceMode.STRUCTURE_PIECE) && B.isVineBlock(data) && B.isSolid(state.placer.get(xx, yy, zz));
// for vine blocks and leaves. Both operands are pure, so the value is unchanged.
boolean wouldReplace = !is(ObjectPlaceMode.STRUCTURE_PIECE) && readsExistingBlock(data)
&& keepsExisting(data, state.placer.get(xx, yy, zz));
boolean place = shouldPlaceObjectBlock(is(ObjectPlaceMode.STRUCTURE_PIECE), air, wouldReplace);
if (data.isCustom() || place) {
state.placer.set(xx, yy, zz, data);
@@ -160,6 +161,15 @@ final class ObjectBlockPass {
return false;
}
private static boolean readsExistingBlock(NativeBlockState data) {
return B.isVineBlock(data) || B.isLeaves(data);
}
/** Vines never replace solid blocks, and leaves never cut a log: the trunk above the cut would float. */
static boolean keepsExisting(NativeBlockState data, NativeBlockState existing) {
return B.isVineBlock(data) ? B.isSolid(existing) : B.isLeaves(data) && B.isLog(existing);
}
private EditResult applyEdits(double[] exact, int index, NativeBlockState data, TileData tile) {
double exactX = exact[index * 3];
double exactY = exact[index * 3 + 1];
@@ -70,8 +70,7 @@ final class PlacementPalette {
NativeBlockState resolved = decayed[entry];
if (resolved == null) {
NativeBlockState data = source(entry);
resolved = preventingDecay && IrisProceduralBlocks.hasProperty(data, "distance")
&& "false".equals(IrisProceduralBlocks.propertyValue(data, "persistent"))
resolved = preventingDecay && "false".equals(IrisProceduralBlocks.propertyValue(data, "persistent"))
? data.withProperty("persistent", "true") : data;
decayed[entry] = resolved;
}
@@ -85,7 +85,7 @@ public class IrisCaveCarver3DCellKernelTest {
@Test
public void packStyleProfilesSharingFieldsMatchGoldenWriteSequence() throws Exception {
assertScenario(Scenario.PACK_SHARED, 0xdbe20a1ca87f9cc7L, 269035);
assertScenario(Scenario.PACK_SHARED, 0xb464b50d050e766dL, 288720);
}
@Test
@@ -0,0 +1,68 @@
package art.arcane.iris.generation.noise;
import art.arcane.volmlib.util.collection.KList;
import art.arcane.volmlib.util.noise.NoiseGenerator;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public final class IrisNoiseGeneratorFractureMirrorTest {
private static final long SEED = 71L;
private static final int GRID_SIZE = 40;
private static final double GRID_STEP = 7.3D;
private static final double MIRRORED = 1.0E-12D;
private static final int ALLOWED_MIRRORED_SAMPLES = 2;
@Test
public void aCompositeFractureIsNotMirroredAcrossTheDiagonal() {
IrisNoiseGenerator fractured = diagonalSymmetricRoot().setFracture(new KList<>(fracture()));
assertTrue(mirroredSamples(fractured) <= ALLOWED_MIRRORED_SAMPLES);
}
@Test
public void theDiagonalSymmetricRootIsMirroredWithoutAFracture() {
assertTrue(mirroredSamples(diagonalSymmetricRoot()) > ALLOWED_MIRRORED_SAMPLES);
}
private static int mirroredSamples(IrisNoiseGenerator noise) {
int mirroredSamples = 0;
for (int i = 0; i < GRID_SIZE; i++) {
for (int j = i + 1; j < GRID_SIZE; j++) {
double a = i * GRID_STEP;
double b = j * GRID_STEP + 0.5D;
if (Math.abs(noise.getNoise(SEED, a, b, null) - noise.getNoise(SEED, b, a, null)) < MIRRORED) {
mirroredSamples++;
}
}
}
return mirroredSamples;
}
private static IrisNoiseGenerator diagonalSymmetricRoot() {
IrisNoiseGenerator root = new IrisNoiseGenerator().setStyle(NoiseStyle.SIMPLEX.style()).setSeed(5L);
root.getGenerator(SEED, null, null).setGenerator(new DiagonalSymmetricGenerator());
return root;
}
private static IrisNoiseGenerator fracture() {
return new IrisNoiseGenerator().setStyle(NoiseStyle.SIMPLEX.style()).setSeed(9L).setZoom(40D).setOpacity(40D);
}
private static final class DiagonalSymmetricGenerator implements NoiseGenerator {
@Override
public double noise(double x) {
return noise(x, 0D, 0D);
}
@Override
public double noise(double x, double z) {
return noise(x, z, 0D);
}
@Override
public double noise(double x, double y, double z) {
return 0.5D + (Math.sin(x * 0.11D) + Math.sin(y * 0.11D) + Math.sin(z * 0.11D)) / 6D;
}
}
}
@@ -225,6 +225,11 @@ public final class IrisNoiseGeneratorFractureTest {
private static double reference(IrisNoiseGenerator node, long superSeed, double xv, double zv,
IrisData data, Engine engine) {
return reference(node, superSeed, xv, zv, data, engine, IrisNoiseGenerator.X_AXIS);
}
private static double reference(IrisNoiseGenerator node, long superSeed, double xv, double zv,
IrisData data, Engine engine, int axis) {
if (!node.isEnabled()) {
return node.getOffsetY();
}
@@ -234,14 +239,16 @@ public final class IrisNoiseGeneratorFractureTest {
for (IrisNoiseGenerator child : node.getFracture()) {
if (child.isEnabled()) {
double offset = child.getOpacity() / 2D;
x += reference(child, superSeed + node.getSeed() + salt, xv, zv, data, engine) - offset;
z += reference(child, superSeed + node.getSeed() + salt, zv, xv, data, engine) - offset;
long childSeed = superSeed + node.getSeed() + salt;
x += reference(child, childSeed, xv, zv, data, engine, IrisNoiseGenerator.X_AXIS) - offset;
z += reference(child, childSeed, xv, zv, data, engine, IrisNoiseGenerator.Z_AXIS) - offset;
}
salt += 819;
}
double sampleX = (x / node.getZoom()) + node.getOffsetX();
double sampleZ = (z / node.getZoom()) + node.getOffsetZ();
double result = node.getGenerator(superSeed, data, engine).noiseFast2D(sampleX, sampleZ) * node.getOpacity();
CNG generator = node.getGenerator(superSeed, data, engine).reseededForAxis(axis);
double result = generator.noiseFast2D(sampleX, sampleZ) * node.getOpacity();
result = node.isNegative() ? -result + node.getOpacity() : result;
result = (node.getExponent() != 1D ? result < 0D ? -Math.pow(-result, node.getExponent())
: Math.pow(result, node.getExponent()) : result) + node.getOffsetY();
@@ -18,7 +18,7 @@ public class NoiseSeedCohesionTest {
private static final long[] SEEDS = {813753L, -935731L, Long.MIN_VALUE};
private static final double[] COORDINATES = {-29_999_983.25D, -513D, -64D, -1D, 0D, 0.125D, 17D, 64D, 8192D, 29_999_983.25D};
private static final double PINNED_SAMPLE_SCALE = 1.0E9D;
private static final long PINNED_HASH_WINDOWS = 2597318414939754011L;
private static final long PINNED_HASH_WINDOWS = 6074446696529759284L;
private static final long PINNED_HASH_MAC = -602781966673993721L;
@Test
@@ -208,6 +208,16 @@ public class IrisDepositModifierOrderingTest {
return false;
}
@Override
public boolean isLog() {
return false;
}
@Override
public boolean isLeaves() {
return false;
}
@Override
public boolean isFoliagePlantable() {
return false;
@@ -466,6 +466,16 @@ public class IrisDepositModifierParityTest {
return false;
}
@Override
public boolean isLog() {
return false;
}
@Override
public boolean isLeaves() {
return false;
}
@Override
public boolean isFoliagePlantable() {
return false;
@@ -339,6 +339,16 @@ public class IrisPerfectionModifierColumnTest {
return false;
}
@Override
public boolean isLog() {
return false;
}
@Override
public boolean isLeaves() {
return false;
}
@Override
public boolean isFoliagePlantable() {
return false;
@@ -252,6 +252,16 @@ final class CompatFixtures {
return false;
}
@Override
public boolean isLog() {
return false;
}
@Override
public boolean isLeaves() {
return false;
}
@Override
public boolean isFoliagePlantable() {
return false;
@@ -38,17 +38,17 @@ public class IrisMathNoiseHotPathParityTest {
.setStyle(NoiseStyle.PERLIN_IRIS.style())
.setExponent(1.15D);
assertEquals(0.5084984235498856D, noiseGenerator.getNoise(9988L, 31.25D, -17.5D, null), 0D);
assertEquals(0.4646529061250786D, noiseGenerator.getNoise(9988L, 31.25D, -17.5D, null), 0D);
IrisGeneratorStyle style = NoiseStyle.IRIS_DOUBLE.style();
assertEquals(0.6224926166486215D, style.createNoCache(new RNG(112233L), null).noiseFast2D(42.5D, -19.75D), 0D);
assertEquals(0.6855680984398492D, style.createNoCache(new RNG(112233L), null).noiseFast2D(42.5D, -19.75D), 0D);
IrisGenerator generator = new IrisGenerator()
.setZoom(9.5D)
.setOpacity(0.91D)
.setComposite(new KList<IrisNoiseGenerator>().qadd(noiseGenerator));
assertEquals(0.43997118860461754D, generator.getHeight(63.0D, -27.0D, 445566L), 0D);
assertEquals(0.5023367573389593D, generator.getHeight(63.0D, -27.0D, 445566L), 0D);
}
@Test
@@ -119,8 +119,12 @@ public class BukkitPersistedBlockStateTest {
IrisCustomData custom = IrisCustomData.of(carrier, Identifier.fromString(key));
ExternalDataSVC provider = mock(ExternalDataSVC.class);
when(provider.getBlockData(Identifier.fromString(key))).thenReturn(Optional.of(custom));
try (MockedStatic<IrisServices> services = mockStatic(IrisServices.class)) {
when(provider.withoutDecay(custom)).thenReturn(custom);
IrisSettings configuration = new IrisSettings();
try (MockedStatic<IrisServices> services = mockStatic(IrisServices.class);
MockedStatic<IrisSettings> settings = mockStatic(IrisSettings.class)) {
services.when(() -> IrisServices.get(ExternalDataSVC.class)).thenReturn(provider);
settings.when(IrisSettings::get).thenReturn(configuration);
NativeBlockState decoded = registries.decodeBlockState(key);
assertEquals(key, decoded.key());
assertEquals(key, decoded.deferredPlacementKey());
@@ -172,6 +172,16 @@ public class IrisObjectTransformsNearestBlockIndexTest {
return false;
}
@Override
public boolean isLog() {
return false;
}
@Override
public boolean isLeaves() {
return false;
}
@Override
public boolean isFoliagePlantable() {
return false;
@@ -0,0 +1,92 @@
package art.arcane.iris.structure.object;
import art.arcane.volmlib.nativelib.terrain.NativeBlockState;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class ObjectLeafAndLogRulesTest {
@Test
public void leavesNeverCutALog() {
assertTrue(ObjectBlockPass.keepsExisting(leaves(), log()));
}
@Test
public void leavesReplaceOtherLeavesAndAir() {
assertFalse(ObjectBlockPass.keepsExisting(leaves(), leaves()));
assertFalse(ObjectBlockPass.keepsExisting(leaves(), null));
}
@Test
public void logsStillReplaceLeaves() {
assertFalse(ObjectBlockPass.keepsExisting(log(), leaves()));
}
@Test
public void vinesStillKeepSolidBlocks() {
NativeBlockState vine = mock(NativeBlockState.class);
when(vine.isVineBlock()).thenReturn(true);
NativeBlockState solid = mock(NativeBlockState.class);
when(solid.isSolid()).thenReturn(true);
assertTrue(ObjectBlockPass.keepsExisting(vine, solid));
assertFalse(ObjectBlockPass.keepsExisting(vine, leaves()));
}
@Test
public void customLeavesLosingTheirDecayAreMadePersistent() {
NativeBlockState persistent = mock(NativeBlockState.class);
NativeBlockState decaying = mock(NativeBlockState.class);
when(decaying.key()).thenReturn("craftengine:test/leaves[persistent=false,distance=7]");
when(decaying.withProperty("persistent", "true")).thenReturn(persistent);
assertSame(persistent, paletteOf(true, decaying).decayed(0));
}
@Test
public void customLeavesWithoutADistanceAreMadePersistentToo() {
NativeBlockState persistent = mock(NativeBlockState.class);
NativeBlockState decaying = mock(NativeBlockState.class);
when(decaying.key()).thenReturn("craftengine:test/leaves[persistent=false]");
when(decaying.withProperty("persistent", "true")).thenReturn(persistent);
assertSame(persistent, paletteOf(true, decaying).decayed(0));
}
@Test
public void decayIsLeftAloneWhenItIsNotPrevented() {
NativeBlockState decaying = mock(NativeBlockState.class);
when(decaying.key()).thenReturn("minecraft:oak_leaves[distance=3,persistent=false]");
assertSame(decaying, paletteOf(false, decaying).decayed(0));
}
@Test
public void blocksWithoutAPersistentPropertyAreLeftAlone() {
NativeBlockState stone = mock(NativeBlockState.class);
when(stone.key()).thenReturn("minecraft:stone");
assertSame(stone, paletteOf(true, stone).decayed(0));
}
private static PlacementPalette paletteOf(boolean preventingDecay, NativeBlockState state) {
return new PlacementPalette(new NativeBlockState[]{state}, new IrisObjectRotation(), 0, 0, 0,
preventingDecay, false, java.util.Set.of());
}
private static NativeBlockState leaves() {
NativeBlockState state = mock(NativeBlockState.class);
when(state.isLeaves()).thenReturn(true);
return state;
}
private static NativeBlockState log() {
NativeBlockState state = mock(NativeBlockState.class);
when(state.isLog()).thenReturn(true);
return state;
}
}
@@ -583,7 +583,17 @@ public class ObjectPlacementGoldenTest {
@Override
public boolean isTreeBlock() {
return material.endsWith("_log") || material.endsWith("_leaves");
return isLog() || isLeaves();
}
@Override
public boolean isLog() {
return material.endsWith("_log");
}
@Override
public boolean isLeaves() {
return material.endsWith("_leaves");
}
@Override
@@ -183,6 +183,16 @@ public class TreePlausibilizerTest {
return false;
}
@Override
public boolean isLog() {
return false;
}
@Override
public boolean isLeaves() {
return false;
}
@Override
public boolean isFoliagePlantable() {
return false;
@@ -89,6 +89,16 @@ public final class KeyedBlockState implements NativeBlockState {
return false;
}
@Override
public boolean isLog() {
return false;
}
@Override
public boolean isLeaves() {
return false;
}
@Override
public boolean isFoliagePlantable() {
return false;
@@ -136,6 +136,16 @@ public final class PlantBlockState implements NativeBlockState {
return false;
}
@Override
public boolean isLog() {
return false;
}
@Override
public boolean isLeaves() {
return false;
}
@Override
public boolean isFoliagePlantable() {
return false;
@@ -208,12 +208,21 @@ public final class StubPlatform implements IrisPlatform {
@Override
public boolean isTreeBlock() {
return isLog() || isLeaves();
}
@Override
public boolean isLog() {
String blockKey = blockKey();
return blockKey.endsWith("_log")
|| blockKey.endsWith("_wood")
|| blockKey.endsWith("_stem")
|| blockKey.endsWith("_hyphae")
|| blockKey.endsWith("_leaves");
|| blockKey.endsWith("_hyphae");
}
@Override
public boolean isLeaves() {
return blockKey().endsWith("_leaves");
}
@Override