Fix Iris entity spawn delegation

This commit is contained in:
Brian Neumann-Fopiano
2026-07-29 18:10:55 -04:00
parent 74741ac83e
commit 7ad4909518
14 changed files with 150 additions and 35 deletions
@@ -37,6 +37,7 @@ import net.minecraft.util.random.WeightedList;
import net.minecraft.world.level.ChunkPos; import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelHeightAccessor; import net.minecraft.world.level.LevelHeightAccessor;
import net.minecraft.world.level.NaturalSpawner;
import net.minecraft.world.level.NoiseColumn; import net.minecraft.world.level.NoiseColumn;
import net.minecraft.world.level.StructureManager; import net.minecraft.world.level.StructureManager;
import net.minecraft.world.level.WorldGenLevel; import net.minecraft.world.level.WorldGenLevel;
@@ -46,6 +47,7 @@ import net.minecraft.world.level.biome.BiomeSource;
import net.minecraft.world.level.biome.MobSpawnSettings; import net.minecraft.world.level.biome.MobSpawnSettings;
import net.minecraft.world.level.levelgen.GenerationStep; import net.minecraft.world.level.levelgen.GenerationStep;
import net.minecraft.world.level.levelgen.Heightmap; import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.level.levelgen.LegacyRandomSource;
import net.minecraft.world.level.levelgen.RandomState; import net.minecraft.world.level.levelgen.RandomState;
import net.minecraft.world.level.levelgen.RandomSupport; import net.minecraft.world.level.levelgen.RandomSupport;
import net.minecraft.world.level.levelgen.WorldgenRandom; import net.minecraft.world.level.levelgen.WorldgenRandom;
@@ -574,8 +576,14 @@ public class IrisChunkGenerator extends CustomChunkGenerator {
} }
@Override @Override
public void spawnOriginalMobs(WorldGenRegion regionlimitedworldaccess) { public void spawnOriginalMobs(WorldGenRegion region) {
delegate.spawnOriginalMobs(regionlimitedworldaccess); ChunkPos center = region.getCenter();
Holder<Biome> visibleBiome = region.getBiome(center.getWorldPosition().atY(region.getMaxY()));
Holder<Biome> vanillaBiome = customBiomeSource.getVanillaSpawnBiome(visibleBiome);
WorldgenRandom random = new WorldgenRandom(new LegacyRandomSource(RandomSupport.generateUniqueSeed()));
random.setDecorationSeed(region.getSeed(), center.getMinBlockX(), center.getMinBlockZ());
NaturalSpawner.spawnMobsForChunkGeneration(
region, vanillaBiome == null ? visibleBiome : vanillaBiome, center, random);
} }
private static WeightedList<MobSpawnSettings.SpawnerData> mergeSpawnTables( private static WeightedList<MobSpawnSettings.SpawnerData> mergeSpawnTables(
@@ -64,4 +64,19 @@ public class IrisChunkGeneratorFailureContractTest {
assertTrue(source.contains("engine.acquireGenerationLease(\"bukkit_nms_base_column\")")); assertTrue(source.contains("engine.acquireGenerationLease(\"bukkit_nms_base_column\")"));
assertTrue(source.contains("catch (GenerationSessionException e)")); assertTrue(source.contains("catch (GenerationSessionException e)"));
} }
@Test
public void vanillaChunkGenerationMobsUseTheVisibleBiomesVanillaDerivative() throws IOException {
String source = Files.readString(Path.of(System.getProperty("iris.nmsChunkGeneratorSource")));
int spawnStart = source.indexOf("public void spawnOriginalMobs");
int spawnEnd = source.indexOf("private static WeightedList", spawnStart);
String spawn = source.substring(spawnStart, spawnEnd);
assertTrue(spawn.contains("customBiomeSource.getVanillaSpawnBiome(visibleBiome)"));
assertTrue(spawn.contains("NaturalSpawner.spawnMobsForChunkGeneration("));
assertTrue(spawn.contains("region.getBiome(center.getWorldPosition().atY(region.getMaxY()))"));
assertTrue(spawn.contains("new LegacyRandomSource(RandomSupport.generateUniqueSeed())"));
assertTrue(spawn.contains("random.setDecorationSeed(region.getSeed()"));
assertFalse(spawn.contains("delegate.spawnOriginalMobs"));
}
} }
@@ -971,7 +971,7 @@ public class Iris extends VolmitPlugin implements Listener, ReloadAware {
final Location spawn = w.getSpawnLocation(); final Location spawn = w.getSpawnLocation();
for (Player i : getServer().getOnlinePlayers()) { for (Player i : getServer().getOnlinePlayers()) {
final Runnable playerTask = () -> { final Runnable playerTask = () -> {
i.setGameMode(GameMode.SPECTATOR); i.setGameMode(GameMode.CREATIVE);
BukkitPlatform.teleportAsync(i, spawn); BukkitPlatform.teleportAsync(i, spawn);
}; };
if (!J.runEntity(i, playerTask)) { if (!J.runEntity(i, playerTask)) {
@@ -657,7 +657,7 @@ public class CommandStudio implements DirectorExecutor {
.getTarget() .getTarget()
.getWorld(); .getWorld();
BukkitPlatform.teleportAsync(player, BukkitWorldBinding.spawnLocation(studioWorld)) BukkitPlatform.teleportAsync(player, BukkitWorldBinding.spawnLocation(studioWorld))
.thenRun(() -> player.setGameMode(GameMode.SPECTATOR)); .thenRun(() -> player.setGameMode(GameMode.CREATIVE));
} }
@Director(description = "Update your dimension projects VSCode workspace", descriptionKey = "iris.director.commandstudio.director.update_your_dimension_projects_vscode_workspace") @Director(description = "Update your dimension projects VSCode workspace", descriptionKey = "iris.director.commandstudio.director.update_your_dimension_projects_vscode_workspace")
@@ -0,0 +1,24 @@
package art.arcane.iris.core.runtime;
import org.junit.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class StudioPlayerModeContractTest {
@Test
public void studioEntryKeepsPlayersEligibleForNaturalSpawning() throws IOException {
String plugin = Files.readString(Path.of("src/main/java/art/arcane/iris/Iris.java"));
String commands = Files.readString(Path.of(
"src/main/java/art/arcane/iris/core/commands/CommandStudio.java"));
assertFalse(plugin.contains("GameMode.SPECTATOR"));
assertFalse(commands.contains("GameMode.SPECTATOR"));
assertTrue(plugin.contains("GameMode.CREATIVE"));
assertTrue(commands.contains("GameMode.CREATIVE"));
}
}
@@ -63,6 +63,7 @@ import net.minecraft.world.entity.MobCategory;
import net.minecraft.world.level.ChunkPos; import net.minecraft.world.level.ChunkPos;
import net.minecraft.world.level.Level; import net.minecraft.world.level.Level;
import net.minecraft.world.level.LevelHeightAccessor; import net.minecraft.world.level.LevelHeightAccessor;
import net.minecraft.world.level.NaturalSpawner;
import net.minecraft.world.level.NoiseColumn; import net.minecraft.world.level.NoiseColumn;
import net.minecraft.world.level.StructureManager; import net.minecraft.world.level.StructureManager;
import net.minecraft.world.level.WorldGenLevel; import net.minecraft.world.level.WorldGenLevel;
@@ -81,6 +82,7 @@ import net.minecraft.world.level.chunk.ChunkGeneratorStructureState;
import net.minecraft.world.level.chunk.LevelChunkSection; import net.minecraft.world.level.chunk.LevelChunkSection;
import net.minecraft.world.level.levelgen.GenerationStep; import net.minecraft.world.level.levelgen.GenerationStep;
import net.minecraft.world.level.levelgen.Heightmap; import net.minecraft.world.level.levelgen.Heightmap;
import net.minecraft.world.level.levelgen.LegacyRandomSource;
import net.minecraft.world.level.levelgen.RandomState; import net.minecraft.world.level.levelgen.RandomState;
import net.minecraft.world.level.levelgen.RandomSupport; import net.minecraft.world.level.levelgen.RandomSupport;
import net.minecraft.world.level.levelgen.WorldgenRandom; import net.minecraft.world.level.levelgen.WorldgenRandom;
@@ -1191,6 +1193,15 @@ public final class IrisModdedChunkGenerator extends ChunkGenerator {
@Override @Override
public void spawnOriginalMobs(WorldGenRegion region) { public void spawnOriginalMobs(WorldGenRegion region) {
Registry<Biome> registry = region.registryAccess().lookupOrThrow(Registries.BIOME);
initializeVanillaSpawnBiomes(registry);
ChunkPos center = region.getCenter();
Holder<Biome> visibleBiome = region.getBiome(center.getWorldPosition().atY(region.getMaxY()));
Holder<Biome> vanillaBiome = vanillaSpawnBiomes.get(visibleBiome.value());
WorldgenRandom random = new WorldgenRandom(new LegacyRandomSource(RandomSupport.generateUniqueSeed()));
random.setDecorationSeed(region.getSeed(), center.getMinBlockX(), center.getMinBlockZ());
NaturalSpawner.spawnMobsForChunkGeneration(
region, vanillaBiome == null ? visibleBiome : vanillaBiome, center, random);
} }
@Override @Override
@@ -712,7 +712,7 @@ public final class ModdedWorldManager implements EngineWorldManager {
} }
private boolean isEntitySpawningEnabledForCurrentWorld() { private boolean isEntitySpawningEnabledForCurrentWorld() {
return entitySpawningEnabled(engine.isStudio(), IrisSettings.get().getStudio().isEnableEntitySpawning()); return entitySpawningEnabled(engine.isStudio(), IrisSettings.get().getStudio().isEntitySpawning());
} }
static boolean entitySpawningEnabled(boolean studio, boolean studioSetting) { static boolean entitySpawningEnabled(boolean studio, boolean studioSetting) {
@@ -9,9 +9,13 @@ import net.minecraft.world.level.biome.MobSpawnSettings;
import org.junit.Test; import org.junit.Test;
import org.junit.BeforeClass; import org.junit.BeforeClass;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List; import java.util.List;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class IrisModdedChunkGeneratorSpawnTest { public class IrisModdedChunkGeneratorSpawnTest {
@BeforeClass @BeforeClass
@@ -44,4 +48,20 @@ public class IrisModdedChunkGeneratorSpawnTest {
assertEquals(explicitCow, entries.get(2).value()); assertEquals(explicitCow, entries.get(2).value());
assertEquals(3, entries.get(2).weight()); assertEquals(3, entries.get(2).weight());
} }
@Test
public void vanillaChunkGenerationMobsUseTheVisibleBiomesVanillaDerivative() throws IOException {
Path sourcePath = Path.of(System.getProperty("iris.moddedCommonSources"),
"art/arcane/iris/modded/IrisModdedChunkGenerator.java");
String source = Files.readString(sourcePath);
int spawnStart = source.indexOf("public void spawnOriginalMobs");
int spawnEnd = source.indexOf("@Override", spawnStart + 1);
String spawn = source.substring(spawnStart, spawnEnd);
assertTrue(spawn.contains("initializeVanillaSpawnBiomes(registry)"));
assertTrue(spawn.contains("vanillaSpawnBiomes.get(visibleBiome.value())"));
assertTrue(spawn.contains("NaturalSpawner.spawnMobsForChunkGeneration("));
assertTrue(spawn.contains("new LegacyRandomSource(RandomSupport.generateUniqueSeed())"));
assertTrue(spawn.contains("random.setDecorationSeed(region.getSeed()"));
}
} }
@@ -295,7 +295,7 @@ public class IrisSettings {
public boolean studio = true; public boolean studio = true;
public boolean openVSCode = true; public boolean openVSCode = true;
public boolean disableTimeAndWeather = true; public boolean disableTimeAndWeather = true;
public boolean enableEntitySpawning = false; public boolean entitySpawning = true;
public boolean autoStartDefaultStudio = false; public boolean autoStartDefaultStudio = false;
} }
@@ -295,7 +295,7 @@ public class IrisProject {
} }
if (sender.isPlayer() && sender.player() != null) { if (sender.isPlayer() && sender.player() != null) {
J.runEntity(sender.player(), () -> sender.player().setGameMode(GameMode.SPECTATOR)); J.runEntity(sender.player(), () -> sender.player().setGameMode(GameMode.CREATIVE));
} }
activeProvider = IrisToolbelt.access(result.world()); activeProvider = IrisToolbelt.access(result.world());
maintenanceWorld = result.world(); maintenanceWorld = result.world();
@@ -82,6 +82,7 @@ public final class WorldRuntimeControlService {
IrisServices.get(art.arcane.iris.core.link.MultiverseCoreLink.class).removeFromConfig(world); IrisServices.get(art.arcane.iris.core.link.MultiverseCoreLink.class).removeFromConfig(world);
setIntGameRule(world, 0, "SPAWN_CHUNK_RADIUS", "spawnChunkRadius"); setIntGameRule(world, 0, "SPAWN_CHUNK_RADIUS", "spawnChunkRadius");
enableStudioEntitySpawning(world);
if (!IrisSettings.get().getStudio().isDisableTimeAndWeather()) { if (!IrisSettings.get().getStudio().isDisableTimeAndWeather()) {
return true; return true;
} }
@@ -100,17 +101,12 @@ public final class WorldRuntimeControlService {
applyStudioWorldRules(world); applyStudioWorldRules(world);
setBooleanGameRule(world, false, "DO_FIRE_TICK", "doFireTick"); setBooleanGameRule(world, false, "DO_FIRE_TICK", "doFireTick");
setBooleanGameRule(world, false, "DO_MOB_SPAWNING", "doMobSpawning");
setBooleanGameRule(world, false, "DO_MOB_LOOT", "doMobLoot"); setBooleanGameRule(world, false, "DO_MOB_LOOT", "doMobLoot");
setBooleanGameRule(world, false, "DO_TRADER_SPAWNING", "doTraderSpawning");
setBooleanGameRule(world, false, "DO_PATROL_SPAWNING", "doPatrolSpawning");
setBooleanGameRule(world, false, "DO_INSOMNIA", "doInsomnia");
setBooleanGameRule(world, true, "DO_IMMEDIATE_RESPAWN", "doImmediateRespawn"); setBooleanGameRule(world, true, "DO_IMMEDIATE_RESPAWN", "doImmediateRespawn");
setBooleanGameRule(world, false, "FALL_DAMAGE", "fallDamage"); setBooleanGameRule(world, false, "FALL_DAMAGE", "fallDamage");
setBooleanGameRule(world, false, "FIRE_DAMAGE", "fireDamage"); setBooleanGameRule(world, false, "FIRE_DAMAGE", "fireDamage");
setBooleanGameRule(world, false, "DROWNING_DAMAGE", "drowningDamage"); setBooleanGameRule(world, false, "DROWNING_DAMAGE", "drowningDamage");
setBooleanGameRule(world, false, "FREEZE_DAMAGE", "freezeDamage"); setBooleanGameRule(world, false, "FREEZE_DAMAGE", "freezeDamage");
setBooleanGameRule(world, false, "DO_WARDEN_SPAWNING", "doWardenSpawning");
setBooleanGameRule(world, false, "MOB_GRIEFING", "mobGriefing"); setBooleanGameRule(world, false, "MOB_GRIEFING", "mobGriefing");
setBooleanGameRule(world, false, "DO_TILE_DROPS", "doTileDrops"); setBooleanGameRule(world, false, "DO_TILE_DROPS", "doTileDrops");
setBooleanGameRule(world, true, "KEEP_INVENTORY", "keepInventory"); setBooleanGameRule(world, true, "KEEP_INVENTORY", "keepInventory");
@@ -121,6 +117,14 @@ public final class WorldRuntimeControlService {
return true; return true;
} }
static void enableStudioEntitySpawning(World world) {
setBooleanGameRule(world, true, "DO_MOB_SPAWNING", "doMobSpawning");
setBooleanGameRule(world, true, "DO_TRADER_SPAWNING", "doTraderSpawning");
setBooleanGameRule(world, true, "DO_PATROL_SPAWNING", "doPatrolSpawning");
setBooleanGameRule(world, true, "DO_INSOMNIA", "doInsomnia");
setBooleanGameRule(world, true, "DO_WARDEN_SPAWNING", "doWardenSpawning");
}
public boolean applyNoonTimeLock(World world) { public boolean applyNoonTimeLock(World world) {
if (world == null) { if (world == null) {
return false; return false;
@@ -40,8 +40,6 @@ import org.bukkit.block.Block;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.event.EventHandler; import org.bukkit.event.EventHandler;
import org.bukkit.event.block.Action; import org.bukkit.event.block.Action;
import org.bukkit.event.entity.CreatureSpawnEvent;
import org.bukkit.event.entity.EntitySpawnEvent;
import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.event.player.PlayerInteractEvent;
import org.bukkit.event.world.WorldUnloadEvent; import org.bukkit.event.world.WorldUnloadEvent;
import org.bukkit.inventory.EquipmentSlot; import org.bukkit.inventory.EquipmentSlot;
@@ -129,26 +127,6 @@ public class ObjectStudioSaveService implements IrisService {
unregister(event.getWorld()); unregister(event.getWorld());
} }
@EventHandler(ignoreCancelled = true)
public void onCreatureSpawn(CreatureSpawnEvent event) {
if (!studios.containsKey(event.getLocation().getWorld().getUID())) return;
CreatureSpawnEvent.SpawnReason reason = event.getSpawnReason();
if (reason == CreatureSpawnEvent.SpawnReason.CUSTOM
|| reason == CreatureSpawnEvent.SpawnReason.COMMAND
|| reason == CreatureSpawnEvent.SpawnReason.SPAWNER_EGG) {
return;
}
event.setCancelled(true);
}
@EventHandler(ignoreCancelled = true)
public void onEntitySpawn(EntitySpawnEvent event) {
if (event instanceof CreatureSpawnEvent) return;
if (!studios.containsKey(event.getLocation().getWorld().getUID())) return;
if (event.getEntity() instanceof org.bukkit.entity.Player) return;
event.setCancelled(true);
}
@EventHandler(ignoreCancelled = true) @EventHandler(ignoreCancelled = true)
public void onPlayerInteract(PlayerInteractEvent event) { public void onPlayerInteract(PlayerInteractEvent event) {
if (event.getHand() != EquipmentSlot.HAND) return; if (event.getHand() != EquipmentSlot.HAND) return;
@@ -979,7 +979,7 @@ public class IrisWorldManager extends EngineAssignedWorldManager {
return true; return true;
} }
return IrisSettings.get().getStudio().isEnableEntitySpawning(); return IrisSettings.get().getStudio().isEntitySpawning();
} }
private KList<IrisEntitySpawn> spawnRandomly(List<IrisEntitySpawn> types) { private KList<IrisEntitySpawn> spawnRandomly(List<IrisEntitySpawn> types) {
@@ -0,0 +1,55 @@
package art.arcane.iris.core.runtime;
import art.arcane.iris.core.IrisSettings;
import art.arcane.iris.core.service.ObjectStudioSaveService;
import org.junit.Test;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Arrays;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class StudioEntitySpawningTest {
@Test
public void studioEntitySpawningDefaultsToEnabled() {
assertTrue(new IrisSettings.IrisSettingsStudio().isEntitySpawning());
}
@Test
public void studioWorldRulesEnableVanillaMobSpawning() throws IOException {
String source = Files.readString(Path.of(
"src/main/java/art/arcane/iris/core/runtime/WorldRuntimeControlService.java"));
int helperStart = source.indexOf("static void enableStudioEntitySpawning");
int helperEnd = source.indexOf("public boolean applyNoonTimeLock", helperStart);
String helper = source.substring(helperStart, helperEnd);
assertTrue(source.contains("enableStudioEntitySpawning(world);"));
assertTrue(helper.contains("setBooleanGameRule(world, true, \"DO_MOB_SPAWNING\""));
assertTrue(helper.contains("setBooleanGameRule(world, true, \"DO_TRADER_SPAWNING\""));
assertTrue(helper.contains("setBooleanGameRule(world, true, \"DO_PATROL_SPAWNING\""));
assertTrue(helper.contains("setBooleanGameRule(world, true, \"DO_INSOMNIA\""));
assertTrue(helper.contains("setBooleanGameRule(world, true, \"DO_WARDEN_SPAWNING\""));
assertFalse(helper.contains("setBooleanGameRule(world, false, \"DO_MOB_SPAWNING\""));
}
@Test
public void generalStudioUsesAPlayerModeEligibleForNaturalSpawning() throws IOException {
String source = Files.readString(Path.of(
"src/main/java/art/arcane/iris/core/project/IrisProject.java"));
assertFalse(source.contains("GameMode.SPECTATOR"));
assertTrue(source.contains("GameMode.CREATIVE"));
}
@Test
public void objectStudioDoesNotCancelEntitySpawns() {
boolean hasSpawnCanceller = Arrays.stream(ObjectStudioSaveService.class.getDeclaredMethods())
.anyMatch(method -> method.getName().equals("onCreatureSpawn")
|| method.getName().equals("onEntitySpawn"));
assertFalse(hasSpawnCanceller);
}
}