Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add removal reason to EntityRemoveFromWorldEvent #11779

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 1555d1984787d360800ae3de9110bc8df80ba7f0..219e097e4275eabbe141ec987fdd991c71c244ba 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 336ab1657ff4d41cdf037eb71b5db1f993ab225f..784215b4a406e1c317ef2250ad6389c15d9d0669 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); // Paper - EntityRemoveFromWorldEvent
+ }
+ }
+ // 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 d7d42aa174d3e5e175db026a3e7937cae38b1d51..4a948b21f4b544fee7b3ac00e986e2e6a15b6f5c 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 @@ -533,7 +533,7 @@ index e7889c9c7b155db46730f5e168bb7fd3d1732a8c..334859c5ff7023c730513301cc11c983
movement = this.maybeBackOffFromEdge(movement, type);
Vec3 vec3 = this.collide(movement);
diff --git a/net/minecraft/world/entity/LivingEntity.java b/net/minecraft/world/entity/LivingEntity.java
index ff513e8c87bf42be756e46f4dbfec8dda2b8cb60..239c443ddc9bacc08a39a8ef2ab17016a2480549 100644
index 04aef3a25e3bb6e5328ab924d2659027cdb4b640..9acf0ddeaa51a55ffdd26be8ef903c38c808f0f9 100644
--- a/net/minecraft/world/entity/LivingEntity.java
+++ b/net/minecraft/world/entity/LivingEntity.java
@@ -3096,6 +3096,14 @@ public abstract class LivingEntity extends Entity implements Attackable {
Expand All @@ -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 f4058e70456e5189638095dd95868c609c509331..8993005e107ee9b7426e01a2d290a71ae7f7dd91 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 a175a2071609c15e1090202a4d26a65b60668bff..8c009c4506ad338b700585f2d3c6be1016f456c0 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); // Paper - EntityRemoveFromWorldEvent
+ }
+ }
+ // 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 0e44373884bebf1e8755267cd0389449d3f8ac7a..e46cb17afbd52a78c7c8ada4fd45460af34b1251 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
Loading
Loading