diff --git a/src/main/java/world/bentobox/bentobox/listeners/NetherPortals.java b/src/main/java/world/bentobox/bentobox/listeners/NetherPortals.java index 49df94e08..74889ee6f 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/NetherPortals.java +++ b/src/main/java/world/bentobox/bentobox/listeners/NetherPortals.java @@ -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; } diff --git a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java index 6553e1e10..8936ec215 100644 --- a/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java +++ b/src/main/java/world/bentobox/bentobox/managers/IslandsManager.java @@ -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); diff --git a/src/test/java/world/bentobox/bentobox/listeners/NetherPortalsTest.java b/src/test/java/world/bentobox/bentobox/listeners/NetherPortalsTest.java index 2cbf65b9b..d5ea71b82 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/NetherPortalsTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/NetherPortalsTest.java @@ -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)}. */ diff --git a/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java b/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java index 97b3bf285..308254d1b 100644 --- a/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java +++ b/src/test/java/world/bentobox/bentobox/managers/IslandsManagerTest.java @@ -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)}. */