Changed island team kick to use confirm API

Fix for:
https://github.com/BentoBoxWorld/bentobox/issues/214
This commit is contained in:
tastybento 2018-08-03 19:22:35 -07:00
parent bad870d3da
commit 53f28eb68d
2 changed files with 43 additions and 57 deletions

View File

@ -1,19 +1,13 @@
package world.bentobox.bentobox.api.commands.island.team; package world.bentobox.bentobox.api.commands.island.team;
import java.util.HashSet;
import java.util.List; import java.util.List;
import java.util.Set;
import java.util.UUID; import java.util.UUID;
import org.bukkit.scheduler.BukkitRunnable;
import world.bentobox.bentobox.api.commands.CompositeCommand; import world.bentobox.bentobox.api.commands.CompositeCommand;
import world.bentobox.bentobox.api.user.User; import world.bentobox.bentobox.api.user.User;
public class IslandTeamKickCommand extends CompositeCommand { public class IslandTeamKickCommand extends CompositeCommand {
Set<UUID> kickSet;
public IslandTeamKickCommand(CompositeCommand islandTeamCommand) { public IslandTeamKickCommand(CompositeCommand islandTeamCommand) {
super(islandTeamCommand, "kick"); super(islandTeamCommand, "kick");
} }
@ -24,7 +18,6 @@ public class IslandTeamKickCommand extends CompositeCommand {
setOnlyPlayer(true); setOnlyPlayer(true);
setParameters("commands.island.team.kick.parameters"); setParameters("commands.island.team.kick.parameters");
setDescription("commands.island.team.kick.description"); setDescription("commands.island.team.kick.description");
kickSet = new HashSet<>();
} }
@Override @Override
@ -56,37 +49,30 @@ public class IslandTeamKickCommand extends CompositeCommand {
user.sendMessage("general.errors.not-in-team"); user.sendMessage("general.errors.not-in-team");
return false; return false;
} }
if (!getSettings().isKickConfirmation() || kickSet.contains(targetUUID)) { if (!getSettings().isKickConfirmation()) {
kickSet.remove(targetUUID); kick(user, 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; return true;
} else { } else {
user.sendMessage("commands.island.team.kick.type-again"); this.askConfirmation(user, () -> kick(user, targetUUID));
kickSet.add(targetUUID);
new BukkitRunnable() {
@Override
public void run() {
if (kickSet.contains(targetUUID)) {
kickSet.remove(targetUUID);
user.sendMessage("general.errors.command-cancelled");
}
}}.runTaskLater(getPlugin(), getSettings().getKickWait() * 20);
return false; return false;
} }
} }
private void kick(User user, UUID 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");
}
} }

View File

@ -1,5 +1,5 @@
/** /**
* *
*/ */
package world.bentobox.bentobox.api.commands.island.team; package world.bentobox.bentobox.api.commands.island.team;
@ -96,17 +96,17 @@ public class IslandTeamKickCommandTest {
ic = mock(CompositeCommand.class); ic = mock(CompositeCommand.class);
when(ic.getSubCommandAliases()).thenReturn(new HashMap<>()); when(ic.getSubCommandAliases()).thenReturn(new HashMap<>());
// Player has island to begin with // Player has island to begin with
im = mock(IslandsManager.class); im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true); when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
when(im.isOwner(Mockito.any(), Mockito.any())).thenReturn(true); when(im.isOwner(Mockito.any(), Mockito.any())).thenReturn(true);
when(im.getTeamLeader(Mockito.any(), Mockito.any())).thenReturn(uuid); when(im.getTeamLeader(Mockito.any(), Mockito.any())).thenReturn(uuid);
when(plugin.getIslands()).thenReturn(im); when(plugin.getIslands()).thenReturn(im);
// Has team // Has team
pm = mock(PlayersManager.class); pm = mock(PlayersManager.class);
when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true); when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true);
when(plugin.getPlayers()).thenReturn(pm); when(plugin.getPlayers()).thenReturn(pm);
// Server & Scheduler // Server & Scheduler
@ -118,7 +118,7 @@ public class IslandTeamKickCommandTest {
LocalesManager lm = mock(LocalesManager.class); LocalesManager lm = mock(LocalesManager.class);
when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation"); when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation");
when(plugin.getLocalesManager()).thenReturn(lm); when(plugin.getLocalesManager()).thenReturn(lm);
// IWM friendly name // IWM friendly name
iwm = mock(IslandWorldManager.class); iwm = mock(IslandWorldManager.class);
when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock"); when(iwm.getFriendlyName(Mockito.any())).thenReturn("BSkyBlock");
@ -135,7 +135,7 @@ public class IslandTeamKickCommandTest {
assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>())); assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>()));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-team")); Mockito.verify(user).sendMessage(Mockito.eq("general.errors.no-team"));
} }
/** /**
* Test method for {@link IslandTeamKickCommand#execute(world.bentobox.bentobox.api.user.User, java.util.List)}. * Test method for {@link IslandTeamKickCommand#execute(world.bentobox.bentobox.api.user.User, java.util.List)}.
*/ */
@ -146,7 +146,7 @@ public class IslandTeamKickCommandTest {
assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>())); assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>()));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.not-leader")); Mockito.verify(user).sendMessage(Mockito.eq("general.errors.not-leader"));
} }
/** /**
* Test method for {@link IslandTeamKickCommand#execute(world.bentobox.bentobox.api.user.User, java.util.List)}. * Test method for {@link IslandTeamKickCommand#execute(world.bentobox.bentobox.api.user.User, java.util.List)}.
*/ */
@ -156,7 +156,7 @@ public class IslandTeamKickCommandTest {
assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>())); assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>()));
// Show help // Show help
} }
/** /**
* Test method for {@link IslandTeamKickCommand#execute(world.bentobox.bentobox.api.user.User, java.util.List)}. * Test method for {@link IslandTeamKickCommand#execute(world.bentobox.bentobox.api.user.User, java.util.List)}.
*/ */
@ -168,7 +168,7 @@ public class IslandTeamKickCommandTest {
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.unknown-player")); Mockito.verify(user).sendMessage(Mockito.eq("general.errors.unknown-player"));
} }
/** /**
* Test method for {@link IslandTeamKickCommand#execute(world.bentobox.bentobox.api.user.User, java.util.List)}. * Test method for {@link IslandTeamKickCommand#execute(world.bentobox.bentobox.api.user.User, java.util.List)}.
*/ */
@ -180,8 +180,8 @@ public class IslandTeamKickCommandTest {
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(user).sendMessage(Mockito.eq("commands.island.kick.cannot-kick")); Mockito.verify(user).sendMessage(Mockito.eq("commands.island.kick.cannot-kick"));
} }
/** /**
* Test method for {@link IslandTeamKickCommand#execute(world.bentobox.bentobox.api.user.User, java.util.List)}. * Test method for {@link IslandTeamKickCommand#execute(world.bentobox.bentobox.api.user.User, java.util.List)}.
*/ */
@ -194,45 +194,45 @@ public class IslandTeamKickCommandTest {
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.not-in-team")); Mockito.verify(user).sendMessage(Mockito.eq("general.errors.not-in-team"));
} }
/** /**
* Test method for {@link IslandTeamKickCommand#execute(world.bentobox.bentobox.api.user.User, java.util.List)}. * Test method for {@link IslandTeamKickCommand#execute(world.bentobox.bentobox.api.user.User, java.util.List)}.
*/ */
@Test @Test
public void testExecuteNoConfirmation() { public void testExecuteNoConfirmation() {
when(s.isKickConfirmation()).thenReturn(false); when(s.isKickConfirmation()).thenReturn(false);
String[] name = {"tastybento"}; String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(notUUID); when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
Set<UUID> members = new HashSet<>(); Set<UUID> members = new HashSet<>();
members.add(notUUID); members.add(notUUID);
when(im.getMembers(Mockito.any(), Mockito.any())).thenReturn(members); when(im.getMembers(Mockito.any(), Mockito.any())).thenReturn(members);
IslandTeamKickCommand itl = new IslandTeamKickCommand(ic); IslandTeamKickCommand itl = new IslandTeamKickCommand(ic);
assertTrue(itl.execute(user, itl.getLabel(), Arrays.asList(name))); assertTrue(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(im).removePlayer(Mockito.any(), Mockito.eq(notUUID)); Mockito.verify(im).removePlayer(Mockito.any(), Mockito.eq(notUUID));
Mockito.verify(user).sendMessage(Mockito.eq("general.success")); Mockito.verify(user).sendMessage(Mockito.eq("general.success"));
} }
/** /**
* Test method for {@link IslandTeamKickCommand#execute(world.bentobox.bentobox.api.user.User, java.util.List)}. * Test method for {@link IslandTeamKickCommand#execute(world.bentobox.bentobox.api.user.User, java.util.List)}.
*/ */
@Test @Test
public void testExecuteWithConfirmation() { public void testExecuteWithConfirmation() {
when(s.isKickConfirmation()).thenReturn(true); when(s.isKickConfirmation()).thenReturn(true);
String[] name = {"tastybento"}; String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(notUUID); when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
Set<UUID> members = new HashSet<>(); Set<UUID> members = new HashSet<>();
members.add(notUUID); members.add(notUUID);
when(im.getMembers(Mockito.any(), Mockito.any())).thenReturn(members); when(im.getMembers(Mockito.any(), Mockito.any())).thenReturn(members);
IslandTeamKickCommand itl = new IslandTeamKickCommand(ic); IslandTeamKickCommand itl = new IslandTeamKickCommand(ic);
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name))); assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
// Confirmation required // Confirmation required
Mockito.verify(user).sendMessage(Mockito.eq("commands.island.team.kick.type-again")); Mockito.verify(user).sendMessage(Mockito.eq("general.confirm"), Mockito.eq("[seconds]"), Mockito.eq("0"));
} }
/** /**
@ -241,14 +241,14 @@ public class IslandTeamKickCommandTest {
@Test @Test
public void testExecuteTestResets() { public void testExecuteTestResets() {
when(s.isKickConfirmation()).thenReturn(false); when(s.isKickConfirmation()).thenReturn(false);
String[] name = {"tastybento"}; String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(notUUID); when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
Set<UUID> members = new HashSet<>(); Set<UUID> members = new HashSet<>();
members.add(notUUID); members.add(notUUID);
when(im.getMembers(Mockito.any(), Mockito.any())).thenReturn(members); when(im.getMembers(Mockito.any(), Mockito.any())).thenReturn(members);
// Require resets // Require resets
when(iwm.isOnLeaveResetEnderChest(Mockito.any())).thenReturn(true); when(iwm.isOnLeaveResetEnderChest(Mockito.any())).thenReturn(true);
Inventory enderChest = mock(Inventory.class); Inventory enderChest = mock(Inventory.class);
@ -262,7 +262,7 @@ public class IslandTeamKickCommandTest {
assertTrue(itl.execute(user, itl.getLabel(), Arrays.asList(name))); assertTrue(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(im).removePlayer(Mockito.any(), Mockito.eq(notUUID)); Mockito.verify(im).removePlayer(Mockito.any(), Mockito.eq(notUUID));
Mockito.verify(user).sendMessage(Mockito.eq("general.success")); Mockito.verify(user).sendMessage(Mockito.eq("general.success"));
Mockito.verify(enderChest).clear(); Mockito.verify(enderChest).clear();
Mockito.verify(inv).clear(); Mockito.verify(inv).clear();
} }