2020-05-06 09:48:49 +00:00
From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001
2018-09-08 00:14:48 +00: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-08-02 05:39:36 +00:00
index 6281f7900afab3ef1c9ba3c034b91cbfa1900f50..7f3401d5d7878e4a0f407e92bf110dbe7c7868a6 100644
2018-09-08 00:14:48 +00:00
--- a/src/main/java/net/minecraft/server/CombatTracker.java
+++ b/src/main/java/net/minecraft/server/CombatTracker.java
2020-06-25 14:09:55 +00:00
@@ -192,6 +192,7 @@ public class CombatTracker {
2018-09-08 00:14:48 +00:00
this.h = null;
}
2020-08-02 05:39:36 +00:00
+ public final void reset() { this.g(); } // Paper - OBFHELPER
2018-09-08 00:14:48 +00:00
public void g() {
int i = this.f ? 300 : 100;
2019-01-01 03:15:55 +00:00
2018-09-08 00:14:48 +00:00
diff --git a/src/main/java/net/minecraft/server/Entity.java b/src/main/java/net/minecraft/server/Entity.java
2020-09-19 11:29:53 +00:00
index e4afda074cdbbc4b1d81844fb60602392de8941c..18a79f9c61c0263526cd632f032d17bdc6cb2139 100644
2018-09-08 00:14:48 +00:00
--- a/src/main/java/net/minecraft/server/Entity.java
+++ b/src/main/java/net/minecraft/server/Entity.java
2020-09-19 11:29:53 +00:00
@@ -1451,6 +1451,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
2018-09-08 00:14:48 +00:00
}
2020-06-25 14:09:55 +00:00
// CraftBukkit end
2018-09-08 00:14:48 +00:00
2020-08-02 05:39:36 +00:00
+ public final void runKillTrigger(Entity entity, int kills, DamageSource damageSource) { this.a(entity, kills, damageSource); } // Paper - OBFHELPER
2018-09-08 00:14:48 +00:00
public void a(Entity entity, int i, DamageSource damagesource) {
if (entity instanceof EntityPlayer) {
CriterionTriggers.c.a((EntityPlayer) entity, this, damagesource);
2020-09-19 11:29:53 +00:00
@@ -2350,6 +2351,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
2018-09-08 23:12:58 +00:00
this.fallDistance = 0.0F;
2018-09-08 00:14:48 +00:00
}
2020-08-25 02:22:08 +00:00
+ public final void onKill(WorldServer worldserver, EntityLiving entityLiving) { this.a(worldserver, entityLiving); } // Paper - OBFHELPER
public void a(WorldServer worldserver, EntityLiving entityliving) {}
2018-09-08 00:14:48 +00:00
2020-08-25 02:22:08 +00:00
protected void l(double d0, double d1, double d2) {
2018-09-08 00:14:48 +00:00
diff --git a/src/main/java/net/minecraft/server/EntityArmorStand.java b/src/main/java/net/minecraft/server/EntityArmorStand.java
2020-09-10 23:47:58 +00:00
index 3b01b560f29e5d2c765f28b53e79119503fcfbac..5c3d2bbf7b2ae22347d251c90c85388779c2cd44 100644
2018-09-08 00:14:48 +00:00
--- a/src/main/java/net/minecraft/server/EntityArmorStand.java
+++ b/src/main/java/net/minecraft/server/EntityArmorStand.java
2020-08-25 02:22:08 +00:00
@@ -703,7 +703,8 @@ public class EntityArmorStand extends EntityLiving {
2018-09-08 00:14:48 +00:00
2019-05-05 11:12:32 +00:00
@Override
2018-09-08 00:14:48 +00: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 07:20:16 +00:00
diff --git a/src/main/java/net/minecraft/server/EntityFox.java b/src/main/java/net/minecraft/server/EntityFox.java
2020-08-25 02:22:08 +00:00
index 83fa99b1cf784eb7f30b14f9f0743cbab7136563..0ac0364ffeb81ac3f379848f6e1afae641658c73 100644
2019-05-06 07:20:16 +00:00
--- a/src/main/java/net/minecraft/server/EntityFox.java
+++ b/src/main/java/net/minecraft/server/EntityFox.java
2020-08-25 02:22:08 +00:00
@@ -577,15 +577,25 @@ public class EntityFox extends EntityAnimal {
2019-05-06 07:20:16 +00:00
}
@Override
- protected void d(DamageSource damagesource) {
2019-05-07 15:20:32 +00:00
- ItemStack itemstack = this.getEquipment(EnumItemSlot.MAINHAND);
2019-05-06 07:20:16 +00:00
+ protected org.bukkit.event.entity.EntityDeathEvent d(DamageSource damagesource) { // Paper
2019-05-07 15:20:32 +00:00
+ ItemStack itemstack = this.getEquipment(EnumItemSlot.MAINHAND).cloneItemStack(); // Paper
+
2019-05-07 12:00:51 +00:00
+ // Paper start - Cancellable death event
2019-08-06 03:31:38 +00:00
+ org.bukkit.event.entity.EntityDeathEvent deathEvent = super.d(damagesource);
2019-05-07 12:00:51 +00:00
+
+ // Below is code to drop
+
+ if (deathEvent == null || deathEvent.isCancelled()) {
+ return deathEvent;
+ }
+ // Paper end
2019-05-07 15:20:32 +00:00
2019-05-06 07:20:16 +00:00
if (!itemstack.isEmpty()) {
2019-05-07 12:00:51 +00:00
this.a(itemstack);
2020-06-25 14:09:55 +00:00
this.setSlot(EnumItemSlot.MAINHAND, ItemStack.b);
2019-05-06 07:20:16 +00:00
}
- super.d(damagesource);
2019-05-07 12:00:51 +00:00
+ return deathEvent; // Paper
2019-05-06 07:20:16 +00:00
}
public static boolean a(EntityFox entityfox, EntityLiving entityliving) {
2019-08-05 16:35:40 +00:00
diff --git a/src/main/java/net/minecraft/server/EntityHorseChestedAbstract.java b/src/main/java/net/minecraft/server/EntityHorseChestedAbstract.java
2020-08-25 02:22:08 +00:00
index 29d1b9bb4e481b6d6fb7185d9adf17f5ec07d4a1..c14d722e9b1ed61d2878ca752797871b6de39ac9 100644
2019-08-05 16:35:40 +00:00
--- a/src/main/java/net/minecraft/server/EntityHorseChestedAbstract.java
+++ b/src/main/java/net/minecraft/server/EntityHorseChestedAbstract.java
2020-06-25 14:09:55 +00:00
@@ -50,11 +50,19 @@ public abstract class EntityHorseChestedAbstract extends EntityHorseAbstract {
2019-08-05 16:35:40 +00: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 14:09:55 +00:00
public void saveData(NBTTagCompound nbttagcompound) {
super.saveData(nbttagcompound);
2018-09-08 00:14:48 +00:00
diff --git a/src/main/java/net/minecraft/server/EntityLiving.java b/src/main/java/net/minecraft/server/EntityLiving.java
2020-09-10 23:47:58 +00:00
index f193c2778d0f04973546256d648692398b932dca..c54641d984bdd04067f9dba66322b01b840904ed 100644
2018-09-08 00:14:48 +00:00
--- a/src/main/java/net/minecraft/server/EntityLiving.java
+++ b/src/main/java/net/minecraft/server/EntityLiving.java
2020-09-02 09:12:25 +00:00
@@ -96,7 +96,7 @@ public abstract class EntityLiving extends Entity {
2020-08-25 02:22:08 +00:00
protected float aL;
protected float aM;
protected float aN;
- protected int aO;
+ protected int aO;protected int getKillCount() { return this.aO; } // Paper - OBFHELPER
2018-09-08 00:14:48 +00:00
public float lastDamage;
2019-05-05 11:12:32 +00:00
protected boolean jumping;
2020-08-25 02:22:08 +00:00
public float aR;
2020-09-02 09:12:25 +00:00
@@ -140,6 +140,7 @@ public abstract class EntityLiving extends Entity {
2020-06-25 14:09:55 +00:00
public Set<UUID> collidableExemptions = new HashSet<>();
2018-09-08 00:14:48 +00:00
public boolean canPickUpLoot;
2018-09-08 23:12:58 +00:00
public org.bukkit.craftbukkit.entity.CraftLivingEntity getBukkitLivingEntity() { return (org.bukkit.craftbukkit.entity.CraftLivingEntity) super.getBukkitEntity(); } // Paper
2018-09-08 00:14:48 +00:00
+ public boolean silentDeath = false; // Paper - mark entity as dying silently for cancellable death event
@Override
public float getBukkitYaw() {
2020-09-02 09:12:25 +00:00
@@ -1249,13 +1250,17 @@ public abstract class EntityLiving extends Entity {
2020-06-25 14:09:55 +00:00
if (knockbackCancelled) this.world.broadcastEntityEffect(this, (byte) 2); // Paper - Disable explosion knockback
if (this.dk()) {
2019-05-05 11:12:32 +00:00
if (!this.f(damagesource)) {
- SoundEffect soundeffect = this.getSoundDeath();
2018-09-08 23:12:58 +00:00
+ // Paper start - moved into CraftEventFactory event caller for cancellable death event
2019-05-05 11:12:32 +00:00
+ //SoundEffect soundeffect = this.getSoundDeath();
2018-09-08 23:12:58 +00:00
- if (flag1 && soundeffect != null) {
2020-06-25 14:09:55 +00:00
- this.playSound(soundeffect, this.getSoundVolume(), this.dG());
2018-09-08 23:12:58 +00:00
- }
2019-12-12 16:20:43 +00:00
+// if (flag1 && soundeffect != null) {
2020-06-25 14:09:55 +00:00
+// this.playSound(soundeffect, this.getSoundVolume(), this.dG());
2019-12-12 16:20:43 +00:00
+// }
2018-09-08 23:12:58 +00: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-09-02 09:12:25 +00:00
@@ -1394,6 +1399,7 @@ public abstract class EntityLiving extends Entity {
2018-09-08 00:14:48 +00:00
Entity entity = damagesource.getEntity();
2019-05-05 11:12:32 +00:00
EntityLiving entityliving = this.getKillingEntity();
2018-09-08 00:14:48 +00:00
2020-08-25 02:22:08 +00:00
+ /* // Paper - move down to make death event cancellable - this is the runKillTrigger below
if (this.aO >= 0 && entityliving != null) {
entityliving.a(this, this.aO, damagesource);
2019-06-25 01:47:58 +00:00
}
2020-09-02 09:12:25 +00:00
@@ -1401,20 +1407,40 @@ public abstract class EntityLiving extends Entity {
2019-06-25 01:47:58 +00:00
if (this.isSleeping()) {
2019-12-12 16:20:43 +00:00
this.entityWakeup();
2019-06-25 01:47:58 +00:00
}
+ */ // Paper
2018-12-17 05:18:06 +00:00
this.killed = true;
2018-09-08 00:14:48 +00:00
- this.getCombatTracker().g();
2020-08-25 02:22:08 +00:00
+ // this.getCombatTracker().g(); // Paper - moved into if below as .reset()
if (this.world instanceof WorldServer) {
if (entity != null) {
- entity.a((WorldServer) this.world, this);
+ // entity.a((WorldServer) this.world, this); // Paper - move below into if for onKill
}
2019-05-05 11:12:32 +00:00
- this.d(damagesource);
2020-08-25 02:22:08 +00:00
+ // Paper start
2019-05-05 11:12:32 +00:00
+ org.bukkit.event.entity.EntityDeathEvent deathEvent = this.d(damagesource);
2019-05-07 12:00:51 +00:00
+ if (deathEvent == null || !deathEvent.isCancelled()) {
2019-05-05 11:12:32 +00:00
+ if (this.getKillCount() >= 0 && entityliving != null) {
+ entityliving.runKillTrigger(this, this.getKillCount(), damagesource);
+ }
+ if (this.isSleeping()) {
2019-12-13 01:18:18 +00:00
+ this.entityWakeup();
2019-05-05 11:12:32 +00:00
+ }
+ this.getCombatTracker().reset();
2020-08-25 02:22:08 +00:00
+ if (entity != null) {
+ entity.onKill((WorldServer) this.world, this);
+ }
2019-05-05 11:12:32 +00:00
+ } else {
+ this.killed = false;
+ this.setHealth((float) deathEvent.getReviveHealth());
2018-09-08 00:14:48 +00:00
+ }
2019-05-05 11:12:32 +00:00
+ // Paper end
2020-08-25 02:22:08 +00:00
this.f(entityliving);
2019-08-05 16:35:40 +00:00
}
+ if (this.killed) { // Paper
this.world.broadcastEntityEffect(this, (byte) 3);
this.setPose(EntityPose.DYING);
+ } // Paper
2018-09-08 00:14:48 +00:00
}
}
2020-09-02 09:12:25 +00:00
@@ -1422,7 +1448,7 @@ public abstract class EntityLiving extends Entity {
2019-12-12 16:20:43 +00: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 14:09:55 +00:00
BlockPosition blockposition = this.getChunkCoordinates();
2019-12-12 16:20:43 +00:00
IBlockData iblockdata = Blocks.WITHER_ROSE.getBlockData();
2020-09-02 09:12:25 +00:00
@@ -1443,7 +1469,8 @@ public abstract class EntityLiving extends Entity {
2019-12-12 16:20:43 +00:00
}
}
2019-05-05 11:12:32 +00:00
- protected void d(DamageSource damagesource) {
2019-06-25 01:47:58 +00:00
+ protected org.bukkit.event.entity.EntityDeathEvent processDeath(DamageSource damagesource) { return d(damagesource); } // Paper - OBFHELPER
2019-05-05 11:12:32 +00:00
+ protected org.bukkit.event.entity.EntityDeathEvent d(DamageSource damagesource) { // Paper
Entity entity = damagesource.getEntity();
int i;
2018-09-08 00:14:48 +00:00
2020-09-02 09:12:25 +00:00
@@ -1461,15 +1488,18 @@ public abstract class EntityLiving extends Entity {
2019-05-05 11:12:32 +00:00
this.dropDeathLoot(damagesource, i, flag);
}
2020-07-22 23:35:44 +00: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-05 16:35:40 +00:00
+ this.postDeathDropItems(deathEvent); // Paper
2020-07-22 23:35:44 +00:00
this.drops = new ArrayList<>();
// CraftBukkit end
2018-09-08 00:14:48 +00:00
2019-12-12 16:20:43 +00:00
// this.dropInventory();// CraftBukkit - moved up
2019-12-13 15:21:58 +00:00
this.dropExperience();
2019-05-05 11:12:32 +00:00
+ return deathEvent; // Paper
2018-09-08 00:14:48 +00:00
}
2019-12-12 16:20:43 +00:00
protected void dropInventory() {}
2019-08-05 16:35:40 +00: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-12 16:20:43 +00:00
// CraftBukkit start
public int getExpReward() {
2020-09-02 09:12:25 +00:00
@@ -1554,6 +1584,7 @@ public abstract class EntityLiving extends Entity {
2019-05-05 23:24:37 +00: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-09-10 23:47:58 +00:00
@@ -2084,10 +2115,12 @@ public abstract class EntityLiving extends Entity {
2019-05-05 23:24:37 +00:00
}
+ public final float getDeathSoundVolume() { return this.getSoundVolume(); } // Paper - OBFHELPER
protected float getSoundVolume() {
2018-09-08 00:14:48 +00:00
return 1.0F;
}
2020-06-25 14:09:55 +00:00
+ public float getSoundPitch() { return dG();} // Paper - OBFHELPER
protected float dG() {
2018-09-08 00:14:48 +00: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-10-17 10:39:45 +00:00
index daeb5a6e005a4b85b5f3ac581c0e7cb6ea64b2c8..db7435c6923235fad63bdf531eca4a95c1b63dc9 100644
2018-09-08 00:14:48 +00:00
--- a/src/main/java/net/minecraft/server/EntityPlayer.java
+++ b/src/main/java/net/minecraft/server/EntityPlayer.java
2020-08-25 02:22:08 +00:00
@@ -82,6 +82,10 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2018-09-09 18:38:27 +00:00
public int ping;
public boolean viewingCredits;
2018-09-08 00:14:48 +00: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-08-25 02:22:08 +00:00
@@ -583,6 +587,15 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2018-09-08 00:14:48 +00:00
2018-09-08 23:12:58 +00:00
String deathmessage = defaultMessage.getString();
2018-09-08 00:14:48 +00: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 03:15:55 +00:00
// SPIGOT-943 - only call if they have an inventory open
if (this.activeContainer != this.defaultContainer) {
2020-08-25 02:22:08 +00:00
@@ -734,8 +747,17 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
2018-09-08 00:14:48 +00: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-08-25 02:22:08 +00:00
index 9a2e6bedfc50fba10f37c01d825f80415bfb69d3..a4afe638515471159d505a5e402a934206d8c1f5 100644
2018-09-08 00:14:48 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/CraftSound.java
+++ b/src/main/java/org/bukkit/craftbukkit/CraftSound.java
2020-08-25 02:22:08 +00:00
@@ -1003,6 +1003,22 @@ public enum CraftSound {
2018-09-08 00:14:48 +00: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-08 23:12:58 +00:00
+ MinecraftKey key = IRegistry.SOUND_EVENT.getKey(effect);
2018-09-08 00:14:48 +00: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-10-17 10:39:45 +00:00
index 982b8a7028385d38ff0bd88aabd94801ae258037..6c3b9df31e9c44f7171cf08014561e4b095133b1 100644
2018-09-08 00:14:48 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
+++ b/src/main/java/org/bukkit/craftbukkit/entity/CraftPlayer.java
2020-10-17 10:39:45 +00:00
@@ -1784,7 +1784,15 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
2018-09-08 00:14:48 +00: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 14:09:55 +00:00
public void injectScaledMaxHealth(Collection<AttributeModifiable> collection, boolean force) {
2018-09-08 00:14:48 +00:00
diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
2020-10-17 10:39:45 +00:00
index 78f3af7de270750734f6d8d1522e0d755408e17e..4d82634cc4f1f4ef16cb027c7edfc8b7638311b5 100644
2018-09-08 00:14:48 +00:00
--- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
+++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java
2020-09-02 09:12:25 +00:00
@@ -791,9 +791,16 @@ public class CraftEventFactory {
2018-09-08 00:14:48 +00: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-09-02 09:12:25 +00:00
@@ -809,8 +816,15 @@ public class CraftEventFactory {
2018-09-08 00:14:48 +00: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-09-02 09:12:25 +00:00
@@ -827,6 +841,31 @@ public class CraftEventFactory {
2018-09-08 00:14:48 +00: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-05 23:24:37 +00:00
+ net.minecraft.server.SoundEffect soundEffect = victim.getDeathSoundEffect();
2018-09-08 00:14:48 +00:00
+ event.setDeathSound(soundEffect != null ? org.bukkit.craftbukkit.CraftSound.getSoundByEffect(soundEffect) : null);
2019-05-05 23:24:37 +00:00
+ event.setDeathSoundCategory(org.bukkit.SoundCategory.valueOf(victim.getSoundCategory().name()));
2018-09-08 00:14:48 +00:00
+ event.setDeathSoundVolume(victim.getDeathSoundVolume());
2019-05-05 23:24:37 +00:00
+ event.setDeathSoundPitch(victim.getSoundPitch());
2018-09-08 00:14:48 +00: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-05 23:24:37 +00: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());
2020-08-02 05:39:36 +00:00
+ victim.world.playSound(source, x, y, z, soundEffect, soundCategory, event.getDeathSoundVolume(), event.getDeathSoundPitch());
2018-09-08 00:14:48 +00:00
+ }
+ }
+ // Paper end
/**
* Server methods
*/