Keep custom leaves from decaying and from cutting logs

- Implement the log and leaves checks added in https://github.com/VolmitSoftware/VolmLib/pull/4 so master compiles against the current VolmLib again
- Let external providers make a block persistent and report block tags
- Make CraftEngine leaves with a persistent property keep their leaves when decay is prevented
- Count CraftEngine blocks with the logs and leaves tags as logs and leaves
- Skip leaf placements that would replace a log another object placed
- Make object placement persist leaves by their persistent property, not their distance property
This commit is contained in:
piexel committed 2026-11-23 23:31:03 +01:00
1 parent 435ed10648
commit 1e04f61957
22 files changed
+402 -10

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();
}
@@ -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);
@@ -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;
}
@@ -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;
@@ -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