From e32e0df220b51c127a29b8c9a0a701cccbae7f57 Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 14 May 2024 11:14:49 -0700 Subject: [PATCH] Fixes #150 EntityDamageByAcidEvent should be cancellable --- .../events/EntityDamageByAcidEvent.java | 17 +++++++++++++++-- .../acidisland/listeners/AcidEffect.java | 16 ++++++++++------ .../bentobox/acidisland/world/AcidTask.java | 4 +++- 3 files changed, 28 insertions(+), 9 deletions(-) diff --git a/src/main/java/world/bentobox/acidisland/events/EntityDamageByAcidEvent.java b/src/main/java/world/bentobox/acidisland/events/EntityDamageByAcidEvent.java index cdeb4f6..e041c02 100644 --- a/src/main/java/world/bentobox/acidisland/events/EntityDamageByAcidEvent.java +++ b/src/main/java/world/bentobox/acidisland/events/EntityDamageByAcidEvent.java @@ -1,21 +1,23 @@ package world.bentobox.acidisland.events; import org.bukkit.entity.Entity; +import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; /** * Fired when an entity (items excluded) receives damage from acid - * @author Poslovitch + * @author Poslovitch, tastybento * @since 1.0 */ -public class EntityDamageByAcidEvent extends Event { +public class EntityDamageByAcidEvent extends Event implements Cancellable { private final Entity entity; private double damage; public enum Acid { RAIN, WATER } private final Acid cause; + private boolean cancelled; private static final HandlerList handlers = new HandlerList(); @Override @@ -64,4 +66,15 @@ public void setDamage(double damage) { public Acid getCause() { return cause; } + + @Override + public boolean isCancelled() { + return cancelled; + } + + @Override + public void setCancelled(boolean cancel) { + this.cancelled = cancel; + + } } diff --git a/src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java b/src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java index 887454a..5b16f65 100644 --- a/src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java +++ b/src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java @@ -187,11 +187,13 @@ protected boolean checkForRain(Player player) { .addPotionEffect(new PotionEffect(t, addon.getSettings().getRainEffectDuation() * 20, 1))); // Apply damage if there is any if (event.getRainDamage() > 0D) { - player.damage(event.getRainDamage()); - player.getWorld().playSound(player.getLocation(), Sound.ENTITY_CREEPER_PRIMED, 3F, 3F); EntityDamageByAcidEvent e = new EntityDamageByAcidEvent(player, event.getRainDamage(), Acid.RAIN); // Fire event Bukkit.getPluginManager().callEvent(e); + if (!e.isCancelled()) { + player.damage(event.getRainDamage()); + player.getWorld().playSound(player.getLocation(), Sound.ENTITY_CREEPER_PRIMED, 3F, 3F); + } } } } @@ -211,12 +213,14 @@ protected boolean continuouslyHurtPlayer(Player player) { event.getPotionEffects().stream().filter(EFFECTS::contains).forEach(t -> player .addPotionEffect(new PotionEffect(t, addon.getSettings().getAcidEffectDuation() * 20, 1))); // Apply damage if there is any - if (event.getTotalDamage() > 0D) { - player.damage(event.getTotalDamage()); - player.getWorld().playSound(player.getLocation(), Sound.ENTITY_CREEPER_PRIMED, 3F, 3F); + if (event.getTotalDamage() > 0D) { EntityDamageByAcidEvent e = new EntityDamageByAcidEvent(player, event.getTotalDamage(), Acid.WATER); // Fire event Bukkit.getPluginManager().callEvent(e); + if (!e.isCancelled()) { + player.damage(event.getTotalDamage()); + player.getWorld().playSound(player.getLocation(), Sound.ENTITY_CREEPER_PRIMED, 3F, 3F); + } } } } @@ -232,7 +236,7 @@ private boolean isSafeFromRain(Player player) { if (isEssentialsGodMode(player) || player.getWorld().getEnvironment().equals(Environment.NETHER) || player.getWorld().getEnvironment().equals(Environment.THE_END) || (addon.getSettings().isHelmetProtection() && (player.getInventory().getHelmet() != null - && player.getInventory().getHelmet().getType().name().contains("HELMET"))) + && player.getInventory().getHelmet().getType().name().contains("HELMET"))) || (!addon.getSettings().isAcidDamageSnow() && player.getLocation().getBlock().getTemperature() < 0.1) // snow falls || player.getLocation().getBlock().getHumidity() == 0 // dry || (player.getActivePotionEffects().stream().map(PotionEffect::getType) diff --git a/src/main/java/world/bentobox/acidisland/world/AcidTask.java b/src/main/java/world/bentobox/acidisland/world/AcidTask.java index 1e9dab2..f5be30e 100644 --- a/src/main/java/world/bentobox/acidisland/world/AcidTask.java +++ b/src/main/java/world/bentobox/acidisland/world/AcidTask.java @@ -86,10 +86,12 @@ void findEntities() { void applyDamage(Entity e, long damage) { if (e instanceof LivingEntity) { double actualDamage = Math.max(0, damage - damage * AcidEffect.getDamageReduced((LivingEntity)e)); - ((LivingEntity)e).damage(actualDamage); EntityDamageByAcidEvent event = new EntityDamageByAcidEvent(e, actualDamage, Acid.WATER); // Fire event Bukkit.getPluginManager().callEvent(event); + if (!event.isCancelled()) { + ((LivingEntity)e).damage(actualDamage); + } } else if (addon.getSettings().getAcidDestroyItemTime() > 0 && e instanceof Item){ // Item if (e.getLocation().getBlock().getType().equals(Material.WATER)) {