Fix for null entities.

https://github.com/tastybento/bskyblock/issues/206
This commit is contained in:
tastybento 2018-07-17 07:54:22 -07:00
parent 5572875257
commit ee440b4f0d
2 changed files with 20 additions and 13 deletions

View File

@ -30,7 +30,7 @@ public class MobSpawnListener extends AbstractFlagListener {
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public boolean onNaturalMobSpawn(CreatureSpawnEvent e) { public boolean onNaturalMobSpawn(CreatureSpawnEvent e) {
// If not in the right world, return // If not in the right world, return
if (!getIWM().inWorld(e.getEntity().getLocation())) { if (e.getEntity() == null || !getIWM().inWorld(e.getEntity().getLocation())) {
return false; return false;
} }
// Deal with natural spawning // Deal with natural spawning
@ -40,7 +40,7 @@ public class MobSpawnListener extends AbstractFlagListener {
|| e.getSpawnReason().equals(SpawnReason.DEFAULT) || e.getSpawnReason().equals(SpawnReason.DEFAULT)
|| e.getSpawnReason().equals(SpawnReason.MOUNT) || e.getSpawnReason().equals(SpawnReason.MOUNT)
|| e.getSpawnReason().equals(SpawnReason.NETHER_PORTAL)) { || e.getSpawnReason().equals(SpawnReason.NETHER_PORTAL)) {
Optional<Island> island = getIslands().getIslandAt(e.getLocation()); Optional<Island> island = getIslands().getIslandAt(e.getLocation());
// Cancel the event if these are true // Cancel the event if these are true
if ((e.getEntity() instanceof Monster || e.getEntity() instanceof Slime)) { if ((e.getEntity() instanceof Monster || e.getEntity() instanceof Slime)) {

View File

@ -61,7 +61,7 @@ public class MobSpawnListenerTest {
// Set up plugin // Set up plugin
plugin = mock(BSkyBlock.class); plugin = mock(BSkyBlock.class);
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin); Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
IslandsManager im = mock(IslandsManager.class); IslandsManager im = mock(IslandsManager.class);
when(plugin.getIslands()).thenReturn(im); when(plugin.getIslands()).thenReturn(im);
@ -100,9 +100,9 @@ public class MobSpawnListenerTest {
when(slime.getLocation()).thenReturn(location); when(slime.getLocation()).thenReturn(location);
cow = mock(Cow.class); cow = mock(Cow.class);
when(cow.getLocation()).thenReturn(location); when(cow.getLocation()).thenReturn(location);
} }
@Before @Before
public void setUp() { public void setUp() {
// Worlds // Worlds
@ -113,10 +113,10 @@ public class MobSpawnListenerTest {
when(iwm.getBSBEndWorld()).thenReturn(world); when(iwm.getBSBEndWorld()).thenReturn(world);
when(iwm.inWorld(any(Location.class))).thenReturn(true); when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(plugin.getIWM()).thenReturn(iwm); when(plugin.getIWM()).thenReturn(iwm);
PowerMockito.mockStatic(Util.class); PowerMockito.mockStatic(Util.class);
when(Util.getWorld(Mockito.any())).thenReturn(mock(World.class)); when(Util.getWorld(Mockito.any())).thenReturn(mock(World.class));
// World Settings // World Settings
WorldSettings ws = mock(WorldSettings.class); WorldSettings ws = mock(WorldSettings.class);
when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws); when(iwm.getWorldSettings(Mockito.any())).thenReturn(ws);
@ -126,7 +126,14 @@ public class MobSpawnListenerTest {
} }
@Test
public void testNullEntity() {
CreatureSpawnEvent e = new CreatureSpawnEvent(null, SpawnReason.NATURAL);
MobSpawnListener l = new MobSpawnListener();
assertFalse(l.onNaturalMobSpawn(e));
assertFalse(e.isCancelled());
}
@Test @Test
public void testNotInWorld() { public void testNotInWorld() {
when(iwm.inWorld(any(Location.class))).thenReturn(false); when(iwm.inWorld(any(Location.class))).thenReturn(false);
@ -138,7 +145,7 @@ public class MobSpawnListenerTest {
// Set up entity // Set up entity
LivingEntity entity = mock(LivingEntity.class); LivingEntity entity = mock(LivingEntity.class);
when(entity.getLocation()).thenReturn(null); when(entity.getLocation()).thenReturn(null);
// Setup event // Setup event
CreatureSpawnEvent e = mock(CreatureSpawnEvent.class); CreatureSpawnEvent e = mock(CreatureSpawnEvent.class);
when(e.getLocation()).thenReturn(location); when(e.getLocation()).thenReturn(location);
@ -153,7 +160,7 @@ public class MobSpawnListenerTest {
// Should not be canceled // Should not be canceled
assertFalse(l.onNaturalMobSpawn(e)); assertFalse(l.onNaturalMobSpawn(e));
} }
@Test @Test
public void testOnNaturalMonsterSpawnBlocked() { public void testOnNaturalMonsterSpawnBlocked() {
IslandsManager im = mock(IslandsManager.class); IslandsManager im = mock(IslandsManager.class);
@ -236,7 +243,7 @@ public class MobSpawnListenerTest {
} }
} }
@Test @Test
public void testOnNaturalMonsterSpawnBlockedNoIsland() { public void testOnNaturalMonsterSpawnBlockedNoIsland() {
IslandsManager im = mock(IslandsManager.class); IslandsManager im = mock(IslandsManager.class);
@ -264,7 +271,7 @@ public class MobSpawnListenerTest {
checkBlocked(e,l); checkBlocked(e,l);
} }
@Test @Test
public void testOnNaturalMobSpawnUnBlockedNoIsland() { public void testOnNaturalMobSpawnUnBlockedNoIsland() {
IslandsManager im = mock(IslandsManager.class); IslandsManager im = mock(IslandsManager.class);
@ -274,7 +281,7 @@ public class MobSpawnListenerTest {
// Block mobs // Block mobs
Flags.MONSTER_SPAWN.setDefaultSetting(true); Flags.MONSTER_SPAWN.setDefaultSetting(true);
Flags.ANIMAL_SPAWN.setDefaultSetting(true); Flags.ANIMAL_SPAWN.setDefaultSetting(true);
// Setup event // Setup event
CreatureSpawnEvent e = mock(CreatureSpawnEvent.class); CreatureSpawnEvent e = mock(CreatureSpawnEvent.class);
when(e.getLocation()).thenReturn(location); when(e.getLocation()).thenReturn(location);