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
2023-12-29 20:57:32 +01:00
index 0100aafc999cadcfa7a904a812355502379c418d..b2864db793e8d764cf06229d7868fcf80247f938 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
2023-12-05 23:12:48 +01:00
@@ -248,6 +248,10 @@ public class ServerPlayer extends Player {
2023-09-22 00:26:51 +02:00
private int containerCounter;
2021-06-11 14:02:28 +02:00
public boolean wonGame;
private int containerUpdateDelay; // Paper
+ // Paper start - cancellable death event
+ public boolean queueHealthUpdatePacket = false;
+ public net.minecraft.network.protocol.game.ClientboundSetHealthPacket queuedHealthUpdatePacket;
+ // Paper end
// CraftBukkit start
public String displayName;
2023-12-29 20:57:32 +01:00
@@ -854,7 +858,7 @@ public class ServerPlayer extends Player {
@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()) {
@@ -882,6 +886,16 @@ public class ServerPlayer extends Player {
2021-08-13 19:08:34 +02:00
String deathmessage = defaultMessage.getString();
this.keepLevel = keepInventory; // SPIGOT-2222: pre-set keepLevel
2023-12-29 21:28:58 +01:00
org.bukkit.event.entity.PlayerDeathEvent event = CraftEventFactory.callPlayerDeathEvent(this, 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) {
2023-12-29 20:57:32 +01:00
@@ -1030,8 +1044,17 @@ public class ServerPlayer extends 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
2023-12-25 23:51:56 +01:00
index 486f22469c3cf7ce302c10072da3f96aa3be0ef3..4df479842f482a8a6a5979c204b6882cd3603e93 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
2023-12-05 23:12:48 +01:00
@@ -259,6 +259,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() {
2023-12-05 23:12:48 +01:00
@@ -1522,13 +1523,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)) {
- SoundEvent soundeffect = this.getDeathSound();
2021-06-13 06:03:02 +02:00
-
2021-06-11 14:02:28 +02:00
- if (flag1 && soundeffect != null) {
- this.playSound(soundeffect, this.getSoundVolume(), this.getVoicePitch());
- }
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);
2023-12-05 23:12:48 +01:00
@@ -1679,7 +1679,7 @@ public abstract class LivingEntity extends Entity implements Attackable {
2021-06-13 06:03:02 +02:00
if (!this.isRemoved() && !this.dead) {
2022-06-08 02:15:06 +02:00
Entity entity = damageSource.getEntity();
2021-06-11 14:02:28 +02:00
LivingEntity entityliving = this.getKillCredit();
2021-06-13 06:03:02 +02: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
}
2023-12-05 23:12:48 +01:00
@@ -1691,24 +1691,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
2023-03-23 22:57:03 +01:00
+
2021-06-11 14:02:28 +02:00
+ // Paper start
2022-06-08 02:15:06 +02:00
+ org.bukkit.event.entity.EntityDeathEvent deathEvent = this.dropAllDeathLoot(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
2023-06-12 19:03:51 +02:00
- if (entity == null || entity.killedEntity(worldserver, this)) {
2021-06-11 14:02:28 +02:00
+ if (this.isSleeping()) {
+ this.stopSleeping();
+ }
2023-06-12 19:03:51 +02:00
+
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
+ }
+
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);
- this.dropAllDeathLoot(damageSource);
- this.createWitherRose(entityliving);
2023-06-12 19:03:51 +02:00
+ } else {
+ this.dead = false;
+ this.setHealth((float) deathEvent.getReviveHealth());
2023-03-23 22:57:03 +01:00
}
2023-06-07 22:19:14 +02:00
- this.level().broadcastEntityEvent(this, (byte) 3);
2021-06-11 14:02:28 +02:00
+ // Paper end
2022-06-08 02:15:06 +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
2023-06-07 22:19:14 +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
}
}
2023-12-05 23:12:48 +01:00
@@ -1716,7 +1751,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();
2023-12-05 23:12:48 +01:00
@@ -1745,7 +1780,11 @@ public abstract class LivingEntity extends Entity implements Attackable {
2021-06-11 14:02:28 +02:00
}
}
- protected void dropAllDeathLoot(DamageSource source) {
2022-02-18 19:16:41 +01:00
+ // Paper start
+ protected boolean clearEquipmentSlots = true;
+ protected Set<EquipmentSlot> clearedEquipmentSlots = new java.util.HashSet<>();
+ protected org.bukkit.event.entity.EntityDeathEvent dropAllDeathLoot(DamageSource source) {
+ // Paper end
2021-06-13 06:03:02 +02:00
Entity entity = source.getEntity();
2021-06-11 14:02:28 +02:00
int i;
2023-12-05 23:12:48 +01:00
@@ -1760,18 +1799,27 @@ public abstract class LivingEntity extends Entity implements Attackable {
2021-12-08 19:25:57 +01:00
this.dropEquipment(); // CraftBukkit - from below
2023-06-07 22:19:14 +02:00
if (this.shouldDropLoot() && this.level().getGameRules().getBoolean(GameRules.RULE_DOMOBLOOT)) {
2021-12-08 19:25:57 +01:00
this.dropFromLootTable(source, flag);
2022-02-18 19:16:41 +01:00
+ // Paper start
+ final boolean prev = this.clearEquipmentSlots;
+ this.clearEquipmentSlots = false;
+ this.clearedEquipmentSlots.clear();
+ // Paper end
2021-06-13 06:03:02 +02:00
this.dropCustomDeathLoot(source, i, 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
- CraftEventFactory.callEntityDeathEvent(this, this.drops);
+ org.bukkit.event.entity.EntityDeathEvent deathEvent = CraftEventFactory.callEntityDeathEvent(this, this.drops); // Paper
+ this.postDeathDropItems(deathEvent); // Paper
this.drops = new ArrayList<>();
// CraftBukkit end
// this.dropInventory();// CraftBukkit - moved up
this.dropExperience();
+ 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
// CraftBukkit start
public int getExpReward() {
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
2023-12-05 23:12:48 +01:00
index a6b89a12471a8c7277b7a5aa50447ff275a667c6..efef5df4cf720058c0b07ef795cfa6a567113811 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
2023-12-05 23:12:48 +01:00
@@ -1075,7 +1075,13 @@ public abstract class Mob extends LivingEntity implements Targeting {
2021-12-08 19:25:57 +01:00
}
this.spawnAtLocation(itemstack);
+ if (this.clearEquipmentSlots) { // Paper
this.setItemSlot(enumitemslot, ItemStack.EMPTY);
2022-02-18 19:16:41 +01:00
+ // 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
2023-12-05 23:12:48 +01:00
index 8670d8b2a08e96df787a91f36c48df8b345080dc..6124209f50300eeaab45b66c2f1a5b2944119450 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
2023-09-22 00:26:51 +02:00
@@ -712,15 +712,25 @@ public class Fox extends Animal implements VariantHolder<Fox.Type> {
2021-06-11 14:02:28 +02:00
}
@Override
- protected void dropAllDeathLoot(DamageSource source) {
- ItemStack itemstack = this.getItemBySlot(EquipmentSlot.MAINHAND);
2021-06-13 06:03:02 +02:00
+ // Paper start - Cancellable death event
+ protected org.bukkit.event.entity.EntityDeathEvent dropAllDeathLoot(DamageSource source) {
+ ItemStack itemstack = this.getItemBySlot(EquipmentSlot.MAINHAND).copy(); // Paper - modified by supercall
2021-06-11 14:02:28 +02:00
+
2021-06-13 06:03:02 +02:00
+ org.bukkit.event.entity.EntityDeathEvent deathEvent = super.dropAllDeathLoot(source);
2021-06-11 14:02:28 +02:00
+
+ // Below is code to drop
+
+ if (deathEvent == null || deathEvent.isCancelled()) {
+ return deathEvent;
+ }
+ // Paper end
if (!itemstack.isEmpty()) {
this.spawnAtLocation(itemstack);
this.setItemSlot(EquipmentSlot.MAINHAND, ItemStack.EMPTY);
}
- super.dropAllDeathLoot(source);
+ return deathEvent; // Paper
}
2023-09-22 00:26:51 +02:00
@Override
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
2023-09-22 19:59:56 +02:00
index bb399f775a5530a01f59332848c8ab9b8eceb2b5..14edfe103e61024b569f33de0b6608f39e749319 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
2023-09-22 00:26:51 +02:00
@@ -70,11 +70,19 @@ 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
}
- this.setChest(false);
+ //this.setCarryingChest(false); // Paper - moved to post death logic
}
}
+ // Paper start
+ protected void postDeathDropItems(org.bukkit.event.entity.EntityDeathEvent event) {
+ if (this.hasChest() && (event == null || !event.isCancelled())) {
+ this.setChest(false);
+ }
+ }
+ // Paper end
+
@Override
2021-06-13 06:03:02 +02:00
public void addAdditionalSaveData(CompoundTag nbt) {
super.addAdditionalSaveData(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
2023-12-06 20:40:37 +01:00
index 948ba97e318506dad96e59121297b5bf8340d2e6..810bead2f19de70786027b190137f743a0c40ee7 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
2023-09-22 00:26:51 +02:00
@@ -492,8 +492,10 @@ public class ArmorStand extends LivingEntity {
2023-06-27 06:22:49 +02:00
}
// CraftBukkit end
if (source.is(DamageTypeTags.IS_EXPLOSION)) {
- this.brokenByAnything(source);
- this.kill();
+ // Paper start - avoid duplicate event call
+ org.bukkit.event.entity.EntityDeathEvent event = this.brokenByAnything(source);
+ if (!event.isCancelled()) this.kill(false);
+ // Paper end
return false;
} else if (source.is(DamageTypeTags.IGNITES_ARMOR_STANDS)) {
if (this.isOnFire()) {
2023-12-05 23:12:48 +01:00
@@ -536,9 +538,9 @@ public class ArmorStand extends LivingEntity {
2023-03-14 20:24:52 +01:00
this.gameEvent(GameEvent.ENTITY_DAMAGE, source.getEntity());
this.lastHit = i;
} else {
- this.brokenByPlayer(source);
+ org.bukkit.event.entity.EntityDeathEvent event = this.brokenByPlayer(source); // Paper
this.showBreakingParticles();
2023-06-27 06:22:49 +02:00
- this.discard(); // CraftBukkit - SPIGOT-4890: remain as this.discard() since above damagesource method will call death event
+ if (!event.isCancelled()) this.kill(false); // Paper - we still need to kill to follow vanilla logic (emit the game event etc...)
2023-03-14 20:24:52 +01:00
}
2023-06-27 06:22:49 +02:00
return true;
2023-12-05 23:12:48 +01:00
@@ -590,8 +592,10 @@ public class ArmorStand extends LivingEntity {
2023-06-27 06:22:49 +02:00
f1 -= amount;
if (f1 <= 0.5F) {
- this.brokenByAnything(damageSource);
- this.kill();
+ // Paper start - avoid duplicate event call
+ org.bukkit.event.entity.EntityDeathEvent event = this.brokenByAnything(damageSource);
+ if (!event.isCancelled()) this.kill(false);
+ // Paper end
} else {
this.setHealth(f1);
this.gameEvent(GameEvent.ENTITY_DAMAGE, damageSource.getEntity());
2023-12-05 23:12:48 +01:00
@@ -599,7 +603,7 @@ public class ArmorStand extends LivingEntity {
2023-03-05 18:48:34 +01:00
}
- private void brokenByPlayer(DamageSource damageSource) {
2023-03-14 20:24:52 +01:00
+ private org.bukkit.event.entity.EntityDeathEvent brokenByPlayer(DamageSource damageSource) { // Paper
ItemStack itemstack = new ItemStack(Items.ARMOR_STAND);
if (this.hasCustomName()) {
2023-12-05 23:12:48 +01:00
@@ -607,10 +611,10 @@ public class ArmorStand extends LivingEntity {
2023-03-14 20:24:52 +01:00
}
2023-10-27 01:34:58 +02:00
this.drops.add(org.bukkit.craftbukkit.inventory.CraftItemStack.asBukkitCopy(itemstack)); // CraftBukkit - add to drops
2023-03-05 18:48:34 +01:00
- this.brokenByAnything(damageSource);
+ return this.brokenByAnything(damageSource); // Paper
}
- private void brokenByAnything(DamageSource damageSource) {
+ private org.bukkit.event.entity.EntityDeathEvent brokenByAnything(DamageSource damageSource) { // Paper
this.playBrokenSound();
// this.dropAllDeathLoot(damagesource); // CraftBukkit - moved down
2023-12-05 23:12:48 +01:00
@@ -632,7 +636,7 @@ public class ArmorStand extends LivingEntity {
2023-03-05 18:48:34 +01:00
this.armorItems.set(i, ItemStack.EMPTY);
}
}
- this.dropAllDeathLoot(damageSource); // CraftBukkit - moved from above
+ return this.dropAllDeathLoot(damageSource); // CraftBukkit - moved from above // Paper
}
2023-12-05 23:12:48 +01:00
@@ -759,7 +763,16 @@ public class ArmorStand extends LivingEntity {
2021-06-11 14:02:28 +02:00
@Override
public void kill() {
2023-10-27 01:34:58 +02:00
- org.bukkit.craftbukkit.event.CraftEventFactory.callEntityDeathEvent(this, this.drops); // CraftBukkit - call event
2023-06-27 06:22:49 +02:00
+ // Paper start
+ kill(true);
+ }
+
+ public void kill(boolean callEvent) {
+ if (callEvent) {
+ // Paper end
2023-10-27 01:34:58 +02:00
+ org.bukkit.event.entity.EntityDeathEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callEntityDeathEvent(this, this.drops); // CraftBukkit - call event // Paper - make cancellable
2021-06-11 14:02:28 +02:00
+ if (event.isCancelled()) return; // Paper - make cancellable
2023-06-27 06:22:49 +02:00
+ } // Paper
2021-06-13 06:03:02 +02:00
this.remove(Entity.RemovalReason.KILLED);
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
2023-12-29 20:57:32 +01:00
index bd6ac69d97ec1c12bf73e9ef07d39a484a39aad1..ce73a8c652cf95bae62808fc27db2ab852691fd8 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
2023-12-28 00:22:44 +01:00
@@ -2408,7 +2408,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
2023-12-29 21:28:58 +01:00
index 5c81eb10e92142d8d4907751edc8c01659d2c62e..10c701eb269b02c763d32c6a0be8fe7b843805c5 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
Updated Upstream (Bukkit/CraftBukkit) (#10034)
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:
f29cb801 Separate checkstyle-suppressions file is not required
86f99bbe SPIGOT-7540, PR-946: Add ServerTickManager API
d4119585 SPIGOT-6903, PR-945: Add BlockData#getMapColor
b7a2ed41 SPIGOT-7530, PR-947: Add Player#removeResourcePack
9dd56255 SPIGOT-7527, PR-944: Add WindCharge#explode()
994a6163 Attempt upgrade of resolver libraries
CraftBukkit Changes:
b3b43a6ad Add Checkstyle check for unused imports
13fb3358e SPIGOT-7544: Scoreboard#getEntries() doesn't get entries but class names
3dda99c06 SPIGOT-7540, PR-1312: Add ServerTickManager API
2ab4508c0 SPIGOT-6903, PR-1311: Add BlockData#getMapColor
1dbdbbed4 PR-1238: Remove unnecessary sign ticking
659728d2a MC-264285, SPIGOT-7439, PR-1237: Fix unbreakable flint and steel is completely consumed while igniting creeper
e37e29ce0 Increase outdated build delay
c00438b39 SPIGOT-7530, PR-1313: Add Player#removeResourcePack
492dd80ce SPIGOT-7527, PR-1310: Add WindCharge#explode()
e11fbb9d7 Upgrade MySQL driver
9f3a0bd2a Attempt upgrade of resolver libraries
60d16d7ca PR-1306: Centralize Bukkit and Minecraft entity conversion
Spigot Changes:
06d602e7 Rebuild patches
2023-12-17 03:09:28 +01:00
@@ -890,9 +890,16 @@ public class CraftEventFactory {
2021-06-11 14:02:28 +02:00
public static EntityDeathEvent callEntityDeathEvent(net.minecraft.world.entity.LivingEntity victim, List<org.bukkit.inventory.ItemStack> drops) {
CraftLivingEntity entity = (CraftLivingEntity) victim.getBukkitEntity();
EntityDeathEvent event = new EntityDeathEvent(entity, drops, victim.getExpReward());
+ 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()) {
Updated Upstream (Bukkit/CraftBukkit) (#10034)
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:
f29cb801 Separate checkstyle-suppressions file is not required
86f99bbe SPIGOT-7540, PR-946: Add ServerTickManager API
d4119585 SPIGOT-6903, PR-945: Add BlockData#getMapColor
b7a2ed41 SPIGOT-7530, PR-947: Add Player#removeResourcePack
9dd56255 SPIGOT-7527, PR-944: Add WindCharge#explode()
994a6163 Attempt upgrade of resolver libraries
CraftBukkit Changes:
b3b43a6ad Add Checkstyle check for unused imports
13fb3358e SPIGOT-7544: Scoreboard#getEntries() doesn't get entries but class names
3dda99c06 SPIGOT-7540, PR-1312: Add ServerTickManager API
2ab4508c0 SPIGOT-6903, PR-1311: Add BlockData#getMapColor
1dbdbbed4 PR-1238: Remove unnecessary sign ticking
659728d2a MC-264285, SPIGOT-7439, PR-1237: Fix unbreakable flint and steel is completely consumed while igniting creeper
e37e29ce0 Increase outdated build delay
c00438b39 SPIGOT-7530, PR-1313: Add Player#removeResourcePack
492dd80ce SPIGOT-7527, PR-1310: Add WindCharge#explode()
e11fbb9d7 Upgrade MySQL driver
9f3a0bd2a Attempt upgrade of resolver libraries
60d16d7ca PR-1306: Centralize Bukkit and Minecraft entity conversion
Spigot Changes:
06d602e7 Rebuild patches
2023-12-17 03:09:28 +01:00
@@ -909,8 +916,15 @@ public class CraftEventFactory {
2023-12-29 21:28:58 +01:00
PlayerDeathEvent event = new PlayerDeathEvent(entity, drops, victim.getExpReward(), 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();
Updated Upstream (Bukkit/CraftBukkit) (#10034)
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:
f29cb801 Separate checkstyle-suppressions file is not required
86f99bbe SPIGOT-7540, PR-946: Add ServerTickManager API
d4119585 SPIGOT-6903, PR-945: Add BlockData#getMapColor
b7a2ed41 SPIGOT-7530, PR-947: Add Player#removeResourcePack
9dd56255 SPIGOT-7527, PR-944: Add WindCharge#explode()
994a6163 Attempt upgrade of resolver libraries
CraftBukkit Changes:
b3b43a6ad Add Checkstyle check for unused imports
13fb3358e SPIGOT-7544: Scoreboard#getEntries() doesn't get entries but class names
3dda99c06 SPIGOT-7540, PR-1312: Add ServerTickManager API
2ab4508c0 SPIGOT-6903, PR-1311: Add BlockData#getMapColor
1dbdbbed4 PR-1238: Remove unnecessary sign ticking
659728d2a MC-264285, SPIGOT-7439, PR-1237: Fix unbreakable flint and steel is completely consumed while igniting creeper
e37e29ce0 Increase outdated build delay
c00438b39 SPIGOT-7530, PR-1313: Add Player#removeResourcePack
492dd80ce SPIGOT-7527, PR-1310: Add WindCharge#explode()
e11fbb9d7 Upgrade MySQL driver
9f3a0bd2a Attempt upgrade of resolver libraries
60d16d7ca PR-1306: Centralize Bukkit and Minecraft entity conversion
Spigot Changes:
06d602e7 Rebuild patches
2023-12-17 03:09:28 +01:00
@@ -927,6 +941,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
*/