From 7deb3728e3697c4123ce2addce4bc27bf9e0a652 Mon Sep 17 00:00:00 2001 From: jojokobi Date: Sat, 5 Oct 2019 19:02:38 +0200 Subject: [PATCH] SPIGOT-5309: Call cancelled EntityDamageEvent when damaging invisible armor stands --- nms-patches/EntityArmorStand.patch | 5 ++- .../craftbukkit/event/CraftEventFactory.java | 38 +++++++++++++++---- 2 files changed, 33 insertions(+), 10 deletions(-) diff --git a/nms-patches/EntityArmorStand.patch b/nms-patches/EntityArmorStand.patch index e59b2edf8f..071f696d05 100644 --- a/nms-patches/EntityArmorStand.patch +++ b/nms-patches/EntityArmorStand.patch @@ -64,9 +64,10 @@ + // CraftBukkit end + this.killEntity(); // CraftBukkit - this.die() -> this.killEntity() return false; - } else if (!this.isInvulnerable(damagesource) && !this.bD && !this.isMarker()) { +- } else if (!this.isInvulnerable(damagesource) && !this.bD && !this.isMarker()) { ++ } else if (!this.isInvulnerable(damagesource) && (true || !this.bD) && !this.isMarker()) { // CraftBukkit + // CraftBukkit start -+ if (org.bukkit.craftbukkit.event.CraftEventFactory.handleNonLivingEntityDamageEvent(this, damagesource, f)) { ++ if (org.bukkit.craftbukkit.event.CraftEventFactory.handleNonLivingEntityDamageEvent(this, damagesource, f, true, this.bD)) { // PAIL: armorStandInvisible + return false; + } + // CraftBukkit end diff --git a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java index 22ba790994..0e78fdfd42 100644 --- a/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java +++ b/src/main/java/org/bukkit/craftbukkit/event/CraftEventFactory.java @@ -739,6 +739,10 @@ public class CraftEventFactory { } private static EntityDamageEvent handleEntityDamageEvent(Entity entity, DamageSource source, Map modifiers, Map> modifierFunctions) { + return handleEntityDamageEvent(entity, source, modifiers, modifierFunctions, false); + } + + private static EntityDamageEvent handleEntityDamageEvent(Entity entity, DamageSource source, Map modifiers, Map> modifierFunctions, boolean cancelled) { if (source.isExplosion()) { DamageCause damageCause; Entity damager = entityDamage; @@ -756,6 +760,7 @@ public class CraftEventFactory { } event = new EntityDamageByEntityEvent(damager.getBukkitEntity(), entity.getBukkitEntity(), damageCause, modifiers, modifierFunctions); } + event.setCancelled(cancelled); callEvent(event); @@ -778,15 +783,19 @@ public class CraftEventFactory { cause = DamageCause.THORNS; } - return callEntityDamageEvent(damager, entity, cause, modifiers, modifierFunctions); + return callEntityDamageEvent(damager, entity, cause, modifiers, modifierFunctions, cancelled); } else if (source == DamageSource.OUT_OF_WORLD) { - EntityDamageEvent event = callEvent(new EntityDamageByBlockEvent(null, entity.getBukkitEntity(), DamageCause.VOID, modifiers, modifierFunctions)); + EntityDamageEvent event = new EntityDamageByBlockEvent(null, entity.getBukkitEntity(), DamageCause.VOID, modifiers, modifierFunctions); + event.setCancelled(cancelled); + callEvent(event); if (!event.isCancelled()) { event.getEntity().setLastDamageCause(event); } return event; } else if (source == DamageSource.LAVA) { - EntityDamageEvent event = callEvent(new EntityDamageByBlockEvent(null, entity.getBukkitEntity(), DamageCause.LAVA, modifiers, modifierFunctions)); + EntityDamageEvent event = (new EntityDamageByBlockEvent(null, entity.getBukkitEntity(), DamageCause.LAVA, modifiers, modifierFunctions)); + event.setCancelled(cancelled); + callEvent(event); if (!event.isCancelled()) { event.getEntity().setLastDamageCause(event); } @@ -804,7 +813,9 @@ public class CraftEventFactory { } else { throw new IllegalStateException(String.format("Unhandled damage of %s by %s from %s", entity, damager, source.translationIndex)); } - EntityDamageEvent event = callEvent(new EntityDamageByBlockEvent(damager, entity.getBukkitEntity(), cause, modifiers, modifierFunctions)); + EntityDamageEvent event = new EntityDamageByBlockEvent(damager, entity.getBukkitEntity(), cause, modifiers, modifierFunctions); + event.setCancelled(cancelled); + callEvent(event); if (!event.isCancelled()) { event.getEntity().setLastDamageCause(event); } @@ -826,7 +837,9 @@ public class CraftEventFactory { } else { throw new IllegalStateException(String.format("Unhandled damage of %s by %s from %s", entity, damager.getHandle(), source.translationIndex)); } - EntityDamageEvent event = callEvent(new EntityDamageByEntityEvent(damager, entity.getBukkitEntity(), cause, modifiers, modifierFunctions)); + EntityDamageEvent event = new EntityDamageByEntityEvent(damager, entity.getBukkitEntity(), cause, modifiers, modifierFunctions); + event.setCancelled(cancelled); + callEvent(event); if (!event.isCancelled()) { event.getEntity().setLastDamageCause(event); } @@ -865,20 +878,24 @@ public class CraftEventFactory { } if (cause != null) { - return callEntityDamageEvent(null, entity, cause, modifiers, modifierFunctions); + return callEntityDamageEvent(null, entity, cause, modifiers, modifierFunctions, cancelled); } throw new IllegalStateException(String.format("Unhandled damage of %s from %s", entity, source.translationIndex)); } private static EntityDamageEvent callEntityDamageEvent(Entity damager, Entity damagee, DamageCause cause, Map modifiers, Map> modifierFunctions) { + return callEntityDamageEvent(damager, damagee, cause, modifiers, modifierFunctions, false); + } + + private static EntityDamageEvent callEntityDamageEvent(Entity damager, Entity damagee, DamageCause cause, Map modifiers, Map> modifierFunctions, boolean cancelled) { EntityDamageEvent event; if (damager != null) { event = new EntityDamageByEntityEvent(damager.getBukkitEntity(), damagee.getBukkitEntity(), cause, modifiers, modifierFunctions); } else { event = new EntityDamageEvent(damagee.getBukkitEntity(), cause, modifiers, modifierFunctions); } - + event.setCancelled(cancelled); callEvent(event); if (!event.isCancelled()) { @@ -920,6 +937,10 @@ public class CraftEventFactory { } public static boolean handleNonLivingEntityDamageEvent(Entity entity, DamageSource source, double damage, boolean cancelOnZeroDamage) { + return handleNonLivingEntityDamageEvent(entity, source, damage, cancelOnZeroDamage, false); + } + + public static boolean handleNonLivingEntityDamageEvent(Entity entity, DamageSource source, double damage, boolean cancelOnZeroDamage, boolean cancelled) { if (entity instanceof EntityEnderCrystal && !(source instanceof EntityDamageSource)) { return false; } @@ -930,7 +951,8 @@ public class CraftEventFactory { modifiers.put(DamageModifier.BASE, damage); functions.put(DamageModifier.BASE, ZERO); - final EntityDamageEvent event = handleEntityDamageEvent(entity, source, modifiers, functions); + final EntityDamageEvent event = handleEntityDamageEvent(entity, source, modifiers, functions, cancelled); + if (event == null) { return false; }