2021-06-11 14:02:28 +02:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
From: Phoenix616 <mail@moep.tv>
Date: Tue, 21 Aug 2018 01:39:35 +0100
Subject: [PATCH] Improve death events
This adds the ability to cancel the death events and to modify the sound
an entity makes when dying. (In cases were no sound should it will be
called with shouldPlaySound set to false allowing unsilencing of silent
entities)
It makes handling of entity deaths a lot nicer as you no longer need
to listen on the damage event and calculate if the entity dies yourself
to cancel the death which has the benefit of also receiving the dropped
items and experience which is otherwise only properly possible by using
internal code.
2022-11-20 00:53:20 +01:00
== AT ==
public net.minecraft.world.entity.LivingEntity getDeathSound()Lnet/minecraft/sounds/SoundEvent;
public net.minecraft.world.entity.LivingEntity getSoundVolume()F
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/server/level/ServerPlayer.java b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2024-06-14 19:15:52 +02:00
index 16f7278ce8d5b8c45026baeb017fdce1ed5945c8..508de7064c08eb22cd66658a98f9833ddc030618 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/server/level/ServerPlayer.java
+++ b/src/main/java/net/minecraft/server/level/ServerPlayer.java
2024-06-13 21:04:27 +02:00
@@ -268,6 +268,10 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player {
2023-09-22 00:26:51 +02:00
private int containerCounter;
2021-06-11 14:02:28 +02:00
public boolean wonGame;
2024-01-24 11:45:17 +01:00
private int containerUpdateDelay; // Paper - Configurable container update tick rate
2021-06-11 14:02:28 +02:00
+ // Paper start - cancellable death event
2024-01-24 11:45:17 +01:00
+ public boolean queueHealthUpdatePacket;
2021-06-11 14:02:28 +02:00
+ public net.minecraft.network.protocol.game.ClientboundSetHealthPacket queuedHealthUpdatePacket;
2024-01-24 11:45:17 +01:00
+ // Paper end - cancellable death event
2021-06-11 14:02:28 +02:00
// CraftBukkit start
2024-04-24 04:46:06 +02:00
public CraftPlayer.TransferCookieConnection transferCookieConnection;
2024-06-13 21:04:27 +02:00
@@ -896,7 +900,7 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player {
2023-12-29 20:57:32 +01:00
@Override
public void die(DamageSource damageSource) {
- this.gameEvent(GameEvent.ENTITY_DIE);
+ // this.gameEvent(GameEvent.ENTITY_DIE); // Paper - move below event cancellation check
boolean flag = this.level().getGameRules().getBoolean(GameRules.RULE_SHOWDEATHMESSAGES);
// CraftBukkit start - fire PlayerDeathEvent
if (this.isRemoved()) {
2024-06-13 21:04:27 +02:00
@@ -924,6 +928,16 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player {
2021-08-13 19:08:34 +02:00
String deathmessage = defaultMessage.getString();
this.keepLevel = keepInventory; // SPIGOT-2222: pre-set keepLevel
2024-05-11 23:48:37 +02:00
org.bukkit.event.entity.PlayerDeathEvent event = CraftEventFactory.callPlayerDeathEvent(this, damageSource, loot, PaperAdventure.asAdventure(defaultMessage), keepInventory); // Paper - Adventure
2021-06-11 14:02:28 +02:00
+ // Paper start - cancellable death event
+ if (event.isCancelled()) {
+ // make compatible with plugins that might have already set the health in an event listener
+ if (this.getHealth() <= 0) {
+ this.setHealth((float) event.getReviveHealth());
+ }
+ return;
+ }
2023-12-29 20:57:32 +01:00
+ this.gameEvent(GameEvent.ENTITY_DIE); // moved from the top of this method
2021-06-11 14:02:28 +02:00
+ // Paper end
// SPIGOT-943 - only call if they have an inventory open
if (this.containerMenu != this.inventoryMenu) {
2024-06-13 21:04:27 +02:00
@@ -1072,8 +1086,17 @@ public class ServerPlayer extends net.minecraft.world.entity.player.Player {
2021-06-11 14:02:28 +02:00
}
}
}
-
- return super.hurt(source, amount);
+ // Paper start - cancellable death events
2021-06-13 06:03:02 +02:00
+ //return super.hurt(source, amount);
2021-06-11 14:02:28 +02:00
+ this.queueHealthUpdatePacket = true;
+ boolean damaged = super.hurt(source, amount);
+ this.queueHealthUpdatePacket = false;
+ if (this.queuedHealthUpdatePacket != null) {
+ this.connection.send(this.queuedHealthUpdatePacket);
+ this.queuedHealthUpdatePacket = null;
+ }
+ return damaged;
+ // Paper end
}
}
}
diff --git a/src/main/java/net/minecraft/world/entity/LivingEntity.java b/src/main/java/net/minecraft/world/entity/LivingEntity.java
2024-06-14 13:07:50 +02:00
index 63c3bef55dae608ddd19cf6c8348b3027460e2af..d8681ace0ba74a6eb3b6abafe4915b5e753c02f0 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/entity/LivingEntity.java
+++ b/src/main/java/net/minecraft/world/entity/LivingEntity.java
2024-06-13 21:04:27 +02:00
@@ -283,6 +283,7 @@ public abstract class LivingEntity extends Entity implements Attackable {
2021-06-11 14:02:28 +02:00
public Set<UUID> collidableExemptions = new HashSet<>();
2021-06-13 06:03:02 +02:00
public boolean bukkitPickUpLoot;
2021-06-11 14:02:28 +02:00
public org.bukkit.craftbukkit.entity.CraftLivingEntity getBukkitLivingEntity() { return (org.bukkit.craftbukkit.entity.CraftLivingEntity) super.getBukkitEntity(); } // Paper
+ public boolean silentDeath = false; // Paper - mark entity as dying silently for cancellable death event
@Override
public float getBukkitYaw() {
2024-06-13 21:04:27 +02:00
@@ -1526,11 +1527,12 @@ public abstract class LivingEntity extends Entity implements Attackable {
2023-10-29 00:50:26 +02:00
2021-06-11 14:02:28 +02:00
if (this.isDeadOrDying()) {
if (!this.checkTotemDeathProtection(source)) {
2024-04-24 04:46:06 +02:00
- if (flag1) {
- this.makeSound(this.getDeathSound());
2021-06-11 14:02:28 +02:00
- }
2021-06-13 06:03:02 +02:00
+ // Paper start - moved into CraftEventFactory event caller for cancellable death event
2021-06-11 14:02:28 +02:00
+ this.silentDeath = !flag1; // mark entity as dying silently
+ // Paper end
this.die(source);
+ this.silentDeath = false; // Paper - cancellable death event - reset to default
}
} else if (flag1) {
this.playHurtSound(source);
2024-06-13 21:04:27 +02:00
@@ -1689,6 +1691,7 @@ public abstract class LivingEntity extends Entity implements Attackable {
2022-06-08 02:15:06 +02:00
Entity entity = damageSource.getEntity();
2021-06-11 14:02:28 +02:00
LivingEntity entityliving = this.getKillCredit();
2024-02-01 10:15:57 +01:00
2021-12-08 19:25:57 +01:00
+ /* // Paper - move down to make death event cancellable - this is the awardKillScore below
2021-06-13 06:03:02 +02:00
if (this.deathScore >= 0 && entityliving != null) {
2022-06-08 02:15:06 +02:00
entityliving.awardKillScore(this, this.deathScore, damageSource);
2021-06-11 14:02:28 +02:00
}
2024-06-13 21:04:27 +02:00
@@ -1700,24 +1703,59 @@ public abstract class LivingEntity extends Entity implements Attackable {
2023-06-07 22:19:14 +02:00
if (!this.level().isClientSide && this.hasCustomName()) {
2021-08-13 19:08:34 +02:00
if (org.spigotmc.SpigotConfig.logNamedDeaths) LivingEntity.LOGGER.info("Named entity {} died: {}", this, this.getCombatTracker().getDeathMessage().getString()); // Spigot
2021-07-07 08:52:40 +02:00
}
2021-12-08 19:25:57 +01:00
+ */ // Paper - move down to make death event cancellable - this is the awardKillScore below
2021-07-07 08:52:40 +02:00
2021-06-11 14:02:28 +02:00
this.dead = true;
- this.getCombatTracker().recheckStatus();
2021-06-13 06:03:02 +02:00
+ // Paper - moved into if below
2023-06-07 22:19:14 +02:00
Level world = this.level();
if (world instanceof ServerLevel) {
ServerLevel worldserver = (ServerLevel) world;
2022-06-08 02:15:06 +02:00
+ // Paper - move below into if for onKill
2024-04-24 04:46:06 +02:00
+
2021-06-11 14:02:28 +02:00
+ // Paper start
2024-06-13 21:04:27 +02:00
+ org.bukkit.event.entity.EntityDeathEvent deathEvent = this.dropAllDeathLoot(worldserver, damageSource);
2021-06-11 14:02:28 +02:00
+ if (deathEvent == null || !deathEvent.isCancelled()) {
2021-06-13 06:03:02 +02:00
+ if (this.deathScore >= 0 && entityliving != null) {
2022-06-08 02:15:06 +02:00
+ entityliving.awardKillScore(this, this.deathScore, damageSource);
2021-06-11 14:02:28 +02:00
+ }
2021-12-08 19:25:57 +01:00
+ // Paper start - clear equipment if event is not cancelled
2022-02-18 19:16:41 +01:00
+ if (this instanceof Mob) {
+ for (EquipmentSlot slot : this.clearedEquipmentSlots) {
+ this.setItemSlot(slot, ItemStack.EMPTY);
+ }
+ this.clearedEquipmentSlots.clear();
2021-12-08 19:25:57 +01:00
+ }
+ // Paper end
2024-02-01 10:15:57 +01:00
+
2021-06-11 14:02:28 +02:00
+ if (this.isSleeping()) {
+ this.stopSleeping();
+ }
2024-06-13 21:04:27 +02:00
- if (entity == null || entity.killedEntity(worldserver, this)) {
2023-06-07 22:19:14 +02:00
+ if (!this.level().isClientSide && this.hasCustomName()) {
2021-08-13 19:08:34 +02:00
+ if (org.spigotmc.SpigotConfig.logNamedDeaths) LivingEntity.LOGGER.info("Named entity {} died: {}", this, this.getCombatTracker().getDeathMessage().getString()); // Spigot
2021-07-07 12:04:34 +02:00
+ }
2024-06-13 21:04:27 +02:00
+
2021-06-13 06:03:02 +02:00
+ this.getCombatTracker().recheckStatus();
2021-06-11 14:02:28 +02:00
+ if (entity != null) {
2023-06-08 00:12:41 +02:00
+ entity.killedEntity((ServerLevel) this.level(), this);
2021-06-11 14:02:28 +02:00
+ }
2023-03-23 22:57:03 +01:00
this.gameEvent(GameEvent.ENTITY_DIE);
2024-06-13 21:04:27 +02:00
- this.dropAllDeathLoot(worldserver, damageSource);
2023-06-12 19:03:51 +02:00
+ } else {
+ this.dead = false;
+ this.setHealth((float) deathEvent.getReviveHealth());
2024-06-13 21:04:27 +02:00
+ }
2021-06-11 14:02:28 +02:00
+ // Paper end
2024-06-13 21:04:27 +02:00
this.createWitherRose(entityliving);
}
2021-06-11 14:02:28 +02:00
2023-06-07 22:19:14 +02:00
+ // Paper start
2021-06-11 14:02:28 +02:00
+ if (this.dead) { // Paper
2024-06-13 21:04:27 +02:00
this.level().broadcastEntityEvent(this, (byte) 3);
- }
2021-06-11 14:02:28 +02:00
this.setPose(Pose.DYING);
2023-06-07 22:19:14 +02:00
+ }
+ // Paper end
2021-06-11 14:02:28 +02:00
}
}
2024-06-13 21:04:27 +02:00
@@ -1725,7 +1763,7 @@ public abstract class LivingEntity extends Entity implements Attackable {
2023-06-07 22:19:14 +02:00
if (!this.level().isClientSide) {
2021-06-11 14:02:28 +02:00
boolean flag = false;
- if (adversary instanceof WitherBoss) {
+ if (this.dead && adversary instanceof WitherBoss) { // Paper
2023-06-07 22:19:14 +02:00
if (this.level().getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING)) {
2021-06-11 14:02:28 +02:00
BlockPos blockposition = this.blockPosition();
BlockState iblockdata = Blocks.WITHER_ROSE.defaultBlockState();
2024-06-13 21:04:27 +02:00
@@ -1754,24 +1792,37 @@ public abstract class LivingEntity extends Entity implements Attackable {
2021-06-11 14:02:28 +02:00
}
}
2024-06-13 21:04:27 +02:00
- protected void dropAllDeathLoot(ServerLevel world, DamageSource damageSource) {
2022-02-18 19:16:41 +01:00
+ // Paper start
+ protected boolean clearEquipmentSlots = true;
+ protected Set<EquipmentSlot> clearedEquipmentSlots = new java.util.HashSet<>();
2024-06-13 21:04:27 +02:00
+ protected org.bukkit.event.entity.EntityDeathEvent dropAllDeathLoot(ServerLevel world, DamageSource damageSource) {
+ // Paper end
boolean flag = this.lastHurtByPlayerTime > 0;
2021-06-11 14:02:28 +02:00
2021-12-08 19:25:57 +01:00
this.dropEquipment(); // CraftBukkit - from below
2024-06-13 21:04:27 +02:00
if (this.shouldDropLoot() && world.getGameRules().getBoolean(GameRules.RULE_DOMOBLOOT)) {
this.dropFromLootTable(damageSource, flag);
2022-02-18 19:16:41 +01:00
+ // Paper start
+ final boolean prev = this.clearEquipmentSlots;
+ this.clearEquipmentSlots = false;
+ this.clearedEquipmentSlots.clear();
+ // Paper end
2024-06-13 21:04:27 +02:00
this.dropCustomDeathLoot(world, damageSource, flag);
2022-02-18 19:16:41 +01:00
+ this.clearEquipmentSlots = prev; // Paper
2021-06-11 14:02:28 +02:00
}
// CraftBukkit start - Call death event
2024-06-13 21:04:27 +02:00
- CraftEventFactory.callEntityDeathEvent(this, damageSource, this.drops);
+ org.bukkit.event.entity.EntityDeathEvent deathEvent = CraftEventFactory.callEntityDeathEvent(this, damageSource, this.drops); // Paper
2021-06-11 14:02:28 +02:00
+ this.postDeathDropItems(deathEvent); // Paper
this.drops = new ArrayList<>();
// CraftBukkit end
2024-06-13 21:04:27 +02:00
// this.dropEquipment();// CraftBukkit - moved up
this.dropExperience(damageSource.getEntity());
2021-06-11 14:02:28 +02:00
+ return deathEvent; // Paper
}
protected void dropEquipment() {}
+ protected void postDeathDropItems(org.bukkit.event.entity.EntityDeathEvent event) {} // Paper - method for post death logic that cannot be ran before the event is potentially cancelled
2024-06-13 21:04:27 +02:00
public int getExpReward(@Nullable Entity entity) { // CraftBukkit
Level world = this.level();
2021-12-08 19:25:57 +01:00
diff --git a/src/main/java/net/minecraft/world/entity/Mob.java b/src/main/java/net/minecraft/world/entity/Mob.java
2024-06-13 21:04:27 +02:00
index b7c216b79684a4dbb93899fd2d3bc5a9e1b04f2e..4b0e269f3580c1c6dac1e5f2dd3cdac1d8e1118a 100644
2021-12-08 19:25:57 +01:00
--- a/src/main/java/net/minecraft/world/entity/Mob.java
+++ b/src/main/java/net/minecraft/world/entity/Mob.java
2024-06-13 21:04:27 +02:00
@@ -1121,6 +1121,12 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
2024-02-10 01:07:10 +01:00
}
+ // Paper start
+ protected boolean shouldSkipLoot(EquipmentSlot slot) { // method to avoid to fallback into the global mob loot logic (i.e fox)
+ return false;
+ }
+ // Paper end
+
@Override
2024-06-13 21:04:27 +02:00
protected void dropCustomDeathLoot(ServerLevel world, DamageSource source, boolean causedByPlayer) {
super.dropCustomDeathLoot(world, source, causedByPlayer);
@@ -1129,6 +1135,7 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
2024-02-10 01:07:10 +01:00
2024-06-13 21:04:27 +02:00
for (int j = 0; j < i; ++j) {
EquipmentSlot enumitemslot = aenumitemslot[j];
2024-02-10 01:07:10 +01:00
+ if (this.shouldSkipLoot(enumitemslot)) continue; // Paper
ItemStack itemstack = this.getItemBySlot(enumitemslot);
float f = this.getEquipmentDropChance(enumitemslot);
2021-12-08 19:25:57 +01:00
2024-06-13 21:04:27 +02:00
@@ -1153,7 +1160,13 @@ public abstract class Mob extends LivingEntity implements EquipmentUser, Leashab
}
this.spawnAtLocation(itemstack);
+ if (this.clearEquipmentSlots) { // Paper
this.setItemSlot(enumitemslot, ItemStack.EMPTY);
+ // Paper start
+ } else {
+ this.clearedEquipmentSlots.add(enumitemslot);
+ }
+ // Paper end
}
2021-12-08 19:25:57 +01:00
}
}
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/world/entity/animal/Fox.java b/src/main/java/net/minecraft/world/entity/animal/Fox.java
2024-06-13 21:04:27 +02:00
index a6788da1505f9e119c03b94488f5e006da13e918..e46c8231ee318eda0512afbb6343b426b4838643 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/entity/animal/Fox.java
+++ b/src/main/java/net/minecraft/world/entity/animal/Fox.java
2024-04-24 04:46:06 +02:00
@@ -704,16 +704,38 @@ public class Fox extends Animal implements VariantHolder<Fox.Type> {
2024-02-10 01:07:10 +01:00
return this.getTrustedUUIDs().contains(uuid);
2021-06-11 14:02:28 +02:00
}
2024-02-10 01:07:10 +01:00
+ // Paper start - handle the bitten item separately like vanilla
2021-06-11 14:02:28 +02:00
@Override
2024-06-13 21:04:27 +02:00
- protected void dropAllDeathLoot(ServerLevel world, DamageSource damageSource) {
2024-02-10 01:07:10 +01:00
+ protected boolean shouldSkipLoot(EquipmentSlot slot) {
+ return slot == EquipmentSlot.MAINHAND;
+ }
+ // Paper end
+
+ @Override
2021-06-13 06:03:02 +02:00
+ // Paper start - Cancellable death event
2024-06-13 21:04:27 +02:00
+ protected org.bukkit.event.entity.EntityDeathEvent dropAllDeathLoot(ServerLevel world, DamageSource damageSource) {
2024-02-10 01:07:10 +01:00
ItemStack itemstack = this.getItemBySlot(EquipmentSlot.MAINHAND);
- if (!itemstack.isEmpty()) {
+ boolean releaseMouth = false;
+ if (!itemstack.isEmpty() && this.level().getGameRules().getBoolean(GameRules.RULE_DOMOBLOOT)) { // Fix MC-153010
this.spawnAtLocation(itemstack);
+ releaseMouth = true;
+ }
2021-06-11 14:02:28 +02:00
+
2024-06-13 21:04:27 +02:00
+ org.bukkit.event.entity.EntityDeathEvent deathEvent = super.dropAllDeathLoot(world, damageSource);
2021-06-11 14:02:28 +02:00
+
+ // Below is code to drop
+
+ if (deathEvent == null || deathEvent.isCancelled()) {
+ return deathEvent;
+ }
2024-02-10 01:07:10 +01:00
+
+ if (releaseMouth) {
+ // Paper end - Cancellable death event
2021-06-11 14:02:28 +02:00
this.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
}
2024-06-13 21:04:27 +02:00
- super.dropAllDeathLoot(world, damageSource);
2024-02-10 01:07:10 +01:00
+ return deathEvent; // Paper - Cancellable death event
2021-06-11 14:02:28 +02:00
}
2024-04-24 04:46:06 +02:00
public static boolean isPathClear(Fox fox, LivingEntity chasedEntity) {
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/world/entity/animal/horse/AbstractChestedHorse.java b/src/main/java/net/minecraft/world/entity/animal/horse/AbstractChestedHorse.java
2024-06-13 21:04:27 +02:00
index 767817fb1418958c89d0db9da4ae7eb8a5a16076..5654c614f07f07ff642ba4851b0cb6fa185924ae 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/entity/animal/horse/AbstractChestedHorse.java
+++ b/src/main/java/net/minecraft/world/entity/animal/horse/AbstractChestedHorse.java
2024-06-13 21:04:27 +02:00
@@ -71,9 +71,17 @@ public abstract class AbstractChestedHorse extends AbstractHorse {
2021-06-13 06:03:02 +02:00
this.spawnAtLocation(Blocks.CHEST);
2021-06-11 14:02:28 +02:00
}
2024-04-12 21:14:06 +02:00
+ //this.setChest(false); // Paper - moved to post death logic
+ }
+ }
+
2021-06-11 14:02:28 +02:00
+ // Paper start
+ protected void postDeathDropItems(org.bukkit.event.entity.EntityDeathEvent event) {
+ if (this.hasChest() && (event == null || !event.isCancelled())) {
2024-04-12 21:14:06 +02:00
this.setChest(false);
}
}
2021-06-11 14:02:28 +02:00
+ // Paper end
2024-04-12 21:14:06 +02:00
2021-06-11 14:02:28 +02:00
@Override
2021-06-13 06:03:02 +02:00
public void addAdditionalSaveData(CompoundTag nbt) {
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/net/minecraft/world/entity/decoration/ArmorStand.java b/src/main/java/net/minecraft/world/entity/decoration/ArmorStand.java
2024-06-13 21:04:27 +02:00
index ee3902cbada46ffb78c42dbf6f00c859546c76e1..92bb0c63330ad3a4cb13b2dc655020714e9b1ffd 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/net/minecraft/world/entity/decoration/ArmorStand.java
+++ b/src/main/java/net/minecraft/world/entity/decoration/ArmorStand.java
2024-06-13 21:04:27 +02:00
@@ -505,8 +505,10 @@ public class ArmorStand extends LivingEntity {
}
// CraftBukkit end
if (source.is(DamageTypeTags.IS_EXPLOSION)) {
- this.brokenByAnything(worldserver, source);
- this.kill(source); // CraftBukkit
+ // Paper start - avoid duplicate event call
+ org.bukkit.event.entity.EntityDeathEvent event = this.brokenByAnything(worldserver, source);
+ if (!event.isCancelled()) this.kill(source, false); // CraftBukkit
+ // Paper end
return false;
} else if (source.is(DamageTypeTags.IGNITES_ARMOR_STANDS)) {
if (this.isOnFire()) {
@@ -549,9 +551,9 @@ public class ArmorStand extends LivingEntity {
this.gameEvent(GameEvent.ENTITY_DAMAGE, source.getEntity());
this.lastHit = i;
} else {
- this.brokenByPlayer(worldserver, source);
+ org.bukkit.event.entity.EntityDeathEvent event = this.brokenByPlayer(worldserver, source); // Paper
this.showBreakingParticles();
- this.discard(EntityRemoveEvent.Cause.DEATH); // CraftBukkit - SPIGOT-4890: remain as this.discard() since above damagesource method will call death event
+ if (!event.isCancelled()) this.kill(source, false); // Paper - we still need to kill to follow vanilla logic (emit the game event etc...)
}
return true;
@@ -604,8 +606,10 @@ public class ArmorStand extends LivingEntity {
2023-06-27 06:22:49 +02:00
f1 -= amount;
if (f1 <= 0.5F) {
2024-06-13 21:04:27 +02:00
- this.brokenByAnything(world, damageSource);
2024-05-11 23:48:37 +02:00
- this.kill(damageSource); // CraftBukkit
2023-06-27 06:22:49 +02:00
+ // Paper start - avoid duplicate event call
2024-06-13 21:04:27 +02:00
+ org.bukkit.event.entity.EntityDeathEvent event = this.brokenByAnything(world, damageSource);
2024-05-11 23:48:37 +02:00
+ if (!event.isCancelled()) this.kill(damageSource, false); // CraftBukkit
2023-06-27 06:22:49 +02:00
+ // Paper end
} else {
this.setHealth(f1);
this.gameEvent(GameEvent.ENTITY_DAMAGE, damageSource.getEntity());
2024-06-13 21:04:27 +02:00
@@ -613,15 +617,15 @@ public class ArmorStand extends LivingEntity {
2023-03-05 18:48:34 +01:00
}
2024-06-13 21:04:27 +02:00
- private void brokenByPlayer(ServerLevel world, DamageSource damageSource) {
+ private org.bukkit.event.entity.EntityDeathEvent brokenByPlayer(ServerLevel world, DamageSource damageSource) { // Paper
2023-03-14 20:24:52 +01:00
ItemStack itemstack = new ItemStack(Items.ARMOR_STAND);
2024-04-24 04:46:06 +02:00
itemstack.set(DataComponents.CUSTOM_NAME, this.getCustomName());
2023-10-27 01:34:58 +02:00
this.drops.add(org.bukkit.craftbukkit.inventory.CraftItemStack.asBukkitCopy(itemstack)); // CraftBukkit - add to drops
2024-06-13 21:04:27 +02:00
- this.brokenByAnything(world, damageSource);
+ return this.brokenByAnything(world, damageSource); // Paper
2023-03-05 18:48:34 +01:00
}
2024-06-13 21:04:27 +02:00
- private void brokenByAnything(ServerLevel world, DamageSource damageSource) {
+ private org.bukkit.event.entity.EntityDeathEvent brokenByAnything(ServerLevel world, DamageSource damageSource) { // Paper
2023-03-05 18:48:34 +01:00
this.playBrokenSound();
2024-06-13 21:04:27 +02:00
// this.dropAllDeathLoot(worldserver, damagesource); // CraftBukkit - moved down
2023-03-05 18:48:34 +01:00
2024-06-13 21:04:27 +02:00
@@ -643,7 +647,7 @@ public class ArmorStand extends LivingEntity {
2023-03-05 18:48:34 +01:00
this.armorItems.set(i, ItemStack.EMPTY);
}
}
2024-06-13 21:04:27 +02:00
- this.dropAllDeathLoot(world, damageSource); // CraftBukkit - moved from above
+ return this.dropAllDeathLoot(world, damageSource); // CraftBukkit - moved from above // Paper
2023-03-05 18:48:34 +01:00
}
2024-06-13 21:04:27 +02:00
@@ -770,7 +774,15 @@ public class ArmorStand extends LivingEntity {
2024-05-11 23:48:37 +02:00
}
2021-06-11 14:02:28 +02:00
2024-05-11 23:48:37 +02:00
public void kill(DamageSource damageSource) {
- org.bukkit.craftbukkit.event.CraftEventFactory.callEntityDeathEvent(this, (damageSource == null ? this.damageSources().genericKill() : damageSource), this.drops); // CraftBukkit - call event
+ // Paper start - make cancellable
+ this.kill(damageSource, true);
2023-06-27 06:22:49 +02:00
+ }
2024-05-11 23:48:37 +02:00
+ public void kill(DamageSource damageSource, boolean callEvent) {
2023-06-27 06:22:49 +02:00
+ if (callEvent) {
2024-05-11 23:48:37 +02:00
+ org.bukkit.event.entity.EntityDeathEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callEntityDeathEvent(this, (damageSource == null ? this.damageSources().genericKill() : damageSource), this.drops); // CraftBukkit - call event
+ if (event.isCancelled()) return;
+ }
2023-06-27 06:22:49 +02:00
+ // Paper end
Updated Upstream (Bukkit/CraftBukkit/Spigot) (#10277)
Upstream has released updates that appear to apply and compile correctly.
This update has not been tested by PaperMC and as with ANY update, please do your own testing
Bukkit Changes:
9a80d38c SPIGOT-336, SPIGOT-3366, SPIGOT-5768, SPIGOT-6409, SPIGOT-6861, PR-722: Add EntityRemoveEvent
258086d9 SPIGOT-7417, PR-967: Add Sign#getTargetSide and Sign#getAllowedEditor
ffaba051 SPIGOT-7584: Add missing Tag.ITEMS_NON_FLAMMABLE_WOOD
CraftBukkit Changes:
98b6c1ac7 SPIGOT-7589 Fix NullPointerException when bans expire
a2736ddb0 SPIGOT-336, SPIGOT-3366, SPIGOT-5768, SPIGOT-6409, SPIGOT-6861, PR-1008: Add EntityRemoveEvent
5bf12cb89 SPIGOT-7565: Throw a more descriptive error message when a developer tries to spawn an entity from a CraftBukkit class
76d95fe7e SPIGOT-7417, PR-1343: Add Sign#getTargetSide and Sign#getAllowedEditor
Spigot Changes:
e9ec5485 Rebuild patches
f1b62e0c Rebuild patches
2024-02-23 14:37:33 +01:00
this.remove(Entity.RemovalReason.KILLED, EntityRemoveEvent.Cause.DEATH); // CraftBukkit - add Bukkit remove cause
2024-05-11 23:48:37 +02:00
// CraftBukkit end
2022-06-08 02:15:06 +02:00
this.gameEvent(GameEvent.ENTITY_DIE);
2021-06-11 14:02:28 +02:00
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
2024-06-14 16:01:00 +02:00
index c0b92358c03d1724e8c134563f9ca98e69081c6a..a33fc9288165ec013cef6b90d72871f0058c0857 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
2024-06-14 16:01:00 +02:00
@@ -2516,7 +2516,14 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
2023-07-04 10:22:56 +02:00
@Override
2021-06-11 14:02:28 +02:00
public void sendHealthUpdate() {
2023-07-04 10:22:56 +02:00
FoodData foodData = this.getHandle().getFoodData();
- this.sendHealthUpdate(this.getScaledHealth(), foodData.getFoodLevel(), foodData.getSaturationLevel());
2021-06-11 14:02:28 +02:00
+ // Paper start - cancellable death event
2023-07-04 10:22:56 +02:00
+ ClientboundSetHealthPacket packet = new ClientboundSetHealthPacket(this.getScaledHealth(), foodData.getFoodLevel(), foodData.getSaturationLevel());
2021-06-11 14:02:28 +02:00
+ if (this.getHandle().queueHealthUpdatePacket) {
+ this.getHandle().queuedHealthUpdatePacket = packet;
+ } else {
+ this.getHandle().connection.send(packet);
+ }
+ // Paper end
}
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
2024-06-14 19:15:52 +02:00
index 635982e7c9bb650f72a987e2f866d6f91b75efcf..5254a97f94d24717bdbba1768fffc3d8d292f0fe 100644
2021-06-11 14:02:28 +02:00
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
2024-06-13 21:04:27 +02:00
@@ -893,9 +893,16 @@ public class CraftEventFactory {
2021-06-11 14:02:28 +02:00
CraftLivingEntity entity = (CraftLivingEntity) victim.getBukkitEntity();
2024-05-11 23:48:37 +02:00
CraftDamageSource bukkitDamageSource = new CraftDamageSource(damageSource);
2024-06-13 21:04:27 +02:00
EntityDeathEvent event = new EntityDeathEvent(entity, bukkitDamageSource, drops, victim.getExpReward(damageSource.getEntity()));
2021-06-11 14:02:28 +02:00
+ populateFields(victim, event); // Paper - make cancellable
CraftWorld world = (CraftWorld) entity.getWorld();
Bukkit.getServer().getPluginManager().callEvent(event);
+ // Paper start - make cancellable
+ if (event.isCancelled()) {
+ return event;
+ }
+ playDeathSound(victim, event);
+ // Paper end
victim.expToDrop = event.getDroppedExp();
for (org.bukkit.inventory.ItemStack stack : event.getDrops()) {
2024-06-13 21:04:27 +02:00
@@ -913,8 +920,15 @@ public class CraftEventFactory {
PlayerDeathEvent event = new PlayerDeathEvent(entity, bukkitDamageSource, drops, victim.getExpReward(damageSource.getEntity()), 0, deathMessage);
2021-06-11 14:02:28 +02:00
event.setKeepInventory(keepInventory);
2021-08-13 19:08:34 +02:00
event.setKeepLevel(victim.keepLevel); // SPIGOT-2222: pre-set keepLevel
2021-06-11 14:02:28 +02:00
+ populateFields(victim, event); // Paper - make cancellable
org.bukkit.World world = entity.getWorld();
Bukkit.getServer().getPluginManager().callEvent(event);
+ // Paper start - make cancellable
+ if (event.isCancelled()) {
+ return event;
+ }
+ playDeathSound(victim, event);
+ // Paper end
victim.keepLevel = event.getKeepLevel();
victim.newLevel = event.getNewLevel();
2024-06-13 21:04:27 +02:00
@@ -931,6 +945,31 @@ public class CraftEventFactory {
2021-06-11 14:02:28 +02:00
return event;
}
+ // Paper start - helper methods for making death event cancellable
+ // Add information to death event
+ private static void populateFields(net.minecraft.world.entity.LivingEntity victim, EntityDeathEvent event) {
+ event.setReviveHealth(event.getEntity().getAttribute(org.bukkit.attribute.Attribute.GENERIC_MAX_HEALTH).getValue());
+ event.setShouldPlayDeathSound(!victim.silentDeath && !victim.isSilent());
2021-10-03 03:42:30 +02:00
+ net.minecraft.sounds.SoundEvent soundEffect = victim.getDeathSound();
2023-09-23 04:06:03 +02:00
+ event.setDeathSound(soundEffect != null ? org.bukkit.craftbukkit.CraftSound.minecraftToBukkit(soundEffect) : null);
2021-06-11 14:02:28 +02:00
+ event.setDeathSoundCategory(org.bukkit.SoundCategory.valueOf(victim.getSoundSource().name()));
2021-06-16 00:24:12 +02:00
+ event.setDeathSoundVolume(victim.getSoundVolume());
2021-06-13 06:03:02 +02:00
+ event.setDeathSoundPitch(victim.getVoicePitch());
2021-06-11 14:02:28 +02:00
+ }
+
+ // Play death sound manually
+ private static void playDeathSound(net.minecraft.world.entity.LivingEntity victim, EntityDeathEvent event) {
+ if (event.shouldPlayDeathSound() && event.getDeathSound() != null && event.getDeathSoundCategory() != null) {
+ net.minecraft.world.entity.player.Player source = victim instanceof net.minecraft.world.entity.player.Player ? (net.minecraft.world.entity.player.Player) victim : null;
+ double x = event.getEntity().getLocation().getX();
+ double y = event.getEntity().getLocation().getY();
+ double z = event.getEntity().getLocation().getZ();
2023-09-23 04:06:03 +02:00
+ net.minecraft.sounds.SoundEvent soundEffect = org.bukkit.craftbukkit.CraftSound.bukkitToMinecraft(event.getDeathSound());
2021-06-13 06:03:02 +02:00
+ net.minecraft.sounds.SoundSource soundCategory = net.minecraft.sounds.SoundSource.valueOf(event.getDeathSoundCategory().name());
2023-06-08 00:28:06 +02:00
+ victim.level().playSound(source, x, y, z, soundEffect, soundCategory, event.getDeathSoundVolume(), event.getDeathSoundPitch());
2021-06-11 14:02:28 +02:00
+ }
+ }
+ // Paper end
/**
* Server methods
*/