Paper/patches/server/0260-Improve-death-events.patch

377 lines
19 KiB
Diff
Raw Normal View History

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.
2021-06-13 06:03:02 +02:00
TODO 1.17: this needs to be checked (actually get off your lazy ass and cancel the events) for the following entities,
maybe more (please check patch overrides for drops for more):
- players, armor stands, foxes, chested donkeys/llamas
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
2021-06-13 06:03:02 +02:00
index 171500918c8ee248689909ae97230eb18adc33e5..44385d07b2deedffd95bcc428d9d8d3f785143db 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
2021-06-13 06:03:02 +02:00
@@ -218,6 +218,10 @@ public class ServerPlayer extends Player {
2021-06-11 14:02:28 +02:00
public int latency;
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;
2021-06-13 06:03:02 +02:00
@@ -751,6 +755,15 @@ public class ServerPlayer extends Player {
2021-06-11 14:02:28 +02:00
Component defaultMessage = this.getCombatTracker().getDeathMessage();
org.bukkit.event.entity.PlayerDeathEvent event = CraftEventFactory.callPlayerDeathEvent(this, loot, PaperAdventure.asAdventure(defaultMessage), defaultMessage.getString(), keepInventory); // Paper - Adventure
+ // 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
// SPIGOT-943 - only call if they have an inventory open
if (this.containerMenu != this.inventoryMenu) {
2021-06-13 06:03:02 +02:00
@@ -898,8 +911,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
2021-06-13 06:03:02 +02:00
index 773c1e0ed3e10157c968d84b19947d99bb01371a..fd0c307a4b340661ba9aff9ae4e0055c139a1ebd 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
2021-06-13 06:03:02 +02:00
@@ -261,6 +261,7 @@ public abstract class LivingEntity extends Entity {
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() {
2021-06-13 06:03:02 +02:00
@@ -1446,13 +1447,12 @@ public abstract class LivingEntity extends Entity {
2021-06-11 14:02:28 +02:00
if (knockbackCancelled) this.level.broadcastEntityEvent(this, (byte) 2); // Paper - Disable explosion knockback
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);
2021-06-13 06:03:02 +02:00
@@ -1601,7 +1601,7 @@ public abstract class LivingEntity extends Entity {
if (!this.isRemoved() && !this.dead) {
2021-06-11 14:02:28 +02:00
Entity entity = source.getEntity();
LivingEntity entityliving = this.getKillCredit();
2021-06-13 06:03:02 +02:00
-
2021-06-11 14:02:28 +02:00
+ /* // Paper - move down to make death event cancellable - this is the runKillTrigger below
2021-06-13 06:03:02 +02:00
if (this.deathScore >= 0 && entityliving != null) {
entityliving.awardKillScore(this, this.deathScore, source);
2021-06-11 14:02:28 +02:00
}
2021-06-13 06:03:02 +02:00
@@ -1609,20 +1609,43 @@ public abstract class LivingEntity extends Entity {
2021-06-11 14:02:28 +02:00
if (this.isSleeping()) {
2021-06-13 06:03:02 +02:00
this.stopSleeping();
2021-06-11 14:02:28 +02:00
}
2021-06-13 06:03:02 +02:00
+ */ // Paper - move down to make death event cancellable - this is the runKillTrigger below
+
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
2021-06-11 14:02:28 +02:00
if (this.level instanceof ServerLevel) {
if (entity != null) {
- entity.killed((ServerLevel) this.level, this);
2021-06-13 06:03:02 +02:00
+ // Paper - move below into if for onKill
2021-06-11 14:02:28 +02:00
}
- this.dropAllDeathLoot(source);
+ // Paper start
2021-06-13 06:03:02 +02:00
+ org.bukkit.event.entity.EntityDeathEvent deathEvent = this.dropAllDeathLoot(source);
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) {
+ entityliving.awardKillScore(this, this.deathScore, source);
2021-06-11 14:02:28 +02:00
+ }
2021-06-13 06:03:02 +02:00
+
2021-06-11 14:02:28 +02:00
+ if (this.isSleeping()) {
+ this.stopSleeping();
+ }
2021-06-13 06:03:02 +02:00
+
+ this.getCombatTracker().recheckStatus();
2021-06-11 14:02:28 +02:00
+ if (entity != null) {
2021-06-13 06:03:02 +02:00
+ entity.killed((ServerLevel) this.level, this);
2021-06-11 14:02:28 +02:00
+ }
+ } else {
+ this.dead = false;
+ this.setHealth((float) deathEvent.getReviveHealth());
+ }
+ // Paper end
this.createWitherRose(entityliving);
}
+ if (this.dead) { // Paper
this.level.broadcastEntityEvent(this, (byte) 3);
this.setPose(Pose.DYING);
+ } // Paper
}
}
2021-06-13 06:03:02 +02:00
@@ -1630,7 +1653,7 @@ public abstract class LivingEntity extends Entity {
2021-06-11 14:02:28 +02:00
if (!this.level.isClientSide) {
boolean flag = false;
- if (adversary instanceof WitherBoss) {
+ if (this.dead && adversary instanceof WitherBoss) { // Paper
if (this.level.getGameRules().getBoolean(GameRules.RULE_MOBGRIEFING)) {
BlockPos blockposition = this.blockPosition();
BlockState iblockdata = Blocks.WITHER_ROSE.defaultBlockState();
2021-06-13 06:03:02 +02:00
@@ -1658,7 +1681,7 @@ public abstract class LivingEntity extends Entity {
2021-06-11 14:02:28 +02:00
}
}
- protected void dropAllDeathLoot(DamageSource source) {
2021-06-13 06:03:02 +02:00
+ protected org.bukkit.event.entity.EntityDeathEvent dropAllDeathLoot(DamageSource source) { // Paper
Entity entity = source.getEntity();
2021-06-11 14:02:28 +02:00
int i;
2021-06-13 06:03:02 +02:00
@@ -1676,15 +1699,18 @@ public abstract class LivingEntity extends Entity {
this.dropCustomDeathLoot(source, i, flag);
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-06-13 06:03:02 +02:00
@@ -1762,8 +1788,9 @@ public abstract class LivingEntity extends Entity {
2021-06-11 14:02:28 +02:00
return SoundEvents.GENERIC_HURT;
}
2021-06-13 06:03:02 +02:00
+
2021-06-11 14:02:28 +02:00
@Nullable
2021-06-13 06:03:02 +02:00
- protected SoundEvent getDeathSound() {
+ public final SoundEvent getDeathSoundPublic() { return this.getDeathSound(); } protected SoundEvent getDeathSound() { // Paper - provide PUBLIC accessor, overrides are hell to deal with
2021-06-11 14:02:28 +02:00
return SoundEvents.GENERIC_DEATH;
2021-06-13 06:03:02 +02:00
}
@@ -2477,7 +2504,7 @@ public abstract class LivingEntity extends Entity {
2021-06-11 14:02:28 +02:00
}
2021-06-13 06:03:02 +02:00
- protected float getSoundVolume() {
+ public final float getSoundVolumePublic() { return this.getSoundVolume(); } protected float getSoundVolume() { // Paper - provide PUBLIC accessor, overrides are hell
2021-06-11 14:02:28 +02:00
return 1.0F;
}
diff --git a/src/main/java/net/minecraft/world/entity/animal/Fox.java b/src/main/java/net/minecraft/world/entity/animal/Fox.java
2021-06-13 06:03:02 +02:00
index c1cdb1905536bda76f34ad3fc796996443839767..31f4e4a93ea5fd3ffe7e60dff2e2a9642b51daa2 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
2021-06-13 06:03:02 +02:00
@@ -691,15 +691,25 @@ public class Fox extends Animal {
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
}
public static boolean isPathClear(Fox fox, LivingEntity chasedEntity) {
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
2021-06-13 06:03:02 +02:00
index 224eca7d20cf4b890a6bc1b314d566e02e716762..7281eb294ddd178ba742088d3c61bf3d529ff0c4 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
@@ -68,11 +68,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
2021-06-13 06:03:02 +02:00
index 4adde11daa5ca97a2bf9c164c43c5c011a89b33d..cb9969d768b13863722aad3dc5daad3c10bb264a 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
2021-06-13 06:03:02 +02:00
@@ -755,7 +755,8 @@ public class ArmorStand extends LivingEntity {
2021-06-11 14:02:28 +02:00
@Override
public void kill() {
- 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
2021-06-13 06:03:02 +02:00
this.remove(Entity.RemovalReason.KILLED);
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
2021-06-13 06:03:02 +02:00
index 91dc9e3b02462211a6964204f5409fbae98027e6..22032108fdaed14663a9d7e0b6757eef5237c4dd 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
2021-06-13 06:03:02 +02:00
@@ -1849,7 +1849,14 @@ public class CraftPlayer extends CraftHumanEntity implements Player {
2021-06-11 14:02:28 +02:00
}
public void sendHealthUpdate() {
2021-06-13 06:03:02 +02:00
- this.getHandle().connection.send(new ClientboundSetHealthPacket(this.getScaledHealth(), this.getHandle().getFoodData().getFoodLevel(), this.getHandle().getFoodData().getSaturationLevel()));
2021-06-11 14:02:28 +02:00
+ // Paper start - cancellable death event
2021-06-13 06:03:02 +02:00
+ ClientboundSetHealthPacket packet = new ClientboundSetHealthPacket(this.getScaledHealth(), this.getHandle().getFoodData().getFoodLevel(), this.getHandle().getFoodData().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
2021-06-13 06:03:02 +02:00
index a81fc5212648056766824113b372a7c772c19e5a..f0c3255e0b4e1ecf4a54213c6458c92a7389b134 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
2021-06-13 06:03:02 +02:00
@@ -770,9 +770,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()) {
2021-06-13 06:03:02 +02:00
@@ -788,8 +795,15 @@ public class CraftEventFactory {
2021-06-11 14:02:28 +02:00
CraftPlayer entity = victim.getBukkitEntity();
PlayerDeathEvent event = new PlayerDeathEvent(entity, drops, victim.getExpReward(), 0, deathMessage, stringDeathMessage); // Paper - Adventure
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();
2021-06-13 06:03:02 +02:00
@@ -806,6 +820,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-06-13 06:03:02 +02:00
+ net.minecraft.sounds.SoundEvent soundEffect = victim.getDeathSoundPublic();
2021-06-11 14:02:28 +02:00
+ event.setDeathSound(soundEffect != null ? org.bukkit.craftbukkit.CraftSound.getBukkit(soundEffect) : null);
+ event.setDeathSoundCategory(org.bukkit.SoundCategory.valueOf(victim.getSoundSource().name()));
2021-06-13 06:03:02 +02:00
+ event.setDeathSoundVolume(victim.getSoundVolumePublic());
+ 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();
2021-06-13 06:03:02 +02:00
+ net.minecraft.sounds.SoundEvent soundEffect = org.bukkit.craftbukkit.CraftSound.getSoundEffect(event.getDeathSound());
+ net.minecraft.sounds.SoundSource soundCategory = net.minecraft.sounds.SoundSource.valueOf(event.getDeathSoundCategory().name());
2021-06-11 14:02:28 +02:00
+ victim.level.playSound(source, x, y, z, soundEffect, soundCategory, event.getDeathSoundVolume(), event.getDeathSoundPitch());
+ }
+ }
+ // Paper end
/**
* Server methods
*/