Skip to content

Commit

Permalink
Add removal reason to EntityRemoveFromWorldEvent
Browse files Browse the repository at this point in the history
  • Loading branch information
TonytheMacaroni committed Dec 24, 2024
1 parent d0d0efe commit e203dd4
Show file tree
Hide file tree
Showing 84 changed files with 315 additions and 169 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import org.bukkit.entity.Entity;
import org.bukkit.event.HandlerList;
import org.bukkit.event.entity.EntityEvent;
import org.bukkit.event.player.PlayerQuitEvent;
import org.jetbrains.annotations.ApiStatus;
import org.jspecify.annotations.NullMarked;

Expand All @@ -17,11 +18,14 @@ public class EntityRemoveFromWorldEvent extends EntityEvent {
private static final HandlerList HANDLER_LIST = new HandlerList();

private final World world;
private final Cause cause;

@ApiStatus.Internal
public EntityRemoveFromWorldEvent(final Entity entity, final World world) {
public EntityRemoveFromWorldEvent(final Entity entity, final World world, final Cause cause) {
super(entity);

this.world = world;
this.cause = cause;
}

/**
Expand All @@ -31,6 +35,14 @@ public World getWorld() {
return this.world;
}

/**
* Gets the reason the entity is being removed from the world.
* @return the removal reason
*/
public Cause getCause() {
return cause;
}

@Override
public HandlerList getHandlers() {
return HANDLER_LIST;
Expand All @@ -39,4 +51,119 @@ public HandlerList getHandlers() {
public static HandlerList getHandlerList() {
return HANDLER_LIST;
}

public enum Cause {

/**
* When an entity dies.
*/
DEATH(true, false),
/**
* When an entity does despawn. This includes mobs which are too far away,
* items or arrows which lay to long on the ground or area effect cloud.
*/
DESPAWN(true, false),
/**
* When an entity gets removed because it drops as an item.
* For example, trident or falling sand.
* <p>
* <b>Note:</b> Depending on other factors, such as gamerules, no item will actually drop,
* the cause, however, will still be drop.
*/
DROP(true, false),
/**
* When an entity gets removed because it enters a block.
* For example, bees or silverfish.
*/
ENTER_BLOCK(true, false),
/**
* When an entity gets removed because it exploded.
* For example, creepers, tnt or firework.
*/
EXPLODE(true, false),
/**
* When an entity gets removed because it hit something. This mainly applies to projectiles.
*/
HIT(true, false),
/**
* When an entity gets removed because it merges with another one.
* For example, items or xp.
*/
MERGE(true, false),
/**
* When an entity gets removed because it is too far below the world.
* This only applies to entities which get removed immediately,
* some entities get damage instead.
*/
OUT_OF_WORLD(true, false),
/**
* When an entity gets removed because it got pickup.
* For example, items, arrows, xp or parrots which get on a player shoulder.
*/
PICKUP(true, false),
/**
* When an entity gets removed with a player because the player quits the game.
* For example, a boat which gets removed with the player when he quits.
*/
PLAYER_QUIT(false, false),
/**
* When a plugin manually removes an entity.
*/
PLUGIN(true, false),
/**
* When an entity gets removed because it transforms into another one.
*/
TRANSFORMATION(true, false),
/**
* When the chunk an entity is in gets unloaded.
*/
UNLOAD(false, true),
/**
* When an entity is discarded, and a more specific cause does not exist.
*/
DISCARD(true, false),
/**
* When an entity changes dimensions.
*/
CHANGED_DIMENSION(false, false),
/**
* When the chunk an entity is in is no longer accessible, but not yet fully unloaded.
*/
INACCESSIBLE(false, false),
/**
* Used when the cause of the removal is unknown.
*/
UNKNOWN(false, false);

private final boolean destroy;
private final boolean save;

Cause(boolean destroy, boolean save) {
this.destroy = destroy;
this.save = save;
}

/**
* Whether the entity instance being removed will be destroyed.
*
* @return whether the entity will be destroyed
*/
public boolean willDestroy() {
return this.destroy;
}

/**
* Whether the entity instance being removed will be saved. This does not account for the value of
* {@link Entity#isPersistent}. Entities removed with {@link Cause#PLAYER_QUIT} are saved
* prior to the event firing, and thus should be modified prior to this using another event, such as
* with {@link PlayerQuitEvent}.
*
* @return whether the entity will be saved
*/
public boolean willSave() {
return this.save;
}

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ index d95413af04121fe91ca0f3b0c70025b9808acef9..ad665c7535c615d2b03a3e7864be435f
import org.slf4j.Logger;

diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java
index 3164f3784131babf9a6663335517a12df7e88a7b..da8848e2a3e3745949eb2356a049451d30f763a7 100644
index 172f4db148b7a575a31ba4360a29e22a58ce3fc2..f9474a740cf3c4c365eff500357cb751e85cee33 100644
--- a/net/minecraft/server/level/ServerLevel.java
+++ b/net/minecraft/server/level/ServerLevel.java
@@ -551,6 +551,7 @@ public class ServerLevel extends Level implements ServerEntityGetter, WorldGenLe
Expand Down Expand Up @@ -455,7 +455,7 @@ index a9f01e616ef6b0d74caf57cd68eb371a4fd30fd5..179f4e4b9b1eb57f78bbb2f9fa34b11e
public void aiStep() {
super.aiStep();
diff --git a/net/minecraft/world/entity/AreaEffectCloud.java b/net/minecraft/world/entity/AreaEffectCloud.java
index 24735284fda151414d99faad401d25ba60995f9a..23b342cc31c7e72ade0e1ccad86a9ccf34380f13 100644
index a920b5ff41da59694696d89b4d78472df562893d..bd909a42c10a40bf8cd12c2c8f19e69680f46b45 100644
--- a/net/minecraft/world/entity/AreaEffectCloud.java
+++ b/net/minecraft/world/entity/AreaEffectCloud.java
@@ -128,6 +128,16 @@ public class AreaEffectCloud extends Entity implements TraceableEntity {
Expand All @@ -467,7 +467,7 @@ index 24735284fda151414d99faad401d25ba60995f9a..23b342cc31c7e72ade0e1ccad86a9ccf
+ public void inactiveTick() {
+ super.inactiveTick();
+ if (this.tickCount >= this.waitTime + this.duration) {
+ this.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause
+ this.discard(com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause
+ }
+ }
+ // Paper end - EAR 2
Expand All @@ -476,7 +476,7 @@ index 24735284fda151414d99faad401d25ba60995f9a..23b342cc31c7e72ade0e1ccad86a9ccf
public void tick() {
super.tick();
diff --git a/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java
index e7889c9c7b155db46730f5e168bb7fd3d1732a8c..334859c5ff7023c730513301cc11c9837b2c7823 100644
index 9ae07e8dff867393aa92e2d10730169f4af0f577..14786aba4bde105b28813e6bcaabc3f8aa40f225 100644
--- a/net/minecraft/world/entity/Entity.java
+++ b/net/minecraft/world/entity/Entity.java
@@ -380,6 +380,15 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
Expand Down Expand Up @@ -552,7 +552,7 @@ index ff513e8c87bf42be756e46f4dbfec8dda2b8cb60..239c443ddc9bacc08a39a8ef2ab17016
public void tick() {
super.tick();
diff --git a/net/minecraft/world/entity/Mob.java b/net/minecraft/world/entity/Mob.java
index f7d69db61d1293510428ae275e8a50571dde5ddf..1ed07fd23985a6bf8cf8300f74c92b7531a79fc6 100644
index c694c1fbdf387e55823936fb4b3ec3a557f3e489..746bc684c7058976b5152a08448f6ce558f37653 100644
--- a/net/minecraft/world/entity/Mob.java
+++ b/net/minecraft/world/entity/Mob.java
@@ -215,6 +215,19 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
Expand Down Expand Up @@ -651,7 +651,7 @@ index 789fea258d70e60d38271ebb31270562dc7eb3ab..d0ab3db7bbd2942db19f473474371b20
}
}
diff --git a/net/minecraft/world/entity/item/ItemEntity.java b/net/minecraft/world/entity/item/ItemEntity.java
index 8b034b6bda937b25dbb3d09b8293fed6d7dc512c..52a7ed0d991758bad0dcedcb7f97fb15ac6c6d04 100644
index 9d9b757bfe3c985de30fa215dfa27951dce1c148..978bd7574aef1cd4cc0f36fcb61f4b4df0404437 100644
--- a/net/minecraft/world/entity/item/ItemEntity.java
+++ b/net/minecraft/world/entity/item/ItemEntity.java
@@ -124,6 +124,29 @@ public class ItemEntity extends Entity implements TraceableEntity {
Expand All @@ -676,7 +676,7 @@ index 8b034b6bda937b25dbb3d09b8293fed6d7dc512c..52a7ed0d991758bad0dcedcb7f97fb15
+ return;
+ }
+ // CraftBukkit end
+ this.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause
+ this.discard(com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent.Cause.DESPAWN); // CraftBukkit - add Bukkit remove cause
+ }
+ }
+ // Paper end - EAR 2
Expand Down Expand Up @@ -764,7 +764,7 @@ index c1e09e701757a300183b62d343ded03033e63aa7..56574f8ef879159edc0114da09300143
public void tick() {
super.tick();
diff --git a/net/minecraft/world/entity/projectile/FireworkRocketEntity.java b/net/minecraft/world/entity/projectile/FireworkRocketEntity.java
index 7c0862c50b44555fb27ce7dc46f4ec95a3eb0022..774ca9e0b56fd175ae246051de762d0c4256ca58 100644
index 552621bf7dd7d6e2f25bc65a66856ca0853f2d18..f30dcf0eac341f433cd7f4de7cc9332977b9fe4a 100644
--- a/net/minecraft/world/entity/projectile/FireworkRocketEntity.java
+++ b/net/minecraft/world/entity/projectile/FireworkRocketEntity.java
@@ -102,6 +102,21 @@ public class FireworkRocketEntity extends Projectile implements ItemSupplier {
Expand Down Expand Up @@ -828,7 +828,7 @@ index c553cf0592dfa606dbbb1e6854d3377b9feb5efb..8341e7f01606fca90e69384c16fc19bb
+
}
diff --git a/net/minecraft/world/level/Level.java b/net/minecraft/world/level/Level.java
index 32f184288f6065259c921293922c1b0163df4dc4..0f346faa82b988e86834c38837f6f11bea7f31c6 100644
index 79854cefdc2a7459f409e9d94b0775dd8365a6a9..4dd1899960db8b02f40c69fd7fd902236a8295f6 100644
--- a/net/minecraft/world/level/Level.java
+++ b/net/minecraft/world/level/Level.java
@@ -153,6 +153,12 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,7 @@ index 0000000000000000000000000000000000000000..1b8193587814225c2ef2c5d9e667436e
+ }
+}
diff --git a/ca/spottedleaf/moonrise/paper/PaperHooks.java b/ca/spottedleaf/moonrise/paper/PaperHooks.java
index 5577287398db2bb9d21f14409ef580b8ab9ea018..4d344559a20a0c35c181e297e81788c747363ec9 100644
index b59a218b4c66588d135471f453a74c994027cb90..95ce090814948eed703faf78fc457a33e217db79 100644
--- a/ca/spottedleaf/moonrise/paper/PaperHooks.java
+++ b/ca/spottedleaf/moonrise/paper/PaperHooks.java
@@ -268,7 +268,7 @@ public final class PaperHooks extends BaseChunkSystemHooks implements PlatformHo
Expand Down Expand Up @@ -26735,7 +26735,7 @@ index 70f6d068b3f3665b282d9750310c883839120ab2..870b9efd445ddadb3725e88351555ad9
if (!passengers.equals(this.lastPassengers)) {
this.broadcastAndSend(new ClientboundSetPassengersPacket(this.entity)); // CraftBukkit
diff --git a/net/minecraft/server/level/ServerLevel.java b/net/minecraft/server/level/ServerLevel.java
index 192977dd661ee795ada13db895db770293e9b402..95a4e37a3c93f9b3c56c7a7376ed521cd46fbb6f 100644
index b85578471ce5a1cedbcc4f2161ed6cfa8dce7b37..55469573bf4420a378fff33fa5410571f57bcccc 100644
--- a/net/minecraft/server/level/ServerLevel.java
+++ b/net/minecraft/server/level/ServerLevel.java
@@ -170,7 +170,7 @@ import net.minecraft.world.phys.shapes.VoxelShape;
Expand Down Expand Up @@ -27976,7 +27976,7 @@ index 4eb040006f5d41b47e5ac9df5d9f19c4315d6343..7fa41dea184b01891f45d8e404bc1cba
this.generatingStep = generatingStep;
this.cache = cache;
diff --git a/net/minecraft/server/players/PlayerList.java b/net/minecraft/server/players/PlayerList.java
index ff0315cffdb282fdc0a1ffd15e2954caa76835c9..5e94dd9e26aa4fd6545dbaae2ae0cb51cb6f13e0 100644
index 17276555ffb331b6c29ef33b31b649cb8a4039b7..0c26bc93dc3ac4d38ffa5697aeaef50faca448f1 100644
--- a/net/minecraft/server/players/PlayerList.java
+++ b/net/minecraft/server/players/PlayerList.java
@@ -1312,7 +1312,7 @@ public abstract class PlayerList {
Expand Down Expand Up @@ -28372,7 +28372,7 @@ index 8cc5c0716392ba06501542ff5cbe71ee43979e5d..09fd99c9cbd23b5f3c899bfb00c9b896
+ // Paper end - block counting
}
diff --git a/net/minecraft/world/entity/Entity.java b/net/minecraft/world/entity/Entity.java
index 45f69a914d5a0565196c4105d61541047301470f..f42bdae2f80805e5069212bd16e3ab6814aef9ee 100644
index 204886659d76058ad0085ea4781ddfbbfc39a6f0..034d3ff8e6ef37f59b56f299edb8ff81902e1630 100644
--- a/net/minecraft/world/entity/Entity.java
+++ b/net/minecraft/world/entity/Entity.java
@@ -135,7 +135,7 @@ import net.minecraft.world.scores.ScoreHolder;
Expand Down Expand Up @@ -29085,14 +29085,14 @@ index 45f69a914d5a0565196c4105d61541047301470f..f42bdae2f80805e5069212bd16e3ab68
@@ -4603,6 +4908,12 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess

@Override
public final void setRemoved(Entity.RemovalReason removalReason, org.bukkit.event.entity.EntityRemoveEvent.Cause cause) {
public final void setRemoved(Entity.RemovalReason removalReason, com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent.Cause cause) { // Paper - EntityRemoveFromWorldEvent
+ // Paper start - rewrite chunk system
+ if (!((ca.spottedleaf.moonrise.patches.chunk_system.level.ChunkSystemLevel)this.level).moonrise$getEntityLookup().canRemoveEntity((Entity)(Object)this)) {
+ LOGGER.warn("Entity " + this + " is currently prevented from being removed from the world since it is processing section status updates", new Throwable());
+ return;
+ }
+ // Paper end - rewrite chunk system
org.bukkit.craftbukkit.event.CraftEventFactory.callEntityRemoveEvent(this, cause);
org.bukkit.craftbukkit.event.CraftEventFactory.callEntityRemoveEvent(this, removalReason, cause);
// CraftBukkit end
final boolean alreadyRemoved = this.removalReason != null; // Paper - Folia schedulers
@@ -4614,7 +4925,7 @@ public abstract class Entity implements SyncedDataHolder, Nameable, EntityAccess
Expand Down Expand Up @@ -29353,7 +29353,7 @@ index 324cc0686f0f5b1371b2bbea5b8c8fdb1f363006..39cd1e3d8192d7077d6b7864d3393309
this(setDirty, true, ImmutableList.of());
}
diff --git a/net/minecraft/world/entity/decoration/ArmorStand.java b/net/minecraft/world/entity/decoration/ArmorStand.java
index 33f6d6862731d22d6d3eeb7cf39a4a42049afae3..a3cc0001a949597e345d7919c3f6109fa4a949ad 100644
index 9213dd0cf8d89129855969de511d30fe25f7a40b..f1f47f960a1c52b26bea441074e876ac8749fba0 100644
--- a/net/minecraft/world/entity/decoration/ArmorStand.java
+++ b/net/minecraft/world/entity/decoration/ArmorStand.java
@@ -316,7 +316,7 @@ public class ArmorStand extends LivingEntity {
Expand Down Expand Up @@ -29503,7 +29503,7 @@ index 300f3ed58109219d97846082941b860585f66fed..e81195df621159da67136f020fa7a6d3

// Paper start - Affects Spawning API
diff --git a/net/minecraft/world/level/Level.java b/net/minecraft/world/level/Level.java
index 771d6ed6a7c889c09efd4ff6e20298c851eaa79f..8331b49185500ab3b4307e9ae05126b4f83a318a 100644
index 6b81142c52cbef6f31606b0f33bafd2443d3f7f3..00900f0b87e0c183d681fed540bf75772a46a774 100644
--- a/net/minecraft/world/level/Level.java
+++ b/net/minecraft/world/level/Level.java
@@ -79,6 +79,7 @@ import net.minecraft.world.level.storage.LevelData;
Expand Down Expand Up @@ -30216,7 +30216,7 @@ index 771d6ed6a7c889c09efd4ff6e20298c851eaa79f..8331b49185500ab3b4307e9ae05126b4
}
this.blockEntityTickers.removeAll(toRemove); // Paper - Fix MC-117075
@@ -865,6 +1505,7 @@ public abstract class Level implements LevelAccessor, AutoCloseable {
entity.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DISCARD);
entity.discard(com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent.Cause.DISCARD); // Paper - EntityRemoveFromWorldEvent
// Paper end - Prevent block entity and entity crashes
}
+ this.moonrise$midTickTasks(); // Paper - rewrite chunk system
Expand Down Expand Up @@ -32481,7 +32481,7 @@ index 7a64b00ff31d1273d0b0b9a3cfd43808c88ef46a..c9d8a1c0a75c34ccd9f5cead02cccd77
this.chunkType = chunkType;
this.heightmapsAfter = heightmapsAfter;
diff --git a/net/minecraft/world/level/chunk/status/ChunkStatusTasks.java b/net/minecraft/world/level/chunk/status/ChunkStatusTasks.java
index c953bc93de8a42bcc12b7e8f46b3ae804e54964e..2ccbdfdcf81556306e098277ecf119d5fd02138c 100644
index 617750597ee21020aae0439b1c48aa6fbd62c760..c44a3628f63e1c3ad3c8fbba67999163eb3b2e55 100644
--- a/net/minecraft/world/level/chunk/status/ChunkStatusTasks.java
+++ b/net/minecraft/world/level/chunk/status/ChunkStatusTasks.java
@@ -182,7 +182,7 @@ public class ChunkStatusTasks {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,7 @@
+
+ @Override
+ public void unloadEntity(final Entity entity) {
+ entity.setRemoved(Entity.RemovalReason.UNLOADED_TO_CHUNK, org.bukkit.event.entity.EntityRemoveEvent.Cause.UNLOAD);
+ entity.setRemoved(Entity.RemovalReason.UNLOADED_TO_CHUNK, com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent.Cause.UNLOAD);
+ }
+
+ @Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
List<Entity> entitiesOfClass = this.getLevel()
.getEntitiesOfClass(Entity.class, structureBounds.inflate(1.0), entity -> !(entity instanceof Player));
- entitiesOfClass.forEach(entity -> entity.remove(Entity.RemovalReason.DISCARDED));
+ entitiesOfClass.forEach(entity -> entity.remove(Entity.RemovalReason.DISCARDED, org.bukkit.event.entity.EntityRemoveEvent.Cause.DISCARD)); // Paper
+ entitiesOfClass.forEach(entity -> entity.remove(Entity.RemovalReason.DISCARDED, com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent.Cause.DISCARD)); // Paper
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
AABB aabb = AABB.of(boundingBox1);
List<Entity> entitiesOfClass = level.getEntitiesOfClass(Entity.class, aabb, entity -> !(entity instanceof Player));
- entitiesOfClass.forEach(Entity::discard);
+ entitiesOfClass.forEach(entity -> entity.discard(org.bukkit.event.entity.EntityRemoveEvent.Cause.DISCARD)); // Paper
+ entitiesOfClass.forEach(entity -> entity.discard(com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent.Cause.DISCARD)); // Paper
}

public static BlockPos getTransformedFarCorner(BlockPos pos, Vec3i offset, Rotation rotation) {
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

private static int resetGameTestInfo(GameTestInfo gameTestInfo) {
- gameTestInfo.getLevel().getEntities(null, gameTestInfo.getStructureBounds()).stream().forEach(entity -> entity.remove(Entity.RemovalReason.DISCARDED));
+ gameTestInfo.getLevel().getEntities(null, gameTestInfo.getStructureBounds()).stream().forEach(entity -> entity.remove(Entity.RemovalReason.DISCARDED, org.bukkit.event.entity.EntityRemoveEvent.Cause.DISCARD)); // Paper
+ gameTestInfo.getLevel().getEntities(null, gameTestInfo.getStructureBounds()).stream().forEach(entity -> entity.remove(Entity.RemovalReason.DISCARDED, com.destroystokyo.paper.event.entity.EntityRemoveFromWorldEvent.Cause.DISCARD)); // Paper
gameTestInfo.getStructureBlockEntity().placeStructure(gameTestInfo.getLevel());
StructureUtils.removeBarriers(gameTestInfo.getStructureBounds(), gameTestInfo.getLevel());
say(gameTestInfo.getLevel(), "Reset succeded for: " + gameTestInfo.getTestName(), ChatFormatting.GREEN);
Loading

0 comments on commit e203dd4

Please sign in to comment.