2020-05-06 17:48:49 +08:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2018-09-08 08:14:48 +08:00
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.
diff --git a/src/main/java/net/minecraft/server/CombatTracker.java b/src/main/java/net/minecraft/server/CombatTracker.java
2020-06-25 22:09:55 +08:00
index 6281f7900afab3ef1c9ba3c034b91cbfa1900f50..3c2f8407906879c8dca07b538f59f4bbd31b278f 100644
2018-09-08 08:14:48 +08:00
--- a/src/main/java/net/minecraft/server/CombatTracker.java
+++ b/src/main/java/net/minecraft/server/CombatTracker.java
2020-06-25 22:09:55 +08:00
@@ -192,6 +192,7 @@ public class CombatTracker {
2018-09-08 08:14:48 +08:00
this.h = null;
}
+ public void reset() { this.g(); } // Paper - OBFHELPER
public void g() {
int i = this.f ? 300 : 100;
2019-01-01 11:15:55 +08:00
2018-09-08 08:14:48 +08:00
diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
2020-06-30 13:20:29 +08:00
index 55f8863f823836065f4de8240bfeb490bd391a55..a9664f03b8b7d2c12565b8c08891c7021aa5482b 100644
2018-09-08 08:14:48 +08:00
--- a/src/main/java/net/minecraft/server/Entity.java
+++ b/src/main/java/net/minecraft/server/Entity.java
2020-06-26 09:58:00 +08:00
@@ -1460,6 +1460,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
2018-09-08 08:14:48 +08:00
}
2020-06-25 22:09:55 +08:00
// CraftBukkit end
2018-09-08 08:14:48 +08:00
+ public void runKillTrigger(Entity entity, int kills, DamageSource damageSource) { this.a(entity, kills, damageSource); } // Paper - OBFHELPER
public void a(Entity entity, int i, DamageSource damagesource) {
if (entity instanceof EntityPlayer) {
CriterionTriggers.c.a((EntityPlayer) entity, this, damagesource);
2020-06-30 13:20:29 +08:00
@@ -2349,6 +2350,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
2018-09-09 07:12:58 +08:00
this.fallDistance = 0.0F;
2018-09-08 08:14:48 +08:00
}
2020-06-25 22:09:55 +08:00
+ public void onKill(EntityLiving entityLiving) { this.a_(entityLiving); } // Paper - OBFHELPER
public void a_(EntityLiving entityliving) {}
2018-09-08 08:14:48 +08:00
2019-12-13 00:20:43 +08:00
protected void k(double d0, double d1, double d2) {
2018-09-08 08:14:48 +08:00
diff --git a/src/main/java/net/minecraft/server/EntityArmorStand.java b/src/main/java/net/minecraft/server/EntityArmorStand.java
2020-06-26 20:04:38 +08:00
index d5c09152acc93b25d626284071599afdbd76b709..51e9f4a6e09474a7489d2872a800308ee3f02e46 100644
2018-09-08 08:14:48 +08:00
--- a/src/main/java/net/minecraft/server/EntityArmorStand.java
+++ b/src/main/java/net/minecraft/server/EntityArmorStand.java
2020-06-26 20:04:38 +08:00
@@ -701,7 +701,8 @@ public class EntityArmorStand extends EntityLiving {
2018-09-08 08:14:48 +08:00
2019-05-05 19:12:32 +08:00
@Override
2018-09-08 08:14:48 +08:00
public void killEntity() {
- org.bukkit.craftbukkit.event.CraftEventFactory.callEntityDeathEvent(this, drops); // CraftBukkit - call event
+ org.bukkit.event.entity.EntityDeathEvent event = org.bukkit.craftbukkit.event.CraftEventFactory.callEntityDeathEvent(this, drops); // CraftBukkit - call event // Paper - make cancellable
+ if (event.isCancelled()) return; // Paper - make cancellable
this.die();
}
2019-05-06 15:20:16 +08:00
diff --git a/src/main/java/net/minecraft/server/EntityFox.java b/src/main/java/net/minecraft/server/EntityFox.java
2020-06-25 22:09:55 +08:00
index 95874526516291607a44ae2213d4d6d65edfe18d..e6275a9fe6cd988fe4661966fd48149c3e73408c 100644
2019-05-06 15:20:16 +08:00
--- a/src/main/java/net/minecraft/server/EntityFox.java
+++ b/src/main/java/net/minecraft/server/EntityFox.java
2020-06-25 22:09:55 +08:00
@@ -576,15 +576,25 @@ public class EntityFox extends EntityAnimal {
2019-05-06 15:20:16 +08:00
}
@Override
- protected void d(DamageSource damagesource) {
2019-05-07 23:20:32 +08:00
- ItemStack itemstack = this.getEquipment(EnumItemSlot.MAINHAND);
2019-05-06 15:20:16 +08:00
+ protected org.bukkit.event.entity.EntityDeathEvent d(DamageSource damagesource) { // Paper
2019-05-07 23:20:32 +08:00
+ ItemStack itemstack = this.getEquipment(EnumItemSlot.MAINHAND).cloneItemStack(); // Paper
+
2019-05-07 20:00:51 +08:00
+ // Paper start - Cancellable death event
2019-08-06 11:31:38 +08:00
+ org.bukkit.event.entity.EntityDeathEvent deathEvent = super.d(damagesource);
2019-05-07 20:00:51 +08:00
+
+ // Below is code to drop
+
+ if (deathEvent == null || deathEvent.isCancelled()) {
+ return deathEvent;
+ }
+ // Paper end
2019-05-07 23:20:32 +08:00
2019-05-06 15:20:16 +08:00
if (!itemstack.isEmpty()) {
2019-05-07 20:00:51 +08:00
this.a(itemstack);
2020-06-25 22:09:55 +08:00
this.setSlot(EnumItemSlot.MAINHAND, ItemStack.b);
2019-05-06 15:20:16 +08:00
}
- super.d(damagesource);
2019-05-07 20:00:51 +08:00
+ return deathEvent; // Paper
2019-05-06 15:20:16 +08:00
}
public static boolean a(EntityFox entityfox, EntityLiving entityliving) {
2019-08-06 00:35:40 +08:00
diff --git a/src/main/java/net/minecraft/server/EntityHorseChestedAbstract.java b/src/main/java/net/minecraft/server/EntityHorseChestedAbstract.java
2020-06-25 22:09:55 +08:00
index c2a3bd8f25e91f79723074d93f6a646a8ce76d8c..4934e71225fe1242615660a379e797e212040ed3 100644
2019-08-06 00:35:40 +08:00
--- a/src/main/java/net/minecraft/server/EntityHorseChestedAbstract.java
+++ b/src/main/java/net/minecraft/server/EntityHorseChestedAbstract.java
2020-06-25 22:09:55 +08:00
@@ -50,11 +50,19 @@ public abstract class EntityHorseChestedAbstract extends EntityHorseAbstract {
2019-08-06 00:35:40 +08:00
this.a((IMaterial) Blocks.CHEST);
}
- this.setCarryingChest(false);
+ //this.setCarryingChest(false); // Paper - moved to post death logic
}
}
+ // Paper start
+ protected void postDeathDropItems(org.bukkit.event.entity.EntityDeathEvent event) {
+ if (this.isCarryingChest() && (event == null || !event.isCancelled())) {
+ this.setCarryingChest(false);
+ }
+ }
+ // Paper end
+
@Override
2020-06-25 22:09:55 +08:00
public void saveData(NBTTagCompound nbttagcompound) {
super.saveData(nbttagcompound);
2018-09-08 08:14:48 +08:00
diff --git a/src/main/java/net/minecraft/server/EntityLiving.java b/src/main/java/net/minecraft/server/EntityLiving.java
2020-07-23 07:35:44 +08:00
index ac5f11a2f5598584ff112a208bfd16aeb574e580..fb53d8e178b5a8e229036f90fafb48a8a142125e 100644
2018-09-08 08:14:48 +08:00
--- a/src/main/java/net/minecraft/server/EntityLiving.java
+++ b/src/main/java/net/minecraft/server/EntityLiving.java
2020-06-25 22:09:55 +08:00
@@ -95,7 +95,7 @@ public abstract class EntityLiving extends Entity {
protected float aS;
2019-12-13 00:20:43 +08:00
protected float aT;
protected float aU;
2020-06-25 22:09:55 +08:00
- protected int aV;
+ protected int aV; protected int getKillCount() { return this.aV; } // Paper - OBFHELPER
2018-09-08 08:14:48 +08:00
public float lastDamage;
2019-05-05 19:12:32 +08:00
protected boolean jumping;
2020-06-25 22:09:55 +08:00
public float aY;
@@ -139,6 +139,7 @@ public abstract class EntityLiving extends Entity {
public Set<UUID> collidableExemptions = new HashSet<>();
2018-09-08 08:14:48 +08:00
public boolean canPickUpLoot;
2018-09-09 07:12:58 +08:00
public org.bukkit.craftbukkit.entity.CraftLivingEntity getBukkitLivingEntity() { return (org.bukkit.craftbukkit.entity.CraftLivingEntity) super.getBukkitEntity(); } // Paper
2018-09-08 08:14:48 +08:00
+ public boolean silentDeath = false; // Paper - mark entity as dying silently for cancellable death event
@Override
public float getBukkitYaw() {
2020-06-27 05:33:49 +08:00
@@ -1248,13 +1249,17 @@ public abstract class EntityLiving extends Entity {
2020-06-25 22:09:55 +08:00
if (knockbackCancelled) this.world.broadcastEntityEffect(this, (byte) 2); // Paper - Disable explosion knockback
if (this.dk()) {
2019-05-05 19:12:32 +08:00
if (!this.f(damagesource)) {
- SoundEffect soundeffect = this.getSoundDeath();
2018-09-09 07:12:58 +08:00
+ // Paper start - moved into CraftEventFactory event caller for cancellable death event
2019-05-05 19:12:32 +08:00
+ //SoundEffect soundeffect = this.getSoundDeath();
2018-09-09 07:12:58 +08:00
- if (flag1 && soundeffect != null) {
2020-06-25 22:09:55 +08:00
- this.playSound(soundeffect, this.getSoundVolume(), this.dG());
2018-09-09 07:12:58 +08:00
- }
2019-12-13 00:20:43 +08:00
+// if (flag1 && soundeffect != null) {
2020-06-25 22:09:55 +08:00
+// this.playSound(soundeffect, this.getSoundVolume(), this.dG());
2019-12-13 00:20:43 +08:00
+// }
2018-09-09 07:12:58 +08:00
+ this.silentDeath = !flag1; // mark entity as dying silently
+ // Paper end
this.die(damagesource);
+ this.silentDeath = false; // Paper - cancellable death event - reset to default
}
} else if (flag1) {
this.c(damagesource);
2020-06-27 05:33:49 +08:00
@@ -1392,6 +1397,7 @@ public abstract class EntityLiving extends Entity {
2018-09-08 08:14:48 +08:00
Entity entity = damagesource.getEntity();
2019-05-05 19:12:32 +08:00
EntityLiving entityliving = this.getKillingEntity();
2018-09-08 08:14:48 +08:00
2019-06-25 09:47:58 +08:00
+ /* // Paper - move down to make death event cancellable
2020-06-25 22:09:55 +08:00
if (this.aV >= 0 && entityliving != null) {
entityliving.a(this, this.aV, damagesource);
2019-06-25 09:47:58 +08:00
}
2020-06-27 05:33:49 +08:00
@@ -1403,16 +1409,36 @@ public abstract class EntityLiving extends Entity {
2019-06-25 09:47:58 +08:00
if (this.isSleeping()) {
2019-12-13 00:20:43 +08:00
this.entityWakeup();
2019-06-25 09:47:58 +08:00
}
+ */ // Paper
2018-12-17 13:18:06 +08:00
this.killed = true;
2018-09-08 08:14:48 +08:00
- this.getCombatTracker().g();
2020-06-25 22:09:55 +08:00
+ //this.getCombatTracker().g(); // Paper
2018-09-08 08:14:48 +08:00
if (!this.world.isClientSide) {
2019-05-05 19:12:32 +08:00
- this.d(damagesource);
+ org.bukkit.event.entity.EntityDeathEvent deathEvent = this.d(damagesource);
2019-05-07 20:00:51 +08:00
+ if (deathEvent == null || !deathEvent.isCancelled()) {
2019-05-05 19:12:32 +08:00
+ if (this.getKillCount() >= 0 && entityliving != null) {
+ entityliving.runKillTrigger(this, this.getKillCount(), damagesource);
+ }
+ if (entity != null) {
+ entity.onKill(this);
+ }
+ if (this.isSleeping()) {
2019-12-13 09:18:18 +08:00
+ this.entityWakeup();
2019-05-05 19:12:32 +08:00
+ }
+ this.getCombatTracker().reset();
+ } else {
+ this.killed = false;
+ this.setHealth((float) deathEvent.getReviveHealth());
2018-09-08 08:14:48 +08:00
+ }
2019-05-05 19:12:32 +08:00
+ // Paper end
2018-09-08 08:14:48 +08:00
+
2020-06-25 22:09:55 +08:00
this.g(entityliving);
2019-08-06 00:35:40 +08:00
}
+ if (this.killed) { // Paper
this.world.broadcastEntityEffect(this, (byte) 3);
this.setPose(EntityPose.DYING);
+ } // Paper
2018-09-08 08:14:48 +08:00
}
}
2020-06-27 05:33:49 +08:00
@@ -1420,7 +1446,7 @@ public abstract class EntityLiving extends Entity {
2019-12-13 00:20:43 +08:00
if (!this.world.isClientSide) {
boolean flag = false;
- if (entityliving instanceof EntityWither) {
+ if (this.killed && entityliving instanceof EntityWither) { // Paper
if (this.world.getGameRules().getBoolean(GameRules.MOB_GRIEFING)) {
2020-06-25 22:09:55 +08:00
BlockPosition blockposition = this.getChunkCoordinates();
2019-12-13 00:20:43 +08:00
IBlockData iblockdata = Blocks.WITHER_ROSE.getBlockData();
2020-06-27 05:33:49 +08:00
@@ -1441,7 +1467,8 @@ public abstract class EntityLiving extends Entity {
2019-12-13 00:20:43 +08:00
}
}
2019-05-05 19:12:32 +08:00
- protected void d(DamageSource damagesource) {
2019-06-25 09:47:58 +08:00
+ protected org.bukkit.event.entity.EntityDeathEvent processDeath(DamageSource damagesource) { return d(damagesource); } // Paper - OBFHELPER
2019-05-05 19:12:32 +08:00
+ protected org.bukkit.event.entity.EntityDeathEvent d(DamageSource damagesource) { // Paper
Entity entity = damagesource.getEntity();
int i;
2018-09-08 08:14:48 +08:00
2020-07-23 07:35:44 +08:00
@@ -1459,15 +1486,18 @@ public abstract class EntityLiving extends Entity {
2019-05-05 19:12:32 +08:00
this.dropDeathLoot(damagesource, i, flag);
}
2020-07-23 07:35:44 +08:00
// CraftBukkit start - Call death event
- CraftEventFactory.callEntityDeathEvent(this, this.drops);
+ org.bukkit.event.entity.EntityDeathEvent deathEvent = CraftEventFactory.callEntityDeathEvent(this, this.drops); // Paper
2019-08-06 00:35:40 +08:00
+ this.postDeathDropItems(deathEvent); // Paper
2020-07-23 07:35:44 +08:00
this.drops = new ArrayList<>();
// CraftBukkit end
2018-09-08 08:14:48 +08:00
2019-12-13 00:20:43 +08:00
// this.dropInventory();// CraftBukkit - moved up
2019-12-13 23:21:58 +08:00
this.dropExperience();
2019-05-05 19:12:32 +08:00
+ return deathEvent; // Paper
2018-09-08 08:14:48 +08:00
}
2019-12-13 00:20:43 +08:00
protected void dropInventory() {}
2019-08-06 00:35:40 +08:00
+ 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
2019-12-13 00:20:43 +08:00
// CraftBukkit start
public int getExpReward() {
2020-07-23 07:35:44 +08:00
@@ -1552,6 +1582,7 @@ public abstract class EntityLiving extends Entity {
2019-05-06 07:24:37 +08:00
return SoundEffects.ENTITY_GENERIC_HURT;
}
+ public final SoundEffect getDeathSoundEffect() { return this.getSoundDeath(); } // Paper - OBFHELPER
@Nullable
protected SoundEffect getSoundDeath() {
return SoundEffects.ENTITY_GENERIC_DEATH;
2020-07-23 07:35:44 +08:00
@@ -2071,10 +2102,12 @@ public abstract class EntityLiving extends Entity {
2019-05-06 07:24:37 +08:00
}
+ public final float getDeathSoundVolume() { return this.getSoundVolume(); } // Paper - OBFHELPER
protected float getSoundVolume() {
2018-09-08 08:14:48 +08:00
return 1.0F;
}
2020-06-25 22:09:55 +08:00
+ public float getSoundPitch() { return dG();} // Paper - OBFHELPER
protected float dG() {
2018-09-08 08:14:48 +08:00
return this.isBaby() ? (this.random.nextFloat() - this.random.nextFloat()) * 0.2F + 1.5F : (this.random.nextFloat() - this.random.nextFloat()) * 0.2F + 1.0F;
}
diff --git a/src/main/java/net/minecraft/server/EntityPlayer.java b/src/main/java/net/minecraft/server/EntityPlayer.java
2020-07-22 16:01:35 +08:00
index 5053b1a09bd992b884a5871cb8f355ffbcb26260..c272bab87f9248cc3e7afe3607522f0b5e9c48d1 100644
2018-09-08 08:14:48 +08:00
--- a/src/main/java/net/minecraft/server/EntityPlayer.java
+++ b/src/main/java/net/minecraft/server/EntityPlayer.java
2020-06-25 22:09:55 +08:00
@@ -80,6 +80,10 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2018-09-10 02:38:27 +08:00
public int ping;
public boolean viewingCredits;
2018-09-08 08:14:48 +08:00
private int containerUpdateDelay; // Paper
+ // Paper start - cancellable death event
+ public boolean queueHealthUpdatePacket = false;
+ public net.minecraft.server.PacketPlayOutUpdateHealth queuedHealthUpdatePacket;
+ // Paper end
// CraftBukkit start
public String displayName;
2020-06-30 13:20:29 +08:00
@@ -579,6 +583,15 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2018-09-08 08:14:48 +08:00
2018-09-09 07:12:58 +08:00
String deathmessage = defaultMessage.getString();
2018-09-08 08:14:48 +08:00
org.bukkit.event.entity.PlayerDeathEvent event = CraftEventFactory.callPlayerDeathEvent(this, loot, deathmessage, keepInventory);
+ // 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;
+ }
+ // Paper end
2019-01-01 11:15:55 +08:00
// SPIGOT-943 - only call if they have an inventory open
if (this.activeContainer != this.defaultContainer) {
2020-06-30 13:20:29 +08:00
@@ -730,8 +743,17 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2018-09-08 08:14:48 +08:00
}
}
}
-
- return super.damageEntity(damagesource, f);
+ // Paper start - cancellable death events
+ //return super.damageEntity(damagesource, f);
+ this.queueHealthUpdatePacket = true;
+ boolean damaged = super.damageEntity(damagesource, f);
+ this.queueHealthUpdatePacket = false;
+ if (this.queuedHealthUpdatePacket != null) {
+ this.playerConnection.sendPacket(this.queuedHealthUpdatePacket);
+ this.queuedHealthUpdatePacket = null;
+ }
+ return damaged;
+ // Paper end
}
}
}
diff --git a/src/main/java/org/bukkit/craftbukkit/CraftSound.java b/src/main/java/org/bukkit/craftbukkit/CraftSound.java
2020-06-25 22:09:55 +08:00
index 7b565ddfffd0b9c78392ee115ef9c20ab89a3380..d78b9fd608da691220f1edeff506b36e3a88f515 100644
2018-09-08 08:14:48 +08:00
--- a/src/main/java/org/bukkit/craftbukkit/CraftSound.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftSound.java
2020-06-25 22:09:55 +08:00
@@ -996,6 +996,22 @@ public enum CraftSound {
2018-09-08 08:14:48 +08:00
WEATHER_RAIN_ABOVE("weather.rain.above");
private final String minecraftKey;
+ // Paper start - cancellable death event
+ public static CraftSound getBySoundEffect(final SoundEffect effect) {
2018-09-09 07:12:58 +08:00
+ MinecraftKey key = IRegistry.SOUND_EVENT.getKey(effect);
2018-09-08 08:14:48 +08:00
+ Preconditions.checkArgument(key != null, "Key for sound effect %s not found?", effect.toString());
+
+ return valueOf(key.getKey().replace('.', '_').toUpperCase(java.util.Locale.ENGLISH));
+ }
+
+ public static Sound getSoundByEffect(final SoundEffect effect) {
+ return Sound.valueOf(getBySoundEffect(effect).name());
+ }
+
+ public static SoundEffect getSoundEffect(final Sound sound) {
+ return getSoundEffect(getSound(sound));
+ }
+ // Paper end
CraftSound(String minecraftKey) {
this.minecraftKey = minecraftKey;
}
diff --git a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
2020-07-23 07:35:44 +08:00
index 7828241b4ab42fd684e62950ddac85b3e43e45be..139ea71a17a014be2be735395c99c1d5354a4383 100644
2018-09-08 08:14:48 +08:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
2020-07-22 16:01:35 +08:00
@@ -1768,7 +1768,15 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
2018-09-08 08:14:48 +08:00
}
public void sendHealthUpdate() {
- getHandle().playerConnection.sendPacket(new PacketPlayOutUpdateHealth(getScaledHealth(), getHandle().getFoodData().getFoodLevel(), getHandle().getFoodData().getSaturationLevel()));
+ // Paper start - cancellable death event
+ //getHandle().playerConnection.sendPacket(new PacketPlayOutUpdateHealth(getScaledHealth(), getHandle().getFoodData().getFoodLevel(), getHandle().getFoodData().getSaturationLevel()));
+ PacketPlayOutUpdateHealth packet = new PacketPlayOutUpdateHealth(getScaledHealth(), getHandle().getFoodData().getFoodLevel(), getHandle().getFoodData().getSaturationLevel());
+ if (this.getHandle().queueHealthUpdatePacket) {
+ this.getHandle().queuedHealthUpdatePacket = packet;
+ } else {
+ this.getHandle().playerConnection.sendPacket(packet);
+ }
+ // Paper end
}
2020-06-25 22:09:55 +08:00
public void injectScaledMaxHealth(Collection<AttributeModifiable> collection, boolean force) {
2018-09-08 08:14:48 +08:00
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
2020-07-23 07:35:44 +08:00
index a86939235a1a5624710744ec3550e6131482c284..59205a848524f281edcab388a9ca40b3e08a9556 100644
2018-09-08 08:14:48 +08:00
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
2020-07-23 07:35:44 +08:00
@@ -784,9 +784,16 @@ public class CraftEventFactory {
2018-09-08 08:14:48 +08:00
public static EntityDeathEvent callEntityDeathEvent(EntityLiving 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()) {
2020-07-23 07:35:44 +08:00
@@ -802,8 +809,15 @@ public class CraftEventFactory {
2018-09-08 08:14:48 +08:00
CraftPlayer entity = victim.getBukkitEntity();
PlayerDeathEvent event = new PlayerDeathEvent(entity, drops, victim.getExpReward(), 0, deathMessage);
event.setKeepInventory(keepInventory);
+ 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();
2020-07-23 07:35:44 +08:00
@@ -820,6 +834,31 @@ public class CraftEventFactory {
2018-09-08 08:14:48 +08:00
return event;
}
+ // Paper start - helper methods for making death event cancellable
+ // Add information to death event
+ private static void populateFields(EntityLiving victim, EntityDeathEvent event) {
+ event.setReviveHealth(event.getEntity().getAttribute(org.bukkit.attribute.Attribute.GENERIC_MAX_HEALTH).getValue());
+ event.setShouldPlayDeathSound(!victim.silentDeath && !victim.isSilent());
2019-05-06 07:24:37 +08:00
+ net.minecraft.server.SoundEffect soundEffect = victim.getDeathSoundEffect();
2018-09-08 08:14:48 +08:00
+ event.setDeathSound(soundEffect != null ? org.bukkit.craftbukkit.CraftSound.getSoundByEffect(soundEffect) : null);
2019-05-06 07:24:37 +08:00
+ event.setDeathSoundCategory(org.bukkit.SoundCategory.valueOf(victim.getSoundCategory().name()));
2018-09-08 08:14:48 +08:00
+ event.setDeathSoundVolume(victim.getDeathSoundVolume());
2019-05-06 07:24:37 +08:00
+ event.setDeathSoundPitch(victim.getSoundPitch());
2018-09-08 08:14:48 +08:00
+ }
+
+ // Play death sound manually
+ private static void playDeathSound(EntityLiving victim, EntityDeathEvent event) {
+ if (event.shouldPlayDeathSound() && event.getDeathSound() != null && event.getDeathSoundCategory() != null) {
+ EntityHuman source = victim instanceof EntityHuman ? (EntityHuman) victim : null;
+ double x = event.getEntity().getLocation().getX();
+ double y = event.getEntity().getLocation().getY();
+ double z = event.getEntity().getLocation().getZ();
2019-05-06 07:24:37 +08:00
+ net.minecraft.server.SoundEffect soundEffect = org.bukkit.craftbukkit.CraftSound.getSoundEffect(event.getDeathSound());
+ net.minecraft.server.SoundCategory soundCategory = net.minecraft.server.SoundCategory.valueOf(event.getDeathSoundCategory().name());
2018-09-08 08:14:48 +08:00
+ victim.world.sendSoundEffect(source, x, y, z, soundEffect, soundCategory, event.getDeathSoundVolume(), event.getDeathSoundPitch());
+ }
+ }
+ // Paper end
/**
* Server methods
*/