Let the server decide where plants stand

- Ask the native canSurvive rule in BukkitBlockState.canPlaceOnto
- Fall back to the block table when the rule cannot answer
- Clear remembered answers on resource and CraftEngine reloads
- Drop the post stage foliage check that never saw decorations
- Test vanilla placement rules, fallbacks and concurrent lookups
This commit is contained in:
Piexel authored and GitHub committed 2026-11-30 11:06:48 -05:00
1 parent 1bc7843a52
commit f7084b244f
6 files changed
+310 -9

No files matched your search

@@ -0,0 +1,262 @@
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.volmlib.nativelib.terrain.SurvivalAnswer;
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 org.junit.BeforeClass;
import org.junit.Test;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.logging.Handler;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/** 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 final Logger NATIVE_LOG = Logger.getLogger("VolmLib-Native");
@BeforeClass
public static void bootstrap() {
HeadlessNativeBootstrap.initialize();
}
@Test
public void grassStandsOnSoilButNotOnStone() {
assertPlacement(true, Blocks.SHORT_GRASS, Blocks.GRASS_BLOCK);
assertPlacement(true, Blocks.SHORT_GRASS, Blocks.PODZOL);
assertPlacement(false, Blocks.SHORT_GRASS, Blocks.STONE);
assertPlacement(false, Blocks.POPPY, Blocks.STONE);
assertPlacement(false, Blocks.FERN, Blocks.OAK_LEAVES);
}
@Test
public void cactusNeedsSandOrCactus() {
for (Block ground : new Block[]{Blocks.CACTUS, Blocks.SAND, Blocks.RED_SAND}) {
assertPlacement(true, Blocks.CACTUS, ground);
}
assertPlacement(false, Blocks.CACTUS, Blocks.STONE);
}
@Test
public void sugarCaneNeedsItsSubstrate() {
for (Block ground : 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}) {
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}) {
assertPlacement(false, Blocks.SUGAR_CANE, ground);
}
}
@Test
public void netherGroundCoverAcceptsNyliumAndSoulSoilButRejectsBarrenBlocks() {
for (Block plant : new Block[]{Blocks.CRIMSON_ROOTS, Blocks.WARPED_ROOTS, Blocks.NETHER_SPROUTS}) {
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}) {
assertPlacement(false, plant, ground);
}
}
}
@Test
public void blocksThatRestOnNoGroundStillFit() {
assertPlacement(true, Blocks.WALL_TORCH, Blocks.STONE);
assertPlacement(true, Blocks.VINE, Blocks.SAND);
assertPlacement(true, Blocks.COCOA, Blocks.STONE);
}
@Test
public void deadBushStaysOffGrassAsPacksExpect() {
assertPlacement(true, Blocks.DEAD_BUSH, Blocks.SAND);
assertPlacement(false, Blocks.DEAD_BUSH, Blocks.GRASS_BLOCK);
}
@Test
public void nothingStandsOnAir() {
assertPlacement(false, Blocks.VINE, Blocks.AIR);
assertPlacement(false, Blocks.SHORT_GRASS, Blocks.CAVE_AIR);
}
@Test
public void plantsThatReadMoreOfTheLevelAreAnsweredByTheirOwnRule() {
List<LogRecord> warnings = warningsDuring(() -> {
assertPlacement(true, Blocks.BAMBOO, Blocks.GRASS_BLOCK);
assertPlacement(false, Blocks.BAMBOO, Blocks.STONE);
assertPlacement(true, Blocks.KELP, Blocks.STONE);
assertPlacement(false, Blocks.KELP, Blocks.MAGMA_BLOCK);
assertPlacement(true, Blocks.SEAGRASS, Blocks.SAND);
assertPlacement(false, Blocks.SEAGRASS, Blocks.MAGMA_BLOCK);
assertPlacement(true, Blocks.LILY_PAD, Blocks.WATER);
assertPlacement(false, Blocks.LILY_PAD, Blocks.STONE);
assertPlacement(true, Blocks.SWEET_BERRY_BUSH, Blocks.GRASS_BLOCK);
assertPlacement(false, Blocks.SWEET_BERRY_BUSH, Blocks.STONE);
assertPlacement(true, Blocks.BROWN_MUSHROOM, Blocks.STONE);
assertPlacement(true, Blocks.RED_MUSHROOM, Blocks.MYCELIUM);
assertPlacement(false, Blocks.BROWN_MUSHROOM, Blocks.GLASS);
assertPlacement(true, Blocks.CRIMSON_FUNGUS, Blocks.CRIMSON_NYLIUM);
assertPlacement(false, Blocks.WARPED_FUNGUS, Blocks.NETHERRACK);
assertPlacement(true, Blocks.WHEAT, Blocks.FARMLAND);
assertPlacement(false, Blocks.CARROTS, Blocks.DIRT);
});
assertEquals(messages(warnings), 0, warnings.size());
}
@Test
public void everyVanillaBlockIsAnsweredWithoutFailingOpen() {
GroundSurvival survival = new GroundSurvival();
List<LogRecord> warnings = warningsDuring(() -> {
for (Block plant : BuiltInRegistries.BLOCK) {
if (needsARunningPaperServer(plant)) {
continue;
}
for (Block ground : new Block[]{Blocks.GRASS_BLOCK, Blocks.STONE, Blocks.WATER}) {
survival.canSurviveOn(plant.defaultBlockState(), ground.defaultBlockState());
}
}
});
assertEquals(messages(warnings), 0, warnings.size());
}
@Test
public void aRuleThatThrowsIsLeftUnansweredAndWarnsOncePerBlock() {
BlockState firstState = throwingStateOf(Blocks.SHORT_GRASS);
BlockState secondState = throwingStateOf(Blocks.SHORT_GRASS);
GroundSurvival survival = new GroundSurvival();
List<LogRecord> warnings = warningsDuring(() -> {
for (BlockState plant : new BlockState[]{firstState, secondState}) {
for (Block ground : new Block[]{Blocks.STONE, Blocks.SAND, Blocks.GRASS_BLOCK}) {
assertEquals(SurvivalAnswer.UNANSWERED, survival.canSurviveOn(plant, ground.defaultBlockState()));
}
}
});
assertEquals(messages(warnings), 1, warnings.size());
}
@Test
public void aRuleThatThrowsFallsBackToThePlacementTable() {
ModdedBlockState grass = ModdedBlockState.deferred(throwingStateOf(Blocks.SHORT_GRASS), Map.of(), "iris:test");
warningsDuring(() -> {
assertTrue(grass.canPlaceOnto(wrap(Blocks.GRASS_BLOCK)));
assertFalse(grass.canPlaceOnto(wrap(Blocks.STONE)));
});
}
@Test
public void aRuleThatOnlyAnswersForSomeGroundsDoesNotPassAsAWallBlock() {
BlockState plant = mock(BlockState.class);
when(plant.getBlock()).thenReturn(Blocks.SHORT_GRASS);
when(plant.canSurvive(any(), any())).thenAnswer(asked -> {
LevelReader level = asked.getArgument(0);
BlockPos plantPos = asked.getArgument(1);
if (level.getBlockState(plantPos.below()).is(Blocks.STONE)) {
return false;
}
throw new IllegalStateException("custom rule failed");
});
GroundSurvival survival = new GroundSurvival();
warningsDuring(() -> assertEquals(SurvivalAnswer.UNANSWERED,
survival.canSurviveOn(plant, Blocks.STONE.defaultBlockState())));
}
@Test
public void generatorThreadsAskingAtOnceGetTheSameAnswers() {
Block[] plants = {Blocks.SHORT_GRASS, Blocks.CACTUS, Blocks.SUGAR_CANE, Blocks.VINE, Blocks.BROWN_MUSHROOM};
Block[] grounds = {Blocks.GRASS_BLOCK, Blocks.SAND, Blocks.STONE, Blocks.WATER};
GroundSurvival alone = new GroundSurvival();
GroundSurvival shared = new GroundSurvival();
List<Integer> threads = Arrays.asList(new Integer[16]);
threads.parallelStream().forEach(ignored -> {
for (Block plant : plants) {
for (Block ground : grounds) {
assertEquals(plant + " on " + ground,
alone.canSurviveOn(plant.defaultBlockState(), ground.defaultBlockState()),
shared.canSurviveOn(plant.defaultBlockState(), ground.defaultBlockState()));
}
}
});
}
// Paper's chorus plant reads Paper's global configuration, which only a running server loads.
private static boolean needsARunningPaperServer(Block plant) {
return plant == Blocks.CHORUS_PLANT;
}
private static BlockState throwingStateOf(Block block) {
BlockState state = mock(BlockState.class);
when(state.getBlock()).thenReturn(block);
when(state.isSingletonState()).thenReturn(true);
when(state.canSurvive(any(), any())).thenThrow(new IllegalStateException("custom rule failed"));
return state;
}
private static void assertPlacement(boolean expected, Block plant, Block ground) {
String pair = plant + " on " + ground;
assertEquals("modded " + pair, expected, moddedPlacement(plant, ground));
assertEquals("bukkit native " + pair, expected ? SurvivalAnswer.SURVIVES : SurvivalAnswer.FAILS,
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");
}
private static List<LogRecord> warningsDuring(Runnable checks) {
List<LogRecord> warnings = new CopyOnWriteArrayList<>();
Handler capture = new Handler() {
@Override
public void publish(LogRecord record) {
if (record.getLevel().intValue() >= Level.WARNING.intValue()) {
warnings.add(record);
}
}
@Override
public void flush() {
}
@Override
public void close() {
}
};
NATIVE_LOG.addHandler(capture);
try {
checks.run();
} finally {
NATIVE_LOG.removeHandler(capture);
}
return warnings;
}
private static String messages(List<LogRecord> warnings) {
StringBuilder text = new StringBuilder();
for (LogRecord warning : warnings) {
text.append(warning.getMessage()).append(" (").append(warning.getThrown()).append(")\n");
}
return text.toString();
}
}
@@ -58,6 +58,7 @@ import art.arcane.iris.platform.agent.AttachSignal;
import art.arcane.iris.platform.bootstrap.Bindings;
import art.arcane.iris.platform.bootstrap.ServerProperties;
import art.arcane.iris.platform.bootstrap.SlimJar;
import art.arcane.iris.platform.bukkit.BlockRulesSVC;
import art.arcane.iris.platform.bukkit.BoardSVC;
import art.arcane.iris.platform.bukkit.BukkitPlatform;
import art.arcane.iris.platform.bukkit.api.IrisApiEventSVC;
@@ -681,6 +682,7 @@ public final class IrisRuntime implements IrisPluginRuntime {
new GlobalCacheSVC(),
new LogFilterSVC(),
new ExternalDataSVC(),
new BlockRulesSVC(),
new EditSVC(),
new ObjectSVC(),
new ObjectStudioSaveService(),
@@ -0,0 +1,24 @@
package art.arcane.iris.platform.bukkit;
import art.arcane.iris.platform.bukkit.nms.INMS;
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. */
public class BlockRulesSVC implements IrisService {
@Override
public void onEnable() {
}
@Override
public void onDisable() {
}
@EventHandler
public void onResourcesReloaded(ServerResourcesReloadedEvent event) {
if (INMS.isBound()) {
INMS.get().forgetBlockRules();
}
}
}