Fixes #150 EntityDamageByAcidEvent should be cancellable

This commit is contained in:
tastybento 2024-05-14 11:14:49 -07:00
parent 1058b5fbc1
commit e32e0df220
3 changed files with 28 additions and 9 deletions

View File

@ -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 class EntityDamageByAcidEvent extends Event {
public Acid getCause() {
return cause;
}
@Override
public boolean isCancelled() {
return cancelled;
}
@Override
public void setCancelled(boolean cancel) {
this.cancelled = cancel;
}
}

View File

@ -187,11 +187,13 @@ public class AcidEffect implements Listener {
.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 @@ public class AcidEffect implements Listener {
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 @@ public class AcidEffect implements Listener {
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)

View File

@ -86,10 +86,12 @@ public class AcidTask {
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)) {