From b431b9a092c26445044a3cb8f8805171fa4e319c Mon Sep 17 00:00:00 2001 From: tastybento Date: Fri, 13 Jul 2018 17:27:03 -0700 Subject: [PATCH] Added lock and ban bypass via Op or perms. https://github.com/tastybento/bskyblock/issues/201 --- .../listeners/flags/LockAndBanListener.java | 12 +- .../flags/LockAndBanListenerTest.java | 149 +++++++++++++++++- 2 files changed, 155 insertions(+), 6 deletions(-) diff --git a/src/main/java/us/tastybento/bskyblock/listeners/flags/LockAndBanListener.java b/src/main/java/us/tastybento/bskyblock/listeners/flags/LockAndBanListener.java index 0d2f71105..46f83f1a4 100644 --- a/src/main/java/us/tastybento/bskyblock/listeners/flags/LockAndBanListener.java +++ b/src/main/java/us/tastybento/bskyblock/listeners/flags/LockAndBanListener.java @@ -93,15 +93,19 @@ public class LockAndBanListener implements Listener { * @return CheckResult LOCKED, BANNED or OPEN. If an island is locked, that will take priority over banned */ private CheckResult check(Player player, Location loc) { - + BSkyBlock plugin = BSkyBlock.getInstance(); + // Ops are allowed everywhere + if (player.isOp()) { + return CheckResult.OPEN; + } // See if the island is locked to non-members or player is banned - return BSkyBlock.getInstance().getIslands().getProtectedIslandAt(loc) + return plugin.getIslands().getProtectedIslandAt(loc) .map(is -> { if (is.isBanned(player.getUniqueId())) { - return CheckResult.BANNED; + return player.hasPermission(plugin.getIWM().getPermissionPrefix(loc.getWorld()) + ".mod.bypassban") ? CheckResult.OPEN : CheckResult.BANNED; } if (!is.isAllowed(User.getInstance(player), Flags.LOCK)) { - return CheckResult.LOCKED; + return player.hasPermission(plugin.getIWM().getPermissionPrefix(loc.getWorld()) + ".mod.bypasslock") ? CheckResult.OPEN : CheckResult.LOCKED; } return CheckResult.OPEN; }).orElse(CheckResult.OPEN); diff --git a/src/test/java/us/tastybento/bskyblock/listeners/flags/LockAndBanListenerTest.java b/src/test/java/us/tastybento/bskyblock/listeners/flags/LockAndBanListenerTest.java index 7fb364761..27be9adc8 100644 --- a/src/test/java/us/tastybento/bskyblock/listeners/flags/LockAndBanListenerTest.java +++ b/src/test/java/us/tastybento/bskyblock/listeners/flags/LockAndBanListenerTest.java @@ -40,6 +40,7 @@ import us.tastybento.bskyblock.api.user.Notifier; import us.tastybento.bskyblock.api.user.User; import us.tastybento.bskyblock.database.objects.Island; import us.tastybento.bskyblock.lists.Flags; +import us.tastybento.bskyblock.managers.IslandWorldManager; import us.tastybento.bskyblock.managers.IslandsManager; import us.tastybento.bskyblock.managers.LocalesManager; import us.tastybento.bskyblock.managers.PlayersManager; @@ -63,6 +64,7 @@ public class LockAndBanListenerTest { private Notifier notifier; private Location inside2; private BukkitScheduler sch; + private Player player; /** * @throws java.lang.Exception @@ -77,6 +79,12 @@ public class LockAndBanListenerTest { // World world = mock(World.class); + // Island world manager + IslandWorldManager iwm = mock(IslandWorldManager.class); + when(iwm.getPermissionPrefix(Mockito.any())).thenReturn("bskyblock"); + + when(plugin.getIWM()).thenReturn(iwm); + // Settings Settings s = mock(Settings.class); when(s.getResetWait()).thenReturn(0L); @@ -84,14 +92,18 @@ public class LockAndBanListenerTest { when(plugin.getSettings()).thenReturn(s); // Player - Player p = mock(Player.class); + player = mock(Player.class); // Sometimes use Mockito.withSettings().verboseLogging() user = mock(User.class); User.setPlugin(plugin); + // User and player are not op when(user.isOp()).thenReturn(false); + when(player.isOp()).thenReturn(false); + // No special perms + when(player.hasPermission(Mockito.anyString())).thenReturn(false); uuid = UUID.randomUUID(); when(user.getUniqueId()).thenReturn(uuid); - when(user.getPlayer()).thenReturn(p); + when(user.getPlayer()).thenReturn(player); when(user.getName()).thenReturn("tastybento"); // No island for player to begin with (set it later in the tests) @@ -423,6 +435,53 @@ public class LockAndBanListenerTest { assertFalse(e.isCancelled()); } + @Test + public void testLoginToLockedIslandAsOp() { + // Make player + Player player = mock(Player.class); + when(player.isOp()).thenReturn(true); + + when(player.getUniqueId()).thenReturn(uuid); + // Give player an island + when(im.hasIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + // Place the player on the island + when(player.getLocation()).thenReturn(inside); + + // Lock island for player + when(island.isAllowed(Mockito.any(), Mockito.eq(Flags.LOCK))).thenReturn(false); + + // Log them in + listener.onPlayerLogin(new PlayerJoinEvent(player, "join message")); + // User should not see a message + Mockito.verify(notifier, Mockito.never()).notify(Mockito.any(), Mockito.anyString()); + // User should not be teleported somewhere + Mockito.verify(im, Mockito.never()).homeTeleport(Mockito.any(), Mockito.eq(player)); + } + + @Test + public void testLoginToLockedIslandWithBypassPerm() { + // Make player + Player player = mock(Player.class); + when(player.isOp()).thenReturn(false); + when(player.hasPermission(Mockito.anyString())).thenReturn(true); + + when(player.getUniqueId()).thenReturn(uuid); + // Give player an island + when(im.hasIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + // Place the player on the island + when(player.getLocation()).thenReturn(inside); + + // Lock island for player + when(island.isAllowed(Mockito.any(), Mockito.eq(Flags.LOCK))).thenReturn(false); + + // Log them in + listener.onPlayerLogin(new PlayerJoinEvent(player, "join message")); + // User should not see a message + Mockito.verify(notifier, Mockito.never()).notify(Mockito.any(), Mockito.anyString()); + // User should not be teleported somewhere + Mockito.verify(im, Mockito.never()).homeTeleport(Mockito.any(), Mockito.eq(player)); + } + @Test public void testLoginToLockedIslandAsMember() { // Make player @@ -463,6 +522,50 @@ public class LockAndBanListenerTest { Mockito.verify(im, Mockito.never()).homeTeleport(Mockito.any(), Mockito.eq(player)); } + @Test + public void testPlayerMoveIntoLockedIslandAsOp() { + // Make player + Player player = mock(Player.class); + when(player.isOp()).thenReturn(true); + + when(player.getUniqueId()).thenReturn(uuid); + // Give player an island + when(im.hasIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + // Place the player just outside island + when(player.getLocation()).thenReturn(outside); + + // Lock island for player + when(island.isAllowed(Mockito.any(), Mockito.eq(Flags.LOCK))).thenReturn(false); + + // Move player + PlayerMoveEvent e = new PlayerMoveEvent(player, outside, inside); + listener.onPlayerMove(e); + assertFalse(e.isCancelled()); + } + + @Test + public void testPlayerMoveIntoLockedIslandWithBypass() { + // Make player + Player player = mock(Player.class); + when(player.isOp()).thenReturn(false); + when(player.hasPermission(Mockito.anyString())).thenReturn(true); + + when(player.getUniqueId()).thenReturn(uuid); + // Give player an island + when(im.hasIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + // Place the player just outside island + when(player.getLocation()).thenReturn(outside); + + // Lock island for player + when(island.isAllowed(Mockito.any(), Mockito.eq(Flags.LOCK))).thenReturn(false); + + // Move player + PlayerMoveEvent e = new PlayerMoveEvent(player, outside, inside); + listener.onPlayerMove(e); + assertFalse(e.isCancelled()); + } + + @Test public void testPlayerMoveIntoLockedIslandAsMember() { // Make player @@ -512,6 +615,48 @@ public class LockAndBanListenerTest { assertFalse(ev.isCancelled()); } + @Test + public void testPlayerMoveInsideLockedIslandAsOp() { + // Make player + Player player = mock(Player.class); + when(player.getUniqueId()).thenReturn(uuid); + when(player.isOp()).thenReturn(true); + // Give player an island + when(im.hasIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + // Place the player inside island + when(player.getLocation()).thenReturn(inside); + + // Lock island for player + when(island.isAllowed(Mockito.any(), Mockito.eq(Flags.LOCK))).thenReturn(false); + + // Move player + PlayerMoveEvent e = new PlayerMoveEvent(player, inside, inside2); + listener.onPlayerMove(e); + assertFalse(e.isCancelled()); + } + + @Test + public void testPlayerMoveInsideLockedIslandWithBypass() { + // Make player + Player player = mock(Player.class); + when(player.getUniqueId()).thenReturn(uuid); + when(player.isOp()).thenReturn(false); + when(player.hasPermission(Mockito.anyString())).thenReturn(true); + // Give player an island + when(im.hasIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); + // Place the player inside island + when(player.getLocation()).thenReturn(inside); + + // Lock island for player + when(island.isAllowed(Mockito.any(), Mockito.eq(Flags.LOCK))).thenReturn(false); + + // Move player + PlayerMoveEvent e = new PlayerMoveEvent(player, inside, inside2); + listener.onPlayerMove(e); + assertFalse(e.isCancelled()); + } + + @Test public void testPlayerMoveInsideLockedIslandAsMember() { // Make player