Finished up /island reset confirm

This commit is contained in:
Tastybento 2018-04-28 16:32:34 -07:00
parent 0bdc146da3
commit 673c404719
3 changed files with 211 additions and 67 deletions

View File

@ -83,7 +83,8 @@ commands:
must-remove-members: "You must remove all members from your island before you can restart it (/island kick <player>)." must-remove-members: "You must remove all members from your island before you can restart it (/island kick <player>)."
none-left: "&cYou have no more resets left!" none-left: "&cYou have no more resets left!"
resets-left: "&cYou have [number] resets left" resets-left: "&cYou have [number] resets left"
confirm: "&cType [label] reset confirm within [seconds]s to confirm reset" confirm: "&cType &b/[label] reset confirm&c within [seconds]s to confirm reset"
cancelled: "&bReset cancelled"
sethome: sethome:
description: "set your teleport point for /island" description: "set your teleport point for /island"
must-be-on-your-island: "You must be on your island to set home!" must-be-on-your-island: "You must be on your island to set home!"

View File

@ -22,7 +22,7 @@ import us.tastybento.bskyblock.managers.island.NewIsland;
public class IslandResetCommand extends CompositeCommand { public class IslandResetCommand extends CompositeCommand {
private Map<UUID, Long> cooldown; private Map<UUID, Long> cooldown;
private Set<UUID> confirm; private Map<UUID, Long> confirm;
public IslandResetCommand(CompositeCommand islandCommand) { public IslandResetCommand(CompositeCommand islandCommand) {
super(islandCommand, "reset", "restart"); super(islandCommand, "reset", "restart");
@ -31,7 +31,7 @@ public class IslandResetCommand extends CompositeCommand {
@Override @Override
public void setup() { public void setup() {
cooldown = new HashMap<>(); cooldown = new HashMap<>();
confirm = new HashSet<>(); confirm = new HashMap<>();
setPermission(Constants.PERMPREFIX + "island.create"); setPermission(Constants.PERMPREFIX + "island.create");
setOnlyPlayer(true); setOnlyPlayer(true);
setDescription("commands.island.reset.description"); setDescription("commands.island.reset.description");
@ -65,8 +65,16 @@ public class IslandResetCommand extends CompositeCommand {
user.sendMessage("commands.island.reset.resets-left", "[number]", String.valueOf(getPlayers().getResetsLeft(user.getUniqueId()))); user.sendMessage("commands.island.reset.resets-left", "[number]", String.valueOf(getPlayers().getResetsLeft(user.getUniqueId())));
} }
} }
// Check for non-confirm command
if (args.size() > 0 && !(confirm.containsKey(user.getUniqueId()) && args.get(0).equalsIgnoreCase("confirm"))) {
showHelp(this, user);
return false;
}
// Check confirmation or reset immediately if no confirmation required // Check confirmation or reset immediately if no confirmation required
if (!getSettings().isResetConfirmation() || (confirm.contains(user.getUniqueId()) && args.size() == 1 && args.get(0).equalsIgnoreCase("confirm"))) { if (!getSettings().isResetConfirmation() || (confirm.containsKey(user.getUniqueId()) && args.size() == 1 && args.get(0).equalsIgnoreCase("confirm"))) {
// Remove the confirmation
confirm.remove(user.getUniqueId());
// Reset the island // Reset the island
Player player = user.getPlayer(); Player player = user.getPlayer();
player.setGameMode(GameMode.SPECTATOR); player.setGameMode(GameMode.SPECTATOR);
@ -87,13 +95,24 @@ public class IslandResetCommand extends CompositeCommand {
} }
setCooldown(user); setCooldown(user);
return true; return true;
} else {
// Require confirmation
user.sendMessage("commands.island.reset.confirm", "[label]", Constants.ISLANDCOMMAND, "[seconds]", String.valueOf(getSettings().getConfirmationTime()));
confirm.add(user.getUniqueId());
Bukkit.getScheduler().runTaskLater(getPlugin(), () -> confirm.remove(user.getUniqueId()), getSettings().getConfirmationTime() * 20L);
return true;
} }
// Confirmation required
if (!confirm.containsKey(user.getUniqueId())) {
user.sendMessage("commands.island.reset.confirm", "[label]", Constants.ISLANDCOMMAND, "[seconds]", String.valueOf(getSettings().getConfirmationTime()));
// Require confirmation
confirm.put(user.getUniqueId(), System.currentTimeMillis() + getSettings().getConfirmationTime() * 1000L);
Bukkit.getScheduler().runTaskLater(getPlugin(), () -> {
if (confirm.containsKey(user.getUniqueId())) {
user.sendMessage("commands.island.reset.cancelled");
confirm.remove(user.getUniqueId());
}
}, getSettings().getConfirmationTime() * 20L);
} else {
int time = (int)((confirm.get(user.getUniqueId()) - System.currentTimeMillis()) / 1000D);
user.sendMessage("commands.island.reset.confirm", "[label]", Constants.ISLANDCOMMAND, "[seconds]", String.valueOf(time));
}
return true;
} }
private int onRestartWaitTime(User user) { private int onRestartWaitTime(User user) {

View File

@ -10,12 +10,14 @@ import static org.mockito.Mockito.when;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
import java.util.UUID; import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player; import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler;
import org.junit.Before; import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.mockito.Mockito; import org.mockito.Mockito;
@ -25,6 +27,7 @@ import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox; import org.powermock.reflect.Whitebox;
import us.tastybento.bskyblock.BSkyBlock; import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Constants;
import us.tastybento.bskyblock.Settings; import us.tastybento.bskyblock.Settings;
import us.tastybento.bskyblock.api.user.User; import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.commands.IslandCommand; import us.tastybento.bskyblock.commands.IslandCommand;
@ -39,36 +42,64 @@ import us.tastybento.bskyblock.managers.island.NewIsland;
* *
*/ */
@RunWith(PowerMockRunner.class) @RunWith(PowerMockRunner.class)
@PrepareForTest({BSkyBlock.class, NewIsland.class }) @PrepareForTest({Bukkit.class, BSkyBlock.class, NewIsland.class })
public class IslandResetCommandTest { public class IslandResetCommandTest {
private static BSkyBlock plugin; private BSkyBlock plugin;
private IslandCommand ic;
/** private UUID uuid;
* @throws java.lang.Exception private User user;
*/ private Settings s;
@BeforeClass private IslandsManager im;
public static void setUpBeforeClass() throws Exception { private PlayersManager pm;
plugin = mock(BSkyBlock.class);
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
/*
NewIsland.Builder builder = mock(NewIsland.Builder.class);
when(builder.player(Mockito.any())).thenReturn(builder);
when(builder.oldIsland(Mockito.any())).thenReturn(builder);
when(builder.reason(Mockito.any())).thenReturn(builder);
when(builder.build()).thenReturn(mock(Island.class));
PowerMockito.mockStatic(NewIsland.class);
when(NewIsland.builder()).thenReturn(builder);
*/
}
/** /**
* @throws java.lang.Exception * @throws java.lang.Exception
*/ */
@Before @Before
public void setUp() throws Exception { public void setUp() throws Exception {
// Set up plugin
plugin = mock(BSkyBlock.class);
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
// Command manager
CommandsManager cm = mock(CommandsManager.class); CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm); when(plugin.getCommandsManager()).thenReturn(cm);
// Settings
s = mock(Settings.class);
when(s.getResetWait()).thenReturn(0L);
when(s.getResetLimit()).thenReturn(3);
when(plugin.getSettings()).thenReturn(s);
// Player
Player p = mock(Player.class);
user = mock(User.class);
when(user.isOp()).thenReturn(false);
uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
// Parent command has no aliases
ic = mock(IslandCommand.class);
when(ic.getSubCommandAliases()).thenReturn(new HashMap<>());
// No island for player to begin with (set it later in the tests)
im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.eq(uuid))).thenReturn(false);
when(im.isOwner(Mockito.eq(uuid))).thenReturn(false);
when(plugin.getIslands()).thenReturn(im);
// Has team
pm = mock(PlayersManager.class);
when(pm.inTeam(Mockito.eq(uuid))).thenReturn(true);
when(plugin.getPlayers()).thenReturn(pm);
// Server & Scheduler
BukkitScheduler sch = mock(BukkitScheduler.class);
PowerMockito.mockStatic(Bukkit.class);
when(Bukkit.getScheduler()).thenReturn(sch);
} }
/** /**
@ -76,69 +107,86 @@ public class IslandResetCommandTest {
* @throws IOException * @throws IOException
*/ */
@Test @Test
public void testExecuteUserListOfString() throws IOException { public void testNoIsland() throws IOException {
Settings s = mock(Settings.class);
when(s.getResetWait()).thenReturn(0L);
when(plugin.getSettings()).thenReturn(s);
Player p = mock(Player.class);
User user = mock(User.class, Mockito.withSettings().verboseLogging());
when(user.isOp()).thenReturn(false);
UUID uuid = UUID.randomUUID();
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
IslandCommand ic = mock(IslandCommand.class);
when(ic.getSubCommandAliases()).thenReturn(new HashMap<>());
IslandResetCommand irc = new IslandResetCommand(ic); IslandResetCommand irc = new IslandResetCommand(ic);
// No island
IslandsManager im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.eq(uuid))).thenReturn(false);
when(im.isOwner(Mockito.eq(uuid))).thenReturn(false);
when(plugin.getIslands()).thenReturn(im);
// Has team
PlayersManager pm = mock(PlayersManager.class);
when(pm.inTeam(Mockito.eq(uuid))).thenReturn(true);
when(plugin.getPlayers()).thenReturn(pm);
// Test the reset command // Test the reset command
// Does not have island // Does not have island
assertFalse(irc.execute(user, new ArrayList<>())); assertFalse(irc.execute(user, new ArrayList<>()));
Mockito.verify(user).sendMessage("general.errors.no-island"); Mockito.verify(user).sendMessage("general.errors.no-island");
}
@Test
public void testNotLeader() throws IOException {
IslandResetCommand irc = new IslandResetCommand(ic);
// Now has island, but is not the leader // Now has island, but is not the leader
when(im.hasIsland(Mockito.eq(uuid))).thenReturn(true); when(im.hasIsland(Mockito.eq(uuid))).thenReturn(true);
assertFalse(irc.execute(user, new ArrayList<>())); assertFalse(irc.execute(user, new ArrayList<>()));
Mockito.verify(user).sendMessage("general.errors.not-leader"); Mockito.verify(user).sendMessage("general.errors.not-leader");
}
@Test
public void testHasTeam() throws IOException {
IslandResetCommand irc = new IslandResetCommand(ic);
// Now has island, but is not the leader
when(im.hasIsland(Mockito.eq(uuid))).thenReturn(true);
// Now is owner, but still has team // Now is owner, but still has team
when(im.isOwner(Mockito.eq(uuid))).thenReturn(true); when(im.isOwner(Mockito.eq(uuid))).thenReturn(true);
assertFalse(irc.execute(user, new ArrayList<>())); assertFalse(irc.execute(user, new ArrayList<>()));
Mockito.verify(user).sendMessage("commands.island.reset.must-remove-members"); Mockito.verify(user).sendMessage("commands.island.reset.must-remove-members");
}
@Test
public void testNoResetsLeft() throws IOException {
IslandResetCommand irc = new IslandResetCommand(ic);
// Now has island, but is not the leader
when(im.hasIsland(Mockito.eq(uuid))).thenReturn(true);
// Now is owner, but still has team
when(im.isOwner(Mockito.eq(uuid))).thenReturn(true);
// Now has no team // Now has no team
when(pm.inTeam(Mockito.eq(uuid))).thenReturn(false); when(pm.inTeam(Mockito.eq(uuid))).thenReturn(false);
// Block based on no resets left // Block based on no resets left
when(s.getResetLimit()).thenReturn(1);
when(pm.getResetsLeft(Mockito.eq(uuid))).thenReturn(0); when(pm.getResetsLeft(Mockito.eq(uuid))).thenReturn(0);
assertFalse(irc.execute(user, new ArrayList<>())); assertFalse(irc.execute(user, new ArrayList<>()));
Mockito.verify(user).sendMessage("commands.island.reset.none-left"); Mockito.verify(user).sendMessage("commands.island.reset.none-left");
}
@Test
public void testConfirmBeforeReset() throws IOException {
IslandResetCommand irc = new IslandResetCommand(ic);
// Now has island, but is not the leader
when(im.hasIsland(Mockito.eq(uuid))).thenReturn(true);
// Now is owner, but still has team
when(im.isOwner(Mockito.eq(uuid))).thenReturn(true);
// Now has no team
when(pm.inTeam(Mockito.eq(uuid))).thenReturn(false);
// Give the user some resets // Give the user some resets
when(pm.getResetsLeft(Mockito.eq(uuid))).thenReturn(1); when(pm.getResetsLeft(Mockito.eq(uuid))).thenReturn(1);
// No confirmation required // Test sending confirm immediately
assertFalse(irc.execute(user, Arrays.asList("confirm")));
}
@Test
public void testNoConfirmationRequired() throws IOException {
IslandResetCommand irc = new IslandResetCommand(ic);
// Now has island, but is not the leader
when(im.hasIsland(Mockito.eq(uuid))).thenReturn(true);
// Now is owner, but still has team
when(im.isOwner(Mockito.eq(uuid))).thenReturn(true);
// Now has no team
when(pm.inTeam(Mockito.eq(uuid))).thenReturn(false);
// Give the user some resets
when(pm.getResetsLeft(Mockito.eq(uuid))).thenReturn(1);
// Set so no confirmation required
when(s.isResetConfirmation()).thenReturn(false); when(s.isResetConfirmation()).thenReturn(false);
// Old island // Old island mock
Island oldIsland = mock(Island.class); Island oldIsland = mock(Island.class);
when(im.getIsland(Mockito.eq(uuid))).thenReturn(oldIsland); when(im.getIsland(Mockito.eq(uuid))).thenReturn(oldIsland);
// Mock up NewIsland // Mock up NewIsland builder
NewIsland.Builder builder = mock(NewIsland.Builder.class); NewIsland.Builder builder = mock(NewIsland.Builder.class);
when(builder.player(Mockito.any())).thenReturn(builder); when(builder.player(Mockito.any())).thenReturn(builder);
when(builder.oldIsland(Mockito.any())).thenReturn(builder); when(builder.oldIsland(Mockito.any())).thenReturn(builder);
@ -147,12 +195,88 @@ public class IslandResetCommandTest {
PowerMockito.mockStatic(NewIsland.class); PowerMockito.mockStatic(NewIsland.class);
when(NewIsland.builder()).thenReturn(builder); when(NewIsland.builder()).thenReturn(builder);
// Reset // Reset command, no confirmation required
assertTrue(irc.execute(user, new ArrayList<>())); assertTrue(irc.execute(user, new ArrayList<>()));
// Verify that build new island was called and the number of resets left shown
Mockito.verify(builder).build(); Mockito.verify(builder).build();
Mockito.verify(user).sendMessage("commands.island.reset.resets-left", "[number]", "1"); Mockito.verify(user).sendMessage("commands.island.reset.resets-left", "[number]", "1");
}
@Test
public void testUnlimitedResets() throws IOException {
IslandResetCommand irc = new IslandResetCommand(ic);
// Now has island, but is not the leader
when(im.hasIsland(Mockito.eq(uuid))).thenReturn(true);
// Now is owner, but still has team
when(im.isOwner(Mockito.eq(uuid))).thenReturn(true);
// Now has no team
when(pm.inTeam(Mockito.eq(uuid))).thenReturn(false);
// Give the user some resets
when(pm.getResetsLeft(Mockito.eq(uuid))).thenReturn(1);
// Set so no confirmation required
when(s.isResetConfirmation()).thenReturn(false);
// Old island mock
Island oldIsland = mock(Island.class);
when(im.getIsland(Mockito.eq(uuid))).thenReturn(oldIsland);
// Mock up NewIsland builder
NewIsland.Builder builder = mock(NewIsland.Builder.class);
when(builder.player(Mockito.any())).thenReturn(builder);
when(builder.oldIsland(Mockito.any())).thenReturn(builder);
when(builder.reason(Mockito.any())).thenReturn(builder);
when(builder.build()).thenReturn(mock(Island.class));
PowerMockito.mockStatic(NewIsland.class);
when(NewIsland.builder()).thenReturn(builder);
// Test with unlimited resets
when(s.getResetLimit()).thenReturn(-1);
// Reset
assertTrue(irc.execute(user, new ArrayList<>()));
// Verify that build new island was called and the number of resets left shown
Mockito.verify(builder).build();
// This should not be shown
Mockito.verify(user, Mockito.never()).sendMessage("commands.island.reset.resets-left", "[number]", "1");
}
@Test
public void testConfirmationRequired() throws IOException {
IslandResetCommand irc = new IslandResetCommand(ic);
// Now has island, but is not the leader
when(im.hasIsland(Mockito.eq(uuid))).thenReturn(true);
// Now is owner, but still has team
when(im.isOwner(Mockito.eq(uuid))).thenReturn(true);
// Now has no team
when(pm.inTeam(Mockito.eq(uuid))).thenReturn(false);
// Give the user some resets
when(pm.getResetsLeft(Mockito.eq(uuid))).thenReturn(1);
// Set so no confirmation required
when(s.isResetConfirmation()).thenReturn(false);
// Old island mock
Island oldIsland = mock(Island.class);
when(im.getIsland(Mockito.eq(uuid))).thenReturn(oldIsland);
// Mock up NewIsland builder
NewIsland.Builder builder = mock(NewIsland.Builder.class);
when(builder.player(Mockito.any())).thenReturn(builder);
when(builder.oldIsland(Mockito.any())).thenReturn(builder);
when(builder.reason(Mockito.any())).thenReturn(builder);
when(builder.build()).thenReturn(mock(Island.class));
PowerMockito.mockStatic(NewIsland.class);
when(NewIsland.builder()).thenReturn(builder);
// Require confirmation
when(s.isResetConfirmation()).thenReturn(true);
when(s.getConfirmationTime()).thenReturn(20);
// Reset
assertTrue(irc.execute(user, new ArrayList<>()));
Mockito.verify(user).sendMessage("commands.island.reset.confirm", "[label]", Constants.ISLANDCOMMAND, "[seconds]", String.valueOf(s.getConfirmationTime()));
// Reset confirm
assertTrue(irc.execute(user, Arrays.asList("confirm")));
Mockito.verify(builder).build();
} }
} }