Skip to content

Commit 89d7033

Browse files
authored
Rewrite despawn time (#564)
* Rewrite entity despawn time Brings ability to despawn weak-loaded entities once they are ticked * Catchup on save * handle edge cases * check earlier for projectiles * [ci/skip] Add patch comment
1 parent 6129df4 commit 89d7033

File tree

1 file changed

+136
-0
lines changed

1 file changed

+136
-0
lines changed
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2+
From: HaHaWTH <[email protected]>
3+
Date: Tue, 9 Nov 2077 00:00:00 +0800
4+
Subject: [PATCH] Rewrite entity despawn time
5+
6+
Brings the ability to despawn weak-loaded entities once they are ticked,
7+
a solution for https://github.com/PaperMC/Paper/issues/12986
8+
9+
diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java
10+
index f58b91b277ba85fa7c0e7ad10157ecbf10023065..b7f9f22abf60c75bc09126d9168fda9a0ee2375f 100644
11+
--- a/net/minecraft/server/level/ServerLevel.java
12+
+++ b/net/minecraft/server/level/ServerLevel.java
13+
@@ -1515,6 +1515,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe
14+
entity.tick();
15+
entity.postTick(); // CraftBukkit
16+
} else {entity.inactiveTick();} // Paper - EAR 2
17+
+ entity.updateLastTick(); // Leaf - Rewrite entity despawn time
18+
19+
for (Entity entity1 : entity.getPassengers()) {
20+
this.tickPassenger(entity, entity1, isActive); // Paper - EAR 2
21+
@@ -1539,6 +1540,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe
22+
ridingEntity.positionRider(passengerEntity);
23+
}
24+
// Paper end - EAR 2
25+
+ passengerEntity.updateLastTick(); // Leaf - Rewrite entity despawn time
26+
27+
for (Entity entity : passengerEntity.getPassengers()) {
28+
this.tickPassenger(passengerEntity, entity, isActive); // Paper - EAR 2
29+
diff --git a/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java
30+
index f6d619709d4e5b0e6d1b943226579d7388835cdb..fefd0df22b32bb6f933bbf9530584c2c9616bfb6 100644
31+
--- a/net/minecraft/world/entity/Entity.java
32+
+++ b/net/minecraft/world/entity/Entity.java
33+
@@ -373,6 +373,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
34+
public boolean fixedPose = false; // Paper - Expand Pose API
35+
private final int despawnTime; // Paper - entity despawn time limit
36+
public int totalEntityAge; // Paper - age-like counter for all entities
37+
+ private int lastTickTicks; // Leaf - Rewrite entity despawn time
38+
public boolean activatedPriorityReset = false; // Pufferfish - DAB
39+
public int activatedPriority = org.dreeam.leaf.config.modules.opt.DynamicActivationofBrain.maximumActivationPrio; // Pufferfish - DAB (golf score)
40+
public final io.papermc.paper.entity.activation.ActivationType activationType = io.papermc.paper.entity.activation.ActivationType.activationTypeFor(this); // Paper - EAR 2/tracking ranges
41+
@@ -402,6 +403,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
42+
private int sectionZ = Integer.MIN_VALUE;
43+
private boolean updatingSectionStatus;
44+
private static final boolean enableFMA = Boolean.getBoolean("Leaf.enableFMA"); // Leaf - Optimize Entity distanceTo
45+
+ private final boolean shouldSkipBaseDespawnCheck = this instanceof net.minecraft.world.entity.projectile.ThrowableProjectile; // Leaf - Rewrite entity despawn time
46+
47+
@Override
48+
public final boolean moonrise$isHardColliding() {
49+
@@ -626,6 +628,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
50+
this.setPos(0.0, 0.0, 0.0);
51+
this.eyeHeight = this.dimensions.eyeHeight();
52+
this.despawnTime = level == null || type == EntityType.PLAYER ? -1 : level.paperConfig().entities.spawning.despawnTime.getOrDefault(type, io.papermc.paper.configuration.type.number.IntOr.Disabled.DISABLED).or(-1); // Paper - entity despawn time limit
53+
+ this.updateLastTick(); // Leaf - Rewrite entity despawn time
54+
}
55+
56+
public boolean isColliding(BlockPos pos, BlockState state) {
57+
@@ -887,15 +890,50 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
58+
}
59+
60+
public void tick() {
61+
+ // Leaf start - Rewrite entity despawn time
62+
+ if (!this.shouldSkipBaseDespawnCheck && this.detectDespawnTime()) {
63+
+ return;
64+
+ }
65+
+ /*
66+
// Paper start - entity despawn time limit
67+
if (this.despawnTime >= 0 && this.totalEntityAge >= this.despawnTime) {
68+
this.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN);
69+
return;
70+
}
71+
// Paper end - entity despawn time limit
72+
+ */
73+
+ // Leaf end - Rewrite entity despawn time
74+
this.baseTick();
75+
}
76+
77+
+ // Leaf start - Rewrite entity despawn time
78+
+ protected final boolean detectDespawnTime() {
79+
+ if (this.despawnTime >= 0) {
80+
+ int missedTicks = this.calculateMissedTicks();
81+
+ if (missedTicks > 1) {
82+
+ if ((this.totalEntityAge += missedTicks) >= this.despawnTime) {
83+
+ this.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN);
84+
+ return true;
85+
+ }
86+
+ } else {
87+
+ if (this.totalEntityAge >= this.despawnTime) {
88+
+ this.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN);
89+
+ return true;
90+
+ }
91+
+ }
92+
+ }
93+
+ return false;
94+
+ }
95+
+
96+
+ private int calculateMissedTicks() {
97+
+ return net.minecraft.server.MinecraftServer.currentTick - this.lastTickTicks;
98+
+ }
99+
+
100+
+ public void updateLastTick() {
101+
+ this.lastTickTicks = net.minecraft.server.MinecraftServer.currentTick;
102+
+ }
103+
+ // Leaf end - Rewrite entity despawn time
104+
+
105+
// CraftBukkit start
106+
public void postTick() {
107+
// 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
108+
@@ -2564,6 +2602,12 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
109+
if (this.maxAirTicks != this.getDefaultMaxAirSupply()) {
110+
output.putInt("Bukkit.MaxAirSupply", this.getMaxAirSupply());
111+
}
112+
+ // Leaf start - Rewrite entity despawn time
113+
+ int missedTicks = this.calculateMissedTicks();
114+
+ if (missedTicks > 1) {
115+
+ this.totalEntityAge += missedTicks;
116+
+ }
117+
+ // Leaf end - Rewrite entity despawn time
118+
output.putInt("Spigot.ticksLived", this.totalEntityAge); // Paper
119+
// CraftBukkit end
120+
output.storeNullable("CustomName", ComponentSerialization.CODEC, this.getCustomName());
121+
diff --git a/net/minecraft/world/entity/projectile/ThrowableProjectile.java b/net/minecraft/world/entity/projectile/ThrowableProjectile.java
122+
index 4c61f4552dc64b1347c7b8dfe9502aac11c75888..be0ebc017bfe92fb2916a9e40971ad19614a33b4 100644
123+
--- a/net/minecraft/world/entity/projectile/ThrowableProjectile.java
124+
+++ b/net/minecraft/world/entity/projectile/ThrowableProjectile.java
125+
@@ -44,6 +44,11 @@ public abstract class ThrowableProjectile extends Projectile {
126+
127+
@Override
128+
public void tick() {
129+
+ // Leaf start - Rewrite entity despawn time - Check earlier for projectiles
130+
+ if (this.detectDespawnTime()) {
131+
+ return;
132+
+ }
133+
+ // Leaf end - Rewrite entity despawn time - Check earlier for projectiles
134+
this.handleFirstTickBubbleColumn();
135+
this.applyGravity();
136+
this.applyInertia();

0 commit comments

Comments
 (0)