Merge pull request #151 from BentoBoxWorld/150_EntityDamageByAcidEvent_should_be_cancellable

Fixes #150 EntityDamageByAcidEvent should be cancellable
This commit is contained in:
tastybento 2024-05-14 11:19:08 -07:00 committed by GitHub
commit 7b232294ed
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 28 additions and 9 deletions

View File

@ -1,21 +1,23 @@
package world.bentobox.acidisland.events; package world.bentobox.acidisland.events;
import org.bukkit.entity.Entity; import org.bukkit.entity.Entity;
import org.bukkit.event.Cancellable;
import org.bukkit.event.Event; import org.bukkit.event.Event;
import org.bukkit.event.HandlerList; import org.bukkit.event.HandlerList;
/** /**
* Fired when an entity (items excluded) receives damage from acid * Fired when an entity (items excluded) receives damage from acid
* @author Poslovitch * @author Poslovitch, tastybento
* @since 1.0 * @since 1.0
*/ */
public class EntityDamageByAcidEvent extends Event { public class EntityDamageByAcidEvent extends Event implements Cancellable {
private final Entity entity; private final Entity entity;
private double damage; private double damage;
public enum Acid { RAIN, WATER } public enum Acid { RAIN, WATER }
private final Acid cause; private final Acid cause;
private boolean cancelled;
private static final HandlerList handlers = new HandlerList(); private static final HandlerList handlers = new HandlerList();
@Override @Override
@ -64,4 +66,15 @@ public class EntityDamageByAcidEvent extends Event {
public Acid getCause() { public Acid getCause() {
return cause; 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))); .addPotionEffect(new PotionEffect(t, addon.getSettings().getRainEffectDuation() * 20, 1)));
// Apply damage if there is any // Apply damage if there is any
if (event.getRainDamage() > 0D) { 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); EntityDamageByAcidEvent e = new EntityDamageByAcidEvent(player, event.getRainDamage(), Acid.RAIN);
// Fire event // Fire event
Bukkit.getPluginManager().callEvent(e); Bukkit.getPluginManager().callEvent(e);
if (!e.isCancelled()) {
player.damage(event.getRainDamage());
player.getWorld().playSound(player.getLocation(), Sound.ENTITY_CREEPER_PRIMED, 3F, 3F);
}
} }
} }
} }
@ -212,11 +214,13 @@ public class AcidEffect implements Listener {
.addPotionEffect(new PotionEffect(t, addon.getSettings().getAcidEffectDuation() * 20, 1))); .addPotionEffect(new PotionEffect(t, addon.getSettings().getAcidEffectDuation() * 20, 1)));
// Apply damage if there is any // Apply damage if there is any
if (event.getTotalDamage() > 0D) { if (event.getTotalDamage() > 0D) {
player.damage(event.getTotalDamage());
player.getWorld().playSound(player.getLocation(), Sound.ENTITY_CREEPER_PRIMED, 3F, 3F);
EntityDamageByAcidEvent e = new EntityDamageByAcidEvent(player, event.getTotalDamage(), Acid.WATER); EntityDamageByAcidEvent e = new EntityDamageByAcidEvent(player, event.getTotalDamage(), Acid.WATER);
// Fire event // Fire event
Bukkit.getPluginManager().callEvent(e); Bukkit.getPluginManager().callEvent(e);
if (!e.isCancelled()) {
player.damage(event.getTotalDamage());
player.getWorld().playSound(player.getLocation(), Sound.ENTITY_CREEPER_PRIMED, 3F, 3F);
}
} }
} }
} }

View File

@ -86,10 +86,12 @@ public class AcidTask {
void applyDamage(Entity e, long damage) { void applyDamage(Entity e, long damage) {
if (e instanceof LivingEntity) { if (e instanceof LivingEntity) {
double actualDamage = Math.max(0, damage - damage * AcidEffect.getDamageReduced((LivingEntity)e)); double actualDamage = Math.max(0, damage - damage * AcidEffect.getDamageReduced((LivingEntity)e));
((LivingEntity)e).damage(actualDamage);
EntityDamageByAcidEvent event = new EntityDamageByAcidEvent(e, actualDamage, Acid.WATER); EntityDamageByAcidEvent event = new EntityDamageByAcidEvent(e, actualDamage, Acid.WATER);
// Fire event // Fire event
Bukkit.getPluginManager().callEvent(event); Bukkit.getPluginManager().callEvent(event);
if (!event.isCancelled()) {
((LivingEntity)e).damage(actualDamage);
}
} else if (addon.getSettings().getAcidDestroyItemTime() > 0 && e instanceof Item){ } else if (addon.getSettings().getAcidDestroyItemTime() > 0 && e instanceof Item){
// Item // Item
if (e.getLocation().getBlock().getType().equals(Material.WATER)) { if (e.getLocation().getBlock().getType().equals(Material.WATER)) {