Skip to content

Commit 73660cc

Browse files
authored
Skip saving entities if they should despawn (#566)
* Skip saving entities if they should despawn Call syncEntityAge() even if despawn time is disabled, as this may be enabled in the future by users. * [ci/skip] format * always load totalEntityAge from existing data
1 parent 42f9551 commit 73660cc

File tree

1 file changed

+33
-13
lines changed

1 file changed

+33
-13
lines changed

leaf-server/minecraft-patches/features/0304-Rewrite-entity-despawn-time.patch

Lines changed: 33 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ index f58b91b277ba85fa7c0e7ad10157ecbf10023065..b7f9f22abf60c75bc09126d9168fda9a
4040
for (Entity entity : passengerEntity.getPassengers()) {
4141
this.tickPassenger(passengerEntity, entity, isActive); // Paper - EAR 2
4242
diff --git a/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java
43-
index f6d619709d4e5b0e6d1b943226579d7388835cdb..137ec39c1458e6adc854df24807f0e26770d95da 100644
43+
index f6d619709d4e5b0e6d1b943226579d7388835cdb..0c35f5f5a9d9afad457e2ab723f90feeefe5413d 100644
4444
--- a/net/minecraft/world/entity/Entity.java
4545
+++ b/net/minecraft/world/entity/Entity.java
4646
@@ -373,6 +373,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
@@ -67,7 +67,7 @@ index f6d619709d4e5b0e6d1b943226579d7388835cdb..137ec39c1458e6adc854df24807f0e26
6767
}
6868

6969
public boolean isColliding(BlockPos pos, BlockState state) {
70-
@@ -887,15 +890,46 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
70+
@@ -887,15 +890,50 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
7171
}
7272

7373
public void tick() {
@@ -89,11 +89,8 @@ index f6d619709d4e5b0e6d1b943226579d7388835cdb..137ec39c1458e6adc854df24807f0e26
8989

9090
+ // Leaf start - Rewrite entity despawn time
9191
+ protected final boolean detectDespawnTime() {
92+
+ this.syncEntityAge();
9293
+ if (this.despawnTime >= 0) {
93-
+ int missedTicks = this.calculateMissedTicks();
94-
+ if (missedTicks > 1) {
95-
+ this.totalEntityAge += missedTicks;
96-
+ }
9794
+ if (this.totalEntityAge >= this.despawnTime) {
9895
+ this.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN);
9996
+ return true;
@@ -106,6 +103,13 @@ index f6d619709d4e5b0e6d1b943226579d7388835cdb..137ec39c1458e6adc854df24807f0e26
106103
+ return net.minecraft.server.MinecraftServer.currentTick - this.lastTickTime;
107104
+ }
108105
+
106+
+ private void syncEntityAge() {
107+
+ int missedTicks = this.calculateMissedTicks();
108+
+ if (missedTicks > 1) {
109+
+ this.totalEntityAge += missedTicks;
110+
+ }
111+
+ }
112+
+
109113
+ public void updateLastTick() {
110114
+ this.lastTickTime = net.minecraft.server.MinecraftServer.currentTick;
111115
+ }
@@ -114,19 +118,35 @@ index f6d619709d4e5b0e6d1b943226579d7388835cdb..137ec39c1458e6adc854df24807f0e26
114118
// CraftBukkit start
115119
public void postTick() {
116120
// No clean way to break out of ticking once the entity has been copied to a new world, so instead we move the portalling later in the tick cycle
117-
@@ -2564,6 +2598,12 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
121+
@@ -2564,6 +2602,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
118122
if (this.maxAirTicks != this.getDefaultMaxAirSupply()) {
119123
output.putInt("Bukkit.MaxAirSupply", this.getMaxAirSupply());
120124
}
121-
+ // Leaf start - Rewrite entity despawn time
122-
+ int missedTicks = this.calculateMissedTicks();
123-
+ if (missedTicks > 1) {
124-
+ this.totalEntityAge += missedTicks;
125-
+ }
126-
+ // Leaf end - Rewrite entity despawn time
125+
+ this.syncEntityAge(); // Leaf - Rewrite entity despawn time
127126
output.putInt("Spigot.ticksLived", this.totalEntityAge); // Paper
128127
// CraftBukkit end
129128
output.storeNullable("CustomName", ComponentSerialization.CODEC, this.getCustomName());
129+
@@ -2721,7 +2760,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
130+
131+
// CraftBukkit start
132+
// Spigot start
133+
- if (this instanceof net.minecraft.world.entity.LivingEntity) {
134+
+ if (true || this instanceof net.minecraft.world.entity.LivingEntity) { // Leaf - Rewrite entity despawn time
135+
this.totalEntityAge = input.getIntOr("Spigot.ticksLived", 0); // Paper
136+
}
137+
// Spigot end
138+
@@ -5327,9 +5366,10 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
139+
140+
@Override
141+
public boolean shouldBeSaved() {
142+
+ this.syncEntityAge(); // Leaf - Rewrite entity despawn time
143+
return (this.removalReason == null || this.removalReason.shouldSave())
144+
&& !this.isPassenger()
145+
- && (!this.isVehicle() || !((ca.spottedleaf.moonrise.patches.chunk_system.entity.ChunkSystemEntity)this).moonrise$hasAnyPlayerPassengers()); // Paper - rewrite chunk system
146+
+ && (!this.isVehicle() || !((ca.spottedleaf.moonrise.patches.chunk_system.entity.ChunkSystemEntity)this).moonrise$hasAnyPlayerPassengers()) && (this.despawnTime < 0 || this.totalEntityAge < this.despawnTime); // Paper - rewrite chunk system // Leaf - Rewrite entity despawn time
147+
}
148+
149+
@Override
130150
diff --git a/net/minecraft/world/entity/projectile/ThrowableProjectile.java b/net/minecraft/world/entity/projectile/ThrowableProjectile.java
131151
index 4c61f4552dc64b1347c7b8dfe9502aac11c75888..be0ebc017bfe92fb2916a9e40971ad19614a33b4 100644
132152
--- a/net/minecraft/world/entity/projectile/ThrowableProjectile.java

0 commit comments

Comments
 (0)