Fixed NPE on portal use when there is no Nether world

https://github.com/BentoBoxWorld/bentobox/issues/365
This commit is contained in:
tastybento 2018-12-08 19:24:31 -08:00
parent a1076dfc3f
commit 33c374c52d
4 changed files with 52 additions and 3 deletions

View File

@ -110,6 +110,7 @@ public class NetherPortals implements Listener {
return;
}
World overWorld = Util.getWorld(e.getFrom().getWorld());
// If entering a portal in the end, teleport home if you have one, else do nothing
if (e.getFrom().getWorld().getEnvironment().equals(Environment.THE_END)) {
if (plugin.getIslands().hasIsland(overWorld, e.getPlayer().getUniqueId())) {
@ -194,7 +195,8 @@ public class NetherPortals implements Listener {
return false;
}
World fromWorld = e.getFrom().getWorld();
if (!e.getCause().equals(TeleportCause.NETHER_PORTAL) || !plugin.getIWM().inWorld(e.getFrom())) {
if (!e.getCause().equals(TeleportCause.NETHER_PORTAL) || !plugin.getIWM().inWorld(e.getFrom())
|| !plugin.getIWM().isNetherGenerate(fromWorld)) {
// Do nothing special
return false;
}

View File

@ -163,7 +163,7 @@ public class IslandsManager {
* @return true if safe, otherwise false
*/
public boolean isSafeLocation(Location l) {
if (l == null) {
if (l == null || l.getWorld() == null) {
return false;
}
Block ground = l.getBlock().getRelative(BlockFace.DOWN);

View File

@ -88,8 +88,10 @@ public class NetherPortalsTest {
end = mock(World.class);
when(end.getEnvironment()).thenReturn(Environment.THE_END);
when(iwm.getEndWorld(Mockito.any())).thenReturn(end);
when(iwm.isEndGenerate(Mockito.any())).thenReturn(true);
when(iwm.getIslandWorld(Mockito.any())).thenReturn(world);
when(iwm.getNetherWorld(Mockito.any())).thenReturn(nether);
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
when(iwm.inWorld(any(World.class))).thenReturn(true);
when(iwm.inWorld(any(Location.class))).thenReturn(true);
when(iwm.getNetherSpawnRadius(Mockito.any())).thenReturn(100);
@ -267,13 +269,47 @@ public class NetherPortalsTest {
*/
@Test
public void testOnEndIslandPortalNotEnd() {
Location from = mock(Location.class);
// Teleport from world to nether
when(from.getWorld()).thenReturn(world);
when(from.toVector()).thenReturn(new Vector(1,2,3));
NetherPortals np = new NetherPortals(plugin);
// Wrong cause
PlayerPortalEvent e = new PlayerPortalEvent(null, null, null, null, TeleportCause.CHORUS_FRUIT);
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.CHORUS_FRUIT);
np.onEndIslandPortal(e);
assertFalse(e.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.NetherPortals#onEndIslandPortal(org.bukkit.event.player.PlayerPortalEvent)}.
*/
@Test
public void testOnEndIslandPortalNoEndWorldGenerated() {
Location from = mock(Location.class);
// Teleport from world to nether
when(from.getWorld()).thenReturn(world);
when(from.toVector()).thenReturn(new Vector(1,2,3));
// No end world
when(iwm.isEndGenerate(Mockito.any())).thenReturn(false);
NetherPortals np = new NetherPortals(plugin);
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.END_PORTAL);
np.onEndIslandPortal(e);
assertFalse(e.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.NetherPortals#onEndIslandPortal(org.bukkit.event.player.PlayerPortalEvent)}.
*/
@Test
public void testOnNetherIslandPortalNoNetherWorldGenerated() {
// No nether world
when(iwm.isNetherGenerate(Mockito.any())).thenReturn(false);
NetherPortals np = new NetherPortals(plugin);
PlayerPortalEvent e = new PlayerPortalEvent(null, null, null, null, TeleportCause.NETHER_PORTAL);
np.onNetherPortal(e);
assertFalse(e.isCancelled());
}
/**
* Test method for {@link world.bentobox.bentobox.listeners.NetherPortals#onEndIslandPortal(org.bukkit.event.player.PlayerPortalEvent)}.
*/

View File

@ -136,6 +136,7 @@ public class IslandsManagerTest {
ground = mock(Block.class);
space2 = mock(Block.class);
when(location.getBlock()).thenReturn(space1);
when(location.getWorld()).thenReturn(world);
when(space1.getRelative(BlockFace.DOWN)).thenReturn(ground);
when(space1.getRelative(BlockFace.UP)).thenReturn(space2);
// A safe spot
@ -194,6 +195,16 @@ public class IslandsManagerTest {
assertFalse(manager.isSafeLocation(null));
}
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#isSafeLocation(org.bukkit.Location)}.
*/
@Test
public void testIsSafeLocationNullWorld() {
when(location.getWorld()).thenReturn(null);
IslandsManager manager = new IslandsManager(plugin);
assertFalse(manager.isSafeLocation(location));
}
/**
* Test method for {@link world.bentobox.bentobox.managers.IslandsManager#isSafeLocation(org.bukkit.Location)}.
*/