diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandBanlistCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandBanlistCommand.java index 3ac1f613d..464c8d0bb 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/IslandBanlistCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/IslandBanlistCommand.java @@ -11,6 +11,8 @@ import world.bentobox.bentobox.database.objects.Island; public class IslandBanlistCommand extends CompositeCommand { + private Island island; + public IslandBanlistCommand(CompositeCommand islandCommand) { super(islandCommand, "banlist", "banned", "bans"); } @@ -23,7 +25,7 @@ public class IslandBanlistCommand extends CompositeCommand { } @Override - public boolean execute(User user, String label, List args) { + public boolean canExecute(User user, String label, List args) { if (!args.isEmpty()) { // Show help showHelp(this, user); @@ -34,7 +36,17 @@ public class IslandBanlistCommand extends CompositeCommand { user.sendMessage("general.errors.no-island"); return false; } - Island island = getIslands().getIsland(getWorld(), user.getUniqueId()); + // Check rank to use command + island = getIslands().getIsland(getWorld(), user.getUniqueId()); + if (island.getRank(user) < island.getRankCommand("ban")) { + user.sendMessage("general.errors.no-permission"); + return false; + } + return true; + } + + @Override + public boolean execute(User user, String label, List args) { // Show all the players banned on the island if (island.getBanned().isEmpty()) { user.sendMessage("commands.island.banlist.noone"); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanlistCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanlistCommandTest.java index bd8a320dc..de4b75331 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanlistCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/IslandBanlistCommandTest.java @@ -3,12 +3,12 @@ package world.bentobox.bentobox.api.commands.island; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyString; import static org.mockito.ArgumentMatchers.eq; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import java.util.ArrayList; import java.util.Collections; import java.util.HashMap; import java.util.HashSet; @@ -38,6 +38,7 @@ import world.bentobox.bentobox.managers.CommandsManager; import world.bentobox.bentobox.managers.IslandWorldManager; import world.bentobox.bentobox.managers.IslandsManager; import world.bentobox.bentobox.managers.PlayersManager; +import world.bentobox.bentobox.managers.RanksManager; /** * @author tastybento @@ -116,27 +117,40 @@ public class IslandBanlistCommandTest { } /** - * Test method for {@link IslandBanlistCommand#execute(User, String, java.util.List)}. + * Test method for {@link IslandBanlistCommand#canExecute(User, String, java.util.List)}. */ @Test public void testWithArgs() { IslandBanlistCommand iubc = new IslandBanlistCommand(ic); - assertFalse(iubc.execute(user, iubc.getLabel(), Collections.singletonList("bill"))); + assertFalse(iubc.canExecute(user, iubc.getLabel(), Collections.singletonList("bill"))); // Verify show help + verify(user).sendMessage("commands.help.header", "[label]", null); } /** - * Test method for {@link IslandBanlistCommand#execute(User, String, java.util.List)}. + * Test method for {@link IslandBanlistCommand#canExecute(User, String, java.util.List)}. */ @Test public void testNoIsland() { // not in team when(im.inTeam(any(), eq(uuid))).thenReturn(false); IslandBanlistCommand iubc = new IslandBanlistCommand(ic); - assertFalse(iubc.execute(user, iubc.getLabel(), new ArrayList<>())); + assertFalse(iubc.canExecute(user, iubc.getLabel(), Collections.emptyList())); verify(user).sendMessage("general.errors.no-island"); } + /** + * Test method for {@link IslandBanlistCommand#canExecute(User, String, java.util.List)}. + */ + @Test + public void testTooLowRank() { + when(island.getRank(any())).thenReturn(RanksManager.MEMBER_RANK); + when(island.getRankCommand(anyString())).thenReturn(RanksManager.OWNER_RANK); + IslandBanlistCommand iubc = new IslandBanlistCommand(ic); + assertFalse(iubc.canExecute(user, iubc.getLabel(), Collections.emptyList())); + verify(user).sendMessage("general.errors.no-permission"); + } + /** * Test method for {@link IslandBanlistCommand#execute(User, String, java.util.List)}. */ @@ -144,7 +158,8 @@ public class IslandBanlistCommandTest { public void testBanlistNooneBanned() { IslandBanlistCommand iubc = new IslandBanlistCommand(ic); when(im.hasIsland(any(), eq(uuid))).thenReturn(true); - assertTrue(iubc.execute(user, iubc.getLabel(), new ArrayList<>())); + iubc.canExecute(user, iubc.getLabel(), Collections.emptyList()); + assertTrue(iubc.execute(user, iubc.getLabel(), Collections.emptyList())); verify(user).sendMessage("commands.island.banlist.noone"); } @@ -167,7 +182,8 @@ public class IslandBanlistCommandTest { when(island.getBanned()).thenReturn(banned); // Respond to name queries when(pm.getName(any(UUID.class))).then((Answer) invocation -> uuidToName.getOrDefault(invocation.getArgument(0, UUID.class), "tastybento")); - assertTrue(iubc.execute(user, iubc.getLabel(), new ArrayList<>())); + iubc.canExecute(user, iubc.getLabel(), Collections.emptyList()); + assertTrue(iubc.execute(user, iubc.getLabel(), Collections.emptyList())); verify(user).sendMessage("commands.island.banlist.the-following"); }