Skip to content

Commit

Permalink
Merge pull request #151 from BentoBoxWorld/150_EntityDamageByAcidEven…
Browse files Browse the repository at this point in the history
…t_should_be_cancellable

Fixes #150 EntityDamageByAcidEvent should be cancellable
  • Loading branch information
tastybento authored May 14, 2024
2 parents 1058b5f + e32e0df commit 7b23229
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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;

}
}
16 changes: 10 additions & 6 deletions src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
}
Expand All @@ -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);
}
}
}
}
Expand All @@ -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)
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/world/bentobox/acidisland/world/AcidTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
Expand Down

0 comments on commit 7b23229

Please sign in to comment.