From 60e85d65122675d93acf00fe766ed1916211ba8b Mon Sep 17 00:00:00 2001 From: tastybento Date: Thu, 19 Sep 2019 15:37:45 -0700 Subject: [PATCH] No wither damage on non-BentoBox game worlds https://github.com/BentoBoxWorld/BentoBox/issues/954 --- .../flags/worldsettings/WitherListener.java | 2 + .../worldsettings/WitherListenerTest.java | 47 +++++++++++++++++++ 2 files changed, 49 insertions(+) diff --git a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListener.java b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListener.java index 17fc995a3..d1538ff27 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListener.java +++ b/src/main/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListener.java @@ -22,6 +22,7 @@ public class WitherListener extends FlagListener { */ @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onExplosion(final EntityExplodeEvent e) { + if (!getIWM().inWorld(e.getLocation())) return; // Remove blocks from the explosion list if required if((e.getEntityType().equals(EntityType.WITHER_SKULL) || e.getEntityType().equals(EntityType.WITHER)) && !Flags.WITHER_DAMAGE.isSetForWorld(e.getLocation().getWorld())) { @@ -36,6 +37,7 @@ public class WitherListener extends FlagListener { */ @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public void onWitherChangeBlocks(final EntityChangeBlockEvent e) { + if (!getIWM().inWorld(e.getBlock().getWorld())) return; e.setCancelled(e.getEntityType().equals(EntityType.WITHER) && !Flags.WITHER_DAMAGE.isSetForWorld(e.getBlock().getWorld())); } } diff --git a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java index 002647167..12aedf529 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/flags/worldsettings/WitherListenerTest.java @@ -3,6 +3,7 @@ package world.bentobox.bentobox.listeners.flags.worldsettings; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; @@ -47,8 +48,12 @@ public class WitherListenerTest { @Mock private Location location; @Mock + private Location location2; + @Mock private World world; @Mock + private World world2; + @Mock private IslandWorldManager iwm; private List blocks; @@ -65,6 +70,8 @@ public class WitherListenerTest { BentoBox plugin = mock(BentoBox.class); Whitebox.setInternalState(BentoBox.class, "instance", plugin); when(plugin.getIWM()).thenReturn(iwm); + when(iwm.inWorld(eq(world))).thenReturn(true); + when(iwm.inWorld(eq(location))).thenReturn(true); map = new HashMap<>(); when(ws.getWorldFlags()).thenReturn(map); when(iwm.getWorldSettings(any())).thenReturn(ws); @@ -73,6 +80,11 @@ public class WitherListenerTest { when(location.getBlockX()).thenReturn(0); when(location.getBlockY()).thenReturn(0); when(location.getBlockZ()).thenReturn(0); + + when(location2.getWorld()).thenReturn(world2); + when(location2.getBlockX()).thenReturn(0); + when(location2.getBlockY()).thenReturn(0); + when(location2.getBlockZ()).thenReturn(0); blocks = new ArrayList<>(); for (int i = 0; i < 4; i++) { @@ -110,6 +122,21 @@ public class WitherListenerTest { assertTrue(blocks.isEmpty()); } + + /** + * Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.WitherListener#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}. + */ + @Test + public void testOnExplosionWitherWrongWorld() { + Entity entity = mock(Entity.class); + when(entity.getLocation()).thenReturn(location2); + when(entity.getWorld()).thenReturn(world2); + when(entity.getType()).thenReturn(EntityType.WITHER); + EntityExplodeEvent e = new EntityExplodeEvent(entity, location2, blocks, 0); + wl.onExplosion(e); + assertFalse(blocks.isEmpty()); + } + /** * Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.WitherListener#onExplosion(org.bukkit.event.entity.EntityExplodeEvent)}. */ @@ -165,11 +192,30 @@ public class WitherListenerTest { when(entity.getType()).thenReturn(EntityType.WITHER); Block block = mock(Block.class); when(block.getLocation()).thenReturn(location); + when(block.getWorld()).thenReturn(world); BlockData blockData = mock(BlockData.class); EntityChangeBlockEvent e = new EntityChangeBlockEvent(entity, block, blockData); wl.onWitherChangeBlocks(e); assertTrue(e.isCancelled()); } + + /** + * Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.WitherListener#WitherChangeBlocks(org.bukkit.event.entity.EntityChangeBlockEvent)}. + */ + @Test + public void testWitherChangeBlocksWrongWorld() { + Entity entity = mock(Entity.class); + when(entity.getLocation()).thenReturn(location2); + when(entity.getWorld()).thenReturn(world2); + when(entity.getType()).thenReturn(EntityType.WITHER); + Block block = mock(Block.class); + when(block.getLocation()).thenReturn(location2); + when(block.getWorld()).thenReturn(world2); + BlockData blockData = mock(BlockData.class); + EntityChangeBlockEvent e = new EntityChangeBlockEvent(entity, block, blockData); + wl.onWitherChangeBlocks(e); + assertFalse(e.isCancelled()); + } /** * Test method for {@link world.bentobox.bentobox.listeners.flags.worldsettings.WitherListener#WitherChangeBlocks(org.bukkit.event.entity.EntityChangeBlockEvent)}. @@ -183,6 +229,7 @@ public class WitherListenerTest { when(entity.getType()).thenReturn(EntityType.WITHER); Block block = mock(Block.class); when(block.getLocation()).thenReturn(location); + when(block.getWorld()).thenReturn(world); BlockData blockData = mock(BlockData.class); EntityChangeBlockEvent e = new EntityChangeBlockEvent(entity, block, blockData); wl.onWitherChangeBlocks(e);