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
This commit is contained in:
tastybento 2018-09-25 22:20:30 -07:00
parent 1f1b446137
commit 2df049f70d
4 changed files with 37 additions and 31 deletions

View File

@ -122,8 +122,11 @@ public abstract class AbstractFlagListener implements Listener {
* @return true if the check is okay, false if it was disallowed * @return true if the check is okay, false if it was disallowed
*/ */
public boolean checkIsland(Event e, Location loc, Flag flag, boolean silent) { public boolean checkIsland(Event e, Location loc, Flag flag, boolean silent) {
// If this is not an Island World, skip // If this is not an Island World or a standard Nether or End, skip
if (!plugin.getIWM().inWorld(loc)) { 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; return true;
} }
// Get the island and if present // Get the island and if present

View File

@ -22,12 +22,14 @@ import org.bukkit.event.world.StructureGrowEvent;
import org.bukkit.util.Vector; import org.bukkit.util.Vector;
import world.bentobox.bentobox.BentoBox; import world.bentobox.bentobox.BentoBox;
import world.bentobox.bentobox.api.localization.TextVariables;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
import world.bentobox.bentobox.lists.Flags;
import world.bentobox.bentobox.util.Util; import world.bentobox.bentobox.util.Util;
import world.bentobox.bentobox.util.teleport.SafeSpotTeleport; import world.bentobox.bentobox.util.teleport.SafeSpotTeleport;
public class NetherPortals implements Listener { 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; private final BentoBox plugin;
public NetherPortals(BentoBox plugin) { public NetherPortals(BentoBox plugin) {
@ -44,7 +46,7 @@ public class NetherPortals implements Listener {
private boolean atSpawn(Location location) { private boolean atSpawn(Location location) {
Vector p = location.toVector().multiply(new Vector(1, 0, 1)); Vector p = location.toVector().multiply(new Vector(1, 0, 1));
Vector spawn = location.getWorld().getSpawnLocation().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); return (spawn.distanceSquared(p) < radiusSquared);
} }
@ -76,7 +78,8 @@ public class NetherPortals implements Listener {
return; return;
} }
if (atSpawn(e.getBlock().getLocation())) { 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); e.setCancelled(true);
} }
} }
@ -91,7 +94,8 @@ public class NetherPortals implements Listener {
return; return;
} }
if (atSpawn(e.getBlockClicked().getLocation())) { 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); e.setCancelled(true);
} }
} }
@ -187,6 +191,9 @@ public class NetherPortals implements Listener {
*/ */
@EventHandler(priority = EventPriority.LOW, ignoreCancelled = true) @EventHandler(priority = EventPriority.LOW, ignoreCancelled = true)
public boolean onNetherPortal(PlayerPortalEvent e) { public boolean onNetherPortal(PlayerPortalEvent e) {
if (e.getFrom() == null) {
return false;
}
World fromWorld = e.getFrom().getWorld(); 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())) {
// Do nothing special // Do nothing special
@ -243,7 +250,8 @@ public class NetherPortals implements Listener {
return; return;
} }
if (atSpawn(e.getBlock().getLocation())) { 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); e.setCancelled(true);
} }
} }

View File

@ -648,6 +648,7 @@ protection:
hint: "No trapdoor use" hint: "No trapdoor use"
locked: "&cThis island is locked!" locked: "&cThis island is locked!"
protected: "&cIsland protected: [description]" protected: "&cIsland protected: [description]"
spawn-protected: "&cSpawn protected: [description]"
panel: panel:
PROTECTION: PROTECTION:

View File

@ -85,7 +85,6 @@ public class NetherPortalsTest {
when(world.getEnvironment()).thenReturn(Environment.NORMAL); when(world.getEnvironment()).thenReturn(Environment.NORMAL);
nether = mock(World.class); nether = mock(World.class);
when(nether.getEnvironment()).thenReturn(Environment.NETHER); when(nether.getEnvironment()).thenReturn(Environment.NETHER);
when(nether.getSpawnLocation()).thenReturn(mock(Location.class));
end = mock(World.class); end = mock(World.class);
when(end.getEnvironment()).thenReturn(Environment.THE_END); when(end.getEnvironment()).thenReturn(Environment.THE_END);
when(iwm.getEndWorld(Mockito.any())).thenReturn(end); when(iwm.getEndWorld(Mockito.any())).thenReturn(end);
@ -102,9 +101,10 @@ public class NetherPortalsTest {
Settings s = mock(Settings.class); Settings s = mock(Settings.class);
when(plugin.getSettings()).thenReturn(s); when(plugin.getSettings()).thenReturn(s);
// Set up spawn // Set up nether spawn
Location netherSpawn = mock(Location.class); Location netherSpawn = mock(Location.class);
when(netherSpawn.toVector()).thenReturn(new Vector(0,0,0)); when(netherSpawn.toVector()).thenReturn(new Vector(0,0,0));
when(netherSpawn.getWorld()).thenReturn(nether);
when(nether.getSpawnLocation()).thenReturn(netherSpawn); when(nether.getSpawnLocation()).thenReturn(netherSpawn);
// Player // Player
@ -531,8 +531,8 @@ public class NetherPortalsTest {
when(from.toVector()).thenReturn(new Vector(1,2,3)); when(from.toVector()).thenReturn(new Vector(1,2,3));
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL); PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL);
// Nether islands active // Nether islands active
when(iwm.isNetherIslands(world)).thenReturn(true); when(iwm.isNetherIslands(Mockito.any())).thenReturn(true);
when(iwm.isNetherGenerate(world)).thenReturn(true); when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
assertTrue(np.onNetherPortal(e)); assertTrue(np.onNetherPortal(e));
// Verify // Verify
assertTrue(e.isCancelled()); assertTrue(e.isCancelled());
@ -554,8 +554,8 @@ public class NetherPortalsTest {
when(from.toVector()).thenReturn(new Vector(1,2,3)); when(from.toVector()).thenReturn(new Vector(1,2,3));
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL); PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL);
// Nether islands active // Nether islands active
when(iwm.isNetherIslands(world)).thenReturn(true); when(iwm.isNetherIslands(Mockito.any())).thenReturn(true);
when(iwm.isNetherGenerate(world)).thenReturn(true); when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
Island island = mock(Island.class); Island island = mock(Island.class);
Location spawnLoc = mock(Location.class); Location spawnLoc = mock(Location.class);
@ -586,8 +586,8 @@ public class NetherPortalsTest {
when(from.toVector()).thenReturn(new Vector(1,2,3)); when(from.toVector()).thenReturn(new Vector(1,2,3));
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL); PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL);
// Nether islands active // Nether islands active
when(iwm.isNetherIslands(world)).thenReturn(true); when(iwm.isNetherIslands(Mockito.any())).thenReturn(true);
when(iwm.isNetherGenerate(world)).thenReturn(true); when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
Island island = mock(Island.class); Island island = mock(Island.class);
when(island.getSpawnPoint(Mockito.any())).thenReturn(null); when(island.getSpawnPoint(Mockito.any())).thenReturn(null);
@ -617,14 +617,11 @@ public class NetherPortalsTest {
when(from.toVector()).thenReturn(new Vector(1,2,3)); when(from.toVector()).thenReturn(new Vector(1,2,3));
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL); PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL);
// Nether islands inactive // Nether islands inactive
when(iwm.isNetherIslands(world)).thenReturn(false); when(iwm.isNetherIslands(Mockito.any())).thenReturn(false);
when(iwm.isNetherGenerate(world)).thenReturn(true); when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
assertTrue(np.onNetherPortal(e)); assertFalse(np.onNetherPortal(e));
// Verify // Verify
assertTrue(e.isCancelled()); assertFalse(e.isCancelled());
// If regular nether, then to = spawn point of nether
Mockito.verify(from, Mockito.never()).toVector();
Mockito.verify(nether).getSpawnLocation();
} }
/** /**
@ -643,16 +640,13 @@ public class NetherPortalsTest {
PlayerPortalEvent e = new PlayerPortalEvent(p, from, null, null, TeleportCause.NETHER_PORTAL); PlayerPortalEvent e = new PlayerPortalEvent(p, from, null, null, TeleportCause.NETHER_PORTAL);
// Nether islands inactive // Nether islands inactive
when(iwm.isNetherIslands(world)).thenReturn(false); when(iwm.isNetherIslands(Mockito.any())).thenReturn(false);
when(iwm.isNetherGenerate(world)).thenReturn(true); when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
// Player should be teleported to their island // Player should be teleported to their island
assertTrue(np.onNetherPortal(e)); assertFalse(np.onNetherPortal(e));
// Verify // Verify
assertTrue(e.isCancelled()); assertFalse(e.isCancelled());
// If regular nether, then to = island location
Mockito.verify(from, Mockito.never()).toVector();
Mockito.verify(im).getIslandLocation(Mockito.any(), Mockito.any());
} }
/** /**
@ -667,8 +661,8 @@ public class NetherPortalsTest {
when(from.toVector()).thenReturn(new Vector(1,2,3)); when(from.toVector()).thenReturn(new Vector(1,2,3));
PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL); PlayerPortalEvent e = new PlayerPortalEvent(null, from, null, null, TeleportCause.NETHER_PORTAL);
// Nether islands active // Nether islands active
when(iwm.isNetherIslands(world)).thenReturn(true); when(iwm.isNetherIslands(Mockito.any())).thenReturn(true);
when(iwm.isNetherGenerate(world)).thenReturn(true); when(iwm.isNetherGenerate(Mockito.any())).thenReturn(true);
assertTrue(np.onNetherPortal(e)); assertTrue(np.onNetherPortal(e));
// Verify // Verify
assertTrue(e.isCancelled()); assertTrue(e.isCancelled());