Fix despawn ranges by defaulting to an ellipsoid shape (#11312)

This commit is contained in:
Jake Potrebic 2024-08-25 11:26:33 -07:00 committed by GitHub
parent 5b2c43edaf
commit 5167856bb8
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
6 changed files with 107 additions and 43 deletions

View File

@ -1418,10 +1418,10 @@ index 0000000000000000000000000000000000000000..990d1bb46e0f9719f4e9af928d80ac6f
+}
diff --git a/src/main/java/io/papermc/paper/configuration/WorldConfiguration.java b/src/main/java/io/papermc/paper/configuration/WorldConfiguration.java
new file mode 100644
index 0000000000000000000000000000000000000000..95e96fe8aa93efbbb2e0a7dd98377fdc4fe0e6dd
index 0000000000000000000000000000000000000000..2f2850d1bc1e27dea3c38ac26908cf130da2e051
--- /dev/null
+++ b/src/main/java/io/papermc/paper/configuration/WorldConfiguration.java
@@ -0,0 +1,561 @@
@@ -0,0 +1,573 @@
+package io.papermc.paper.configuration;
+
+import com.google.common.collect.HashBasedTable;
@ -1483,6 +1483,7 @@ index 0000000000000000000000000000000000000000..95e96fe8aa93efbbb2e0a7dd98377fdc
+import org.spongepowered.configurate.objectmapping.meta.PostProcess;
+import org.spongepowered.configurate.objectmapping.meta.Required;
+import org.spongepowered.configurate.objectmapping.meta.Setting;
+import org.spongepowered.configurate.serialize.SerializationException;
+
+@SuppressWarnings({"FieldCanBeLocal", "FieldMayBeFinal", "NotNullFieldNotInitialized", "InnerClassMayBeStatic"})
+public class WorldConfiguration extends ConfigurationPart {
@ -1609,6 +1610,7 @@ index 0000000000000000000000000000000000000000..95e96fe8aa93efbbb2e0a7dd98377fdc
+ public Reference2IntMap<MobCategory> spawnLimits = Util.make(new Reference2IntOpenHashMap<>(NaturalSpawner.SPAWNING_CATEGORIES.length), map -> Arrays.stream(NaturalSpawner.SPAWNING_CATEGORIES).forEach(mobCategory -> map.put(mobCategory, -1)));
+ @MergeMap
+ public Map<MobCategory, DespawnRangePair> despawnRanges = Arrays.stream(MobCategory.values()).collect(Collectors.toMap(Function.identity(), category -> DespawnRangePair.createDefault()));
+ public DespawnRange.Shape despawnRangeShape = DespawnRange.Shape.ELLIPSOID;
+ @MergeMap
+ public Reference2IntMap<MobCategory> ticksPerSpawn = Util.make(new Reference2IntOpenHashMap<>(NaturalSpawner.SPAWNING_CATEGORIES.length), map -> Arrays.stream(NaturalSpawner.SPAWNING_CATEGORIES).forEach(mobCategory -> map.put(mobCategory, -1)));
+
@ -1622,6 +1624,16 @@ index 0000000000000000000000000000000000000000..95e96fe8aa93efbbb2e0a7dd98377fdc
+ }
+ }
+
+ @PostProcess
+ public void precomputeDespawnDistances() throws SerializationException {
+ for (Map.Entry<MobCategory, DespawnRangePair> entry : this.despawnRanges.entrySet()) {
+ final MobCategory category = entry.getKey();
+ final DespawnRangePair range = entry.getValue();
+ range.hard().preComputed(category.getDespawnDistance(), category.getSerializedName());
+ range.soft().preComputed(category.getNoDespawnDistance(), category.getSerializedName());
+ }
+ }
+
+ public WaterAnimalSpawnHeight wateranimalSpawnHeight;
+
+ public class WaterAnimalSpawnHeight extends ConfigurationPart {
@ -4203,31 +4215,82 @@ index 0000000000000000000000000000000000000000..a3eaa47cfcfc4fd2a607f9b375230fad
+}
diff --git a/src/main/java/io/papermc/paper/configuration/type/DespawnRange.java b/src/main/java/io/papermc/paper/configuration/type/DespawnRange.java
new file mode 100644
index 0000000000000000000000000000000000000000..8f55258ecc8bfd2ab4e08f31562f7b3bde5c766b
index 0000000000000000000000000000000000000000..8a792a000e924b3ddc572edc788598811e9ef71c
--- /dev/null
+++ b/src/main/java/io/papermc/paper/configuration/type/DespawnRange.java
@@ -0,0 +1,58 @@
@@ -0,0 +1,109 @@
+package io.papermc.paper.configuration.type;
+
+import io.papermc.paper.configuration.type.number.IntOr;
+import java.lang.reflect.Type;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.spongepowered.configurate.ConfigurationNode;
+import org.spongepowered.configurate.objectmapping.meta.Required;
+import org.spongepowered.configurate.serialize.SerializationException;
+import org.spongepowered.configurate.serialize.TypeSerializer;
+
+public record DespawnRange(
+ @Required IntOr.Default horizontalLimit,
+ @Required IntOr.Default verticalLimit,
+ @Required boolean wasDefinedViaLongSyntax
+) {
+/*
+(x/a)^2 + (y/b)^2 + (z/c)^2 < 1
+a == c
+ac = horizontal limit
+b = vertical limit
+x^2/ac^2 + y^2/b^2 + z^2/ac^2 < 1
+(x^2 + z^2)/ac^2 + y^2/b^2 < 1
+x^2 + z^2 + (y^2 * (ac^2/b^2)) < ac^2
+ */
+public final class DespawnRange {
+
+ public static final TypeSerializer<DespawnRange> SERIALIZER = new Serializer();
+
+ private final IntOr.Default horizontalLimit;
+ private final IntOr.Default verticalLimit;
+ private final boolean wasDefinedViaLongSyntax;
+
+ // cached values
+ private double preComputedHorizontalLimitSquared; // ac^2
+ private double preComputedHorizontalLimitSquaredOverVerticalLimitSquared; // ac^2/b^2
+ private int preComputedVanillaDefaultLimit;
+
+ public DespawnRange(final IntOr.Default generalLimit) {
+ this(generalLimit, generalLimit, false);
+ }
+
+ public static final TypeSerializer<DespawnRange> SERIALIZER = new Serializer();
+ public DespawnRange(final IntOr.Default horizontalLimit, final IntOr.Default verticalLimit, final boolean wasDefinedViaLongSyntax) {
+ this.horizontalLimit = horizontalLimit;
+ this.verticalLimit = verticalLimit;
+ this.wasDefinedViaLongSyntax = wasDefinedViaLongSyntax;
+ }
+
+ public void preComputed(int defaultDistanceLimit, String identifier) throws SerializationException {
+ if (this.verticalLimit.or(defaultDistanceLimit) <= 0) {
+ throw new SerializationException("Vertical limit must be greater than 0 for " + identifier);
+ }
+ if (this.horizontalLimit.or(defaultDistanceLimit) <= 0) {
+ throw new SerializationException("Horizontal limit must be greater than 0 for " + identifier);
+ }
+ this.preComputedVanillaDefaultLimit = defaultDistanceLimit;
+ this.preComputedHorizontalLimitSquared = Math.pow(this.horizontalLimit.or(defaultDistanceLimit), 2);
+ if (!this.horizontalLimit.isDefined() && !this.verticalLimit.isDefined()) {
+ this.preComputedHorizontalLimitSquaredOverVerticalLimitSquared = 1.0;
+ } else {
+ this.preComputedHorizontalLimitSquaredOverVerticalLimitSquared = this.preComputedHorizontalLimitSquared / Math.pow(this.verticalLimit.or(defaultDistanceLimit), 2);
+ }
+ }
+
+ public boolean shouldDespawn(final Shape shape, final double dxSqr, final double dySqr, final double dzSqr, final double dy) {
+ if (shape == Shape.ELLIPSOID) {
+ return dxSqr + dzSqr + (dySqr * this.preComputedHorizontalLimitSquaredOverVerticalLimitSquared) > this.preComputedHorizontalLimitSquared;
+ } else {
+ return dxSqr + dzSqr > this.preComputedHorizontalLimitSquared || dy > this.verticalLimit.or(this.preComputedVanillaDefaultLimit);
+ }
+ }
+
+ public boolean wasDefinedViaLongSyntax() {
+ return this.wasDefinedViaLongSyntax;
+ }
+
+ public enum Shape {
+ CYLINDER, ELLIPSOID
+ }
+
+ static final class Serializer implements TypeSerializer<DespawnRange> {
+
@ -4257,10 +4320,10 @@ index 0000000000000000000000000000000000000000..8f55258ecc8bfd2ab4e08f31562f7b3b
+ }
+
+ if (despawnRange.wasDefinedViaLongSyntax()) {
+ node.node(HORIZONTAL).set(despawnRange.horizontalLimit());
+ node.node(VERTICAL).set(despawnRange.verticalLimit());
+ node.node(HORIZONTAL).set(despawnRange.horizontalLimit);
+ node.node(VERTICAL).set(despawnRange.verticalLimit);
+ } else {
+ node.set(despawnRange.verticalLimit());
+ node.set(despawnRange.verticalLimit);
+ }
+ }
+ }
@ -4829,10 +4892,10 @@ index 0000000000000000000000000000000000000000..5833c06b0707906ab7d10786ecd115f2
+
diff --git a/src/main/java/io/papermc/paper/configuration/type/number/IntOr.java b/src/main/java/io/papermc/paper/configuration/type/number/IntOr.java
new file mode 100644
index 0000000000000000000000000000000000000000..09f16e5dde565801b153bd6705637c5f71427c8a
index 0000000000000000000000000000000000000000..73a7b664923121daedac8f01a26253438da68119
--- /dev/null
+++ b/src/main/java/io/papermc/paper/configuration/type/number/IntOr.java
@@ -0,0 +1,81 @@
@@ -0,0 +1,85 @@
+package io.papermc.paper.configuration.type.number;
+
+import com.google.common.base.Preconditions;
@ -4854,6 +4917,10 @@ index 0000000000000000000000000000000000000000..09f16e5dde565801b153bd6705637c5f
+
+ OptionalInt value();
+
+ default boolean isDefined() {
+ return this.value().isPresent();
+ }
+
+ default int intValue() {
+ return this.value().orElseThrow();
+ }

View File

@ -5,10 +5,10 @@ Subject: [PATCH] Add configurable entity despawn distances
diff --git a/src/main/java/net/minecraft/world/entity/Mob.java b/src/main/java/net/minecraft/world/entity/Mob.java
index 0593d828c911c94c9833bf12b9c294e5dac1f4e8..e672b04e9e123caecec30645df8858d6eda4c931 100644
index 0593d828c911c94c9833bf12b9c294e5dac1f4e8..02c8c52930640114592148fd5509ad8fffee5435 100644
--- a/src/main/java/net/minecraft/world/entity/Mob.java
+++ b/src/main/java/net/minecraft/world/entity/Mob.java
@@ -863,20 +863,27 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
@@ -863,20 +863,24 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
Player entityhuman = this.level().getNearestPlayer(this, -1.0D);
if (entityhuman != null) {
@ -18,18 +18,15 @@ index 0593d828c911c94c9833bf12b9c294e5dac1f4e8..e672b04e9e123caecec30645df8858d6
-
- if (d0 > (double) j && this.removeWhenFarAway(d0)) {
+ // Paper start - Configurable despawn distances
+ // Read configration data and square it for later comparison
+ final MobCategory category = this.getType().getCategory();
+ final io.papermc.paper.configuration.WorldConfiguration.Entities.Spawning.DespawnRangePair despawnRangePair = this.level().paperConfig().entities.spawning.despawnRanges.get(this.getType().getCategory());
+ final int hardHorizontalLimitSquared = (int) Math.pow(despawnRangePair.hard().horizontalLimit().or(category.getDespawnDistance()), 2);
+ final int softHorizontalLimitSquared = (int) Math.pow(despawnRangePair.soft().horizontalLimit().or(category.getNoDespawnDistance()), 2);
+ final int hardVerticalLimit = despawnRangePair.hard().verticalLimit().or(category.getDespawnDistance());
+ final int softVerticalLimit = despawnRangePair.soft().verticalLimit().or(category.getNoDespawnDistance());
+ // Compute vertical/horizontal distances
+ final double horizontalDistanceSquared = entityhuman.distanceToSqr(this.getX(), entityhuman.getY(), this.getZ());
+ final double verticalDistance = Math.abs(entityhuman.getY() - this.getY());
+ final io.papermc.paper.configuration.type.DespawnRange.Shape shape = this.level().paperConfig().entities.spawning.despawnRangeShape;
+ final double dy = Math.abs(entityhuman.getY() - this.getY());
+ final double dySqr = Math.pow(dy, 2);
+ final double dxSqr = Math.pow(entityhuman.getX() - this.getX(), 2);
+ final double dzSqr = Math.pow(entityhuman.getZ() - this.getZ(), 2);
+ final double distanceSquared = dxSqr + dzSqr + dySqr;
+ // Despawn if hard/soft limit is exceeded
+ if ((horizontalDistanceSquared > hardHorizontalLimitSquared || verticalDistance > hardVerticalLimit) && this.removeWhenFarAway(horizontalDistanceSquared)) {
+ if (despawnRangePair.hard().shouldDespawn(shape, dxSqr, dySqr, dzSqr, dy) && this.removeWhenFarAway(distanceSquared)) {
this.discard(EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause
}
-
@ -39,8 +36,8 @@ index 0593d828c911c94c9833bf12b9c294e5dac1f4e8..e672b04e9e123caecec30645df8858d6
- if (this.noActionTime > 600 && this.random.nextInt(800) == 0 && d0 > (double) l && this.removeWhenFarAway(d0)) {
- this.discard(EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause
- } else if (d0 < (double) l) {
+ if (horizontalDistanceSquared > softHorizontalLimitSquared || verticalDistance > softVerticalLimit) {
+ if (this.noActionTime > 600 && this.random.nextInt(800) == 0 && this.removeWhenFarAway(horizontalDistanceSquared)) {
+ if (despawnRangePair.soft().shouldDespawn(shape, dxSqr, dySqr, dzSqr, dy)) {
+ if (this.noActionTime > 600 && this.random.nextInt(800) == 0 && this.removeWhenFarAway(distanceSquared)) {
+ this.discard(EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause
+ }
+ } else {

View File

@ -93,10 +93,10 @@ index cccc60602360f25f0aeddbd16dad2bb63a1728a8..ada79af49d1cafe25ca6c1fb456e1c4c
}
}
diff --git a/src/main/java/net/minecraft/world/entity/Mob.java b/src/main/java/net/minecraft/world/entity/Mob.java
index b076a6f4710c125767cea374c384228354854e02..4dbd8a3425058692600e6a1493cd5531df20fa72 100644
index 62c3b3d992aff2aa20857e2b3bee464a377a0857..25a71cc5ca8cf8a5070cd24eb56fe0d79e765669 100644
--- a/src/main/java/net/minecraft/world/entity/Mob.java
+++ b/src/main/java/net/minecraft/world/entity/Mob.java
@@ -1698,7 +1698,7 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
@@ -1695,7 +1695,7 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
if (f1 > 0.0F && target instanceof LivingEntity) {
LivingEntity entityliving = (LivingEntity) target;
@ -273,7 +273,7 @@ index 6476c644d3da824c5ee4190cb45cde678ff1188f..b216140a8be65e210250358af8daf493
// CraftBukkit end
entity.setDeltaMovement(entity.getDeltaMovement().add(vec3d1));
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
index cfdabb93c2d30845af9108552ed9bee9929250ce..9225746382bcecb0bab655a8232fecc09169225d 100644
index e3af0db8082e4e90902197f96f1c833405bf5f63..3059a21b554db99af96c76f72dd08591c00e3e08 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -1938,19 +1938,33 @@ public class CraftEventFactory {

View File

@ -229,10 +229,10 @@ index 39dff0a38b53624c935f27cc86ff036c831f407f..bdee5725029eda3a0e7bee407286480c
public int getExpReward(@Nullable Entity entity) { // CraftBukkit
Level world = this.level();
diff --git a/src/main/java/net/minecraft/world/entity/Mob.java b/src/main/java/net/minecraft/world/entity/Mob.java
index 4dbd8a3425058692600e6a1493cd5531df20fa72..2c00746ac5394ea3c292d214ee595cdf42d75cfd 100644
index 25a71cc5ca8cf8a5070cd24eb56fe0d79e765669..b46572f6e3b52f498b395d3b8c5def2aa799ff03 100644
--- a/src/main/java/net/minecraft/world/entity/Mob.java
+++ b/src/main/java/net/minecraft/world/entity/Mob.java
@@ -1126,6 +1126,12 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
@@ -1123,6 +1123,12 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
}
@ -245,7 +245,7 @@ index 4dbd8a3425058692600e6a1493cd5531df20fa72..2c00746ac5394ea3c292d214ee595cdf
@Override
protected void dropCustomDeathLoot(ServerLevel world, DamageSource source, boolean causedByPlayer) {
super.dropCustomDeathLoot(world, source, causedByPlayer);
@@ -1134,6 +1140,7 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
@@ -1131,6 +1137,7 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
for (int j = 0; j < i; ++j) {
EquipmentSlot enumitemslot = aenumitemslot[j];
@ -253,7 +253,7 @@ index 4dbd8a3425058692600e6a1493cd5531df20fa72..2c00746ac5394ea3c292d214ee595cdf
ItemStack itemstack = this.getItemBySlot(enumitemslot);
float f = this.getEquipmentDropChance(enumitemslot);
@@ -1158,7 +1165,13 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
@@ -1155,7 +1162,13 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
}
this.spawnAtLocation(itemstack);
@ -443,7 +443,7 @@ index a7e611aaeb457820ad303b95822d8ea86b060477..004ac565d4124f6059d530034cf0c9a2
public void injectScaledMaxHealth(Collection<AttributeInstance> collection, boolean force) {
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
index d63cf5933fdcff6d71c7ddbfed3640acd1646b70..1f4c686e92e7449556ee5067553ed4473ba2f50e 100644
index 0613bdf3c2325d5cab64783af7211b07fcf5124a..6a018f9c289a539b07855d75e4cc2d3c2828ded1 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -903,9 +903,16 @@ public class CraftEventFactory {

View File

@ -78,10 +78,10 @@ index 30d7dd9646ba9d6a9396dc140a61eb2cac07dfc6..bc03f1fe2a311bf61449879751360c67
default void closeRangeLeashBehaviour(Entity entity) {}
diff --git a/src/main/java/net/minecraft/world/entity/Mob.java b/src/main/java/net/minecraft/world/entity/Mob.java
index 2c00746ac5394ea3c292d214ee595cdf42d75cfd..0048433e06bb1ec5176dc5b81afc9bfead3591f0 100644
index b46572f6e3b52f498b395d3b8c5def2aa799ff03..e87360e21e6eb7b0161c34a3ac6cb83d18bcd1e8 100644
--- a/src/main/java/net/minecraft/world/entity/Mob.java
+++ b/src/main/java/net/minecraft/world/entity/Mob.java
@@ -1624,8 +1624,11 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
@@ -1621,8 +1621,11 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
boolean flag1 = super.startRiding(entity, force);
if (flag1 && this.isLeashed()) {
@ -121,7 +121,7 @@ index 3c0af74ed65610b1d5e3b72fdcf28c5a3423f0da..01173fc7177d78588978e087e63efda0
flag1 = true;
}
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
index 6e5bfdc0a5b39977c4550876470c9a2534160056..e3cf4febcb51db96fcd162b2af77dd92e7d49365 100644
index 6cc729446ff21a5718ff01fcea1496c71f77480c..ad49a7cb16de3dac7a5232abded3635a4b0b8848 100644
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
@@ -1598,8 +1598,10 @@ public class CraftEventFactory {

View File

@ -5,10 +5,10 @@ Subject: [PATCH] Fix silent equipment change for mobs
diff --git a/src/main/java/net/minecraft/world/entity/Mob.java b/src/main/java/net/minecraft/world/entity/Mob.java
index 0048433e06bb1ec5176dc5b81afc9bfead3591f0..3f9c2a2478b84ee59f43ff9e48e2644db8a14dd7 100644
index e87360e21e6eb7b0161c34a3ac6cb83d18bcd1e8..cf1f6a0081f0dbcf43842ec23accf6c3ae9b79d8 100644
--- a/src/main/java/net/minecraft/world/entity/Mob.java
+++ b/src/main/java/net/minecraft/world/entity/Mob.java
@@ -1109,19 +1109,26 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
@@ -1106,19 +1106,26 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
@Override
public void setItemSlot(EquipmentSlot slot, ItemStack stack) {