TreeSVC and TreeDefinitionIndex ask the SaplingGrowth services

The module picks the tree placement and places it into a list of blocks; the host reads the world through
SaplingGrowth.Surroundings and applies the blocks. Custom (external) blocks in a grown tree are placed as their base
block.
This commit is contained in:
piexel committed 2026-10-09 22:05:41 +02:00
1 parent 946ad61f7f
commit 8bf4fb0115
6 files changed
+169 -255

No files matched your search

@@ -26,6 +26,7 @@ import com.volmit.service.BlockDrops;
import com.volmit.service.LocateResult;
import com.volmit.service.Loot;
import com.volmit.service.ObjectsAt;
import com.volmit.service.SaplingGrowth;
import com.volmit.service.StructureLocator;
import com.volmit.world.PackWorld;
import java.util.ArrayList;
@@ -100,6 +101,31 @@ public final class ModuleServices {
return new BlockDrops.Result(drops, replacesVanillaDrops);
}
/** The tree grown by the first module that grows one for {@code sapling}; empty leaves the growth to the platform. */
public static Optional<SaplingGrowth.Growth> growSapling(Engine engine, SaplingGrowth.Sapling sapling, SaplingGrowth.Surroundings surroundings) {
PackWorld world = engine.getGeneration().world();
for (SaplingGrowth provider : catalog(engine.getData()).services(SaplingGrowth.class)) {
Optional<SaplingGrowth.Growth> growth = provider.grow(world, sapling, surroundings);
if (growth.isPresent()) {
return growth;
}
}
return Optional.empty();
}
/** The objects every module places as trees. */
public static SaplingGrowth.TreeObjects treeObjects(Engine engine) {
PackWorld world = engine.getGeneration().world();
Set<String> objects = new LinkedHashSet<>();
Set<String> procedural = new LinkedHashSet<>();
for (SaplingGrowth provider : catalog(engine.getData()).services(SaplingGrowth.class)) {
SaplingGrowth.TreeObjects trees = provider.treeObjects(world);
objects.addAll(trees.objects());
procedural.addAll(trees.procedural());
}
return new SaplingGrowth.TreeObjects(objects, procedural);
}
/** Every structure key the pack's modules know, placed or not. */
public static Set<String> structureKeys(IrisData data) {
Set<String> keys = new LinkedHashSet<>();
@@ -18,38 +18,28 @@
package art.arcane.iris.core.service;
import art.arcane.iris.engine.object.placement.IrisObjectPlacement;
import art.arcane.iris.engine.object.procedural.tree.IrisTreeModes;
import art.arcane.iris.spi.IrisLogging;
import art.arcane.iris.spi.IrisServices;
import art.arcane.iris.core.loader.IrisData;
import art.arcane.iris.spi.IrisPlatforms;
import art.arcane.iris.core.modules.ModuleServices;
import art.arcane.iris.core.modules.PlatformBlockStates;
import art.arcane.iris.core.tools.IrisToolbelt;
import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.engine.framework.EngineLifecycleTasks;
import art.arcane.iris.engine.framework.TreeBlockMaterial;
import art.arcane.iris.engine.object.placement.IObjectPlacer;
import art.arcane.iris.engine.object.world.biome.IrisBiome;
import art.arcane.iris.engine.object.world.dimension.IrisDimension;
import art.arcane.iris.engine.object.prefab.IrisObject;
import art.arcane.iris.engine.object.world.region.IrisRegion;
import art.arcane.iris.engine.object.placement.IrisTreeSize;
import art.arcane.iris.engine.object.prefab.TileData;
import art.arcane.iris.engine.platform.PlatformChunkGenerator;
import art.arcane.iris.platform.bukkit.BukkitBlockState;
import art.arcane.iris.spi.PlatformBlockState;
import art.arcane.volmlib.util.collection.KList;
import art.arcane.volmlib.util.collection.KMap;
import art.arcane.volmlib.util.data.Cuboid;
import art.arcane.iris.util.common.data.IrisCustomData;
import com.volmit.placement.PlacedBlock;
import com.volmit.service.SaplingGrowth;
import art.arcane.volmlib.util.math.BlockPosition;
import art.arcane.volmlib.util.math.RNG;
import art.arcane.iris.util.common.plugin.IrisService;
import art.arcane.iris.util.common.scheduling.J;
import org.bukkit.Bukkit;
import org.bukkit.HeightMap;
import org.bukkit.Location;
import org.bukkit.Material;
import org.bukkit.TreeType;
import org.bukkit.World;
import org.bukkit.block.Block;
import org.bukkit.block.BlockState;
@@ -60,7 +50,7 @@ import org.bukkit.event.EventPriority;
import org.bukkit.event.world.StructureGrowEvent;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ThreadLocalRandom;
import java.util.function.Predicate;
@@ -117,134 +107,43 @@ public class TreeSVC implements IrisService {
return;
}
IrisDimension dimension = engine.getDimension();
if (dimension == null) {
if (engine.getDimension() == null) {
IrisLogging.debug(this.getClass().getName() + " passed it off to vanilla because could not get Dimension for this world");
IrisLogging.reportError(new NullPointerException(event.getWorld().getName() + " could not be accessed despite being an Iris world"));
return;
}
if (!dimension.getTreeSettings().isEnabled()) {
IrisLogging.debug(this.getClass().getName() + " cancelled because tree overrides are disabled");
return;
}
BlockData first = event.getLocation().getBlock().getBlockData().clone();
Cuboid saplingPlane = getSaplings(event.getLocation(), blockData -> blockData instanceof Sapling && blockData.getMaterial().equals(first.getMaterial()), event.getWorld());
IrisLogging.debug("Sapling grew @ " + event.getLocation() + " for " + event.getSpecies().name() + " usedBoneMeal is " + event.isFromBonemeal());
IrisLogging.debug("Sapling plane is: " + saplingPlane.getSizeX() + " by " + saplingPlane.getSizeZ());
IrisObjectPlacement placement = getObjectPlacement(worldAccess, event.getLocation(), event.getSpecies(), new IrisTreeSize(1, 1));
int minHeight = event.getWorld().getMinHeight();
SaplingGrowth.Sapling sapling = new SaplingGrowth.Sapling(
relativePosition(event.getLocation(), minHeight),
relativePosition(saplingPlane.getCenter(), minHeight),
event.getSpecies().name());
Optional<SaplingGrowth.Growth> growth = ModuleServices.growSapling(engine, sapling, new WorldSurroundings(event.getWorld(), saplingPlane));
if (placement == null) {
if (growth.isEmpty()) {
IrisLogging.debug(this.getClass().getName() + " had options but did not manage to find objectPlacements for them");
return;
}
saplingPlane.forEach(block -> block.setType(Material.AIR));
IrisObject object = worldAccess.getData().getObjectLoader().load(placement.getPlace().getRandom(RNG.r));
String treeMarker = object.getLoadKey() + "@" + ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE);
String treeMarker = growth.get().object() + "@" + ThreadLocalRandom.current().nextInt(Integer.MAX_VALUE);
List<BlockState> blockStateList = new KList<>();
KMap<Location, BlockData> dataCache = new KMap<>();
// TODO: REAL CLASSES!!!!
IObjectPlacer placer = new IObjectPlacer() {
@Override
public int getHighest(int x, int z, IrisData data) {
return event.getWorld().getHighestBlockYAt(x, z);
}
@Override
public int getHighest(int x, int z, IrisData data, boolean ignoreFluid) {
return event.getWorld().getHighestBlockYAt(x, z, ignoreFluid ? HeightMap.OCEAN_FLOOR : HeightMap.WORLD_SURFACE);
}
@Override
public void set(int x, int y, int z, PlatformBlockState s) {
BlockData d = (BlockData) s.nativeHandle();
Block b = event.getWorld().getBlockAt(x, y, z);
// Listeners get the post-growth tree, per the StructureGrowEvent contract;
// a fresh snapshot here handed every plugin an all-air "tree".
BlockState state = b.getState();
if (d instanceof IrisCustomData data)
state.setBlockData(data.getBase());
else state.setBlockData(d);
blockStateList.add(state);
dataCache.put(new Location(event.getWorld(), x, y, z), d);
}
@Override
public PlatformBlockState get(int x, int y, int z) {
return BukkitBlockState.of(event.getWorld().getBlockAt(x, y, z).getBlockData());
}
@Override
public boolean isPreventingDecay() {
return true;
}
@Override
public boolean isCarved(int x, int y, int z) {
return false;
}
@Override
public boolean isSurfaceSolid(int x, int y, int z) {
return event.getWorld().getBlockAt(x, y, z).getType().isSolid();
}
@Override
public boolean isSolid(int x, int y, int z) {
return event.getWorld().getBlockAt(x, y, z).getBlockData().getMaterial().isSolid();
}
@Override
public boolean isUnderwater(int x, int z) {
return false;
}
@Override
public int getFluidHeight() {
return worldFluidHeight(engine);
}
@Override
public boolean isDebugSmartBore() {
return false;
}
@Override
public void setTile(int xx, int yy, int zz, TileData tile) {
}
@Override
public <T> void setData(int xx, int yy, int zz, T data) {
}
@Override
public <T> T getData(int xx, int yy, int zz, Class<T> t) {
return null;
}
@Override
public Engine getEngine() {
return engine;
}
};
object.place(
saplingPlane.getCenter().getBlockX(),
(saplingPlane.getCenter().getBlockY() + object.getH() / 2),
saplingPlane.getCenter().getBlockZ(),
placer,
placement,
RNG.r,
Objects.requireNonNull(worldAccess).getData()
);
for (PlacedBlock placed : growth.get().blocks()) {
Block b = event.getWorld().getBlockAt(placed.position().x(), placed.position().y() + minHeight, placed.position().z());
BlockData d = (BlockData) IrisPlatforms.get().registries().block(placed.state().key()).nativeHandle();
// Listeners get the post-growth tree, per the StructureGrowEvent contract;
// a fresh snapshot here handed every plugin an all-air "tree".
BlockState state = b.getState();
state.setBlockData(d);
blockStateList.add(state);
dataCache.put(b.getLocation(), d);
}
event.setCancelled(true);
@@ -266,10 +165,7 @@ public class TreeSVC implements IrisService {
if (d == null) continue;
Block block = l.getBlock();
if (d instanceof IrisCustomData data) {
block.setBlockData(data.getBase(), false);
IrisServices.get(ExternalDataSVC.class).processUpdate(engine, block, data.getCustom());
} else block.setBlockData(d, false);
block.setBlockData(d, false);
int mantleY = block.getY() - event.getWorld().getMinHeight();
// Lease-gated: the grow task can land after the engine started closing, and
// an unleased mantle write would race the region flush.
@@ -290,58 +186,36 @@ public class TreeSVC implements IrisService {
}
}
static int worldFluidHeight(Engine engine) {
return engine.getMinHeight() + engine.getDimension().getFluidHeight();
private static com.volmit.world.BlockPosition relativePosition(Location location, int minHeight) {
return new com.volmit.world.BlockPosition(location.getBlockX(), location.getBlockY() - minHeight, location.getBlockZ());
}
/**
* Finds a single object placement (which may contain more than one object) for the requirements species, location &
* size
*
* @param worldAccess The world to access (check for biome, region, dimension, etc)
* @param location The location of the growth event (For biome/region finding)
* @param type The bukkit TreeType to match
* @param size The size of the sapling area
* @return An object placement which contains the matched tree, or null if none were found / it's disabled.
*/
private IrisObjectPlacement getObjectPlacement(PlatformChunkGenerator worldAccess, Location location, TreeType type, IrisTreeSize size) {
/** The world around the saplings as the modules read it; the saplings that grow count as air, as they are about to. */
private static final class WorldSurroundings implements SaplingGrowth.Surroundings {
private static final com.volmit.block.BlockState AIR = com.volmit.block.BlockState.parse("minecraft:air");
KList<IrisObjectPlacement> placements = new KList<>();
boolean isUseAll = worldAccess.getEngine().getDimension().getTreeSettings().getMode().equals(IrisTreeModes.ALL);
private final World world;
private final Cuboid saplingPlane;
// Retrieve objectPlacements of type `species` from biome
IrisBiome biome = worldAccess.getEngine().getBiome(location.getBlockX(), location.getBlockY()-worldAccess.getTarget().getWorld().minHeight(), location.getBlockZ());
placements.addAll(matchObjectPlacements(biome.getObjects(), size, type));
// Add more or find any in the region
if (isUseAll || placements.isEmpty()) {
IrisRegion region = worldAccess.getEngine().getRegion(location.getBlockX(), location.getBlockZ());
placements.addAll(matchObjectPlacements(region.getObjects(), size, type));
private WorldSurroundings(World world, Cuboid saplingPlane) {
this.world = world;
this.saplingPlane = saplingPlane;
}
// Check if no matches were found, return a random one if they are
return placements.isNotEmpty() ? placements.getRandom() : null;
}
@Override
public int highest(int x, int z, boolean ignoreFluid) {
int worldY = ignoreFluid ? world.getHighestBlockYAt(x, z, HeightMap.OCEAN_FLOOR) : world.getHighestBlockYAt(x, z);
return worldY - world.getMinHeight();
}
/**
* Filters out mismatches and returns matches
*
* @param objects The object placements to check
* @param size The size of the sapling area to filter with
* @param type The type of the tree to filter with
* @return A list of objectPlacements that matched. May be empty.
*/
private KList<IrisObjectPlacement> matchObjectPlacements(KList<IrisObjectPlacement> objects, IrisTreeSize size, TreeType type) {
KList<IrisObjectPlacement> p = new KList<>();
for (IrisObjectPlacement i : objects) {
if (i.matches(size, type)) {
p.add(i);
@Override
public com.volmit.block.BlockState blockAt(int x, int y, int z) {
int worldY = y + world.getMinHeight();
if (saplingPlane.contains(x, worldY, z)) {
return AIR;
}
return com.volmit.block.BlockState.parse(PlatformBlockStates.namespaced(BukkitBlockState.of(world.getBlockAt(x, worldY, z).getBlockData()).key()));
}
return p;
}
/**
@@ -1,14 +1,10 @@
package art.arcane.iris.core.service.tree;
import art.arcane.iris.engine.object.placement.IrisObjectPlacement;
import art.arcane.iris.core.modules.ModuleServices;
import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.engine.framework.StructurePlacementMarker;
import art.arcane.iris.engine.object.world.biome.IrisBiome;
import art.arcane.iris.engine.object.procedural.IrisProceduralObjects;
import art.arcane.iris.engine.object.procedural.tree.IrisProceduralTree;
import art.arcane.iris.engine.object.world.region.IrisRegion;
import com.volmit.service.SaplingGrowth;
import java.util.HashSet;
import java.util.Set;
public final class TreeDefinitionIndex {
@@ -23,26 +19,8 @@ public final class TreeDefinitionIndex {
}
public static TreeDefinitionIndex build(Engine engine) {
Set<String> explicitObjectKeys = new HashSet<>();
Set<String> proceduralTreeKeys = new HashSet<>();
for (IrisRegion region : engine.getDimension().getAllRegions(engine)) {
if (region == null) {
continue;
}
collectPlacements(region.getObjects(), explicitObjectKeys);
collectProceduralTrees(region.getProceduralObjects(), proceduralTreeKeys);
}
for (IrisBiome biome : engine.getDimension().getReachableBiomes(engine)) {
if (biome == null) {
continue;
}
collectPlacements(biome.getObjects(), explicitObjectKeys);
collectProceduralTrees(biome.getProceduralObjects(), proceduralTreeKeys);
}
return new TreeDefinitionIndex(explicitObjectKeys, proceduralTreeKeys);
SaplingGrowth.TreeObjects trees = ModuleServices.treeObjects(engine);
return new TreeDefinitionIndex(trees.objects(), trees.procedural());
}
public boolean isTreeMarker(String marker) {
@@ -60,38 +38,4 @@ public final class TreeDefinitionIndex {
}
return objectKey.startsWith("trees/") || explicitObjectKeys.contains(objectKey);
}
private static void collectPlacements(Iterable<IrisObjectPlacement> placements, Set<String> objectKeys) {
if (placements == null) {
return;
}
for (IrisObjectPlacement placement : placements) {
if (placement == null
|| placement.getTrees() == null
|| placement.getTrees().isEmpty()
|| placement.getPlace() == null) {
continue;
}
for (String objectKey : placement.getPlace()) {
if (objectKey != null && !objectKey.isBlank()) {
objectKeys.add(objectKey);
}
}
}
}
private static void collectProceduralTrees(IrisProceduralObjects proceduralObjects, Set<String> objectKeys) {
if (proceduralObjects == null || proceduralObjects.getTrees() == null) {
return;
}
for (IrisProceduralTree tree : proceduralObjects.getTrees()) {
if (tree == null || tree.getName() == null || tree.getName().isBlank()) {
continue;
}
int variants = Math.max(1, tree.getVariants());
for (int index = 0; index < variants; index++) {
objectKeys.add(tree.getVariantLoadKey(index));
}
}
}
}
@@ -1,22 +0,0 @@
package art.arcane.iris.core.service;
import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.engine.object.world.dimension.IrisDimension;
import org.junit.Test;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class TreeSVCFluidHeightTest {
@Test
public void liveWorldTreePlacementConvertsLocalFluidHeightToWorldY() {
Engine engine = mock(Engine.class);
IrisDimension dimension = mock(IrisDimension.class);
when(engine.getMinHeight()).thenReturn(-64);
when(engine.getDimension()).thenReturn(dimension);
when(dimension.getFluidHeight()).thenReturn(127);
assertEquals(63, TreeSVC.worldFluidHeight(engine));
}
}
@@ -10,6 +10,10 @@ import art.arcane.iris.engine.object.procedural.tree.IrisProceduralTree;
import art.arcane.iris.engine.object.world.region.IrisRegion;
import art.arcane.iris.engine.object.placement.IrisTree;
import art.arcane.volmlib.util.collection.KList;
import art.arcane.iris.core.loader.IrisData;
import art.arcane.iris.core.modules.ModuleResourceFixture;
import art.arcane.iris.engine.framework.EngineGeneration;
import art.arcane.iris.engine.mantle.components.ObjectModule;
import org.junit.Test;
import static org.junit.Assert.assertFalse;
@@ -38,6 +42,10 @@ public class TreeDefinitionIndexTest {
biome.setProceduralObjects(proceduralObjects);
when(engine.getDimension()).thenReturn(dimension);
IrisData data = mock(IrisData.class);
when(engine.getData()).thenReturn(data);
ModuleResourceFixture.serveModules(data, new ObjectModule());
when(engine.getGeneration()).thenReturn(new EngineGeneration(engine));
when(dimension.getAllRegions(engine)).thenReturn(new KList<IrisRegion>().qadd(region));
when(dimension.getReachableBiomes(engine)).thenReturn(new KList<IrisBiome>().qadd(biome));
@@ -0,0 +1,84 @@
/*
* 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.engine.mantle.components;
import art.arcane.iris.core.loader.IrisData;
import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.engine.framework.EngineGeneration;
import art.arcane.iris.engine.object.placement.IrisObjectPlacement;
import art.arcane.iris.engine.object.procedural.tree.IrisTreeSettings;
import art.arcane.iris.engine.object.world.biome.IrisBiome;
import art.arcane.iris.engine.object.world.dimension.IrisDimension;
import art.arcane.iris.engine.object.world.region.IrisRegion;
import com.volmit.service.SaplingGrowth;
import com.volmit.world.BlockPosition;
import com.volmit.world.PackWorld;
import org.junit.Before;
import org.junit.Test;
import java.util.Optional;
import static org.junit.Assert.assertTrue;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/** Trees only replace the platform's growth when the dimension asks for it and a placement of the area grows that species. */
public class TreeGrowingTest {
private static final SaplingGrowth.Sapling OAK_SAPLING = new SaplingGrowth.Sapling(
new BlockPosition(10, 70, 20), new BlockPosition(10, 70, 20), "TREE");
private Engine engine;
private IrisDimension dimension;
private PackWorld world;
@Before
public void buildWorld() {
engine = mock(Engine.class);
dimension = mock(IrisDimension.class);
when(engine.getDimension()).thenReturn(dimension);
when(engine.getData()).thenReturn(mock(IrisData.class));
world = new EngineGeneration(engine).world();
}
@Test
public void aDimensionWithoutTreeOverridesLeavesTheGrowthToThePlatform() {
when(dimension.getTreeSettings()).thenReturn(new IrisTreeSettings());
Optional<SaplingGrowth.Growth> growth = new TreeGrowing().grow(world, OAK_SAPLING, mock(SaplingGrowth.Surroundings.class));
assertTrue(growth.isEmpty());
verify(engine, never()).getBiome(anyInt(), anyInt(), anyInt());
}
@Test
public void withoutAPlacementThatGrowsTheSpeciesTheGrowthIsLeftToThePlatform() {
when(dimension.getTreeSettings()).thenReturn(new IrisTreeSettings().setEnabled(true));
IrisBiome biome = new IrisBiome();
biome.getObjects().add(new IrisObjectPlacement());
when(engine.getBiome(10, 70, 20)).thenReturn(biome);
when(engine.getRegion(10, 20)).thenReturn(new IrisRegion());
Optional<SaplingGrowth.Growth> growth = new TreeGrowing().grow(world, OAK_SAPLING, mock(SaplingGrowth.Surroundings.class));
assertTrue(growth.isEmpty());
}
}