From 6708937c17eb9a7380ad95b060261434609d48b5 Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 14 Aug 2020 15:31:07 -0700 Subject: [PATCH] Protects visitors from acid damage. Sets default for to allow acid damage for visitors. Fixes https://github.com/BentoBoxWorld/AcidIsland/issues/93 --- .../acidisland/listeners/AcidEffect.java | 13 ++++- .../bentobox/acidisland/world/AcidTask.java | 1 - src/main/resources/config.yml | 1 - .../acidisland/listeners/AcidEffectTest.java | 51 +++++++++++++++++-- 4 files changed, 59 insertions(+), 7 deletions(-) diff --git a/src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java b/src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java index 1353d7f..a17f255 100644 --- a/src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java +++ b/src/main/java/world/bentobox/acidisland/listeners/AcidEffect.java @@ -19,6 +19,7 @@ import org.bukkit.entity.Player; import org.bukkit.event.EventHandler; import org.bukkit.event.EventPriority; import org.bukkit.event.Listener; +import org.bukkit.event.entity.EntityDamageEvent.DamageCause; import org.bukkit.event.entity.PlayerDeathEvent; import org.bukkit.event.player.PlayerMoveEvent; import org.bukkit.inventory.EntityEquipment; @@ -36,6 +37,7 @@ import world.bentobox.acidisland.AcidIsland; import world.bentobox.acidisland.events.AcidEvent; import world.bentobox.acidisland.events.AcidRainEvent; import world.bentobox.acidisland.world.AcidTask; +import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.util.Util; /** @@ -188,6 +190,9 @@ public class AcidEffect implements Listener { || (!addon.getSettings().isAcidDamageSnow() && player.getLocation().getBlock().getTemperature() < 0.1) // snow falls || player.getLocation().getBlock().getHumidity() == 0 // dry || (player.getActivePotionEffects().stream().map(PotionEffect::getType).anyMatch(IMMUNE_EFFECTS::contains)) + // Protect visitors + || (addon.getPlugin().getIWM().getIvSettings(player.getWorld()).contains(DamageCause.CUSTOM.name()) + && !addon.getIslands().userIsOnIsland(player.getWorld(), User.getInstance(player))) ) { return true; } @@ -207,7 +212,13 @@ public class AcidEffect implements Listener { */ private boolean isSafeFromAcid(Player player) { // Check for GodMode - if (isEssentialsGodMode(player)) return true; + if (isEssentialsGodMode(player) + // Protect visitors + || (addon.getPlugin().getIWM().getIvSettings(player.getWorld()).contains(DamageCause.CUSTOM.name()) + && !addon.getIslands().userIsOnIsland(player.getWorld(), User.getInstance(player))) + ) { + return true; + } // Not in liquid or on snow if (!player.getLocation().getBlock().getType().equals(Material.WATER) && !player.getLocation().getBlock().getType().equals(Material.BUBBLE_COLUMN) diff --git a/src/main/java/world/bentobox/acidisland/world/AcidTask.java b/src/main/java/world/bentobox/acidisland/world/AcidTask.java index 80a98f1..1407e39 100644 --- a/src/main/java/world/bentobox/acidisland/world/AcidTask.java +++ b/src/main/java/world/bentobox/acidisland/world/AcidTask.java @@ -22,7 +22,6 @@ import org.bukkit.scheduler.BukkitTask; import world.bentobox.acidisland.AcidIsland; import world.bentobox.acidisland.listeners.AcidEffect; -import world.bentobox.bentobox.BentoBox; public class AcidTask { private final AcidIsland addon; diff --git a/src/main/resources/config.yml b/src/main/resources/config.yml index f87f224..3fa9fc3 100644 --- a/src/main/resources/config.yml +++ b/src/main/resources/config.yml @@ -443,7 +443,6 @@ protection: invincible-visitors: - BLOCK_EXPLOSION - CONTACT - - CUSTOM - DROWNING - ENTITY_ATTACK - ENTITY_EXPLOSION diff --git a/src/test/java/world/bentobox/acidisland/listeners/AcidEffectTest.java b/src/test/java/world/bentobox/acidisland/listeners/AcidEffectTest.java index 898d106..5027fd5 100644 --- a/src/test/java/world/bentobox/acidisland/listeners/AcidEffectTest.java +++ b/src/test/java/world/bentobox/acidisland/listeners/AcidEffectTest.java @@ -12,6 +12,7 @@ import static org.mockito.Mockito.when; import java.util.ArrayList; import java.util.Collection; +import java.util.Collections; import org.bukkit.Bukkit; import org.bukkit.GameMode; @@ -55,6 +56,9 @@ import com.earth2me.essentials.User; import world.bentobox.acidisland.AISettings; import world.bentobox.acidisland.AcidIsland; +import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.managers.IslandWorldManager; +import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.PlayersManager; import world.bentobox.bentobox.util.Util; @@ -102,6 +106,12 @@ public class AcidEffectTest { private Essentials essentials; @Mock private User essentialsUser; + @Mock + private BentoBox plugin; + @Mock + private IslandWorldManager iwm; + @Mock + private IslandsManager im; /** @@ -165,6 +175,16 @@ public class AcidEffectTest { when(world.getMaxHeight()).thenReturn(5); when(world.getEnvironment()).thenReturn(Environment.NORMAL); + // Plugin + when(addon.getPlugin()).thenReturn(plugin); + when(plugin.getIWM()).thenReturn(iwm); + // CUSTOM damage protection + when(iwm.getIvSettings(any())).thenReturn(Collections.singletonList("CUSTOM")); + + // Island manager + when(addon.getIslands()).thenReturn(im); + when(im.userIsOnIsland(any(), any())).thenReturn(true); + ae = new AcidEffect(addon); } @@ -251,6 +271,29 @@ public class AcidEffectTest { verify(settings, times(2)).getAcidDamageDelay(); } + /** + * Test method for {@link world.bentobox.acidisland.listeners.AcidEffect#onPlayerMove(org.bukkit.event.player.PlayerMoveEvent)}. + */ + @Test + public void testOnPlayerMoveVisitorNoAcidAndRainDamage() { + when(im.userIsOnIsland(any(), any())).thenReturn(false); + PlayerMoveEvent e = new PlayerMoveEvent(player, from, to); + ae.onPlayerMove(e); + verify(settings, never()).getAcidDamageDelay(); + } + + /** + * Test method for {@link world.bentobox.acidisland.listeners.AcidEffect#onPlayerMove(org.bukkit.event.player.PlayerMoveEvent)}. + */ + @Test + public void testOnPlayerMoveVisitorAcidAndRainDamage() { + // No protection against CUSTOM damage + when(iwm.getIvSettings(any())).thenReturn(Collections.emptyList()); + PlayerMoveEvent e = new PlayerMoveEvent(player, from, to); + ae.onPlayerMove(e); + verify(settings, times(2)).getAcidDamageDelay(); + } + /** * Test method for {@link world.bentobox.acidisland.listeners.AcidEffect#onPlayerMove(org.bukkit.event.player.PlayerMoveEvent)}. */ @@ -346,8 +389,8 @@ public class AcidEffectTest { PlayerMoveEvent e = new PlayerMoveEvent(player, from, to); ae.onPlayerMove(e); - // 2 times only - verify(addon, times(2)).getPlugin(); + // 3 times only + verify(addon, times(3)).getPlugin(); } /** @@ -362,8 +405,8 @@ public class AcidEffectTest { PlayerMoveEvent e = new PlayerMoveEvent(player, from, to); ae.onPlayerMove(e); - // 2 times only - verify(addon, times(2)).getPlugin(); + // 3 times only + verify(addon, times(3)).getPlugin(); } /**