[Change] Height check for Mob Spawns does no longer use the heightmap (quiqueck/BetterEnd#245)

This commit is contained in:
Frank 2023-06-24 12:46:37 +02:00
parent 522e472e03
commit fb194ee209
2 changed files with 49 additions and 19 deletions

View file

@ -2,6 +2,7 @@ package org.betterx.bclib.api.v2.spawning;
import org.betterx.bclib.entity.BCLEntityWrapper; import org.betterx.bclib.entity.BCLEntityWrapper;
import org.betterx.bclib.interfaces.SpawnRule; import org.betterx.bclib.interfaces.SpawnRule;
import org.betterx.bclib.util.BlocksHelper;
import net.minecraft.core.BlockPos; import net.minecraft.core.BlockPos;
import net.minecraft.world.Difficulty; import net.minecraft.world.Difficulty;
@ -64,7 +65,7 @@ public class SpawnRuleBuilder<M extends Mob> {
*/ */
public SpawnRuleBuilder preventSpawn() { public SpawnRuleBuilder preventSpawn() {
entryInstance = getFromCache("prevent", () -> { entryInstance = getFromCache("prevent", () -> {
return new SpawnRuleEntry(-1, (type, world, spawnReason, pos, random) -> false); return new SpawnRuleEntry(-1, (type, world, spawnReason, pos, random) -> false, "Prevent Spawn");
}); });
rules.add(entryInstance); rules.add(entryInstance);
return this; return this;
@ -79,7 +80,8 @@ public class SpawnRuleBuilder<M extends Mob> {
entryInstance = getFromCache("not_peaceful", () -> { entryInstance = getFromCache("not_peaceful", () -> {
return new SpawnRuleEntry( return new SpawnRuleEntry(
0, 0,
(type, world, spawnReason, pos, random) -> world.getDifficulty() != Difficulty.PEACEFUL (type, world, spawnReason, pos, random) -> world.getDifficulty() != Difficulty.PEACEFUL,
"Not Peaceful"
); );
}); });
rules.add(entryInstance); rules.add(entryInstance);
@ -98,8 +100,17 @@ public class SpawnRuleBuilder<M extends Mob> {
if (pos.getY() < world.getMinBuildHeight() + 2) { if (pos.getY() < world.getMinBuildHeight() + 2) {
return false; return false;
} }
return pos.getY() > world.getHeight(Types.WORLD_SURFACE, pos.getX(), pos.getZ()) + minHeight; return BlocksHelper.findSurfaceBelow(
}); world,
pos,
pos.getY() - minHeight,
(bs) -> !BlocksHelper.isFree(bs)
)
.isEmpty();
//return pos.getY() > world.getHeight(Types.WORLD_SURFACE, pos.getX(), pos.getZ()) + minHeight;
},
"Above Ground"
);
}); });
rules.add(entryInstance); rules.add(entryInstance);
return this; return this;
@ -115,7 +126,8 @@ public class SpawnRuleBuilder<M extends Mob> {
return new SpawnRuleEntry( return new SpawnRuleEntry(
0, 0,
(type, world, spawnReason, pos, random) -> pos.getY() < world.dimensionType() (type, world, spawnReason, pos, random) -> pos.getY() < world.dimensionType()
.logicalHeight() .logicalHeight(),
"Below Max Height"
); );
}); });
rules.add(entryInstance); rules.add(entryInstance);
@ -132,7 +144,7 @@ public class SpawnRuleBuilder<M extends Mob> {
return new SpawnRuleEntry(0, (type, world, spawnReason, pos, random) -> { return new SpawnRuleEntry(0, (type, world, spawnReason, pos, random) -> {
BlockPos below = pos.below(); BlockPos below = pos.below();
return world.getBlockState(below).isValidSpawn(world, below, type); return world.getBlockState(below).isValidSpawn(world, below, type);
}); }, "Only On Valid Blocks");
}); });
rules.add(entryInstance); rules.add(entryInstance);
return this; return this;
@ -162,7 +174,7 @@ public class SpawnRuleBuilder<M extends Mob> {
} }
} }
return false; return false;
}); }, "Only On Blocks");
}); });
rules.add(entryInstance); rules.add(entryInstance);
@ -177,7 +189,11 @@ public class SpawnRuleBuilder<M extends Mob> {
*/ */
public SpawnRuleBuilder withChance(int chance) { public SpawnRuleBuilder withChance(int chance) {
entryInstance = getFromCache("with_chance_" + chance, () -> { entryInstance = getFromCache("with_chance_" + chance, () -> {
return new SpawnRuleEntry(1, (type, world, spawnReason, pos, random) -> random.nextInt(chance) == 0); return new SpawnRuleEntry(
1,
(type, world, spawnReason, pos, random) -> random.nextInt(chance) == 0,
"With Chance"
);
}); });
rules.add(entryInstance); rules.add(entryInstance);
return this; return this;
@ -193,7 +209,8 @@ public class SpawnRuleBuilder<M extends Mob> {
entryInstance = getFromCache("below_brightness_" + lightLevel, () -> { entryInstance = getFromCache("below_brightness_" + lightLevel, () -> {
return new SpawnRuleEntry( return new SpawnRuleEntry(
2, 2,
(type, world, spawnReason, pos, random) -> world.getMaxLocalRawBrightness(pos) <= lightLevel (type, world, spawnReason, pos, random) -> world.getMaxLocalRawBrightness(pos) <= lightLevel,
"Below Brightness"
); );
}); });
rules.add(entryInstance); rules.add(entryInstance);
@ -210,7 +227,8 @@ public class SpawnRuleBuilder<M extends Mob> {
entryInstance = getFromCache("above_brightness_" + lightLevel, () -> { entryInstance = getFromCache("above_brightness_" + lightLevel, () -> {
return new SpawnRuleEntry( return new SpawnRuleEntry(
2, 2,
(type, world, spawnReason, pos, random) -> world.getMaxLocalRawBrightness(pos) >= lightLevel (type, world, spawnReason, pos, random) -> world.getMaxLocalRawBrightness(pos) >= lightLevel,
"Above Brightness"
); );
}); });
rules.add(entryInstance); rules.add(entryInstance);
@ -255,7 +273,7 @@ public class SpawnRuleBuilder<M extends Mob> {
} catch (Exception e) { } catch (Exception e) {
return true; return true;
} }
}); }, "Max Nearby " + count + "/" + side);
}); });
rules.add(entryInstance); rules.add(entryInstance);
return this; return this;
@ -289,7 +307,7 @@ public class SpawnRuleBuilder<M extends Mob> {
* @return same {@link SpawnRuleBuilder} instance. * @return same {@link SpawnRuleBuilder} instance.
*/ */
public SpawnRuleBuilder customRule(SpawnRule rule) { public SpawnRuleBuilder customRule(SpawnRule rule) {
rules.add(new SpawnRuleEntry(7, rule)); rules.add(new SpawnRuleEntry(7, rule, "Custom Rule"));
return this; return this;
} }
@ -306,9 +324,18 @@ public class SpawnRuleBuilder<M extends Mob> {
SpawnPredicate<M> predicate = (entityType, serverLevelAccessor, mobSpawnType, blockPos, random) -> { SpawnPredicate<M> predicate = (entityType, serverLevelAccessor, mobSpawnType, blockPos, random) -> {
for (SpawnRuleEntry rule : rulesCopy) { for (SpawnRuleEntry rule : rulesCopy) {
if (!rule.canSpawn(entityType, serverLevelAccessor, mobSpawnType, blockPos, random)) { if (!rule.canSpawn(entityType, serverLevelAccessor, mobSpawnType, blockPos, random)) {
// BCLib.LOGGER.info("Rejected Spawn of "
// + entityType.getDescriptionId()
// + " at " + blockPos.getX() + " " + blockPos.getY() + " " + blockPos.getZ()
// + " because " + rule.debugName
// + " at " + serverLevelAccessor.getBlockState(blockPos)
// + " above " + serverLevelAccessor.getBlockState(blockPos.below())
// );
return false; return false;
} }
} }
// BCLib.LOGGER.info("Spawning " + entityType.getDescriptionId() + " at " + blockPos.getX() + " " + blockPos.getY() + " " + blockPos.getZ() + "");
return true; return true;
}; };
@ -359,11 +386,12 @@ public class SpawnRuleBuilder<M extends Mob> {
* @return new or existing {@link SpawnRuleEntry}. * @return new or existing {@link SpawnRuleEntry}.
*/ */
private static SpawnRuleEntry getFromCache(String name, Supplier<SpawnRuleEntry> supplier) { private static SpawnRuleEntry getFromCache(String name, Supplier<SpawnRuleEntry> supplier) {
SpawnRuleEntry entry = RULES_CACHE.get(name); return supplier.get();
if (entry == null) { // SpawnRuleEntry entry = RULES_CACHE.get(name);
entry = supplier.get(); // if (entry == null) {
RULES_CACHE.put(name, entry); // entry = supplier.get();
} // RULES_CACHE.put(name, entry);
return entry; // }
// return entry;
} }
} }

View file

@ -14,10 +14,12 @@ import org.jetbrains.annotations.NotNull;
public class SpawnRuleEntry<M extends Mob> implements Comparable<SpawnRuleEntry> { public class SpawnRuleEntry<M extends Mob> implements Comparable<SpawnRuleEntry> {
private final SpawnRule rule; private final SpawnRule rule;
private final byte priority; private final byte priority;
//public final String debugName;
public SpawnRuleEntry(int priority, SpawnRule rule) { public SpawnRuleEntry(int priority, SpawnRule rule, String debugName) {
this.priority = (byte) priority; this.priority = (byte) priority;
this.rule = rule; this.rule = rule;
//this.debugName = debugName;
} }
protected boolean canSpawn( protected boolean canSpawn(