From f2b53bb2f89d5a8d06657ecb46be3a7268796d90 Mon Sep 17 00:00:00 2001 From: Florian CUNY Date: Sun, 8 Dec 2019 11:11:02 +0100 Subject: [PATCH] Added potion effects for acid rain Implemented https://github.com/BentoBoxWorld/AcidIsland/issues/24 Also improved some config comments --- .../world/bentobox/acidisland/AISettings.java | 28 ++++++++++++++++-- .../bentobox/acidisland/events/AcidEvent.java | 2 +- .../acidisland/events/AcidRainEvent.java | 29 ++++++++++++++++++- .../acidisland/listeners/AcidEffect.java | 15 ++++++---- .../acidisland/events/AcidRainEventTest.java | 21 +++++++++++++- 5 files changed, 85 insertions(+), 10 deletions(-) diff --git a/src/main/java/world/bentobox/acidisland/AISettings.java b/src/main/java/world/bentobox/acidisland/AISettings.java index ec847cc..a35743d 100644 --- a/src/main/java/world/bentobox/acidisland/AISettings.java +++ b/src/main/java/world/bentobox/acidisland/AISettings.java @@ -69,7 +69,7 @@ public class AISettings implements WorldSettings { @ConfigEntry(path = "acid.damage.acid.item") private long acidDestroyItemTime = 0; - @ConfigComment("Damage from acid rain") + @ConfigComment("Damage from acid rain (and snow, if toggled on).") @ConfigEntry(path = "acid.damage.rain") private int acidRainDamage = 1; @@ -82,12 +82,18 @@ public class AISettings implements WorldSettings { @ConfigEntry(path = "acid.damage.delay") private long acidDamageDelay = 2; - @ConfigComment("Portion effects from going into acid water") + @ConfigComment("Potion effects from going into acid water") @ConfigComment("You can list multiple effects") @ConfigEntry(path = "acid.damage.effects") @Adapter(PotionEffectListAdapter.class) private List acidEffects = new ArrayList<>(); + @ConfigComment("Potion effects from going into acid rain and snow") + @ConfigComment("You can list multiple effects.") + @ConfigEntry(path = "acid.damage.rain-effects", since = "1.9.1") + @Adapter(PotionEffectListAdapter.class) + private List acidRainEffects = new ArrayList<>(); + @ConfigComment("If player wears a helmet then they will not suffer from acid rain") @ConfigEntry(path = "acid.damage.protection.helmet") private boolean helmetProtection; @@ -1507,4 +1513,22 @@ public class AISettings implements WorldSettings { { this.createIslandOnFirstLoginAbortOnLogout = createIslandOnFirstLoginAbortOnLogout; } + + /** + * + * @return + * @since 1.9.1 + */ + public List getAcidRainEffects() { + return acidRainEffects; + } + + /** + * + * @param acidRainEffects + * @since 1.9.1 + */ + public void setAcidRainEffects(List acidRainEffects) { + this.acidRainEffects = acidRainEffects; + } } diff --git a/src/main/java/world/bentobox/acidisland/events/AcidEvent.java b/src/main/java/world/bentobox/acidisland/events/AcidEvent.java index 80d9ff0..667f6ce 100644 --- a/src/main/java/world/bentobox/acidisland/events/AcidEvent.java +++ b/src/main/java/world/bentobox/acidisland/events/AcidEvent.java @@ -21,7 +21,7 @@ public class AcidEvent extends Event implements Cancellable { private Player player; private double totalDamage; private final double protection; - private List potionEffects = new ArrayList<>(); + private List potionEffects; /** * @param player - player diff --git a/src/main/java/world/bentobox/acidisland/events/AcidRainEvent.java b/src/main/java/world/bentobox/acidisland/events/AcidRainEvent.java index c6f6962..60e8e20 100644 --- a/src/main/java/world/bentobox/acidisland/events/AcidRainEvent.java +++ b/src/main/java/world/bentobox/acidisland/events/AcidRainEvent.java @@ -4,6 +4,9 @@ import org.bukkit.entity.Player; import org.bukkit.event.Cancellable; import org.bukkit.event.Event; import org.bukkit.event.HandlerList; +import org.bukkit.potion.PotionEffectType; + +import java.util.List; /** * This event is fired when a player is going to be burned by acid rain @@ -17,6 +20,10 @@ public class AcidRainEvent extends Event implements Cancellable { private Player player; private double rainDamage; private final double protection; + /** + * @since 1.9.1 + */ + private List potionEffects; private boolean cancelled; @@ -26,10 +33,11 @@ public class AcidRainEvent extends Event implements Cancellable { * @param rainDamage * @param protection */ - public AcidRainEvent(Player player, double rainDamage, double protection) { + public AcidRainEvent(Player player, double rainDamage, double protection, List potionEffects) { this.player = player; this.rainDamage = rainDamage; this.protection = protection; + this.potionEffects = potionEffects; } /** @@ -69,6 +77,25 @@ public class AcidRainEvent extends Event implements Cancellable { this.rainDamage = rainDamage; } + /** + * Returns the potion effects that will be applied to the player. + * @return + * @since 1.9.1 + */ + public List getPotionEffects() { + return potionEffects; + } + + /** + * + * @param potionEffects the potionEffects to set + * @return + * @since 1.9.1 + */ + public void setPotionEffects(List potionEffects) { + this.potionEffects = potionEffects; + } + @Override public HandlerList getHandlers() { return handlers; diff --git a/src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java b/src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java index 0f30431..308a60d 100644 --- a/src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java +++ b/src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java @@ -116,11 +116,16 @@ public class AcidEffect implements Listener { } else if (wetPlayers.containsKey(player) && wetPlayers.get(player) < System.currentTimeMillis()) { double protection = addon.getSettings().getAcidRainDamage() * getDamageReduced(player); double totalDamage = Math.max(0, addon.getSettings().getAcidRainDamage() - protection); - AcidRainEvent e = new AcidRainEvent(player, totalDamage, protection); - addon.getServer().getPluginManager().callEvent(e); - if (!e.isCancelled()) { - player.damage(e.getRainDamage()); - player.getWorld().playSound(playerLoc, Sound.ENTITY_CREEPER_PRIMED, 3F, 3F); + AcidRainEvent event = new AcidRainEvent(player, totalDamage, protection, addon.getSettings().getAcidRainEffects()); + addon.getServer().getPluginManager().callEvent(event); + if (!event.isCancelled()) { + event.getPotionEffects().stream().filter(EFFECTS::contains).forEach(t -> player.addPotionEffect(new PotionEffect(t, 600, 1))); + event.getPotionEffects().stream().filter(e -> e.equals(PotionEffectType.POISON)).forEach(t -> player.addPotionEffect(new PotionEffect(t, 200, 1))); + // Apply damage if there is any + if (event.getRainDamage() > 0D) { + player.damage(event.getRainDamage()); + player.getWorld().playSound(playerLoc, Sound.ENTITY_CREEPER_PRIMED, 3F, 3F); + } } } } diff --git a/src/test/java/world/bentobox/acidisland/events/AcidRainEventTest.java b/src/test/java/world/bentobox/acidisland/events/AcidRainEventTest.java index 7aac502..c8899a6 100644 --- a/src/test/java/world/bentobox/acidisland/events/AcidRainEventTest.java +++ b/src/test/java/world/bentobox/acidisland/events/AcidRainEventTest.java @@ -7,19 +7,27 @@ import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.mock; import org.bukkit.entity.Player; +import org.bukkit.potion.PotionEffectType; +import org.junit.Assert; import org.junit.Before; import org.junit.Test; import org.mockito.Mock; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; + public class AcidRainEventTest { @Mock private Player player; + private List effects; private AcidRainEvent e; @Before public void setUp() throws Exception { - e = new AcidRainEvent(player, 10, 5); + effects = Arrays.asList(PotionEffectType.values()); + e = new AcidRainEvent(player, 10, 5, effects); } @Test @@ -55,6 +63,17 @@ public class AcidRainEventTest { assertTrue(e.getRainDamage() == 50D); } + @Test + public void testGetPotionEffects() { + Assert.assertArrayEquals(PotionEffectType.values(), e.getPotionEffects().toArray()); + } + + @Test + public void testSetPotionEffects() { + e.setPotionEffects(new ArrayList<>()); + assertTrue(e.getPotionEffects().isEmpty()); + } + @Test public void testIsCancelled() { assertFalse(e.isCancelled());