Spawn water and lava entities only inside matching fluid

Fluid entities were placed above the sea and checked against the block below and for air, so they could not spawn where they live. They now take a Y inside the fluid, are always surface-checked (marker spawns too), and both hosts test the block itself and require the fluid across the entity's volume.

Upstream: e33e6a632
This commit is contained in:
piexel committed 2026-11-10 01:10:16 +01:00
1 parent 73863e8219
commit 9b86b34187
9 files changed
+199 -26

No files matched your search

@@ -22,6 +22,7 @@ import art.arcane.iris.core.IrisSettings;
import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.core.content.ContentEntity;
import art.arcane.iris.core.content.ContentItem;
import art.arcane.iris.core.content.ContentSpawns;
import art.arcane.iris.modded.api.ModdedCustomContentRegistry;
import art.arcane.iris.spi.IrisLogging;
import art.arcane.volmlib.util.math.RNG;
@@ -248,7 +249,8 @@ public final class ModdedEntitySpawner {
}
}
static boolean isAreaClearForSpawn(ServerLevel level, ContentEntity entity, int blockX, int blockY, int blockZ) {
static boolean isAreaClearForSpawn(ServerLevel level, ContentEntity entity, ContentSpawns.Surface surface,
int blockX, int blockY, int blockZ) {
if (entity.isSpecial()) {
return true;
}
@@ -256,10 +258,33 @@ public final class ModdedEntitySpawner {
if (type == null) {
return true;
}
if (surface.isFluid()) {
return isFluidAreaClearForSpawn(blockX, blockY, blockZ, type.getWidth(), type.getHeight(),
position -> ModdedWorldManager.matchesSurface(surface, level.getBlockState(position)));
}
return isAreaClearForSpawn(blockX, blockY, blockZ, type.getWidth(), type.getHeight(),
position -> level.getBlockState(position).is(Blocks.AIR));
}
static boolean isFluidAreaClearForSpawn(int blockX, int blockY, int blockZ,
float width, float height, Predicate<BlockPos> isFluid) {
int startX = (int) Math.floor(blockX + 0.5 - width / 2D);
int endX = (int) Math.floor(Math.nextDown(blockX + 0.5 + width / 2D));
int endY = (int) Math.floor(Math.nextDown(blockY + 0.5 + height));
int startZ = (int) Math.floor(blockZ + 0.5 - width / 2D);
int endZ = (int) Math.floor(Math.nextDown(blockZ + 0.5 + width / 2D));
for (int x = startX; x <= endX; x++) {
for (int y = blockY; y <= endY; y++) {
for (int z = startZ; z <= endZ; z++) {
if (!isFluid.test(new BlockPos(x, y, z))) {
return false;
}
}
}
}
return true;
}
static boolean isAreaClearForSpawn(int blockX, int blockY, int blockZ, float width, float height, Predicate<BlockPos> isAir) {
int radius = (int) (width / 2F);
int endY = blockY + (int) height;
@@ -465,12 +465,14 @@ public final class ModdedWorldManager implements EngineWorldManager {
}
private static boolean surfaceAllows(ContentSpawns.Attempt attempt, ServerLevel level) {
BlockState below = level.getBlockState(new BlockPos(attempt.x(), attempt.worldY() - 1, attempt.z()));
return matchesSurface(attempt.surface(), below)
&& ModdedEntitySpawner.isAreaClearForSpawn(level, attempt.entity(), attempt.x(), attempt.worldY(), attempt.z());
int surfaceY = attempt.surface().isFluid() ? attempt.worldY() : attempt.worldY() - 1;
BlockState surfaceBlock = level.getBlockState(new BlockPos(attempt.x(), surfaceY, attempt.z()));
return matchesSurface(attempt.surface(), surfaceBlock)
&& ModdedEntitySpawner.isAreaClearForSpawn(level, attempt.entity(), attempt.surface(),
attempt.x(), attempt.worldY(), attempt.z());
}
private static boolean matchesSurface(ContentSpawns.Surface surface, BlockState below) {
static boolean matchesSurface(ContentSpawns.Surface surface, BlockState below) {
if (ModdedBlockResolution.isSolid(below)) {
return surface == ContentSpawns.Surface.LAND || surface == ContentSpawns.Surface.OVERWORLD
|| (surface == ContentSpawns.Surface.ANIMAL && isAnimalGround(below));
@@ -51,6 +51,34 @@ public class ModdedEntitySpawnerParityTest {
assertFalse(clear);
}
@Test
public void fractionalFluidVolumeIncludesHorizontalEdgesAndCenteredHeight() {
Set<BlockPos> checked = new HashSet<>();
assertTrue(ModdedEntitySpawner.isFluidAreaClearForSpawn(-1, 78, -1, 1.3F, 0.8F, position -> {
checked.add(position);
return true;
}));
assertEquals(18, checked.size());
assertTrue(checked.contains(new BlockPos(-2, 79, -2)));
assertTrue(checked.contains(new BlockPos(0, 78, 0)));
assertFalse(ModdedEntitySpawner.isFluidAreaClearForSpawn(-1, 78, -1, 1.3F, 0.8F,
position -> !position.equals(new BlockPos(-2, 79, -2))));
}
@Test
public void exactUpperBoundaryDoesNotRequireFluidOutsideTheBody() {
Set<BlockPos> checked = new HashSet<>();
assertTrue(ModdedEntitySpawner.isFluidAreaClearForSpawn(0, 78, 0, 1F, 0.5F, position -> {
checked.add(position);
return true;
}));
assertEquals(Set.of(new BlockPos(0, 78, 0)), checked);
}
@Test
public void spawnSafetyRequiresTheTargetChunkAndAllEightNeighbors() {
Set<Long> loaded = new HashSet<>();
@@ -93,7 +93,8 @@ public interface EntitySpawns {
/**
* One entity to spawn at a block. The host spawns it only if the block's light level lies in the range (when the
* range is restricted) and, when {@code checkSurface}, the block below suits the entity's {@code surface} and the
* space the entity needs is free.
* space the entity needs is free. An entity of a fluid surface is always checked, and needs the block it spawns in
* and the space it fills to be that fluid.
*
* @param at world X and Z, Y the height above the dimension's bottom
* @param entity what to spawn
@@ -126,6 +127,11 @@ public interface EntitySpawns {
/** Solid ground or water. */
OVERWORLD,
/** Within lava. */
LAVA
LAVA;
/** Whether the entity lives in the fluid, so the block it spawns in must be that fluid. */
public boolean isFluid() {
return this == WATER || this == LAVA;
}
}
}
@@ -30,7 +30,11 @@ public final class ContentSpawns {
/** The ground or fluid an entity spawns on. */
public enum Surface {
LAND, ANIMAL, WATER, OVERWORLD, LAVA
LAND, ANIMAL, WATER, OVERWORLD, LAVA;
public boolean isFluid() {
return this == WATER || this == LAVA;
}
}
/**
@@ -368,12 +368,38 @@ final class WorldEntitySpawner {
}
private static boolean surfaceAllows(Location l, EntitySpawns.Attempt attempt) {
if (!BukkitSurfaces.matches(attempt.surface(), l.clone().subtract(0, 1, 0).getBlock())) {
EntitySpawns.Surface surface = attempt.surface();
boolean fluid = surface.isFluid();
if (!BukkitSurfaces.matches(surface, l.clone().subtract(0, fluid ? 0 : 1, 0).getBlock())) {
return false;
}
Vector3d boundingBox = BukkitPlatform.entityBoundingBox(BukkitEntitySpawner.bukkitType(attempt.entity()));
return boundingBox == null || isAreaClearForSpawn(l, boundingBox);
if (boundingBox == null) {
return true;
}
return fluid ? isFluidAreaClearForSpawn(l, boundingBox, surface) : isAreaClearForSpawn(l, boundingBox);
}
private static boolean isFluidAreaClearForSpawn(Location center, Vector3d boundingBox, EntitySpawns.Surface surface) {
World world = center.getWorld();
int startX = (int) Math.floor(center.getX() + 0.5 - boundingBox.x / 2);
int endX = (int) Math.floor(Math.nextDown(center.getX() + 0.5 + boundingBox.x / 2));
int startY = center.getBlockY();
int endY = (int) Math.floor(Math.nextDown(center.getY() + 0.5 + boundingBox.y));
int startZ = (int) Math.floor(center.getZ() + 0.5 - boundingBox.z / 2);
int endZ = (int) Math.floor(Math.nextDown(center.getZ() + 0.5 + boundingBox.z / 2));
for (int x = startX; x <= endX; x++) {
for (int y = startY; y <= endY; y++) {
for (int z = startZ; z <= endZ; z++) {
if (!BukkitSurfaces.matches(surface, world.getBlockAt(x, y, z))) {
return false;
}
}
}
}
return true;
}
private static boolean isAreaClearForSpawn(Location center, Vector3d boundingBox) {
@@ -25,7 +25,9 @@ import art.arcane.iris.engine.framework.Engine;
import art.arcane.iris.engine.framework.EngineGeneration;
import art.arcane.iris.engine.framework.SeedManager;
import art.arcane.iris.builtin.spawn.EntityDefinition;
import art.arcane.iris.builtin.spawn.SpawnGroup;
import art.arcane.iris.builtin.spawn.SpawnMarker;
import art.arcane.iris.builtin.spawn.SpawnSurface;
import art.arcane.iris.builtin.spawn.Spawner;
import art.arcane.iris.engine.object.TestSettings;
import art.arcane.iris.engine.object.world.IrisWorld;
@@ -143,6 +145,53 @@ public class EntitySpawningTest {
}
}
@Test
public void aMarkerSpawnOfAFluidEntityStillChecksItsSurface() {
Spawner spawner = TestSettings.of(Spawner.class, "{\"spawns\": [{\"entity\": \"cod\", \"minSpawns\": 1, \"maxSpawns\": 1}]}");
fixture.resource(SpawnResources.SPAWNERS, "reef/fish", spawner);
fixture.resource(SpawnResources.MARKERS, "reef", TestSettings.of(SpawnMarker.class, "{}"));
fixture.resource(SpawnResources.ENTITIES, "cod", TestSettings.of(EntityDefinition.class, "{\"type\": \"minecraft:cod\", \"surface\": \"WATER\"}"));
when(engine.getMinHeight()).thenReturn(-64);
EntitySpawns.Plan plan = new EntitySpawning()
.plan(world, new EntitySpawns.Marker(MARKER_BLOCK, world.resources().idOf("reef"), world.resources().idOf("reef/fish"), false))
.orElseThrow();
assertEquals(1, plan.attempts().size());
assertTrue(plan.attempts().get(0).checkSurface());
assertEquals(EntitySpawns.Surface.WATER, plan.attempts().get(0).surface());
}
@Test
public void landEntitiesStandOnTheTopWhileFluidEntitiesSpawnWithinTheFluid() {
assertEquals(Integer.valueOf(79), EntitySpawning.surfaceSpawnY(SpawnGroup.NORMAL, SpawnSurface.LAND, 77, 78));
assertEquals(Integer.valueOf(78), EntitySpawning.surfaceSpawnY(SpawnGroup.NORMAL, SpawnSurface.WATER, 77, 78));
assertEquals(Integer.valueOf(78), EntitySpawning.surfaceSpawnY(SpawnGroup.NORMAL, SpawnSurface.LAVA, 77, 78));
}
@Test
public void aColumnWithoutFluidHasNoPlaceForAFluidEntity() {
assertNull(EntitySpawning.surfaceSpawnY(SpawnGroup.NORMAL, SpawnSurface.WATER, 78, 78));
assertNull(EntitySpawning.surfaceSpawnY(SpawnGroup.UNDERWATER, SpawnSurface.WATER, 78, 78));
assertNull(EntitySpawning.surfaceSpawnY(SpawnGroup.BEACH, SpawnSurface.LAND, 78, 78));
}
@Test
public void underwaterAndBeachEntitiesSpawnBetweenTheFloorAndTheTop() {
for (SpawnGroup group : List.of(SpawnGroup.UNDERWATER, SpawnGroup.BEACH)) {
for (int i = 0; i < 50; i++) {
int y = EntitySpawning.surfaceSpawnY(group, SpawnSurface.WATER, 60, 64);
assertTrue(y >= 61 && y <= 64);
}
}
}
@Test
public void caveEntitiesNeverTakeAColumnHeight() {
assertNull(EntitySpawning.surfaceSpawnY(SpawnGroup.CAVE, SpawnSurface.LAND, 60, 64));
assertNull(EntitySpawning.surfaceSpawnY(SpawnGroup.CAVE, SpawnSurface.WATER, 60, 64));
}
@Test
public void aSpawnerThatCannotFireOrHasNoEntitiesPlansNothing() {
fixture.resource(SpawnResources.SPAWNERS, "empty", new Spawner());
@@ -36,5 +36,9 @@ public enum SpawnSurface {
OVERWORLD,
@Desc("Within lava")
LAVA
LAVA;
public boolean isFluid() {
return this == WATER || this == LAVA;
}
}
@@ -22,6 +22,7 @@ import art.arcane.iris.builtin.IrisSeedPurposes;
import art.arcane.iris.builtin.loot.LootResolver;
import art.arcane.iris.builtin.spawn.EntityDefinition;
import art.arcane.iris.builtin.spawn.EntitySpawn;
import art.arcane.iris.builtin.spawn.SpawnGroup;
import art.arcane.iris.builtin.spawn.SpawnMarker;
import art.arcane.iris.builtin.spawn.SpawnSurface;
import art.arcane.iris.builtin.spawn.Spawner;
@@ -155,28 +156,47 @@ public final class EntitySpawning implements EntitySpawns {
private Optional<Plan> planChunkSpawn(PackWorld world, RunningWorld running, Chunk chunk, Candidate chosen) {
EntitySpawn spawn = chosen.spawn();
EntityDefinition entity = entityOf(world, chosen);
if (entity == null) {
return Optional.empty();
}
int count = LootResolver.inclusive(RNG.r, spawn.minSpawns(), spawn.maxSpawns());
List<Attempt> attempts = new ArrayList<>();
for (int i = 0; i < count; i++) {
int x = (chunk.chunkX() * CHUNK_SIZE) + RNG.r.i(CHUNK_SIZE);
int z = (chunk.chunkZ() * CHUNK_SIZE) + RNG.r.i(CHUNK_SIZE);
BlockPosition at = spawnPosition(running, chosen.spawner(), chunk.chunkX(), chunk.chunkZ(), x, z);
BlockPosition at = spawnPosition(running, chosen.spawner(), entity.surface(), chunk.chunkX(), chunk.chunkZ(), x, z);
if (at != null) {
addAttempt(attempts, world, chosen, at, true);
addAttempt(attempts, world, chosen, entity, at, true);
}
}
return attempts.isEmpty() ? Optional.empty() : Optional.of(new Plan(world.resources().idOf(chosen.key()), chunk.chunkX(), chunk.chunkZ(), false, attempts));
}
/** Where a spawner of its group puts an entity at the column; null when it has no place for one. */
private static BlockPosition spawnPosition(RunningWorld running, Spawner spawner, int chunkX, int chunkZ, int x, int z) {
int height = running.highest(x, z, true);
int heightWithFluid = running.highest(x, z, false);
return switch (spawner.group()) {
case NORMAL -> new BlockPosition(x, heightWithFluid + 1, z);
case CAVE -> caveSpawnPosition(running, chunkX, chunkZ);
case UNDERWATER, BEACH -> new BlockPosition(x, RNG.r.i(height + 1, heightWithFluid), z);
};
/** Where a spawner of its group puts an entity of the surface at the column; null when it has no place for one. */
private static BlockPosition spawnPosition(RunningWorld running, Spawner spawner, SpawnSurface surface, int chunkX, int chunkZ, int x, int z) {
if (spawner.group() == SpawnGroup.CAVE) {
return caveSpawnPosition(running, chunkX, chunkZ);
}
int floor = running.highest(x, z, true);
int top = running.highest(x, z, false);
Integer y = surfaceSpawnY(spawner.group(), surface, floor, top);
return y == null ? null : new BlockPosition(x, y, z);
}
/**
* The height an entity of the surface spawns at in a column whose solid ground ends at {@code floor} and whose
* fluid or ground ends at {@code top}; null when the column has no room for it. Entities that need fluid are
* placed within it, whichever the group.
*/
static Integer surfaceSpawnY(SpawnGroup group, SpawnSurface surface, int floor, int top) {
if (group == SpawnGroup.CAVE) {
return null;
}
if (group == SpawnGroup.NORMAL && !surface.isFluid()) {
return top + 1;
}
return top > floor ? LootResolver.inclusive(RNG.r, floor + 1, top) : null;
}
static BlockPosition caveSpawnPosition(RunningWorld running, int chunkX, int chunkZ) {
@@ -199,6 +219,11 @@ public final class EntitySpawning implements EntitySpawns {
}
EntitySpawn spawn = RarityWeighting.expand(candidates, EntitySpawn::rarity).getRandom();
EntityDefinition entity = entityOf(world, new Candidate(spawnerKey, spawner, spawn));
if (entity == null) {
return Optional.empty();
}
BlockPosition position = marker.position();
int chunkX = PowerOfTwoCoordinates.blockToChunkFloor(position.x());
int chunkZ = PowerOfTwoCoordinates.blockToChunkFloor(position.z());
@@ -214,7 +239,7 @@ public final class EntitySpawning implements EntitySpawns {
List<Attempt> attempts = new ArrayList<>();
BlockPosition above = new BlockPosition(position.x(), position.y() + AIR_ABOVE_MARKER, position.z());
for (int i = 0; i < count; i++) {
addAttempt(attempts, world, new Candidate(spawnerKey, spawner, spawn), above, false);
addAttempt(attempts, world, new Candidate(spawnerKey, spawner, spawn), entity, above, false);
}
return attempts.isEmpty() ? Optional.empty() : Optional.of(new Plan(marker.spawner(), chunkX, chunkZ, exhaustsMarker, attempts));
}
@@ -223,16 +248,20 @@ public final class EntitySpawning implements EntitySpawns {
return marker.exhaustionChance() > RNG.r.nextDouble();
}
private void addAttempt(List<Attempt> attempts, PackWorld world, Candidate candidate, BlockPosition at, boolean checkSurface) {
private static EntityDefinition entityOf(PackWorld world, Candidate candidate) {
EntityDefinition entity = SpawnResources.entity(world.resources(), candidate.spawn().entity());
if (entity == null) {
world.log().debug(" You are trying to spawn an entity that does not exist!");
return;
}
return entity;
}
/** Entities that need fluid are always checked: the fluid is what keeps them where they can live. */
private void addAttempt(List<Attempt> attempts, PackWorld world, Candidate candidate, EntityDefinition entity, BlockPosition at, boolean checkSurface) {
Range light = candidate.spawner().allowedLightLevels();
boolean checked = checkSurface || entity.surface().isFluid();
resolution(world).resolve(entity, entityRandom(world).rng()).ifPresent(spec -> attempts.add(new Attempt(at, spec,
surfaceOf(entity.surface()), (int) Math.ceil(light.min()), (int) Math.floor(light.max()), checkSurface)));
surfaceOf(entity.surface()), (int) Math.ceil(light.min()), (int) Math.floor(light.max()), checked)));
}
private static EntityResolution resolution(PackWorld world) {