mirror of
https://github.com/BentoBoxWorld/BentoBox.git
synced 2024-11-24 11:45:31 +01:00
Finished up /island reset confirm
This commit is contained in:
parent
0bdc146da3
commit
673c404719
@ -83,7 +83,8 @@ commands:
|
||||
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!"
|
||||
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:
|
||||
description: "set your teleport point for /island"
|
||||
must-be-on-your-island: "You must be on your island to set home!"
|
||||
|
@ -22,7 +22,7 @@ import us.tastybento.bskyblock.managers.island.NewIsland;
|
||||
public class IslandResetCommand extends CompositeCommand {
|
||||
|
||||
private Map<UUID, Long> cooldown;
|
||||
private Set<UUID> confirm;
|
||||
private Map<UUID, Long> confirm;
|
||||
|
||||
public IslandResetCommand(CompositeCommand islandCommand) {
|
||||
super(islandCommand, "reset", "restart");
|
||||
@ -31,7 +31,7 @@ public class IslandResetCommand extends CompositeCommand {
|
||||
@Override
|
||||
public void setup() {
|
||||
cooldown = new HashMap<>();
|
||||
confirm = new HashSet<>();
|
||||
confirm = new HashMap<>();
|
||||
setPermission(Constants.PERMPREFIX + "island.create");
|
||||
setOnlyPlayer(true);
|
||||
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())));
|
||||
}
|
||||
}
|
||||
// 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
|
||||
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
|
||||
Player player = user.getPlayer();
|
||||
player.setGameMode(GameMode.SPECTATOR);
|
||||
@ -87,13 +95,24 @@ public class IslandResetCommand extends CompositeCommand {
|
||||
}
|
||||
setCooldown(user);
|
||||
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) {
|
||||
|
@ -10,12 +10,14 @@ import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.HashMap;
|
||||
import java.util.UUID;
|
||||
|
||||
import org.bukkit.Bukkit;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.scheduler.BukkitScheduler;
|
||||
import org.junit.Before;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
@ -25,6 +27,7 @@ import org.powermock.modules.junit4.PowerMockRunner;
|
||||
import org.powermock.reflect.Whitebox;
|
||||
|
||||
import us.tastybento.bskyblock.BSkyBlock;
|
||||
import us.tastybento.bskyblock.Constants;
|
||||
import us.tastybento.bskyblock.Settings;
|
||||
import us.tastybento.bskyblock.api.user.User;
|
||||
import us.tastybento.bskyblock.commands.IslandCommand;
|
||||
@ -39,36 +42,64 @@ import us.tastybento.bskyblock.managers.island.NewIsland;
|
||||
*
|
||||
*/
|
||||
@RunWith(PowerMockRunner.class)
|
||||
@PrepareForTest({BSkyBlock.class, NewIsland.class })
|
||||
@PrepareForTest({Bukkit.class, BSkyBlock.class, NewIsland.class })
|
||||
public class IslandResetCommandTest {
|
||||
|
||||
private static BSkyBlock plugin;
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@BeforeClass
|
||||
public static void setUpBeforeClass() throws Exception {
|
||||
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);
|
||||
*/
|
||||
}
|
||||
private BSkyBlock plugin;
|
||||
private IslandCommand ic;
|
||||
private UUID uuid;
|
||||
private User user;
|
||||
private Settings s;
|
||||
private IslandsManager im;
|
||||
private PlayersManager pm;
|
||||
|
||||
/**
|
||||
* @throws java.lang.Exception
|
||||
*/
|
||||
@Before
|
||||
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);
|
||||
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
|
||||
*/
|
||||
@Test
|
||||
public void testExecuteUserListOfString() 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<>());
|
||||
|
||||
public void testNoIsland() throws IOException {
|
||||
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
|
||||
// Does not have island
|
||||
assertFalse(irc.execute(user, new ArrayList<>()));
|
||||
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
|
||||
when(im.hasIsland(Mockito.eq(uuid))).thenReturn(true);
|
||||
assertFalse(irc.execute(user, new ArrayList<>()));
|
||||
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
|
||||
when(im.isOwner(Mockito.eq(uuid))).thenReturn(true);
|
||||
assertFalse(irc.execute(user, new ArrayList<>()));
|
||||
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
|
||||
when(pm.inTeam(Mockito.eq(uuid))).thenReturn(false);
|
||||
|
||||
// Block based on no resets left
|
||||
when(s.getResetLimit()).thenReturn(1);
|
||||
when(pm.getResetsLeft(Mockito.eq(uuid))).thenReturn(0);
|
||||
|
||||
assertFalse(irc.execute(user, new ArrayList<>()));
|
||||
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
|
||||
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);
|
||||
|
||||
// Old island
|
||||
// Old island mock
|
||||
Island oldIsland = mock(Island.class);
|
||||
when(im.getIsland(Mockito.eq(uuid))).thenReturn(oldIsland);
|
||||
|
||||
// Mock up NewIsland
|
||||
// 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);
|
||||
@ -147,12 +195,88 @@ public class IslandResetCommandTest {
|
||||
PowerMockito.mockStatic(NewIsland.class);
|
||||
when(NewIsland.builder()).thenReturn(builder);
|
||||
|
||||
// Reset
|
||||
// Reset command, no confirmation required
|
||||
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(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();
|
||||
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user