From 2df049f70da64f99410aea80cc6f156f3570678f Mon Sep 17 00:00:00 2001 From: tastybento Date: Tue, 25 Sep 2018 22:20:30 -0700 Subject: [PATCH] Implements protection for standard Nether and End This was missing so it needed to be added. Related to: https://github.com/BentoBoxWorld/addon-bskyblock/issues/9 --- .../api/flags/AbstractFlagListener.java | 7 +++- .../bentobox/listeners/NetherPortals.java | 18 +++++--- src/main/resources/locales/en-US.yml | 1 + .../bentobox/listeners/NetherPortalsTest.java | 42 ++++++++----------- 4 files changed, 37 insertions(+), 31 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/flags/AbstractFlagListener.java b/src/main/java/world/bentobox/bentobox/api/flags/AbstractFlagListener.java index ada3508be..1a7cf7c4b 100644 --- a/src/main/java/world/bentobox/bentobox/api/flags/AbstractFlagListener.java +++ b/src/main/java/world/bentobox/bentobox/api/flags/AbstractFlagListener.java @@ -122,8 +122,11 @@ public abstract class AbstractFlagListener implements Listener { * @return true if the check is okay, false if it was disallowed */ public boolean checkIsland(Event e, Location loc, Flag flag, boolean silent) { - // If this is not an Island World, skip - if (!plugin.getIWM().inWorld(loc)) { + // If this is not an Island World or a standard Nether or End, skip + if (!plugin.getIWM().inWorld(loc) + || (plugin.getIWM().isNether(loc.getWorld()) && !plugin.getIWM().isNetherIslands(loc.getWorld())) + || (plugin.getIWM().isEnd(loc.getWorld()) && !plugin.getIWM().isEndIslands(loc.getWorld())) + ) { return true; } // Get the island and if present diff --git a/src/main/java/world/bentobox/bentobox/listeners/NetherPortals.java b/src/main/java/world/bentobox/bentobox/listeners/NetherPortals.java index 4e09fe694..74c3555d5 100644 --- a/src/main/java/world/bentobox/bentobox/listeners/NetherPortals.java +++ b/src/main/java/world/bentobox/bentobox/listeners/NetherPortals.java @@ -22,12 +22,14 @@ import org.bukkit.event.world.StructureGrowEvent; import org.bukkit.util.Vector; import world.bentobox.bentobox.BentoBox; +import world.bentobox.bentobox.api.localization.TextVariables; import world.bentobox.bentobox.api.user.User; +import world.bentobox.bentobox.lists.Flags; import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.teleport.SafeSpotTeleport; public class NetherPortals implements Listener { - private static final String ERROR_NO_PERMISSION = "general.errors.no-permission"; + private static final String SPAWN_PROTECTED = "protection.spawn-protected"; private final BentoBox plugin; public NetherPortals(BentoBox plugin) { @@ -44,7 +46,7 @@ public class NetherPortals implements Listener { private boolean atSpawn(Location location) { Vector p = location.toVector().multiply(new Vector(1, 0, 1)); Vector spawn = location.getWorld().getSpawnLocation().toVector().multiply(new Vector(1, 0, 1)); - int radiusSquared = plugin.getIWM().getNetherSpawnRadius(location.getWorld()) ^ 2; + int radiusSquared = plugin.getIWM().getNetherSpawnRadius(location.getWorld()) * plugin.getIWM().getNetherSpawnRadius(location.getWorld()); return (spawn.distanceSquared(p) < radiusSquared); } @@ -76,7 +78,8 @@ public class NetherPortals implements Listener { return; } if (atSpawn(e.getBlock().getLocation())) { - User.getInstance(e.getPlayer()).sendMessage(ERROR_NO_PERMISSION); + User user = User.getInstance(e.getPlayer()); + user.sendMessage(SPAWN_PROTECTED, TextVariables.DESCRIPTION, user.getTranslation(Flags.BREAK_BLOCKS.getHintReference())); e.setCancelled(true); } } @@ -91,7 +94,8 @@ public class NetherPortals implements Listener { return; } if (atSpawn(e.getBlockClicked().getLocation())) { - User.getInstance(e.getPlayer()).sendMessage(ERROR_NO_PERMISSION); + User user = User.getInstance(e.getPlayer()); + user.sendMessage(SPAWN_PROTECTED, TextVariables.DESCRIPTION, user.getTranslation(Flags.BUCKET.getHintReference())); e.setCancelled(true); } } @@ -187,6 +191,9 @@ public class NetherPortals implements Listener { */ @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) public boolean onNetherPortal(PlayerPortalEvent e) { + if (e.getFrom() == null) { + return false; + } World fromWorld = e.getFrom().getWorld(); if (!e.getCause().equals(TeleportCause.NETHER_PORTAL) || !plugin.getIWM().inWorld(e.getFrom())) { // Do nothing special @@ -243,7 +250,8 @@ public class NetherPortals implements Listener { return; } if (atSpawn(e.getBlock().getLocation())) { - User.getInstance(e.getPlayer()).sendMessage(ERROR_NO_PERMISSION); + User user = User.getInstance(e.getPlayer()); + user.sendMessage(SPAWN_PROTECTED, TextVariables.DESCRIPTION, user.getTranslation(Flags.PLACE_BLOCKS.getHintReference())); e.setCancelled(true); } } diff --git a/src/main/resources/locales/en-US.yml b/src/main/resources/locales/en-US.yml index 48879c015..5d738e505 100644 --- a/src/main/resources/locales/en-US.yml +++ b/src/main/resources/locales/en-US.yml @@ -648,6 +648,7 @@ protection: hint: "No trapdoor use" locked: "&cThis island is locked!" protected: "&cIsland protected: [description]" + spawn-protected: "&cSpawn protected: [description]" panel: PROTECTION: diff --git a/src/test/java/world/bentobox/bentobox/listeners/NetherPortalsTest.java b/src/test/java/world/bentobox/bentobox/listeners/NetherPortalsTest.java index 2f307b625..d502e677d 100644 --- a/src/test/java/world/bentobox/bentobox/listeners/NetherPortalsTest.java +++ b/src/test/java/world/bentobox/bentobox/listeners/NetherPortalsTest.java @@ -85,7 +85,6 @@ public class NetherPortalsTest { when(world.getEnvironment()).thenReturn(Environment.NORMAL); nether = mock(World.class); when(nether.getEnvironment()).thenReturn(Environment.NETHER); - when(nether.getSpawnLocation()).thenReturn(mock(Location.class)); end = mock(World.class); when(end.getEnvironment()).thenReturn(Environment.THE_END); when(iwm.getEndWorld(Mockito.any())).thenReturn(end); @@ -102,9 +101,10 @@ public class NetherPortalsTest { Settings s = mock(Settings.class); when(plugin.getSettings()).thenReturn(s); - // Set up spawn + // Set up nether spawn Location netherSpawn = mock(Location.class); when(netherSpawn.toVector()).thenReturn(new Vector(0,0,0)); + when(netherSpawn.getWorld()).thenReturn(nether); when(nether.getSpawnLocation()).thenReturn(netherSpawn); // Player @@ -531,8 +531,8 @@ public class NetherPortalsTest { when(from.toVector()).thenReturn(new Vector(1,2,3)); PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL); // Nether islands active - when(iwm.isNetherIslands(world)).thenReturn(true); - when(iwm.isNetherGenerate(world)).thenReturn(true); + when(iwm.isNetherIslands(Mockito.any())).thenReturn(true); + when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true); assertTrue(np.onNetherPortal(e)); // Verify assertTrue(e.isCancelled()); @@ -554,8 +554,8 @@ public class NetherPortalsTest { when(from.toVector()).thenReturn(new Vector(1,2,3)); PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL); // Nether islands active - when(iwm.isNetherIslands(world)).thenReturn(true); - when(iwm.isNetherGenerate(world)).thenReturn(true); + when(iwm.isNetherIslands(Mockito.any())).thenReturn(true); + when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true); Island island = mock(Island.class); Location spawnLoc = mock(Location.class); @@ -586,8 +586,8 @@ public class NetherPortalsTest { when(from.toVector()).thenReturn(new Vector(1,2,3)); PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL); // Nether islands active - when(iwm.isNetherIslands(world)).thenReturn(true); - when(iwm.isNetherGenerate(world)).thenReturn(true); + when(iwm.isNetherIslands(Mockito.any())).thenReturn(true); + when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true); Island island = mock(Island.class); when(island.getSpawnPoint(Mockito.any())).thenReturn(null); @@ -617,14 +617,11 @@ public class NetherPortalsTest { when(from.toVector()).thenReturn(new Vector(1,2,3)); PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL); // Nether islands inactive - when(iwm.isNetherIslands(world)).thenReturn(false); - when(iwm.isNetherGenerate(world)).thenReturn(true); - assertTrue(np.onNetherPortal(e)); + when(iwm.isNetherIslands(Mockito.any())).thenReturn(false); + when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true); + assertFalse(np.onNetherPortal(e)); // Verify - assertTrue(e.isCancelled()); - // If regular nether, then to = spawn point of nether - Mockito.verify(from, Mockito.never()).toVector(); - Mockito.verify(nether).getSpawnLocation(); + assertFalse(e.isCancelled()); } /** @@ -643,16 +640,13 @@ public class NetherPortalsTest { PlayerPortalEvent e = new PlayerPortalEvent(p, from, null, null, TeleportCause.NETHER_PORTAL); // Nether islands inactive - when(iwm.isNetherIslands(world)).thenReturn(false); - when(iwm.isNetherGenerate(world)).thenReturn(true); + when(iwm.isNetherIslands(Mockito.any())).thenReturn(false); + when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true); // Player should be teleported to their island - assertTrue(np.onNetherPortal(e)); + assertFalse(np.onNetherPortal(e)); // Verify - assertTrue(e.isCancelled()); - // If regular nether, then to = island location - Mockito.verify(from, Mockito.never()).toVector(); - Mockito.verify(im).getIslandLocation(Mockito.any(), Mockito.any()); + assertFalse(e.isCancelled()); } /** @@ -667,8 +661,8 @@ public class NetherPortalsTest { when(from.toVector()).thenReturn(new Vector(1,2,3)); PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL); // Nether islands active - when(iwm.isNetherIslands(world)).thenReturn(true); - when(iwm.isNetherGenerate(world)).thenReturn(true); + when(iwm.isNetherIslands(Mockito.any())).thenReturn(true); + when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true); assertTrue(np.onNetherPortal(e)); // Verify assertTrue(e.isCancelled());