diff --git a/adapters/bukkit/nms/v26_3_R1/src/test/java/art/arcane/iris/probe/HeadlessGroundSurvivalTest.java b/adapters/bukkit/nms/v26_3_R1/src/test/java/art/arcane/iris/probe/HeadlessGroundSurvivalTest.java index 6e49d3ad3..3b60195ba 100644 --- a/adapters/bukkit/nms/v26_3_R1/src/test/java/art/arcane/iris/probe/HeadlessGroundSurvivalTest.java +++ b/adapters/bukkit/nms/v26_3_R1/src/test/java/art/arcane/iris/probe/HeadlessGroundSurvivalTest.java @@ -1,23 +1,22 @@ package art.arcane.iris.probe; -import art.arcane.volmlib.nativelib.common.block.GroundSurvival; -import art.arcane.volmlib.nativelib.minecraft26_2.modded.ModdedBlockState; +import art.arcane.iris.modded.ModdedGroundSurvival; +import art.arcane.iris.platform.bukkit.ServerGroundSurvival; import net.minecraft.world.level.block.Block; import net.minecraft.world.level.block.Blocks; import org.junit.BeforeClass; import org.junit.Test; -import java.util.Map; - import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; -import static org.junit.Assert.assertTrue; /** Both platforms ask the server's own canSurvive rule where a plant may stand, against the real block tags. */ public final class HeadlessGroundSurvivalTest { + private static ServerGroundSurvival bukkit; + @BeforeClass - public static void bootstrap() { + public static void bootstrap() throws ReflectiveOperationException { HeadlessNativeBootstrap.initialize(); + bukkit = ServerGroundSurvival.connect(HeadlessGroundSurvivalTest.class.getClassLoader()); } @Test @@ -45,7 +44,7 @@ public final class HeadlessGroundSurvivalTest { assertPlacement(true, Blocks.SUGAR_CANE, ground); } for (Block ground : new Block[]{Blocks.STONE, Blocks.GRAVEL, Blocks.CLAY, Blocks.DIRT_PATH, - Blocks.FARMLAND, Blocks.AIR, Blocks.WATER}) { + Blocks.FARMLAND, Blocks.WATER}) { assertPlacement(false, Blocks.SUGAR_CANE, ground); } } @@ -56,7 +55,7 @@ public final class HeadlessGroundSurvivalTest { for (Block ground : new Block[]{Blocks.CRIMSON_NYLIUM, Blocks.WARPED_NYLIUM, Blocks.SOUL_SOIL}) { assertPlacement(true, plant, ground); } - for (Block ground : new Block[]{Blocks.NETHERRACK, Blocks.STONE, Blocks.GRAVEL, Blocks.AIR}) { + for (Block ground : new Block[]{Blocks.NETHERRACK, Blocks.STONE, Blocks.GRAVEL}) { assertPlacement(false, plant, ground); } } @@ -68,26 +67,10 @@ public final class HeadlessGroundSurvivalTest { assertPlacement(true, Blocks.VINE, Blocks.SAND); } - @Test - public void deadBushStaysOffGrassAsPacksExpect() { - assertTrue(moddedPlacement(Blocks.DEAD_BUSH, Blocks.SAND)); - assertFalse(moddedPlacement(Blocks.DEAD_BUSH, Blocks.GRASS_BLOCK)); - } - private static void assertPlacement(boolean expected, Block plant, Block ground) { String pair = plant + " on " + ground; - assertEquals("modded " + pair, expected, moddedPlacement(plant, ground)); - if (!ground.defaultBlockState().isAir()) { - assertEquals("bukkit native " + pair, expected, - new GroundSurvival().canSurviveOn(plant.defaultBlockState(), ground.defaultBlockState())); - } - } - - private static boolean moddedPlacement(Block plant, Block ground) { - return wrap(plant).canPlaceOnto(wrap(ground)); - } - - private static ModdedBlockState wrap(Block block) { - return ModdedBlockState.deferred(block.defaultBlockState(), Map.of(), "iris:test"); + assertEquals("modded " + pair, expected, + new ModdedGroundSurvival().canSurviveOn(plant.defaultBlockState(), ground.defaultBlockState())); + assertEquals("bukkit " + pair, expected, bukkit.canSurviveOn(plant.defaultBlockState(), ground.defaultBlockState())); } } diff --git a/adapters/bukkit/plugin/src/main/java/art/arcane/iris/platform/bukkit/BlockRulesSVC.java b/adapters/bukkit/plugin/src/main/java/art/arcane/iris/platform/bukkit/BlockRulesSVC.java index ed94ded2b..087e25389 100644 --- a/adapters/bukkit/plugin/src/main/java/art/arcane/iris/platform/bukkit/BlockRulesSVC.java +++ b/adapters/bukkit/plugin/src/main/java/art/arcane/iris/platform/bukkit/BlockRulesSVC.java @@ -1,24 +1,26 @@ package art.arcane.iris.platform.bukkit; -import art.arcane.iris.platform.bukkit.nms.INMS; +import art.arcane.iris.generation.block.PlantPlacement; import art.arcane.iris.platform.bukkit.plugin.IrisService; import io.papermc.paper.event.server.ServerResourcesReloadedEvent; import org.bukkit.event.EventHandler; -/** Lets the server's placement rules apply again after a resource reload, which may have changed the block tags they read. */ +/** Lets the server's own rules decide where plants can stand, and asks them again after a resource reload. */ public class BlockRulesSVC implements IrisService { + private PlantPlacement.ServerRule previousRule; + @Override public void onEnable() { + previousRule = PlantPlacement.bindServerRule(BukkitPlacementRules::canPlaceOnto); } @Override public void onDisable() { + PlantPlacement.restoreServerRule(previousRule); } @EventHandler public void onResourcesReloaded(ServerResourcesReloadedEvent event) { - if (INMS.isBound()) { - INMS.get().forgetBlockRules(); - } + BukkitPlacementRules.forgetServerRules(); } } diff --git a/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedEngineBootstrap.java b/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedEngineBootstrap.java index b2b94a0d9..763e282e2 100644 --- a/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedEngineBootstrap.java +++ b/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedEngineBootstrap.java @@ -56,6 +56,7 @@ import art.arcane.iris.generation.runtime.EngineWorldManagerProvider; import art.arcane.iris.generation.runtime.PreservationRegistry; import art.arcane.iris.generation.block.BlockDataMergeSupport; import art.arcane.iris.structure.object.IrisObjectRotation; +import art.arcane.iris.generation.block.PlantPlacement; import art.arcane.iris.generation.block.TileData; import art.arcane.iris.modded.api.ModdedCustomContentRegistry; import art.arcane.iris.modded.catalog.ModdedWorldgenCatalogs; @@ -497,6 +498,9 @@ public final class ModdedEngineBootstrap { DecoratorPlatformHooks.Bindings previousDecoratorBindings = DecoratorPlatformHooks.bind(decoratorHooks, decoratorHooks); rollback.add(() -> DecoratorPlatformHooks.restore(previousDecoratorBindings)); + PlantPlacement.ServerRule previousPlacementRule = PlantPlacement.bindServerRule(new ModdedGroundSurvival()); + rollback.add(() -> PlantPlacement.restoreServerRule(previousPlacementRule)); + ModdedPreservationService preservation = createdServices.register( ModdedPreservationService.class, new ModdedPreservationService()); createdServices.register(ModdedLogFilterService.class, new ModdedLogFilterService()); diff --git a/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedGroundSurvival.java b/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedGroundSurvival.java new file mode 100644 index 000000000..479e9a002 --- /dev/null +++ b/adapters/modded-common/src/main/java/art/arcane/iris/modded/ModdedGroundSurvival.java @@ -0,0 +1,146 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.modded; + +import art.arcane.iris.generation.block.PlantPlacement; +import art.arcane.iris.spi.IrisLogging; +import art.arcane.volmlib.nativelib.terrain.NativeBlockState; +import net.minecraft.core.BlockPos; +import net.minecraft.core.registries.BuiltInRegistries; +import net.minecraft.world.level.LevelReader; +import net.minecraft.world.level.block.Block; +import net.minecraft.world.level.block.Blocks; +import net.minecraft.world.level.block.state.BlockState; + +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** Asks the game's own canSurvive rule whether a plant stands on a ground block, in a tiny level holding just the two. */ +public final class ModdedGroundSurvival implements PlantPlacement.ServerRule { + private static final BlockPos PLANT_POS = new BlockPos(0, 64, 0); + private static final BlockPos GROUND_POS = PLANT_POS.below(); + private static final int MIN_BUILD_Y = -64; + private static final int BUILD_HEIGHT = 384; + // Crops need 8 or more and mushrooms less than 13. A chunk that is still generating has no light to ask for. + private static final int LIGHT_EVERY_PLANT_ACCEPTS = 10; + + private final Map> survivesOn = new ConcurrentHashMap<>(); + private final Map restsOnGround = new ConcurrentHashMap<>(); + + @Override + public boolean canPlaceOnto(NativeBlockState plant, NativeBlockState ground) { + return canSurviveOn((BlockState) plant.placementHandle(), (BlockState) ground.placementHandle()); + } + + public boolean canSurviveOn(BlockState plant, BlockState ground) { + return survivesOn.computeIfAbsent(plant, ignored -> new ConcurrentHashMap<>()) + .computeIfAbsent(ground, known -> decide(plant, known)); + } + + private boolean decide(BlockState plant, BlockState ground) { + try { + return survives(plant, ground) || !restsOnGround(plant); + } catch (RuntimeException e) { + IrisLogging.warn("Could not ask whether " + plant + " survives on " + ground + ", letting it stand: " + e); + return true; + } + } + + // Wall and ceiling blocks fail on every ground, which says nothing about the ground they were decorated onto. + private boolean restsOnGround(BlockState plant) { + return restsOnGround.computeIfAbsent(plant, this::survivesOnSomeGround); + } + + private boolean survivesOnSomeGround(BlockState plant) { + for (Block block : BuiltInRegistries.BLOCK) { + if (survives(plant, block.defaultBlockState())) { + return true; + } + } + return false; + } + + private boolean survives(BlockState plant, BlockState ground) { + return plant.canSurvive(levelAround(plant, ground), PLANT_POS); + } + + // A proxy, since LevelReader gains abstract methods with every release and loaders add their own. + private LevelReader levelAround(BlockState plant, BlockState ground) { + InvocationHandler answers = (proxy, method, args) -> answer(proxy, method, args, plant, ground); + return (LevelReader) Proxy.newProxyInstance(LevelReader.class.getClassLoader(), new Class[]{LevelReader.class}, answers); + } + + private Object answer(Object proxy, Method method, Object[] args, BlockState plant, BlockState ground) throws Throwable { + switch (method.getName()) { + case "getBlockState", "getBlockStateIfLoaded" -> { + return stateAt((BlockPos) args[0], plant, ground); + } + case "getFluidState", "getFluidIfLoaded" -> { + return stateAt((BlockPos) args[0], plant, ground).getFluidState(); + } + case "getRawBrightness", "getMaxLocalRawBrightness", "getBrightness" -> { + return LIGHT_EVERY_PLANT_ACCEPTS; + } + case "getBlockEntity" -> { + return null; + } + case "getMinY", "getMinBuildHeight" -> { + return MIN_BUILD_Y; + } + case "getHeight" -> { + if (args == null) { + return BUILD_HEIGHT; + } + } + case "isClientSide" -> { + return false; + } + case "hashCode" -> { + return System.identityHashCode(proxy); + } + case "equals" -> { + return proxy == args[0]; + } + case "toString" -> { + return "ground survival level"; + } + default -> { + } + } + + if (method.isDefault()) { + return InvocationHandler.invokeDefault(proxy, method, args); + } + throw new IllegalStateException("Ground survival level can't answer " + method.getName()); + } + + // Water beside the ground for sugar cane and air beside the plant for cactus, so only the ground can refuse. + private BlockState stateAt(BlockPos pos, BlockState plant, BlockState ground) { + if (pos.equals(PLANT_POS)) { + return plant; + } + if (pos.equals(GROUND_POS)) { + return ground; + } + return pos.getY() == GROUND_POS.getY() ? Blocks.WATER.defaultBlockState() : Blocks.AIR.defaultBlockState(); + } +} diff --git a/adapters/modded-common/src/test/java/art/arcane/iris/modded/ModdedBlockResolutionContractTest.java b/adapters/modded-common/src/test/java/art/arcane/iris/modded/ModdedBlockResolutionContractTest.java index f7949059f..81dd2a00d 100644 --- a/adapters/modded-common/src/test/java/art/arcane/iris/modded/ModdedBlockResolutionContractTest.java +++ b/adapters/modded-common/src/test/java/art/arcane/iris/modded/ModdedBlockResolutionContractTest.java @@ -102,10 +102,46 @@ public class ModdedBlockResolutionContractTest { } @Test - public void cactusIsADecorant() { + public void cactusPlacementRequiresNativeCactusSupport() { + assertTrue(NativeBlockProperties.canPlaceOnto(Blocks.CACTUS, Blocks.CACTUS)); + assertTrue(NativeBlockProperties.canPlaceOnto(Blocks.CACTUS, Blocks.SAND)); + assertTrue(NativeBlockProperties.canPlaceOnto(Blocks.CACTUS, Blocks.RED_SAND)); + assertFalse(NativeBlockProperties.canPlaceOnto(Blocks.CACTUS, Blocks.STONE)); assertTrue(NativeBlockProperties.isDecorant(Blocks.CACTUS.defaultBlockState())); } + @Test + public void sugarCanePlacementRequiresNativeSubstrate() { + for (Block block : new Block[]{Blocks.SUGAR_CANE, Blocks.GRASS_BLOCK, Blocks.DIRT, Blocks.COARSE_DIRT, + Blocks.PODZOL, Blocks.MYCELIUM, Blocks.ROOTED_DIRT, Blocks.MOSS_BLOCK, Blocks.PALE_MOSS_BLOCK, + Blocks.MUD, Blocks.MUDDY_MANGROVE_ROOTS, Blocks.SAND, Blocks.RED_SAND, Blocks.SUSPICIOUS_SAND}) { + assertTrue(block.toString(), NativeBlockProperties.canPlaceOnto(Blocks.SUGAR_CANE, block)); + } + for (Block block : new Block[]{Blocks.STONE, Blocks.GRAVEL, Blocks.CLAY, Blocks.DIRT_PATH, + Blocks.FARMLAND, Blocks.AIR, Blocks.WATER}) { + assertFalse(block.toString(), NativeBlockProperties.canPlaceOnto(Blocks.SUGAR_CANE, block)); + } + } + + @Test + public void netherGroundCoverAcceptsNyliumAndSoulSoilButRejectsBarrenBlocks() { + for (Block plant : new Block[]{Blocks.CRIMSON_ROOTS, Blocks.WARPED_ROOTS, Blocks.NETHER_SPROUTS}) { + for (Block substrate : new Block[]{Blocks.CRIMSON_NYLIUM, Blocks.WARPED_NYLIUM, + Blocks.SOUL_SOIL, Blocks.GRASS_BLOCK, Blocks.COARSE_DIRT, Blocks.PODZOL}) { + assertTrue(plant + " on " + substrate, NativeBlockProperties.canPlaceOnto(plant, substrate)); + } + for (Block substrate : new Block[]{Blocks.NETHERRACK, Blocks.SOUL_SAND, + Blocks.STONE, Blocks.GRAVEL, Blocks.AIR, Blocks.WATER}) { + assertFalse(plant + " on " + substrate, NativeBlockProperties.canPlaceOnto(plant, substrate)); + } + } + for (Block fungus : new Block[]{Blocks.CRIMSON_FUNGUS, Blocks.WARPED_FUNGUS}) { + for (Block substrate : new Block[]{Blocks.CRIMSON_NYLIUM, Blocks.WARPED_NYLIUM, Blocks.SOUL_SOIL}) { + assertTrue(fungus + " on " + substrate, NativeBlockProperties.canPlaceOnto(fungus, substrate)); + } + } + } + @Test public void sulfurAndDripstoneTipsReceivePostLoadUpdates() { for (Block block : new Block[]{Blocks.SULFUR_SPIKE, Blocks.POINTED_DRIPSTONE}) { diff --git a/build.gradle b/build.gradle index 3b2bfe364..54351d54b 100644 --- a/build.gradle +++ b/build.gradle @@ -812,7 +812,7 @@ project(':adapters:bukkit:nms:v26_3_R1') { 'com.github.VolmitSoftware.VolmLib:native-minecraft26_2-sources:master-SNAPSHOT') List headlessNativeTypes = ['ModdedBlockState', 'ModdedBiome', 'NativeBlockProperties', 'NativeBlockResolver', 'NativeBlockFaces', 'NativeStateRotator', 'NativeStateMerger', - 'NativeRegistryAccess', 'ModdedItem', 'ModdedEntityType', 'NativeTileReader', 'NativeTileData', 'GroundSurvival'] + 'NativeRegistryAccess', 'ModdedItem', 'ModdedEntityType', 'NativeTileReader', 'NativeTileData'] TaskProvider unpackHeadlessNativeSources = tasks.register('unpackHeadlessNativeSources', Sync) { dependsOn(headlessNativeSources) inputs.property('nativeTypes', headlessNativeTypes) @@ -821,7 +821,8 @@ project(':adapters:bukkit:nms:v26_3_R1') { } from(rootProject.file('adapters/modded-common/src/main/java')) { include('art/arcane/iris/modded/ModdedDecoratorHooks.java', - 'art/arcane/iris/modded/ModdedStateRotator.java', 'art/arcane/iris/modded/ModdedTileData.java') + 'art/arcane/iris/modded/ModdedStateRotator.java', 'art/arcane/iris/modded/ModdedTileData.java', + 'art/arcane/iris/modded/ModdedGroundSurvival.java') } into(layout.buildDirectory.dir('generated/sources/headless-native')) } diff --git a/core/src/main/java/art/arcane/iris/generation/block/B.java b/core/src/main/java/art/arcane/iris/generation/block/B.java index f9bdbd994..8a3dfe7c7 100644 --- a/core/src/main/java/art/arcane/iris/generation/block/B.java +++ b/core/src/main/java/art/arcane/iris/generation/block/B.java @@ -121,7 +121,7 @@ public class B { } public static boolean canPlaceOnto(NativeBlockState mat, NativeBlockState onto) { - return mat != null && onto != null && mat.canPlaceOnto(onto); + return mat != null && onto != null && PlantPlacement.canPlaceOnto(mat, onto); } public static boolean matches(NativeBlockState filter, NativeBlockState state) { diff --git a/core/src/main/java/art/arcane/iris/generation/block/PlantPlacement.java b/core/src/main/java/art/arcane/iris/generation/block/PlantPlacement.java new file mode 100644 index 000000000..dd8c0204d --- /dev/null +++ b/core/src/main/java/art/arcane/iris/generation/block/PlantPlacement.java @@ -0,0 +1,65 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.generation.block; + +import art.arcane.volmlib.nativelib.terrain.NativeBlockState; + +/** + * Where a plant can stand. A platform that can ask the server's own placement rules binds them here; without one, + * each block state answers from Iris's block table. + */ +public final class PlantPlacement { + private static final String DEAD_BUSH = "minecraft:dead_bush"; + private static final String GRASS_BLOCK = "minecraft:grass_block"; + private static volatile ServerRule serverRule; + + private PlantPlacement() { + } + + public interface ServerRule { + boolean canPlaceOnto(NativeBlockState plant, NativeBlockState ground); + } + + public static synchronized ServerRule bindServerRule(ServerRule rule) { + ServerRule previous = serverRule; + serverRule = rule; + return previous; + } + + public static synchronized void restoreServerRule(ServerRule previous) { + serverRule = previous; + } + + static boolean canPlaceOnto(NativeBlockState plant, NativeBlockState ground) { + ServerRule rule = serverRule; + if (rule == null) { + return plant.canPlaceOnto(ground); + } + if (ground.isAir() || isDeadBushOnGrass(plant, ground)) { + return false; + } + return rule.canPlaceOnto(plant, ground); + } + + // Vanilla allows this, but packs mix dead bushes with other plants for their sandy patches, and blacklisting grass + // on such a decorator would take the other plants off the grass too. + private static boolean isDeadBushOnGrass(NativeBlockState plant, NativeBlockState ground) { + return DEAD_BUSH.equals(plant.materialKey()) && GRASS_BLOCK.equals(ground.materialKey()); + } +} diff --git a/core/src/main/java/art/arcane/iris/generation/decoration/IrisSugarCane.java b/core/src/main/java/art/arcane/iris/generation/decoration/IrisSugarCane.java index d22d0bcfa..d6565dbad 100644 --- a/core/src/main/java/art/arcane/iris/generation/decoration/IrisSugarCane.java +++ b/core/src/main/java/art/arcane/iris/generation/decoration/IrisSugarCane.java @@ -1,5 +1,6 @@ package art.arcane.iris.generation.decoration; +import art.arcane.iris.generation.block.B; import art.arcane.iris.generation.runtime.IrisComplex; import art.arcane.iris.generation.runtime.Engine; import art.arcane.iris.generation.biome.IrisBiome; @@ -34,13 +35,13 @@ final class IrisSugarCane { int baseY = y - 1; NativeBlockState support = data.get(x, baseY, z); NativeBlockState target = data.get(x, y, z); - if (support == null || !block.canPlaceOnto(support) || target != null && !target.isAir()) { + if (support == null || !B.canPlaceOnto(block, support) || target != null && !target.isAir()) { return false; } while (isSugarCane(support) && baseY > 0) { support = data.get(x, --baseY, z); } - if (support == null || isSugarCane(support) || !block.canPlaceOnto(support)) { + if (support == null || isSugarCane(support) || !B.canPlaceOnto(block, support)) { return false; } for (int[] offset : NEIGHBORS) { diff --git a/core/src/main/java/art/arcane/iris/integration/data/CraftEngineDataProvider.java b/core/src/main/java/art/arcane/iris/integration/data/CraftEngineDataProvider.java index f6c94a393..71d718e45 100644 --- a/core/src/main/java/art/arcane/iris/integration/data/CraftEngineDataProvider.java +++ b/core/src/main/java/art/arcane/iris/integration/data/CraftEngineDataProvider.java @@ -8,7 +8,7 @@ import art.arcane.iris.integration.ExternalDataSVC; import art.arcane.iris.generation.cache.Cache; import art.arcane.iris.generation.runtime.Engine; import art.arcane.iris.platform.bukkit.BukkitBlockResolution; -import art.arcane.iris.platform.bukkit.nms.INMS; +import art.arcane.iris.platform.bukkit.BukkitPlacementRules; import art.arcane.iris.spi.IrisLogging; import art.arcane.iris.spi.IrisServices; import art.arcane.iris.generation.block.IrisCustomData; @@ -84,7 +84,7 @@ public class CraftEngineDataProvider extends ExternalDataProvider implements Lis @EventHandler public void onReload(CraftEngineReloadEvent event) { - forgetServerBlockRules(); + BukkitPlacementRules.forgetServerRules(); if (!updateContent()) { return; } @@ -94,13 +94,6 @@ public class CraftEngineDataProvider extends ExternalDataProvider implements Lis } } - // Custom blocks carry their own placement rules, so answers about them can change with a reload. - private static void forgetServerBlockRules() { - if (INMS.isBound()) { - INMS.get().forgetBlockRules(); - } - } - @Override public @NotNull List getBlockProperties(@NotNull Identifier blockId) throws MissingResourceException { ImmutableBlockState blockState = findBlockState(blockId); 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 12beb49b9..5d0064ace 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 @@ -429,21 +429,7 @@ public final class BukkitBlockState implements NativeBlockState { @Override public boolean canPlaceOnto(NativeBlockState onto) { - BlockData ground = (BlockData) onto.nativeHandle(); - if (ground.getMaterial().isAir() || isDeadBushOnGrass(ground)) { - return false; - } - return INMS.get().canSurviveOn(realBlockOf(data), realBlockOf(ground)); - } - - // Vanilla allows this, but packs mix dead bushes with other plants for their sandy patches, and blacklisting grass - // on such a decorator would take the other plants off the grass too. - private boolean isDeadBushOnGrass(BlockData ground) { - return data.getMaterial() == Material.DEAD_BUSH && ground.getMaterial() == Material.GRASS_BLOCK; - } - - private static BlockData realBlockOf(BlockData data) { - return data instanceof IrisCustomData custom ? custom.getBase() : data; + return BukkitBlockResolution.canPlaceOnto(data.getMaterial(), ((BlockData) onto.nativeHandle()).getMaterial()); } @Override diff --git a/core/src/main/java/art/arcane/iris/platform/bukkit/BukkitPlacementRules.java b/core/src/main/java/art/arcane/iris/platform/bukkit/BukkitPlacementRules.java new file mode 100644 index 000000000..01ca400c6 --- /dev/null +++ b/core/src/main/java/art/arcane/iris/platform/bukkit/BukkitPlacementRules.java @@ -0,0 +1,78 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.platform.bukkit; + +import art.arcane.iris.spi.IrisLogging; +import art.arcane.volmlib.nativelib.terrain.NativeBlockState; + +import java.lang.reflect.Method; + +/** Where a plant can stand on Bukkit: the server's own canSurvive rule, or Iris's block table when the server can't be asked. */ +public final class BukkitPlacementRules { + private static final String CRAFT_BLOCK_DATA = "org.bukkit.craftbukkit.block.data.CraftBlockData"; + + private BukkitPlacementRules() { + } + + public static boolean canPlaceOnto(NativeBlockState plant, NativeBlockState ground) { + ServerRules server = ServerConnection.RULES; + if (server == null || !server.reaches(plant) || !server.reaches(ground)) { + return plant.canPlaceOnto(ground); + } + return server.canPlaceOnto(plant, ground); + } + + /** Drops the remembered answers, which a resource or custom block reload may have changed. */ + public static void forgetServerRules() { + ServerRules server = ServerConnection.RULES; + if (server != null) { + server.survival().forgetRules(); + } + } + + private record ServerRules(ServerGroundSurvival survival, Class craftBlockData, Method serverState) { + boolean reaches(NativeBlockState state) { + return craftBlockData.isInstance(state.placementHandle()); + } + + // Custom blocks are asked as the server block they place, so their own placement rules apply. + boolean canPlaceOnto(NativeBlockState plant, NativeBlockState ground) { + try { + return survival.canSurviveOn(serverState.invoke(plant.placementHandle()), serverState.invoke(ground.placementHandle())); + } catch (ReflectiveOperationException e) { + return plant.canPlaceOnto(ground); + } + } + } + + private static final class ServerConnection { + static final ServerRules RULES = open(); + + private static ServerRules open() { + ClassLoader loader = BukkitPlacementRules.class.getClassLoader(); + try { + Class craftBlockData = Class.forName(CRAFT_BLOCK_DATA, false, loader); + return new ServerRules(ServerGroundSurvival.connect(loader), craftBlockData, craftBlockData.getMethod("getState")); + } catch (ReflectiveOperationException | LinkageError e) { + IrisLogging.debug("Placement uses Iris's block table, the server's rules are out of reach: " + e); + return null; + } + } + } +} diff --git a/core/src/main/java/art/arcane/iris/platform/bukkit/ServerGroundSurvival.java b/core/src/main/java/art/arcane/iris/platform/bukkit/ServerGroundSurvival.java new file mode 100644 index 000000000..9d437761e --- /dev/null +++ b/core/src/main/java/art/arcane/iris/platform/bukkit/ServerGroundSurvival.java @@ -0,0 +1,189 @@ +/* + * Iris is a World Generator for Minecraft Servers + * Copyright (c) 2026 Arcane Arts (Volmit Software) + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +package art.arcane.iris.platform.bukkit; + +import art.arcane.iris.spi.IrisLogging; + +import java.lang.reflect.Constructor; +import java.lang.reflect.InvocationHandler; +import java.lang.reflect.InvocationTargetException; +import java.lang.reflect.Method; +import java.lang.reflect.Proxy; +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +/** + * Asks the server's own canSurvive rule whether a plant stands on a ground block, in a tiny level holding just the + * two. Iris ships no server-mapped code for Bukkit, so the server's classes are reached by their runtime names. + */ +public final class ServerGroundSurvival { + private static final int PLANT_Y = 64; + private static final int MIN_BUILD_Y = -64; + private static final int BUILD_HEIGHT = 384; + // Crops need 8 or more and mushrooms less than 13. A chunk that is still generating has no light to ask for. + private static final int LIGHT_EVERY_PLANT_ACCEPTS = 10; + + private final ServerClasses server; + private final Map> survivesOn = new ConcurrentHashMap<>(); + private final Map restsOnGround = new ConcurrentHashMap<>(); + + private ServerGroundSurvival(ServerClasses server) { + this.server = server; + } + + public static ServerGroundSurvival connect(ClassLoader loader) throws ReflectiveOperationException { + return new ServerGroundSurvival(ServerClasses.load(loader)); + } + + /** Whether the server lets the server block state {@code plant} stand on the server block state {@code ground}. */ + public boolean canSurviveOn(Object plant, Object ground) { + return survivesOn.computeIfAbsent(plant, ignored -> new ConcurrentHashMap<>()) + .computeIfAbsent(ground, known -> decide(plant, known)); + } + + public void forgetRules() { + survivesOn.clear(); + restsOnGround.clear(); + } + + private boolean decide(Object plant, Object ground) { + try { + return survives(plant, ground) || !restsOnGround(plant); + } catch (ReflectiveOperationException | RuntimeException e) { + IrisLogging.warn("Could not ask whether " + plant + " survives on " + ground + ", letting it stand: " + rootCause(e)); + return true; + } + } + + // Wall and ceiling blocks fail on every ground, which says nothing about the ground they were decorated onto. + private boolean restsOnGround(Object plant) throws ReflectiveOperationException { + Boolean known = restsOnGround.get(plant); + if (known != null) { + return known; + } + boolean rests = survivesOnSomeGround(plant); + restsOnGround.put(plant, rests); + return rests; + } + + private boolean survivesOnSomeGround(Object plant) throws ReflectiveOperationException { + for (Object block : server.blocks()) { + if (survives(plant, server.defaultBlockState().invoke(block))) { + return true; + } + } + return false; + } + + private boolean survives(Object plant, Object ground) throws ReflectiveOperationException { + return (boolean) server.canSurvive().invoke(plant, levelAround(plant, ground), server.plantPos()); + } + + // A proxy, since LevelReader gains abstract methods with every release and Paper's chunk system adds its own. + private Object levelAround(Object plant, Object ground) { + InvocationHandler answers = (proxy, method, args) -> answer(proxy, method, args, plant, ground); + return Proxy.newProxyInstance(server.levelReader().getClassLoader(), new Class[]{server.levelReader()}, answers); + } + + private Object answer(Object proxy, Method method, Object[] args, Object plant, Object ground) throws Throwable { + switch (method.getName()) { + case "getBlockState", "getBlockStateIfLoaded" -> { + return stateAt(args[0], plant, ground); + } + case "getFluidState", "getFluidIfLoaded" -> { + return server.getFluidState().invoke(stateAt(args[0], plant, ground)); + } + case "getRawBrightness", "getMaxLocalRawBrightness", "getBrightness" -> { + return LIGHT_EVERY_PLANT_ACCEPTS; + } + case "getBlockEntity" -> { + return null; + } + case "getMinY", "getMinBuildHeight" -> { + return MIN_BUILD_Y; + } + case "getHeight" -> { + if (args == null) { + return BUILD_HEIGHT; + } + } + case "isClientSide" -> { + return false; + } + case "hashCode" -> { + return System.identityHashCode(proxy); + } + case "equals" -> { + return proxy == args[0]; + } + case "toString" -> { + return "ground survival level"; + } + default -> { + } + } + + if (method.isDefault()) { + return InvocationHandler.invokeDefault(proxy, method, args); + } + throw new IllegalStateException("Ground survival level can't answer " + method.getName()); + } + + // Water beside the ground for sugar cane and air beside the plant for cactus, so only the ground can refuse. + private Object stateAt(Object pos, Object plant, Object ground) throws ReflectiveOperationException { + if (pos.equals(server.plantPos())) { + return plant; + } + if (pos.equals(server.groundPos())) { + return ground; + } + return (int) server.getY().invoke(pos) == PLANT_Y - 1 ? server.water() : server.air(); + } + + private static String rootCause(Throwable failure) { + Throwable cause = failure instanceof InvocationTargetException invocation && invocation.getCause() != null + ? invocation.getCause() : failure; + return cause.getClass().getSimpleName() + ": " + cause.getMessage(); + } + + private record ServerClasses(Class levelReader, Method canSurvive, Method getFluidState, Method getY, + Method defaultBlockState, Object plantPos, Object groundPos, Object water, Object air, + Iterable blocks) { + static ServerClasses load(ClassLoader loader) throws ReflectiveOperationException { + Class blockState = Class.forName("net.minecraft.world.level.block.state.BlockState", false, loader); + Class levelReader = Class.forName("net.minecraft.world.level.LevelReader", false, loader); + Class blockPos = Class.forName("net.minecraft.core.BlockPos", false, loader); + Class block = Class.forName("net.minecraft.world.level.block.Block", false, loader); + Class blocks = Class.forName("net.minecraft.world.level.block.Blocks", true, loader); + Class registries = Class.forName("net.minecraft.core.registries.BuiltInRegistries", true, loader); + Method defaultBlockState = block.getMethod("defaultBlockState"); + Constructor position = blockPos.getConstructor(int.class, int.class, int.class); + return new ServerClasses(levelReader, + blockState.getMethod("canSurvive", levelReader, blockPos), + blockState.getMethod("getFluidState"), + blockPos.getMethod("getY"), + defaultBlockState, + position.newInstance(0, PLANT_Y, 0), + position.newInstance(0, PLANT_Y - 1, 0), + defaultBlockState.invoke(blocks.getField("WATER").get(null)), + defaultBlockState.invoke(blocks.getField("AIR").get(null)), + (Iterable) registries.getField("BLOCK").get(null)); + } + } +} diff --git a/core/src/main/java/art/arcane/iris/platform/bukkit/nms/INMSBinding.java b/core/src/main/java/art/arcane/iris/platform/bukkit/nms/INMSBinding.java index 0f1ac3aad..9704a918c 100644 --- a/core/src/main/java/art/arcane/iris/platform/bukkit/nms/INMSBinding.java +++ b/core/src/main/java/art/arcane/iris/platform/bukkit/nms/INMSBinding.java @@ -25,7 +25,6 @@ import art.arcane.volmlib.nativelib.terrain.NativeSectionVolume; import art.arcane.volmlib.nativelib.terrain.BukkitTerrainBuffer; import org.bukkit.block.data.BlockData; -import art.arcane.iris.platform.bukkit.BukkitBlockResolution; import art.arcane.volmlib.util.nbt.mca.NBTWorldSupport; @@ -323,15 +322,6 @@ public interface INMSBinding extends NativeTerrainAccess { KMap> getBlockProperties(); - /** Whether the server lets {@code plant} stand on {@code ground}; without a native binding, Iris's own table of plants and grounds answers. */ - default boolean canSurviveOn(BlockData plant, BlockData ground) { - return BukkitBlockResolution.canPlaceOnto(plant.getMaterial(), ground.getMaterial()); - } - - /** Drops answers remembered from the server's block rules, which a resource reload may have changed. */ - default void forgetBlockRules() { - } - private void validateDimensionTypes(WorldCreator c) { if (!(c.generator() instanceof PlatformChunkGenerator generator)) { return; diff --git a/core/src/test/java/art/arcane/iris/generation/block/PlantPlacementTest.java b/core/src/test/java/art/arcane/iris/generation/block/PlantPlacementTest.java new file mode 100644 index 000000000..343d2e11a --- /dev/null +++ b/core/src/test/java/art/arcane/iris/generation/block/PlantPlacementTest.java @@ -0,0 +1,55 @@ +package art.arcane.iris.generation.block; + +import art.arcane.volmlib.nativelib.terrain.NativeBlockState; +import org.junit.After; +import org.junit.Test; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class PlantPlacementTest { + private final PlantPlacement.ServerRule previous = PlantPlacement.bindServerRule(null); + + @After + public void restoreRule() { + PlantPlacement.restoreServerRule(previous); + } + + @Test + public void withoutAServerRuleTheBlockTableAnswers() { + NativeBlockState plant = block("minecraft:short_grass"); + NativeBlockState ground = block("iris:grass_block"); + when(plant.canPlaceOnto(ground)).thenReturn(true); + + assertTrue(B.canPlaceOnto(plant, ground)); + } + + @Test + public void aBoundServerRuleOverridesTheBlockTable() { + NativeBlockState plant = block("minecraft:short_grass"); + NativeBlockState ground = block("iris:grass_block"); + PlantPlacement.bindServerRule((p, g) -> p == plant && g == ground); + + assertTrue(B.canPlaceOnto(plant, ground)); + assertFalse(B.canPlaceOnto(plant, block("minecraft:stone"))); + } + + @Test + public void nothingStandsOnAirAndDeadBushesStayOffGrass() { + PlantPlacement.bindServerRule((p, g) -> true); + NativeBlockState air = block("minecraft:air"); + when(air.isAir()).thenReturn(true); + + assertFalse(B.canPlaceOnto(block("minecraft:short_grass"), air)); + assertFalse(B.canPlaceOnto(block("minecraft:dead_bush"), block("minecraft:grass_block"))); + assertTrue(B.canPlaceOnto(block("minecraft:dead_bush"), block("minecraft:sand"))); + } + + private static NativeBlockState block(String key) { + NativeBlockState state = mock(NativeBlockState.class); + when(state.materialKey()).thenReturn(key); + return state; + } +}