From 11d257c64ab9abf622bef1dae725c2cd82ca8a49 Mon Sep 17 00:00:00 2001 From: tastybento Date: Mon, 25 Jun 2018 15:17:16 -0700 Subject: [PATCH] Added reset for inventory and enderchests. Money is to do because it required Vault integration. --- locales/en-US.yml | 2 - .../commands/island/IslandResetCommand.java | 10 +++++ .../teams/IslandTeamInviteAcceptCommand.java | 10 +++++ .../island/teams/IslandTeamKickCommand.java | 10 +++++ .../island/teams/IslandTeamLeaveCommand.java | 19 ++++++-- .../teams/IslandTeamKickCommandTest.java | 41 +++++++++++++++-- .../teams/IslandTeamLeaveCommandTest.java | 44 +++++++++++++++++-- 7 files changed, 123 insertions(+), 13 deletions(-) diff --git a/locales/en-US.yml b/locales/en-US.yml index 84f85c57e..b39d19772 100644 --- a/locales/en-US.yml +++ b/locales/en-US.yml @@ -211,12 +211,10 @@ commands: leave: cannot-leave: "&cTeamleaders cannot leave! Become a member first, or kick all members." description: "leave your island" - type-again: "&cEnter the leave command again to confirm" left-your-island: "&c[name] left your island" kick: description: "remove a member from your island" parameters: "" - type-again: "&cEnter the kick command again to confirm" leader-kicked: "&cThe leader kicked you from the island!" cannot-kick: "&cYou cannot kick yourself!" demote: diff --git a/src/main/java/us/tastybento/bskyblock/commands/island/IslandResetCommand.java b/src/main/java/us/tastybento/bskyblock/commands/island/IslandResetCommand.java index b35c0e0dd..5346e9b96 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/island/IslandResetCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/island/IslandResetCommand.java @@ -78,6 +78,16 @@ public class IslandResetCommand extends CompositeCommand { Island oldIsland = getIslands().getIsland(getWorld(), player.getUniqueId()); // Remove them from this island (it still exists and will be deleted later) getIslands().removePlayer(getWorld(), player.getUniqueId()); + // Remove money inventory etc. + if (getIWM().isOnLeaveResetEnderChest(getWorld())) { + user.getPlayer().getEnderChest().clear(); + } + if (getIWM().isOnLeaveResetInventory(getWorld())) { + user.getPlayer().getInventory().clear(); + } + if (getSettings().isUseEconomy() && getIWM().isOnLeaveResetMoney(getWorld())) { + // TODO: needs Vault + } // Create new island and then delete the old one try { NewIsland.builder() diff --git a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteAcceptCommand.java b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteAcceptCommand.java index 6f08e8eb9..43cfadee7 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteAcceptCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamInviteAcceptCommand.java @@ -77,6 +77,16 @@ public class IslandTeamInviteAcceptCommand extends CompositeCommand { user.teleport(newHome); // Remove player as owner of the old island getIslands().removePlayer(getWorld(), playerUUID); + // Remove money inventory etc. + if (getIWM().isOnLeaveResetEnderChest(getWorld())) { + user.getPlayer().getEnderChest().clear(); + } + if (getIWM().isOnLeaveResetInventory(getWorld())) { + user.getPlayer().getInventory().clear(); + } + if (getSettings().isUseEconomy() && getIWM().isOnLeaveResetMoney(getWorld())) { + // TODO: needs Vault + } // Add the player as a team member of the new island getIslands().setJoinTeam(teamIsland, playerUUID); // Set the player's home diff --git a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamKickCommand.java b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamKickCommand.java index 9847eb11f..89a8e5ce7 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamKickCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamKickCommand.java @@ -60,6 +60,16 @@ public class IslandTeamKickCommand extends CompositeCommand { kickSet.remove(targetUUID); User.getInstance(targetUUID).sendMessage("commands.island.team.kick.leader-kicked"); getIslands().removePlayer(getWorld(), targetUUID); + // Remove money inventory etc. + if (getIWM().isOnLeaveResetEnderChest(getWorld())) { + user.getPlayer().getEnderChest().clear(); + } + if (getIWM().isOnLeaveResetInventory(getWorld())) { + user.getPlayer().getInventory().clear(); + } + if (getSettings().isUseEconomy() && getIWM().isOnLeaveResetMoney(getWorld())) { + // TODO: needs Vault + } user.sendMessage("general.success"); return true; } else { diff --git a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamLeaveCommand.java b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamLeaveCommand.java index c6eba2098..599c9df74 100644 --- a/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamLeaveCommand.java +++ b/src/main/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamLeaveCommand.java @@ -31,11 +31,12 @@ public class IslandTeamLeaveCommand extends CompositeCommand { return false; } if (!getSettings().isLeaveConfirmation()) { - leave(user); + leave(user); + return true; } else { this.askConfirmation(user, () -> leave(user)); - } - return true; + return false; + } } private void leave(User user) { @@ -43,7 +44,17 @@ public class IslandTeamLeaveCommand extends CompositeCommand { if (leaderUUID != null) { User.getInstance(leaderUUID).sendMessage("commands.island.team.leave.left-your-island", TextVariables.NAME, user.getName()); } - getIslands().removePlayer(getWorld(), user.getUniqueId()); + getIslands().setLeaveTeam(getWorld(), user.getUniqueId()); + // Remove money inventory etc. + if (getIWM().isOnLeaveResetEnderChest(getWorld())) { + user.getPlayer().getEnderChest().clear(); + } + if (getIWM().isOnLeaveResetInventory(getWorld())) { + user.getPlayer().getInventory().clear(); + } + if (getSettings().isUseEconomy() && getIWM().isOnLeaveResetMoney(getWorld())) { + // TODO: needs Vault + } user.sendMessage("general.success"); } diff --git a/src/test/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamKickCommandTest.java b/src/test/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamKickCommandTest.java index c8aefafd0..a47d86ecc 100644 --- a/src/test/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamKickCommandTest.java +++ b/src/test/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamKickCommandTest.java @@ -17,6 +17,8 @@ import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.PlayerInventory; import org.bukkit.scheduler.BukkitScheduler; import org.junit.Before; import org.junit.Test; @@ -52,6 +54,8 @@ public class IslandTeamKickCommandTest { private IslandsManager im; private PlayersManager pm; private UUID notUUID; + private IslandWorldManager iwm; + private Player player; /** * @throws java.lang.Exception @@ -73,7 +77,7 @@ public class IslandTeamKickCommandTest { when(plugin.getSettings()).thenReturn(s); // Player - Player p = mock(Player.class); + player = mock(Player.class); // Sometimes use Mockito.withSettings().verboseLogging() user = mock(User.class); when(user.isOp()).thenReturn(false); @@ -83,7 +87,7 @@ public class IslandTeamKickCommandTest { notUUID = UUID.randomUUID(); } when(user.getUniqueId()).thenReturn(uuid); - when(user.getPlayer()).thenReturn(p); + when(user.getPlayer()).thenReturn(player); when(user.getName()).thenReturn("tastybento"); User.setPlugin(plugin); @@ -115,7 +119,7 @@ public class IslandTeamKickCommandTest { when(plugin.getLocalesManager()).thenReturn(lm); // IWM friendly name - IslandWorldManager iwm = mock(IslandWorldManager.class); + iwm = mock(IslandWorldManager.class); when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock"); when(plugin.getIWM()).thenReturn(iwm); } @@ -230,4 +234,35 @@ public class IslandTeamKickCommandTest { Mockito.verify(user).sendMessage(Mockito.eq("commands.island.team.kick.type-again")); } + /** + * Test method for {@link us.tastybento.bskyblock.commands.island.teams.IslandTeamKickCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}. + */ + @Test + public void testExecuteTestResets() { + when(s.isKickConfirmation()).thenReturn(false); + + String[] name = {"tastybento"}; + when(pm.getUUID(Mockito.any())).thenReturn(notUUID); + + Set members = new HashSet<>(); + members.add(notUUID); + when(im.getMembers(Mockito.any(), Mockito.any())).thenReturn(members); + + // Require resets + when(iwm.isOnLeaveResetEnderChest(Mockito.any())).thenReturn(true); + Inventory enderChest = mock(Inventory.class); + when(player.getEnderChest()).thenReturn(enderChest); + when(iwm.isOnLeaveResetInventory(Mockito.any())).thenReturn(true); + PlayerInventory inv = mock(PlayerInventory.class); + when(player.getInventory()).thenReturn(inv); + when(iwm.isOnLeaveResetMoney(Mockito.any())).thenReturn(true); + + IslandTeamKickCommand itl = new IslandTeamKickCommand(ic); + assertTrue(itl.execute(user, Arrays.asList(name))); + Mockito.verify(im).removePlayer(Mockito.any(), Mockito.eq(notUUID)); + Mockito.verify(user).sendMessage(Mockito.eq("general.success")); + + Mockito.verify(enderChest).clear(); + Mockito.verify(inv).clear(); + } } diff --git a/src/test/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamLeaveCommandTest.java b/src/test/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamLeaveCommandTest.java index 3ed4fbf4d..b4b3b5887 100644 --- a/src/test/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamLeaveCommandTest.java +++ b/src/test/java/us/tastybento/bskyblock/commands/island/teams/IslandTeamLeaveCommandTest.java @@ -14,6 +14,8 @@ import java.util.UUID; import org.bukkit.Bukkit; import org.bukkit.entity.Player; +import org.bukkit.inventory.Inventory; +import org.bukkit.inventory.PlayerInventory; import org.bukkit.scheduler.BukkitScheduler; import org.junit.Before; import org.junit.Test; @@ -29,6 +31,7 @@ import us.tastybento.bskyblock.Settings; import us.tastybento.bskyblock.api.user.User; import us.tastybento.bskyblock.commands.IslandCommand; import us.tastybento.bskyblock.managers.CommandsManager; +import us.tastybento.bskyblock.managers.IslandWorldManager; import us.tastybento.bskyblock.managers.IslandsManager; import us.tastybento.bskyblock.managers.PlayersManager; @@ -45,6 +48,8 @@ public class IslandTeamLeaveCommandTest { private User user; private Settings s; private IslandsManager im; + private IslandWorldManager iwm; + private Player player; /** * @throws java.lang.Exception @@ -66,13 +71,13 @@ public class IslandTeamLeaveCommandTest { when(plugin.getSettings()).thenReturn(s); // Player - Player p = mock(Player.class); + player = mock(Player.class); // Sometimes use Mockito.withSettings().verboseLogging() user = mock(User.class); when(user.isOp()).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"); // Parent command has no aliases @@ -95,6 +100,9 @@ public class IslandTeamLeaveCommandTest { PowerMockito.mockStatic(Bukkit.class); when(Bukkit.getScheduler()).thenReturn(sch); + // Island World Manager + iwm = mock(IslandWorldManager.class); + when(plugin.getIWM()).thenReturn(iwm); } /** @@ -131,7 +139,7 @@ public class IslandTeamLeaveCommandTest { IslandTeamLeaveCommand itl = new IslandTeamLeaveCommand(ic); assertTrue(itl.execute(user, new ArrayList<>())); - Mockito.verify(im).removePlayer(Mockito.any(), Mockito.eq(uuid)); + Mockito.verify(im).setLeaveTeam(Mockito.any(), Mockito.eq(uuid)); Mockito.verify(user).sendMessage(Mockito.eq("general.success")); } @@ -151,7 +159,35 @@ public class IslandTeamLeaveCommandTest { IslandTeamLeaveCommand itl = new IslandTeamLeaveCommand(ic); assertFalse(itl.execute(user, new ArrayList<>())); // Confirmation required - Mockito.verify(user).sendMessage(Mockito.eq("commands.island.team.leave.type-again")); + Mockito.verify(user).sendMessage(Mockito.eq("general.confirm"), Mockito.eq("[seconds]"), Mockito.eq("0")); } + /** + * Test method for {@link us.tastybento.bskyblock.commands.island.teams.IslandTeamLeaveCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}. + */ + @Test + public void testExecuteTestResets() { + when(s.isLeaveConfirmation()).thenReturn(false); + when(im.hasIsland(Mockito.any(), Mockito.eq(uuid))).thenReturn(false); + when(im.isOwner(Mockito.any(), Mockito.eq(uuid))).thenReturn(false); + // Add a team leader - null + when(im.getTeamLeader(Mockito.any(), Mockito.any())).thenReturn(null); + + // Require resets + when(iwm.isOnLeaveResetEnderChest(Mockito.any())).thenReturn(true); + Inventory enderChest = mock(Inventory.class); + when(player.getEnderChest()).thenReturn(enderChest); + when(iwm.isOnLeaveResetInventory(Mockito.any())).thenReturn(true); + PlayerInventory inv = mock(PlayerInventory.class); + when(player.getInventory()).thenReturn(inv); + when(iwm.isOnLeaveResetMoney(Mockito.any())).thenReturn(true); + + IslandTeamLeaveCommand itl = new IslandTeamLeaveCommand(ic); + assertTrue(itl.execute(user, new ArrayList<>())); + Mockito.verify(im).setLeaveTeam(Mockito.any(), Mockito.eq(uuid)); + Mockito.verify(user).sendMessage(Mockito.eq("general.success")); + + Mockito.verify(enderChest).clear(); + Mockito.verify(inv).clear(); + } }