Keep the server placement check inside Iris

- Ask canSurvive from Iris itself instead of VolmLib, by runtime names on Bukkit and directly on modded
- Platforms bind their rule through PlantPlacement, block states answer from the table when none is bound
- Route sugar cane through the same check
- Bind the rule on Bukkit enable and modded bootstrap, restore it on shutdown
This commit is contained in:
piexel committed 2026-11-25 23:57:59 +01:00
1 parent 202d9bbbd4
commit 60a9b5c7c4
15 files changed
+602 -73

No files matched your search

@@ -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()));
}
}
@@ -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();
}
}
@@ -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());
@@ -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 <https://www.gnu.org/licenses/>.
*/
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<BlockState, Map<BlockState, Boolean>> survivesOn = new ConcurrentHashMap<>();
private final Map<BlockState, Boolean> 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();
}
}
@@ -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}) {