No wither damage on non-BentoBox game worlds

https://github.com/BentoBoxWorld/BentoBox/issues/954
This commit is contained in:
tastybento 2019-09-19 15:37:45 -07:00
parent 6cd8c70bd4
commit 60e85d6512
2 changed files with 49 additions and 0 deletions

View File

@ -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()));
}
}

View File

@ -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<Block> 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);