mirror of
https://github.com/VolmitSoftware/Iris.git
synced 2026-11-28 00:34:19 +00:00
Let the server decide where decorators fit
Ported from v3e (af333cc93). Whether a plant stands on a ground block is now the server's own canSurvive rule, asked in a two-block level, so custom ground (CraftEngine and others) grows plants too. Plants that fail on every ground, like wall and ceiling blocks, keep fitting as before. Dead bushes stay off grass, as packs expect. Without the NMS binding Iris's own table answers. Test server, world v3eports (seed 1337, radius 500): 4,225 chunks, 0 failed, no errors; plants 70,161 vs 68,905 in iriswave6 over the same 961 sampled chunks, every plant type present.
This commit is contained in:
1 parent
6429b6ea5e
commit
d5e75114ab
4 files changed
+143
-1
No files matched your search
+115
@@ -0,0 +1,115 @@
|
||||
package art.arcane.iris.core.nms.v26_2_R1;
|
||||
|
||||
import art.arcane.iris.spi.IrisLogging;
|
||||
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 server's own rules whether a plant survives on a ground block, in a tiny level holding just the two. */
|
||||
final class GroundSurvival {
|
||||
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, Boolean> restsOnGround = new ConcurrentHashMap<>();
|
||||
|
||||
boolean canSurviveOn(BlockState plant, BlockState ground) {
|
||||
try {
|
||||
return survives(plant, ground) || !restsOnGround(plant);
|
||||
} catch (RuntimeException e) {
|
||||
IrisLogging.reportError(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 Paper's chunk system adds its 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" -> {
|
||||
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();
|
||||
}
|
||||
}
|
||||
+6
@@ -179,6 +179,7 @@ public class NMSBinding implements INMSBinding {
|
||||
private final BlockData AIR = Material.AIR.createBlockData();
|
||||
private final AtomicCache<MCAIdMap<net.minecraft.world.level.biome.Biome>> biomeMapCache = new AtomicCache<>();
|
||||
private final AtomicBoolean injected = new AtomicBoolean();
|
||||
private final GroundSurvival groundSurvival = new GroundSurvival();
|
||||
private volatile ResettableClassFileTransformer levelStorageAccessTransformer;
|
||||
private volatile ResettableClassFileTransformer serverLevelTransformer;
|
||||
private final AtomicCache<MCAIdMapper<BlockState>> registryCache = new AtomicCache<>();
|
||||
@@ -1642,6 +1643,11 @@ public class NMSBinding implements INMSBinding {
|
||||
);
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean canSurviveOn(BlockData plant, BlockData ground) {
|
||||
return groundSurvival.canSurviveOn(((CraftBlockData) plant).getState(), ((CraftBlockData) ground).getState());
|
||||
}
|
||||
|
||||
@Override
|
||||
public KMap<Material, List<BlockProperty>> getBlockProperties() {
|
||||
KMap<Material, List<BlockProperty>> states = new KMap<>();
|
||||
|
||||
@@ -47,6 +47,8 @@ import org.bukkit.Material;
|
||||
import org.bukkit.World;
|
||||
import org.bukkit.WorldCreator;
|
||||
import org.bukkit.block.Biome;
|
||||
import org.bukkit.block.data.BlockData;
|
||||
import art.arcane.iris.platform.bukkit.BukkitBlockResolution;
|
||||
import org.bukkit.entity.Entity;
|
||||
import org.bukkit.entity.EntityType;
|
||||
import org.bukkit.event.entity.CreatureSpawnEvent;
|
||||
@@ -276,6 +278,11 @@ public interface INMSBinding {
|
||||
|
||||
KMap<Material, List<BlockProperty>> getBlockProperties();
|
||||
|
||||
/** Whether the server lets {@code plant} stand on {@code ground}; without a binding, Iris's own table of plants and grounds answers. */
|
||||
default boolean canSurviveOn(BlockData plant, BlockData ground) {
|
||||
return BukkitBlockResolution.canPlaceOnto(plant.getMaterial(), ground.getMaterial());
|
||||
}
|
||||
|
||||
private void validateDimensionTypes(WorldCreator c) {
|
||||
if (!(c.generator() instanceof PlatformChunkGenerator generator)) {
|
||||
return;
|
||||
|
||||
@@ -414,7 +414,21 @@ public final class BukkitBlockState implements PlatformBlockState {
|
||||
|
||||
@Override
|
||||
public boolean canPlaceOnto(PlatformBlockState onto) {
|
||||
return BukkitBlockResolution.canPlaceOnto(data.getMaterial(), ((BlockData) onto.nativeHandle()).getMaterial());
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
Reference in new issue
Block a user