2018-12-15 01:17:27 +00:00
From 96a7cd354c1a98e255bc42cb30ed417744295115 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
2018-11-27 23:14:57 +00:00
index 9a74601b0..6e60d15cc 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
2018-09-08 23:12:58 +00:00
@@ -163,6 +163,7 @@ public class CombatTracker {
2018-09-08 00:14:48 +00:00
this.h = null;
}
+ public void reset() { this.g(); } // Paper - OBFHELPER
public void g() {
int i = this.f ? 300 : 100;
2018-09-08 23:12:58 +00:00
if (this.g && (!this.b.isAlive() || this.b.ticksLived - this.c > i)) {
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
2018-12-08 10:09:55 +00:00
index da0e9b0a3..ab8490ff9 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
2018-10-26 03:26:29 +00:00
@@ -1539,6 +1539,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
2018-09-08 00:14:48 +00:00
return false;
}
+ 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);
2018-10-26 03:26:29 +00:00
@@ -2408,6 +2409,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
}
+ public void onKill(EntityLiving entityLiving) { this.b(entityLiving); } // Paper - OBFHELPER
public void b(EntityLiving entityliving) {}
protected boolean i(double d0, double d1, double d2) {
2018-12-08 10:09:55 +00:00
@@ -3073,6 +3075,7 @@ public abstract class Entity implements INamableTileEntity, ICommandListener, Ke
2018-09-08 00:14:48 +00:00
return EnumPistonReaction.NORMAL;
}
2018-09-08 23:12:58 +00:00
+ public SoundCategory getDeathSoundCategory() { return bV();} // Paper - OBFHELPER
public SoundCategory bV() {
2018-09-08 00:14:48 +00:00
return SoundCategory.NEUTRAL;
}
diff --git a/src/main/java/net/minecraft/server/EntityArmorStand.java b/src/main/java/net/minecraft/server/EntityArmorStand.java
2018-12-08 10:09:55 +00:00
index c5bddb1da..0a9666541 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
2018-12-08 10:09:55 +00:00
@@ -659,7 +659,8 @@ public class EntityArmorStand extends EntityLiving {
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();
}
diff --git a/src/main/java/net/minecraft/server/EntityLiving.java b/src/main/java/net/minecraft/server/EntityLiving.java
2018-12-08 10:09:55 +00:00
index 5ea5d4b78..46a4e37a2 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
2018-09-08 23:12:58 +00:00
@@ -76,14 +76,14 @@ public abstract class EntityLiving extends Entity {
public float aU;
2018-09-08 00:14:48 +00:00
public EntityHuman killer;
public int lastDamageByPlayerTime; // Paper - public
2018-09-08 23:12:58 +00:00
- protected boolean aX;
+ protected boolean aX; protected void setDying(boolean dying) { this.aX = dying; } protected boolean isDying() { return this.aX; } // Paper - OBFHELPER
2018-09-08 00:14:48 +00:00
protected int ticksFarFromPlayer;
protected float aZ;
protected float ba;
2018-09-08 23:12:58 +00:00
protected float bb;
protected float bc;
protected float bd;
- protected int be;
+ protected int be; protected int getKillCount() { return this.be; } // Paper - OBFHELPER
2018-09-08 00:14:48 +00:00
public float lastDamage;
2018-09-08 23:12:58 +00:00
protected boolean bg;
public float bh;
@@ -124,6 +124,7 @@ public abstract class EntityLiving extends Entity {
2018-09-08 00:14:48 +00:00
public boolean collides = true;
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() {
2018-09-08 23:12:58 +00:00
@@ -1071,13 +1072,17 @@ public abstract class EntityLiving extends Entity {
if (this.getHealth() <= 0.0F) {
if (!this.e(damagesource)) {
- SoundEffect soundeffect = this.cs();
+ // Paper start - moved into CraftEventFactory event caller for cancellable death event
+ //SoundEffect soundeffect = this.cs();
- if (flag1 && soundeffect != null) {
- this.a(soundeffect, this.cD(), this.cE());
- }
+ //if (flag1 && soundeffect != null) {
+ // this.a(soundeffect, this.cD(), this.cE());
+ //}
+ 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);
@@ -1205,16 +1210,20 @@ public abstract class EntityLiving extends Entity {
2018-09-08 00:14:48 +00:00
Entity entity = damagesource.getEntity();
2018-09-08 23:12:58 +00:00
EntityLiving entityliving = this.cv();
2018-09-08 00:14:48 +00:00
2018-09-08 23:12:58 +00:00
- if (this.be >= 0 && entityliving != null) {
- entityliving.a(this, this.be, damagesource);
2018-09-08 00:14:48 +00:00
- }
+ // Paper start - move down to make death event cancellable
2018-09-08 23:12:58 +00:00
+ //if (this.be >= 0 && entityliving != null) {
+ // entityliving.a(this, this.be, damagesource);
2018-09-08 00:14:48 +00:00
+ //}
- if (entity != null) {
- entity.b(this);
- }
+ //if (entity != null) {
+ // entity.b(this);
+ //}
2018-09-08 23:12:58 +00:00
- this.aX = true;
2018-09-08 00:14:48 +00:00
- this.getCombatTracker().g();
2018-09-11 21:48:17 +00:00
+ this.aX = true; // Paper - Always set at start, unset later if cancelled - GH-1432
2018-09-08 00:14:48 +00:00
+ //this.getCombatTracker().g();
+
+ org.bukkit.event.entity.EntityDeathEvent deathEvent = null;
2018-09-08 23:12:58 +00:00
+ // Paper end
2018-09-08 00:14:48 +00:00
if (!this.world.isClientSide) {
int i = 0;
2018-09-11 21:48:17 +00:00
@@ -1227,15 +1236,33 @@ public abstract class EntityLiving extends Entity {
2018-09-08 00:14:48 +00:00
this.a(flag, i, damagesource);
// CraftBukkit start - Call death event
- CraftEventFactory.callEntityDeathEvent(this, this.drops);
+ deathEvent = CraftEventFactory.callEntityDeathEvent(this, this.drops); // Paper - cancellable death event
this.drops = new ArrayList<org.bukkit.inventory.ItemStack>();
} else {
- CraftEventFactory.callEntityDeathEvent(this);
+ deathEvent = CraftEventFactory.callEntityDeathEvent(this); // Paper - cancellable death event
// CraftBukkit end
}
}
- this.world.broadcastEntityEffect(this, (byte) 3);
+ // Paper start - cancellable death event
+ if (deathEvent == null || !deathEvent.isCancelled()) {
+ // triggers and stats got moved down
+ if (this.getKillCount() >= 0 && entityliving != null) {
+ entityliving.runKillTrigger(this, this.getKillCount(), damagesource);
+ }
+
+ if (entity != null) {
+ entity.onKill(this);
+ }
+
+ this.getCombatTracker().reset();
+ this.setDying(true);
+ this.world.broadcastEntityEffect(this, (byte) 3);
+ } else {
2018-09-11 21:38:57 +00:00
+ this.setDying(false); // Paper - reset if cancelled
2018-09-08 00:14:48 +00:00
+ this.setHealth((float) deathEvent.getReviveHealth());
+ }
+ // Paper end
}
}
2018-09-11 21:48:17 +00:00
@@ -1289,6 +1316,7 @@ public abstract class EntityLiving extends Entity {
2018-09-08 23:12:58 +00:00
return SoundEffects.ENTITY_GENERIC_HURT;
2018-09-08 00:14:48 +00:00
}
2018-09-08 23:12:58 +00:00
+ @Nullable public SoundEffect getDeathSoundEffect() { return cs();} // Paper - OBFHELPER
2018-09-08 00:14:48 +00:00
@Nullable
2018-09-08 23:12:58 +00:00
protected SoundEffect cs() {
return SoundEffects.ENTITY_GENERIC_DEATH;
2018-09-17 06:46:39 +00:00
@@ -1709,10 +1737,12 @@ public abstract class EntityLiving extends Entity {
2018-09-08 00:14:48 +00:00
}
2018-09-08 23:12:58 +00:00
+ public float getDeathSoundVolume() { return cD();} // Paper - OBFHELPER
protected float cD() {
2018-09-08 00:14:48 +00:00
return 1.0F;
}
2018-09-08 23:12:58 +00:00
+ public float getDeathSoundPitch() { return cE();} // Paper - OBFHELPER
protected float cE() {
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
2018-12-08 10:09:55 +00:00
index 6933d8876..d429e552e 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
2018-09-09 18:38:27 +00:00
@@ -75,6 +75,10 @@ public class EntityPlayer extends EntityHuman implements ICrafting {
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;
2018-11-03 00:23:27 +00:00
@@ -509,6 +513,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
String deathMessage = event.getDeathMessage();
2018-11-03 00:23:27 +00:00
@@ -636,8 +649,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
2018-11-27 23:14:57 +00:00
index 17fab031b..ee8219e3b 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
2018-09-08 23:12:58 +00:00
@@ -674,6 +674,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
2018-11-27 23:14:57 +00:00
index 8be0a47ba..f56ef6f71 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
2018-09-08 23:12:58 +00:00
@@ -1711,7 +1711,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
}
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
2018-12-08 10:09:55 +00:00
index cd6ae3b8e..4a0a456a0 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
2018-12-08 10:09:55 +00:00
@@ -508,9 +508,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()) {
2018-12-08 10:09:55 +00:00
@@ -526,8 +533,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();
2018-12-08 10:09:55 +00:00
@@ -548,6 +562,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());
+ SoundEffect soundEffect = victim.getDeathSoundEffect();
+ event.setDeathSound(soundEffect != null ? org.bukkit.craftbukkit.CraftSound.getSoundByEffect(soundEffect) : null);
+ event.setDeathSoundCategory(org.bukkit.SoundCategory.valueOf(victim.getDeathSoundCategory().name()));
+ event.setDeathSoundVolume(victim.getDeathSoundVolume());
+ event.setDeathSoundPitch(victim.getDeathSoundPitch());
+ }
+
+ // 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();
+ SoundEffect soundEffect = org.bukkit.craftbukkit.CraftSound.getSoundEffect(event.getDeathSound());
+ SoundCategory soundCategory = SoundCategory.valueOf(event.getDeathSoundCategory().name());
+ victim.world.sendSoundEffect(source, x, y, z, soundEffect, soundCategory, event.getDeathSoundVolume(), event.getDeathSoundPitch());
+ }
+ }
+ // Paper end
/**
* Server methods
*/
--
2018-12-15 01:17:27 +00:00
2.20.0
2018-09-08 00:14:48 +00:00