From 6429b6ea5e11823d6713bf6785b2d11c0e1f441c Mon Sep 17 00:00:00 2001 From: piexel Date: Thu, 5 Nov 2026 21:28:16 +0100 Subject: [PATCH] Generate CraftEngine blocks in place, keep their leaves and tags Ported from v3e (31615dc05, ee7f54429, 92b02db15, 3ba277aa4). CraftEngine blocks now resolve to their own block data and generate with the chunk, warning once about properties the block does not have, so only furniture is placed after generation. A deferred block with a state finds its provider by its id alone. Where leaf decay is prevented, CraftEngine leaves are made persistent too, and logs and leaves include CraftEngine blocks with those tags, for IrisModule 501862b. --- .../iris/core/link/ExternalDataProvider.java | 20 +++++ .../link/data/CraftEngineDataProvider.java | 80 +++++++++++++------ .../iris/core/service/ExternalDataSVC.java | 26 +++++- .../bukkit/BukkitBlockResolution.java | 6 +- .../platform/bukkit/BukkitBlockState.java | 16 +++- .../service/ExternalDataSVCStateTest.java | 21 +++++ .../ObjectPlacementRegressionTest.java | 22 +++++ 7 files changed, 164 insertions(+), 27 deletions(-) create mode 100644 core/src/test/java/art/arcane/iris/core/service/ExternalDataSVCStateTest.java diff --git a/core/src/main/java/art/arcane/iris/core/link/ExternalDataProvider.java b/core/src/main/java/art/arcane/iris/core/link/ExternalDataProvider.java index 3afb4819c..3717cd3ad 100644 --- a/core/src/main/java/art/arcane/iris/core/link/ExternalDataProvider.java +++ b/core/src/main/java/art/arcane/iris/core/link/ExternalDataProvider.java @@ -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; @@ -113,6 +114,25 @@ public abstract class ExternalDataProvider { */ public void processUpdate(@NotNull Engine engine, @NotNull Block block, @NotNull Identifier blockId) {} + /** + * @param data The block to keep + * @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 + * @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. * diff --git a/core/src/main/java/art/arcane/iris/core/link/data/CraftEngineDataProvider.java b/core/src/main/java/art/arcane/iris/core/link/data/CraftEngineDataProvider.java index 58f77eecf..bf199885e 100644 --- a/core/src/main/java/art/arcane/iris/core/link/data/CraftEngineDataProvider.java +++ b/core/src/main/java/art/arcane/iris/core/link/data/CraftEngineDataProvider.java @@ -8,6 +8,7 @@ import art.arcane.iris.core.nms.container.BlockProperty; import art.arcane.iris.core.service.ExternalDataSVC; import art.arcane.volmlib.util.cache.CacheKey; import art.arcane.iris.engine.framework.Engine; +import art.arcane.iris.spi.IrisLogging; import art.arcane.iris.util.common.data.IrisCustomData; import art.arcane.volmlib.util.collection.KMap; import art.arcane.volmlib.util.math.RNG; @@ -23,6 +24,7 @@ import net.momirealms.craftengine.core.block.property.Property; import net.momirealms.craftengine.core.entity.furniture.FurnitureDefinition; import net.momirealms.craftengine.core.util.Key; import org.bukkit.Location; +import org.bukkit.NamespacedKey; import org.bukkit.block.Block; import org.bukkit.block.data.BlockData; import org.bukkit.inventory.ItemStack; @@ -33,10 +35,12 @@ import java.util.Collection; import java.util.List; import java.util.Map; import java.util.MissingResourceException; +import java.util.Optional; import java.util.function.Function; import java.util.stream.Stream; public class CraftEngineDataProvider extends ExternalDataProvider { + private static final String PERSISTENT_PROPERTY = "persistent"; private static final BlockProperty[] FURNITURE_PROPERTIES = new BlockProperty[]{ BlockProperty.ofBoolean("randomYaw", false), BlockProperty.ofDouble("yaw", 0, 0, 360f, false, true), @@ -89,13 +93,38 @@ public class CraftEngineDataProvider extends ExternalDataProvider { @Override public @NotNull BlockData getBlockData(@NotNull Identifier blockId, @NotNull KMap state) throws MissingResourceException { Key key = Key.of(blockId.namespace(), blockId.key()); - if (CraftEngineBlocks.byId(key) == null && CraftEngineFurniture.byId(key) == null) { + BlockDefinition customBlock = CraftEngineBlocks.byId(key); + if (customBlock != null) { + return CraftEngineBlocks.getBukkitBlockData(stateOf(customBlock, key, state)); + } + if (CraftEngineFurniture.byId(key) == null) { throw new MissingResourceException("Failed to find BlockData!", blockId.namespace(), blockId.key()); } return IrisCustomData.of(BukkitBlockResolution.getAir(), ExternalDataSVC.buildState(blockId, state)); } + private static ImmutableBlockState stateOf(BlockDefinition block, Key key, Map state) { + ImmutableBlockState blockState = block.defaultState(); + for (Map.Entry entry : state.entrySet()) { + Property property = block.getProperty(entry.getKey()); + if (property == null) { + IrisLogging.warnOnce("craftengine:property:" + key + ":" + entry.getKey(), + "Invalid property %s for block %s", entry.getKey(), key); + continue; + } + Optional value = property.optional(entry.getValue()); + if (value.isEmpty()) { + IrisLogging.warnOnce("craftengine:value:" + key + ":" + entry.getKey() + "=" + entry.getValue(), + "Invalid property %s=%s for block %s", entry.getKey(), entry.getValue(), key); + continue; + } + blockState = ImmutableBlockState.with(blockState, property, value.get()); + } + return blockState; + } + + /** Only furniture is placed after generation: blocks resolve to their own block data and generate in place. */ @Override public void processUpdate(@NotNull Engine engine, @NotNull Block block, @NotNull Identifier blockId) { art.arcane.iris.core.nms.container.Pair> statePair = ExternalDataSVC.parseState(blockId); @@ -103,28 +132,6 @@ public class CraftEngineDataProvider extends ExternalDataProvider { KMap state = statePair.getB(); Key key = Key.of(baseBlockId.namespace(), baseBlockId.key()); - BlockDefinition customBlock = CraftEngineBlocks.byId(key); - if (customBlock != null) { - ImmutableBlockState blockState = customBlock.defaultState(); - - for (Map.Entry entry : state.entrySet()) { - Property property = customBlock.getProperty(entry.getKey()); - if (property == null) { - continue; - } - - Comparable tag = property.optional(entry.getValue()).orElse(null); - if (tag == null) { - continue; - } - - blockState = ImmutableBlockState.with(blockState, property, tag); - } - - CraftEngineBlocks.place(block.getLocation(), blockState, false); - return; - } - FurnitureDefinition furniture = CraftEngineFurniture.byId(key); if (furniture == null) { return; @@ -135,6 +142,33 @@ public class CraftEngineDataProvider extends ExternalDataProvider { CraftEngineFurniture.place(location, furniture, variant, false); } + @Override + public @NotNull BlockData withoutDecay(@NotNull BlockData data) { + ImmutableBlockState state = customStateOf(data); + if (state == null) { + return data; + } + Property persistent = state.getProperty(PERSISTENT_PROPERTY); + if (persistent == null || persistent.valueClass() != Boolean.class || state.get(persistent)) { + return data; + } + return CraftEngineBlocks.getBukkitBlockData(state.with(persistent, true)); + } + + @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) { + if (data instanceof IrisCustomData) { + return null; + } + ImmutableBlockState state = CraftEngineBlocks.getCustomBlockState(data); + return state == null || state.isEmpty() ? null : state; + } + private static Location parseYawAndPitch(@NotNull Engine engine, @NotNull Block block, @NotNull Map state) { Location location = block.getLocation(); long seed = engine.getSeedManager().getSeed() + CacheKey.key(block.getX(), block.getZ()) + block.getY(); diff --git a/core/src/main/java/art/arcane/iris/core/service/ExternalDataSVC.java b/core/src/main/java/art/arcane/iris/core/service/ExternalDataSVC.java index fe5fc0e4e..0aaa2f80e 100644 --- a/core/src/main/java/art/arcane/iris/core/service/ExternalDataSVC.java +++ b/core/src/main/java/art/arcane/iris/core/service/ExternalDataSVC.java @@ -31,6 +31,7 @@ import art.arcane.volmlib.util.collection.KMap; import art.arcane.iris.util.common.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; @@ -166,8 +167,26 @@ public class ExternalDataSVC implements IrisService { return stack.isEmpty() ? OptionalInt.empty() : OptionalInt.of(stack.get().getType().getMaxDurability()); } + /** 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) { + 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)); + } + + /** Places the provider block {@code blockId}, which may carry a state like {@code ns:chair[yaw=90]}. */ public void processUpdate(Engine engine, Block block, Identifier blockId) { - Optional provider = activeProviders.stream().filter(p -> p.isValidProvider(blockId, DataType.BLOCK)).findFirst(); + Identifier baseBlockId = withoutState(blockId); + Optional provider = activeProviders.stream().filter(p -> p.isValidProvider(baseBlockId, DataType.BLOCK)).findFirst(); if (provider.isEmpty()) { IrisLogging.warnOnce("external-provider:block:" + blockId, "No matching Provider found for modded material \"%s\"!", blockId); @@ -205,6 +224,11 @@ public class ExternalDataSVC implements IrisService { .toList(); } + static Identifier withoutState(Identifier key) { + int open = key.key().indexOf('['); + return open < 0 ? key : new Identifier(key.namespace(), key.key().substring(0, open)); + } + public static Pair> parseState(Identifier key) { String raw = key.key(); int open = raw.indexOf('['); diff --git a/core/src/main/java/art/arcane/iris/platform/bukkit/BukkitBlockResolution.java b/core/src/main/java/art/arcane/iris/platform/bukkit/BukkitBlockResolution.java index 189232f40..c3e61b0ea 100644 --- a/core/src/main/java/art/arcane/iris/platform/bukkit/BukkitBlockResolution.java +++ b/core/src/main/java/art/arcane/iris/platform/bukkit/BukkitBlockResolution.java @@ -100,8 +100,12 @@ public final class BukkitBlockResolution { } Identifier key = Identifier.fromString(ix); - Optional bd = IrisServices.get(ExternalDataSVC.class).getBlockData(key); + ExternalDataSVC external = IrisServices.get(ExternalDataSVC.class); + Optional bd = external.getBlockData(key); debug("Loading block data " + key); + if (bd.isPresent() && shouldPreventLeafDecay()) { + return external.withoutDecay(bd.get()); + } return bd.orElse(null); } diff --git a/core/src/main/java/art/arcane/iris/platform/bukkit/BukkitBlockState.java b/core/src/main/java/art/arcane/iris/platform/bukkit/BukkitBlockState.java index af939b48c..13da39571 100644 --- a/core/src/main/java/art/arcane/iris/platform/bukkit/BukkitBlockState.java +++ b/core/src/main/java/art/arcane/iris/platform/bukkit/BukkitBlockState.java @@ -19,10 +19,13 @@ package art.arcane.iris.platform.bukkit; import art.arcane.iris.core.nms.INMS; +import art.arcane.iris.core.service.ExternalDataSVC; +import art.arcane.iris.spi.IrisServices; import art.arcane.iris.spi.PlatformBlockState; import art.arcane.iris.spi.PlatformFace; import art.arcane.iris.util.common.data.IrisCustomData; import org.bukkit.Bukkit; +import org.bukkit.Material; import org.bukkit.Tag; import org.bukkit.block.BlockFace; import org.bukkit.block.BlockSupport; @@ -272,7 +275,7 @@ public final class BukkitBlockState implements PlatformBlockState { public boolean isLog() { Boolean cached = log; if (cached == null) { - cached = Tag.LOGS.isTagged(data.getMaterial()); + cached = hasTag(Tag.LOGS); log = cached; } return cached; @@ -282,12 +285,21 @@ public final class BukkitBlockState implements PlatformBlockState { public boolean isLeaves() { Boolean cached = leaves; if (cached == null) { - cached = Tag.LEAVES.isTagged(data.getMaterial()); + cached = hasTag(Tag.LEAVES); leaves = cached; } return cached; } + // 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 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 hasGravity() { Boolean cached = gravity; diff --git a/core/src/test/java/art/arcane/iris/core/service/ExternalDataSVCStateTest.java b/core/src/test/java/art/arcane/iris/core/service/ExternalDataSVCStateTest.java new file mode 100644 index 000000000..f21805b14 --- /dev/null +++ b/core/src/test/java/art/arcane/iris/core/service/ExternalDataSVCStateTest.java @@ -0,0 +1,21 @@ +package art.arcane.iris.core.service; + +import art.arcane.iris.core.link.Identifier; +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class ExternalDataSVCStateTest { + @Test + public void aDeferredBlockFindsItsProviderByTheIdWithoutItsState() { + assertEquals(new Identifier("craftengine", "chair"), + ExternalDataSVC.withoutState(new Identifier("craftengine", "chair[yaw=90,randomPitch=true]"))); + } + + @Test + public void anIdWithoutStateStaysAsItIs() { + Identifier chair = new Identifier("craftengine", "chair"); + + assertEquals(chair, ExternalDataSVC.withoutState(chair)); + } +} diff --git a/core/src/test/java/art/arcane/iris/engine/object/placement/ObjectPlacementRegressionTest.java b/core/src/test/java/art/arcane/iris/engine/object/placement/ObjectPlacementRegressionTest.java index 50b0cf861..49d339efe 100644 --- a/core/src/test/java/art/arcane/iris/engine/object/placement/ObjectPlacementRegressionTest.java +++ b/core/src/test/java/art/arcane/iris/engine/object/placement/ObjectPlacementRegressionTest.java @@ -71,6 +71,7 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertSame; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; +import static org.mockito.ArgumentMatchers.anyBoolean; import static org.mockito.ArgumentMatchers.anyInt; import static org.mockito.ArgumentMatchers.anyDouble; import static org.mockito.ArgumentMatchers.anyString; @@ -95,6 +96,7 @@ public class ObjectPlacementRegressionTest { IrisPlatforms.unbind(); PlatformRegistries registries = mock(PlatformRegistries.class); when(registries.block(anyString())).thenAnswer(invocation -> registryState(invocation.getArgument(0))); + when(registries.blockOrNull(anyString(), anyBoolean())).thenAnswer(invocation -> registryState(invocation.getArgument(0))); IrisPlatform platform = mock(IrisPlatform.class); when(platform.registries()).thenReturn(registries); IrisPlatforms.bind(platform); @@ -210,6 +212,23 @@ public class ObjectPlacementRegressionTest { assertTrue(placer.sampledColumns().size() >= 25); } + @Test + public void leavesNeverCutALogAnotherObjectPlaced() { + PlatformBlockState log = registryState("minecraft:oak_log"); + PlatformBlockState leaves = registryState("minecraft:oak_leaves"); + RecordingPlacer placer = new RecordingPlacer(null); + IrisObject trunk = new IrisObject(1, 1, 1); + trunk.setUnsigned(0, 0, 0, log); + IrisObject canopy = new IrisObject(1, 1, 1); + canopy.setUnsigned(0, 0, 0, leaves); + + placed(trunk, 0, ANCHOR_Y, 0, placer, placement().build(), new RNG(2L), data); + placed(canopy, 0, ANCHOR_Y, 0, placer, placement().build(), new RNG(2L), data); + + assertEquals(1, placer.writes().size()); + assertEquals("minecraft:oak_log", placer.writes().get(0).state().key()); + } + @Test public void snowUsesOnlyBlocksThatWereWritten() { RecordingPlacer placer = new RecordingPlacer(null); @@ -732,12 +751,15 @@ public class ObjectPlacementRegressionTest { return PlatformBlockStates.namespaced(builtIn.key()); } + /** A platform state mock; logs and leaves are told by their key, as the platform tells them by tag. */ private static PlatformBlockState state(String key, boolean solid) { PlatformBlockState state = mock(PlatformBlockState.class); when(state.key()).thenReturn(key); when(state.materialKey()).thenReturn(key); when(state.isSolid()).thenReturn(solid); when(state.isOccluding()).thenReturn(solid); + when(state.isLog()).thenReturn(key.endsWith("_log")); + when(state.isLeaves()).thenReturn(key.endsWith("_leaves")); return state; }