IrisPackWorld implements LiveWorld.Source: the engine's answers for a block, the platform's sky and loot tables, an object's loot and the saved spawner cooldowns, as values; the world manager no longer tidies cooldowns itself (the spawn module does)

This commit is contained in:
piexel committed 2026-10-10 18:21:37 +02:00
1 parent 540b4beaed
commit 2b49ffb759
5 files changed
+430 -8

No files matched your search

@@ -0,0 +1,58 @@
/*
* 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.core.modules;
import art.arcane.iris.builtin.spawn.SpawnCooldowns;
import art.arcane.iris.engine.framework.Engine;
import java.util.function.ToLongFunction;
/** The cooldowns of an engine's spawners, in the data the engine saves with its world. */
final class EngineSpawnCooldowns implements SpawnCooldowns {
private final Engine engine;
EngineSpawnCooldowns(Engine engine) {
this.engine = engine;
}
@Override
public boolean canSpawn(String spawner, long intervalMilliseconds) {
return engine.getEngineData().getCooldown(spawner).canSpawn(intervalMilliseconds);
}
@Override
public void spawned(String spawner) {
engine.getEngineData().getCooldown(spawner).spawn();
}
@Override
public boolean canSpawnInChunk(int chunkX, int chunkZ, String spawner, long intervalMilliseconds) {
return engine.getEngineData().getChunk(chunkX, chunkZ).getCooldown(spawner).canSpawn(intervalMilliseconds);
}
@Override
public void spawnedInChunk(int chunkX, int chunkZ, String spawner) {
engine.getEngineData().getChunk(chunkX, chunkZ).getCooldown(spawner).spawn();
}
@Override
public void forgetExpired(ToLongFunction<String> intervalMilliseconds) {
engine.getEngineData().cleanup(intervalMilliseconds);
}
}
@@ -0,0 +1,155 @@
/*
* 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.core.modules;
import art.arcane.iris.builtin.LiveWorld;
import art.arcane.iris.builtin.spawn.SpawnCooldowns;
import art.arcane.iris.core.loader.IrisRegistrant;
import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.engine.object.placement.IrisObjectPlacement;
import art.arcane.iris.engine.object.value.IrisPosition;
import art.arcane.iris.engine.object.world.biome.IrisBiome;
import art.arcane.iris.engine.framework.PlacedObject;
import art.arcane.iris.engine.object.world.biome.InferredType;
import art.arcane.iris.spi.IrisPlatforms;
import art.arcane.iris.spi.PlatformWorld;
import art.arcane.iris.util.common.scheduling.J;
import art.arcane.volmlib.util.matter.slices.MarkerMatter;
import com.volmit.IrisFeature;
import com.volmit.resource.ResourceId;
import com.volmit.world.BlockPosition;
import java.util.List;
import java.util.Optional;
import java.util.Set;
/**
* An engine as {@link LiveWorld} shows it to the built-in services: the engine's own answers for a block, turned into pack
* resource ids and values.
*/
final class IrisLiveWorld implements LiveWorld {
private final Engine engine;
private final IrisPackResources resources;
private final SpawnCooldowns cooldowns;
private volatile Set<String> platformLootTables;
IrisLiveWorld(Engine engine, IrisPackResources resources) {
this.engine = engine;
this.resources = resources;
this.cooldowns = new EngineSpawnCooldowns(engine);
}
@Override
public int bottomY() {
return engine.getMinHeight();
}
@Override
public ResourceId surfaceBiome(int x, int z) {
return idOf(engine.getSurfaceBiome(x, z));
}
@Override
public ResourceId caveBiome(int x, int y, int z) {
IrisBiome cave = engine.getCaveBiome(x, y, z);
return cave == null ? surfaceBiome(x, z) : idOf(cave);
}
@Override
public BiomeKind surfaceKind(int x, int z) {
InferredType type = engine.getSurfaceBiome(x, z).getInferredType();
return BiomeKind.valueOf(type.name());
}
@Override
public double terrainHeight(int x, int z) {
return engine.getComplex().getHeightStream().get(x, z);
}
@Override
public int highestBlock(int x, int z, boolean ignoreFluid) {
return engine.getHeight(x, z, ignoreFluid);
}
@Override
public <F extends IrisFeature> Optional<F> settings(Class<F> type, Level level, int x, int y, int z) {
IrisRegistrant owner = switch (level) {
case DIMENSION -> engine.getDimension();
case REGION -> engine.getRegion(x, z);
case SURFACE_BIOME -> engine.getSurfaceBiome(x, z);
case BIOME -> engine.getBiome(x, y, z);
case CAVE_BIOME -> engine.getCaveBiome(x, y, z);
};
return owner == null ? Optional.empty() : owner.getModuleSettings().find(type);
}
@Override
public Optional<Sky> sky() {
PlatformWorld world = engine.getWorld().platformWorld();
if (world == null) {
return Optional.empty();
}
return Optional.of(new Sky(world.getTime(), world.isStorming(), world.isThundering()));
}
@Override
public boolean studio() {
return engine.isStudio();
}
@Override
public Set<String> platformLootTables() {
Set<String> known = platformLootTables;
if (known == null) {
known = Set.copyOf(IrisPlatforms.get().registries().lootTableKeys());
platformLootTables = known;
}
return known;
}
@Override
public List<BlockPosition> caveFloors(int chunkX, int chunkZ) {
if (J.isFolia()) {
return List.of();
}
int bottom = engine.getMinHeight();
return engine.getMantle().findMarkers(chunkX, chunkZ, MarkerMatter.CAVE_FLOOR).stream()
.map(floor -> new BlockPosition(floor.getX(), floor.getY() - bottom, floor.getZ()))
.toList();
}
@Override
public Optional<ObjectLoots> objectLoots(int x, int y, int z) {
PlacedObject placed = engine.getObjectPlacement(x, y, z);
IrisObjectPlacement placement = placed == null ? null : placed.getPlacement();
if (placement == null) {
return Optional.empty();
}
return Optional.of(new ObjectLoots(placement.getLoot(), placement.getVanillaLoot(), placement.isOverrideGlobalLoot()));
}
@Override
public SpawnCooldowns spawnCooldowns() {
return cooldowns;
}
private ResourceId idOf(IrisBiome biome) {
return resources.idOf(biome.getLoadKey());
}
}
@@ -19,6 +19,7 @@
package art.arcane.iris.core.modules;
import art.arcane.iris.builtin.IrisSeed;
import art.arcane.iris.builtin.LiveWorld;
import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.engine.framework.SeedManager;
import com.volmit.ModuleLog;
@@ -35,12 +36,13 @@ import java.util.function.Function;
* pack's resources reload under it. Iris's built-in modules reach the engine through {@link #of(PackWorld)}, as built-in
* reaches do through {@link IrisPackSettings#of}.
*/
public final class IrisPackWorld implements PackWorld, IrisSeed.Source {
public final class IrisPackWorld implements PackWorld, IrisSeed.Source, LiveWorld.Source {
private final Engine engine;
private final IrisPackResources resources;
private final WorldStates states;
private final ModuleLog log;
private final Map<ModuleLog, IrisPackWorld> viewsByLog;
private final LiveWorld liveWorld;
IrisPackWorld(Engine engine) {
this(engine, engine.getData().getPackResources(), new WorldStates(), IrisApiLogger.hostLog(), new ConcurrentHashMap<>());
@@ -53,6 +55,7 @@ public final class IrisPackWorld implements PackWorld, IrisSeed.Source {
this.states = states;
this.log = log;
this.viewsByLog = viewsByLog;
this.liveWorld = new IrisLiveWorld(engine, resources);
}
/** Iris's own view of {@code world}, which Iris hands to every stage, service and lifecycle hook. */
@@ -99,6 +102,11 @@ public final class IrisPackWorld implements PackWorld, IrisSeed.Source {
return viewsByLog.computeIfAbsent(log, moduleLog -> new IrisPackWorld(engine, resources, states, moduleLog, viewsByLog));
}
@Override
public LiveWorld liveWorld() {
return liveWorld;
}
@Override
public long seed(IrisSeed seed) {
SeedManager seeds = engine.getSeedManager();
@@ -58,7 +58,6 @@ public class IrisWorldManager extends EngineAssignedWorldManager {
private final KList<Runnable> updateQueue = new KList<>();
final ChronoLatch cl;
private final ChronoLatch clw;
private final ChronoLatch cln;
private final ChronoLatch chunkUpdater;
private final ChronoLatch chunkDiscovery;
private final KMap<Long, Future<?>> cleanup = new KMap<>();
@@ -89,7 +88,6 @@ public class IrisWorldManager extends EngineAssignedWorldManager {
public IrisWorldManager() {
super(null);
cl = null;
cln = null;
clw = null;
looper = null;
chunkUpdater = null;
@@ -101,7 +99,6 @@ public class IrisWorldManager extends EngineAssignedWorldManager {
super(engine);
chunkUpdater = new ChronoLatch(3000);
chunkDiscovery = new ChronoLatch(5000);
cln = new ChronoLatch(60000);
cl = new ChronoLatch(3000);
clw = new ChronoLatch(1000, true);
cleanupService = createCleanupExecutor(getTarget().getWorld().name());
@@ -157,10 +154,6 @@ public class IrisWorldManager extends EngineAssignedWorldManager {
chunkMaintenance.discoverChunks();
}
if (cln.flip()) {
getEngine().getEngineData().cleanup(getEngine());
}
if (!IrisSettings.get().getWorld().isMarkerEntitySpawningSystem()
&& !IrisSettings.get().getWorld().isAmbientEntitySpawningSystem()) {
return 3000L;
@@ -0,0 +1,208 @@
/*
* 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.core.modules;
import art.arcane.iris.builtin.LiveWorld;
import art.arcane.iris.builtin.LiveWorld.BiomeKind;
import art.arcane.iris.builtin.LiveWorld.Level;
import art.arcane.iris.builtin.LiveWorld.ObjectLoots;
import art.arcane.iris.builtin.LiveWorld.Sky;
import art.arcane.iris.builtin.loot.LootSettings;
import art.arcane.iris.builtin.loot.ObjectLoot;
import art.arcane.iris.builtin.spawn.SpawnCooldowns;
import art.arcane.iris.core.loader.IrisData;
import art.arcane.iris.core.loader.IrisRegistrant;
import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.engine.framework.EngineGeneration;
import art.arcane.iris.engine.UsableBlockPlatform;
import art.arcane.iris.engine.framework.PlacedObject;
import art.arcane.iris.engine.mantle.EngineMantle;
import art.arcane.iris.engine.object.TestSettings;
import art.arcane.iris.engine.object.placement.IrisObjectPlacement;
import art.arcane.iris.engine.object.runtime.IrisEngineData;
import art.arcane.iris.engine.object.value.IrisPosition;
import art.arcane.iris.engine.object.world.IrisWorld;
import art.arcane.iris.engine.object.world.biome.InferredType;
import art.arcane.iris.engine.object.world.biome.IrisBiome;
import art.arcane.iris.engine.object.world.region.IrisRegion;
import art.arcane.iris.engine.object.world.dimension.IrisDimension;
import art.arcane.iris.spi.PlatformWorld;
import art.arcane.volmlib.util.collection.KList;
import com.volmit.world.BlockPosition;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import java.util.List;
import java.util.Optional;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/** What the built-in services ask a running world: the engine's answers for a block, as values. */
public class IrisLiveWorldTest {
private static final int X = 10;
private static final int Y = 70;
private static final int Z = -20;
private static final int BOTTOM = -64;
private Engine engine;
private LiveWorld live;
@BeforeClass
public static void bindPlatform() {
UsableBlockPlatform.bind();
}
@AfterClass
public static void unbindPlatform() {
UsableBlockPlatform.unbind();
}
@Before
public void buildWorld() {
engine = mock(Engine.class);
IrisData data = mock(IrisData.class);
ModuleResourceFixture.serve(data);
when(engine.getData()).thenReturn(data);
when(engine.getMinHeight()).thenReturn(BOTTOM);
live = LiveWorld.of(new EngineGeneration(engine).world());
}
@Test
public void everyLevelReadsTheSettingsOfItsOwnerAtTheBlock() {
IrisDimension dimension = owner(IrisDimension.class, "dimension");
IrisRegion region = owner(IrisRegion.class, "region");
IrisBiome surface = owner(IrisBiome.class, "surface");
IrisBiome here = owner(IrisBiome.class, "here");
IrisBiome cave = owner(IrisBiome.class, "cave");
when(engine.getDimension()).thenReturn(dimension);
when(engine.getRegion(X, Z)).thenReturn(region);
when(engine.getSurfaceBiome(X, Z)).thenReturn(surface);
when(engine.getBiome(X, Y, Z)).thenReturn(here);
when(engine.getCaveBiome(X, Y, Z)).thenReturn(cave);
assertEquals(List.of("dimension", "region", "surface", "here", "cave"), List.of(
tableAt(Level.DIMENSION), tableAt(Level.REGION), tableAt(Level.SURFACE_BIOME), tableAt(Level.BIOME), tableAt(Level.CAVE_BIOME)));
}
@Test
public void aLevelWithoutSettingsOrOwnerHasNone() {
IrisRegion bare = mock(IrisRegion.class);
when(bare.getModuleSettings()).thenReturn(ModuleSettings.NONE);
when(engine.getRegion(X, Z)).thenReturn(bare);
assertTrue(live.settings(LootSettings.class, Level.REGION, X, Y, Z).isEmpty());
assertTrue(live.settings(LootSettings.class, Level.BIOME, X, Y, Z).isEmpty());
}
@Test
public void biomesAreAskedByTheIdsThePackGivesThem() {
IrisBiome surface = mock(IrisBiome.class);
when(surface.getLoadKey()).thenReturn("Plains/Meadow");
when(surface.getInferredType()).thenReturn(InferredType.SHORE);
when(engine.getSurfaceBiome(X, Z)).thenReturn(surface);
when(engine.getCaveBiome(X, Y, Z)).thenReturn(null);
assertEquals(engine.getData().getPackResources().idOf("Plains/Meadow"), live.surfaceBiome(X, Z));
assertEquals(live.surfaceBiome(X, Z), live.caveBiome(X, Y, Z));
assertEquals(BiomeKind.SHORE, live.surfaceKind(X, Z));
assertEquals(BOTTOM, live.bottomY());
}
@Test
public void theSkyIsTheWorldsTimeAndWeatherOnceThereIsAPlatformWorld() {
IrisWorld world = mock(IrisWorld.class);
when(engine.getWorld()).thenReturn(world);
assertEquals(Optional.empty(), live.sky());
PlatformWorld platform = mock(PlatformWorld.class);
when(platform.getTime()).thenReturn(18000L);
when(platform.isThundering()).thenReturn(true);
when(world.platformWorld()).thenReturn(platform);
assertEquals(Optional.of(new Sky(18000L, false, true)), live.sky());
}
@Test
public void caveFloorsAreHeightsAboveTheBottom() {
EngineMantle mantle = mock(EngineMantle.class);
when(engine.getMantle()).thenReturn(mantle);
when(mantle.findMarkers(2, 3, art.arcane.volmlib.util.matter.slices.MarkerMatter.CAVE_FLOOR))
.thenReturn(new KList<>(new IrisPosition(40, 0, 50), new IrisPosition(41, 30, 51)));
assertEquals(List.of(new BlockPosition(40, 64, 50), new BlockPosition(41, 94, 51)), live.caveFloors(2, 3));
}
@Test
public void anObjectsLootIsWhatItsPlacementDefines() {
IrisObjectPlacement placement = new IrisObjectPlacement();
placement.getLoot().add(new ObjectLoot("chest", 2));
placement.setOverrideGlobalLoot(true);
when(engine.getObjectPlacement(X, Y, Z)).thenReturn(new PlacedObject(placement, null, 1, X, Z));
when(engine.getObjectPlacement(X + 1, Y, Z)).thenReturn(new PlacedObject(null, null, 1, X + 1, Z));
ObjectLoots loots = live.objectLoots(X, Y, Z).orElseThrow();
assertEquals(1, loots.packTables().size());
assertEquals("chest", loots.packTables().getFirst().name());
assertTrue(loots.platformTables().isEmpty());
assertTrue(loots.overridesGlobalLoot());
assertTrue(loots.defined());
assertTrue(live.objectLoots(X + 1, Y, Z).isEmpty());
assertTrue(live.objectLoots(X + 2, Y, Z).isEmpty());
}
@Test
public void spawnCooldownsCountPerWorldAndPerChunkAndForgetWhatRanOut() {
IrisEngineData saved = new IrisEngineData();
when(engine.getEngineData()).thenReturn(saved);
SpawnCooldowns cooldowns = live.spawnCooldowns();
assertTrue(cooldowns.canSpawn("wolves", 600_000L));
cooldowns.spawned("wolves");
cooldowns.spawnedInChunk(3, 4, "wolves");
cooldowns.spawned("gone");
assertFalse(cooldowns.canSpawn("wolves", 600_000L));
assertFalse(cooldowns.canSpawnInChunk(3, 4, "wolves", 600_000L));
assertTrue(cooldowns.canSpawnInChunk(3, 5, "wolves", 600_000L));
cooldowns.forgetExpired(spawner -> spawner.equals("wolves") ? 600_000L : -1L);
assertFalse("a cooldown still running stays", cooldowns.canSpawn("wolves", 600_000L));
assertFalse(cooldowns.canSpawnInChunk(3, 4, "wolves", 600_000L));
assertTrue("a spawner the pack lacks is forgotten", cooldowns.canSpawn("gone", 600_000L));
}
private <T extends IrisRegistrant> T owner(Class<T> type, String table) {
T owner = mock(type);
LootSettings loot = TestSettings.of(LootSettings.class, "{\"tables\": [\"" + table + "\"]}");
when(owner.getModuleSettings()).thenReturn(BuiltInPack.settings(loot));
return owner;
}
private String tableAt(Level level) {
return live.settings(LootSettings.class, level, X, Y, Z).orElseThrow().tables().getFirst();
}
}