From 2cb1e7c30626984f0b3041aec124b1ab52be4800 Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 26 Nov 2020 18:05:26 -0800 Subject: [PATCH] Fixes https://github.com/BentoBoxWorld/BentoBox/issues/1579 --- .../flags/protection/TNTListener.java | 18 +++---- .../flags/protection/TNTListenerTest.java | 54 +++++++++++++++++++ 2 files changed, 62 insertions(+), 10 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/TNTListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/TNTListener.java index b91deeae2..2a55db41a 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/protection/TNTListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/protection/TNTListener.java @@ -18,7 +18,6 @@ import org.bukkit.event.entity.EntityDamageEvent; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.player.PlayerInteractEvent; -import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.api.flags.FlagListener; import world.bentobox.bentobox.lists.Flags; @@ -85,11 +84,11 @@ public class TNTListener extends FlagListener { */ @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onExplosion(final EntityExplodeEvent e) { - if (TNT_TYPES.contains(e.getEntityType())) { + // Check world and types + if (getIWM().inWorld(e.getLocation()) && TNT_TYPES.contains(e.getEntityType())) { // Remove any blocks from the explosion list if required e.blockList().removeIf(b -> protect(b.getLocation())); e.setCancelled(protect(e.getLocation())); - BentoBox.getInstance().logDebug(e.getEventName() + " " + e.isCancelled()); } } @@ -105,13 +104,12 @@ public class TNTListener extends FlagListener { */ @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onExplosion(final EntityDamageByEntityEvent e) { - // Check if this a TNT exploding - if (!e.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION) || !TNT_TYPES.contains(e.getDamager().getType())) { - return; + // Check if this in world, an explosion, and TNT exploding + if (getIWM().inWorld(e.getEntity().getLocation()) + && e.getCause().equals(EntityDamageEvent.DamageCause.ENTITY_EXPLOSION) + && TNT_TYPES.contains(e.getDamager().getType())) { + // Check if it is disallowed, then cancel it. + e.setCancelled(protect(e.getEntity().getLocation())); } - // Check if it is disallowed, then cancel it. - e.setCancelled(protect(e.getEntity().getLocation())); - - BentoBox.getInstance().logDebug(e.getEventName() + " " + e.isCancelled()); } } diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java index b01a7c206..4ad9dbe2a 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/protection/TNTListenerTest.java @@ -35,6 +35,8 @@ import org.bukkit.entity.Zombie; import org.bukkit.event.Event.Result; import org.bukkit.event.block.Action; import org.bukkit.event.entity.EntityChangeBlockEvent; +import org.bukkit.event.entity.EntityDamageByEntityEvent; +import org.bukkit.event.entity.EntityDamageEvent.DamageCause; import org.bukkit.event.entity.EntityExplodeEvent; import org.bukkit.event.player.PlayerInteractEvent; import org.bukkit.inventory.ItemStack; @@ -176,6 +178,13 @@ public class TNTListenerTest { // Entity when(entity.getType()).thenReturn(EntityType.PRIMED_TNT); when(entity.getWorld()).thenReturn(world); + when(entity.getLocation()).thenReturn(location); + + // Player + when(player.getLocation()).thenReturn(location); + + // In world + when(iwm.inWorld(any(Location.class))).thenReturn(true); listener = new TNTListener(); @@ -238,6 +247,16 @@ public class TNTListenerTest { assertFalse(list.isEmpty()); } + @Test + public void testOnExplosionWrongWorld() { + when(iwm.inWorld(any(Location.class))).thenReturn(false); + List list = new ArrayList<>(); + list.add(block); + EntityExplodeEvent e = new EntityExplodeEvent(entity, location, list, 0); + listener.onExplosion(e); + assertFalse(list.isEmpty()); + } + @Test public void testOnTNTDamageInWorldTNTNotProjectile() { // Block on fire @@ -389,5 +408,40 @@ public class TNTListenerTest { } + @Test + public void testOnEntityExplosion() { + EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(entity, player, DamageCause.ENTITY_EXPLOSION, 20D); + listener.onExplosion(e); + assertTrue(e.isCancelled()); + } + + @Test + public void testOnEntityExplosionOutsideIsland() { + Flags.WORLD_TNT_DAMAGE.setDefaultSetting(false); + assertFalse(Flags.WORLD_TNT_DAMAGE.isSetForWorld(world)); + when(im.getProtectedIslandAt(any())).thenReturn(Optional.empty()); + EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(entity, player, DamageCause.ENTITY_EXPLOSION, 20D); + listener.onExplosion(e); + assertTrue(e.isCancelled()); + } + + @Test + public void testOnEntityExplosionOutsideIslandAllowed() { + Flags.WORLD_TNT_DAMAGE.setDefaultSetting(true); + assertTrue(Flags.WORLD_TNT_DAMAGE.isSetForWorld(world)); + when(im.getProtectedIslandAt(any())).thenReturn(Optional.empty()); + EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(entity, player, DamageCause.ENTITY_EXPLOSION, 20D); + listener.onExplosion(e); + assertFalse(e.isCancelled()); + } + + @Test + public void testOnEntityExplosionWrongWorld() { + when(iwm.inWorld(any(Location.class))).thenReturn(false); + EntityDamageByEntityEvent e = new EntityDamageByEntityEvent(entity, player, DamageCause.ENTITY_EXPLOSION, 20D); + listener.onExplosion(e); + assertFalse(e.isCancelled()); + + } }