From ad0931ffcbb22755e5398f1351467cd224c8bb62 Mon Sep 17 00:00:00 2001 From: BONNe Date: Sun, 3 Apr 2022 08:12:41 +0300 Subject: [PATCH] Fixes kick command (#1960) PR #1957 broke kick command and noone could kick players from teams. This should fix it. --- .../island/team/IslandTeamKickCommand.java | 2 +- .../team/IslandTeamKickCommandTest.java | 74 ++++++++++++++++++- 2 files changed, 72 insertions(+), 4 deletions(-) diff --git a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommand.java b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommand.java index e89bc3594..44ca46783 100644 --- a/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommand.java +++ b/src/main/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommand.java @@ -69,7 +69,7 @@ public boolean execute(User user, String label, List args) { return false; } - int targetRank = Objects.requireNonNull(island).getRank(user); + int targetRank = Objects.requireNonNull(island).getRank(targetUUID); if (rank <= targetRank) { user.sendMessage("commands.island.team.kick.cannot-kick-rank", TextVariables.NAME, getPlayers().getName(targetUUID)); diff --git a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java index 3e1c3ac28..b51cec80c 100644 --- a/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java +++ b/src/test/java/world/bentobox/bentobox/api/commands/island/team/IslandTeamKickCommandTest.java @@ -184,6 +184,10 @@ public void setUp() throws Exception { RanksManager rm = new RanksManager(); when(plugin.getRanksManager()).thenReturn(rm); + // Ranks + when(island.getRank(uuid)).thenReturn(RanksManager.OWNER_RANK); + when(island.getRank(user)).thenReturn(RanksManager.OWNER_RANK); + when(island.getRank(notUUID)).thenReturn(RanksManager.MEMBER_RANK); } @After @@ -206,11 +210,75 @@ public void testExecuteNoTeam() { * Test method for {@link IslandTeamKickCommand#execute(User, String, java.util.List)} */ @Test - public void testExecuteNotTeamOwner() { - when(im.getOwner(any(), any())).thenReturn(notUUID); + public void testExecuteLowerTeamRank() { + when(island.getRank(user)).thenReturn(RanksManager.MEMBER_RANK); + when(island.getRank(notUUID)).thenReturn(RanksManager.SUB_OWNER_RANK); + + when(pm.getUUID(any())).thenReturn(notUUID); + when(pm.getName(notUUID)).thenReturn("poslovitch"); + + Set members = new HashSet<>(); + members.add(notUUID); + when(im.getMembers(any(), any())).thenReturn(members); + + IslandTeamKickCommand itl = new IslandTeamKickCommand(ic); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("poslovitch"))); + verify(user).sendMessage(eq("commands.island.team.kick.cannot-kick-rank"), eq(TextVariables.NAME), eq("poslovitch")); + } + + + /** + * Test method for {@link IslandTeamKickCommand#execute(User, String, java.util.List)} + */ + @Test + public void testExecuteEqualTeamRank() { + when(island.getRank(user)).thenReturn(RanksManager.SUB_OWNER_RANK); + when(island.getRank(notUUID)).thenReturn(RanksManager.SUB_OWNER_RANK); + + when(pm.getUUID(any())).thenReturn(notUUID); + when(pm.getName(notUUID)).thenReturn("poslovitch"); + + Set members = new HashSet<>(); + members.add(notUUID); + when(im.getMembers(any(), any())).thenReturn(members); + + IslandTeamKickCommand itl = new IslandTeamKickCommand(ic); + assertFalse(itl.execute(user, itl.getLabel(), Collections.singletonList("poslovitch"))); + verify(user).sendMessage(eq("commands.island.team.kick.cannot-kick-rank"), eq(TextVariables.NAME), eq("poslovitch")); + } + + /** + * Test method for {@link IslandTeamKickCommand#execute(User, String, java.util.List)} + */ + @Test + public void testExecuteLargerTeamRank() { + when(island.getRank(user)).thenReturn(RanksManager.SUB_OWNER_RANK); + when(island.getRank(notUUID)).thenReturn(RanksManager.MEMBER_RANK); + + when(pm.getUUID(any())).thenReturn(notUUID); + when(pm.getName(notUUID)).thenReturn("poslovitch"); + + Set members = new HashSet<>(); + members.add(notUUID); + when(im.getMembers(any(), any())).thenReturn(members); + + IslandTeamKickCommand itl = new IslandTeamKickCommand(ic); + assertTrue(itl.execute(user, itl.getLabel(), Collections.singletonList("poslovitch"))); + verify(im).removePlayer(any(), eq(notUUID)); + verify(user).sendMessage("commands.island.team.kick.success", TextVariables.NAME, "poslovitch"); + } + + /** + * Test method for {@link IslandTeamKickCommand#execute(User, String, java.util.List)} + */ + @Test + public void testExecuteNoCommandRank() { + when(island.getRankCommand(anyString())).thenReturn(RanksManager.SUB_OWNER_RANK); + when(island.getRank(user)).thenReturn(RanksManager.MEMBER_RANK); + IslandTeamKickCommand itl = new IslandTeamKickCommand(ic); assertFalse(itl.execute(user, itl.getLabel(), Collections.emptyList())); - verify(user).sendMessage(eq("general.errors.not-owner")); + verify(user).sendMessage(eq("general.errors.insufficient-rank"), eq(TextVariables.RANK), eq("ranks.member")); } /**