Added clear resets and clear reset all admin commands.

Clear reset all uses a timestamp stored in config.yml. If a player logs
in and the last time they logged in was before that timestamp, then
their resets are cleared. Note that as opposed to ASkyBlock, the player
object stores the number of resets done for a world and not the number
of resets left. This is a better design because it means that admins can
change the max number  of resets and every player file does not have to
be adjusted.

Location of commit (30,000ft above Nevada desert, just coming into Las
Vegas).
This commit is contained in:
tastybento 2018-07-25 09:47:57 -07:00
parent 541ee35256
commit 9c41ceb5f2
7 changed files with 356 additions and 64 deletions

View File

@ -52,6 +52,12 @@ commands:
help:
parameters: ""
description: "admin command"
clearresets:
parameters: "<player>"
description: "clears player reset count for this world"
cleared: "&2Resets cleared"
clearresetsall:
description: "clears all player reset counts for this world"
team:
add:
parameters: "<leader> <player>"
@ -115,7 +121,7 @@ commands:
owner: "Owner: [owner] ([uuid])"
last-login: "Last login: [date]"
deaths: "Deaths: [number]"
resets-left: "Resets left: [number]/[total]"
resets-left: "Resets: [number] (Max: [total])"
team-members-title: "Team members:"
team-owner-format: "&a[name] [rank]"
team-member-format: "&b[name] [rank]"

View File

@ -5,8 +5,8 @@ import java.util.List;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.localization.TextVariables;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.commands.admin.AdminClearResetAllCommand;
import us.tastybento.bskyblock.commands.admin.AdminClearResetCommand;
import us.tastybento.bskyblock.commands.admin.AdminClearResetsAllCommand;
import us.tastybento.bskyblock.commands.admin.AdminClearResetsCommand;
import us.tastybento.bskyblock.commands.admin.AdminGetRankCommand;
import us.tastybento.bskyblock.commands.admin.AdminInfoCommand;
import us.tastybento.bskyblock.commands.admin.AdminRegisterCommand;
@ -57,8 +57,8 @@ public class AdminCommand extends CompositeCommand {
// Range
new AdminRangeCommand(this);
// Resets
new AdminClearResetCommand(this);
new AdminClearResetAllCommand(this);
new AdminClearResetsCommand(this);
new AdminClearResetsAllCommand(this);
}
@Override

View File

@ -1,52 +0,0 @@
package us.tastybento.bskyblock.commands.admin;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.util.Util;
public class AdminClearResetAllCommand extends CompositeCommand {
public AdminClearResetAllCommand(CompositeCommand parent) {
super(parent, "clearresetall");
}
@Override
public void setup() {
setPermission("admin.clearresetall");
setParameters("commands.admin.clearreset.parameters");
setDescription("commands.admin.clearreset.description");
}
@Override
public boolean execute(User user, String label, List<String> args) {
// If args are not right, show help
if (!args.isEmpty()) {
showHelp(this, user);
return false;
}
// Set the reset epoch to now
getIWM().setResetEpoch(getWorld());
// Reset all current players
Bukkit.getOnlinePlayers().stream().map(Player::getUniqueId).filter(getPlayers()::isKnown).forEach(u -> getPlayers().setResets(getWorld(), u, 0));
user.sendMessage("general.success");
return true;
}
@Override
public Optional<List<String>> tabComplete(User user, String alias, List<String> args) {
String lastArg = !args.isEmpty() ? args.get(args.size()-1) : "";
if (args.isEmpty()) {
// Don't show every player on the server. Require at least the first letter
return Optional.empty();
}
List<String> options = new ArrayList<>(Util.getOnlinePlayerList(user));
return Optional.of(Util.tabLimit(options, lastArg));
}
}

View File

@ -0,0 +1,40 @@
package us.tastybento.bskyblock.commands.admin;
import java.util.List;
import org.bukkit.Bukkit;
import org.bukkit.entity.Player;
import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.user.User;
public class AdminClearResetsAllCommand extends CompositeCommand {
public AdminClearResetsAllCommand(CompositeCommand parent) {
super(parent, "clearresetsall");
}
@Override
public void setup() {
setPermission("admin.clearresetsall");
setDescription("commands.admin.clearresetsall.description");
}
@Override
public boolean execute(User user, String label, List<String> args) {
// If args are not right, show help
if (!args.isEmpty()) {
showHelp(this, user);
return false;
}
this.askConfirmation(user, () -> {
// Set the reset epoch to now
getIWM().setResetEpoch(getWorld());
// Reset all current players
Bukkit.getOnlinePlayers().stream().map(Player::getUniqueId).filter(getPlayers()::isKnown).forEach(u -> getPlayers().setResets(getWorld(), u, 0));
user.sendMessage("general.success");
});
return false;
}
}

View File

@ -9,17 +9,17 @@ import us.tastybento.bskyblock.api.commands.CompositeCommand;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.util.Util;
public class AdminClearResetCommand extends CompositeCommand {
public class AdminClearResetsCommand extends CompositeCommand {
public AdminClearResetCommand(CompositeCommand parent) {
super(parent, "clearreset");
public AdminClearResetsCommand(CompositeCommand parent) {
super(parent, "clearresets");
}
@Override
public void setup() {
setPermission("admin.clearreset");
setParameters("commands.admin.clearreset.parameters");
setDescription("commands.admin.clearreset.description");
setParameters("commands.admin.clearresets.parameters");
setDescription("commands.admin.clearresets.description");
}
@Override
@ -40,8 +40,8 @@ public class AdminClearResetCommand extends CompositeCommand {
return false;
}
// Clear resets
user.sendMessage("commands.admin.clearreset.cleared");
getPlayers().setResets(getWorld(), user.getUniqueId(), 0);
user.sendMessage("commands.admin.clearresets.cleared");
getPlayers().setResets(getWorld(), targetUUID, 0);
user.sendMessage("general.success");
return true;
}

View File

@ -0,0 +1,124 @@
package us.tastybento.bskyblock.commands.admin;
import static org.junit.Assert.assertFalse;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Settings;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.commands.AdminCommand;
import us.tastybento.bskyblock.managers.CommandsManager;
import us.tastybento.bskyblock.managers.IslandWorldManager;
import us.tastybento.bskyblock.managers.IslandsManager;
import us.tastybento.bskyblock.managers.LocalesManager;
import us.tastybento.bskyblock.managers.PlayersManager;
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BSkyBlock.class, User.class })
public class AdminClearResetsAllCommandTest {
private AdminCommand ac;
private User user;
private IslandsManager im;
private PlayersManager pm;
private UUID notUUID;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
BSkyBlock plugin = mock(BSkyBlock.class);
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Settings
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);
// Sometimes use Mockito.withSettings().verboseLogging()
user = mock(User.class);
when(user.isOp()).thenReturn(false);
UUID uuid = UUID.randomUUID();
notUUID = UUID.randomUUID();
while(notUUID.equals(uuid)) {
notUUID = UUID.randomUUID();
}
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
User.setPlugin(plugin);
// Parent command has no aliases
ac = mock(AdminCommand.class);
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
// Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class);
World world = mock(World.class);
when(iwm.getBSBIslandWorld()).thenReturn(world);
when(plugin.getIWM()).thenReturn(iwm);
// Player has island to begin with
im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true);
when(im.isOwner(Mockito.any(),Mockito.any())).thenReturn(true);
when(im.getTeamLeader(Mockito.any(),Mockito.any())).thenReturn(uuid);
when(plugin.getIslands()).thenReturn(im);
// Has team
pm = mock(PlayersManager.class);
when(im.inTeam(Mockito.any(), 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);
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation");
when(plugin.getLocalesManager()).thenReturn(lm);
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.admin.AdminClearResetsAllCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteCheckConfirm() {
AdminClearResetsAllCommand itl = new AdminClearResetsAllCommand(ac);
assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>()));
Mockito.verify(user).sendMessage(Mockito.eq("general.confirm"), Mockito.eq("[seconds]"), Mockito.any());
}
}

View File

@ -0,0 +1,174 @@
/**
*
*/
package us.tastybento.bskyblock.commands.admin;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.UUID;
import org.bukkit.Bukkit;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.scheduler.BukkitScheduler;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
import org.powermock.modules.junit4.PowerMockRunner;
import org.powermock.reflect.Whitebox;
import us.tastybento.bskyblock.BSkyBlock;
import us.tastybento.bskyblock.Settings;
import us.tastybento.bskyblock.api.user.User;
import us.tastybento.bskyblock.commands.AdminCommand;
import us.tastybento.bskyblock.managers.CommandsManager;
import us.tastybento.bskyblock.managers.IslandWorldManager;
import us.tastybento.bskyblock.managers.IslandsManager;
import us.tastybento.bskyblock.managers.LocalesManager;
import us.tastybento.bskyblock.managers.PlayersManager;
/**
* @author ben
*
*/
@RunWith(PowerMockRunner.class)
@PrepareForTest({Bukkit.class, BSkyBlock.class, User.class })
public class AdminClearResetsCommandTest {
private AdminCommand ac;
private User user;
private IslandsManager im;
private PlayersManager pm;
private UUID notUUID;
/**
* @throws java.lang.Exception
*/
@Before
public void setUp() throws Exception {
// Set up plugin
BSkyBlock plugin = mock(BSkyBlock.class);
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
// Command manager
CommandsManager cm = mock(CommandsManager.class);
when(plugin.getCommandsManager()).thenReturn(cm);
// Settings
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);
// Sometimes use Mockito.withSettings().verboseLogging()
user = mock(User.class);
when(user.isOp()).thenReturn(false);
UUID uuid = UUID.randomUUID();
notUUID = UUID.randomUUID();
while(notUUID.equals(uuid)) {
notUUID = UUID.randomUUID();
}
when(user.getUniqueId()).thenReturn(uuid);
when(user.getPlayer()).thenReturn(p);
when(user.getName()).thenReturn("tastybento");
User.setPlugin(plugin);
// Parent command has no aliases
ac = mock(AdminCommand.class);
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
// Island World Manager
IslandWorldManager iwm = mock(IslandWorldManager.class);
World world = mock(World.class);
when(iwm.getBSBIslandWorld()).thenReturn(world);
when(plugin.getIWM()).thenReturn(iwm);
// Player has island to begin with
im = mock(IslandsManager.class);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true);
when(im.isOwner(Mockito.any(),Mockito.any())).thenReturn(true);
when(im.getTeamLeader(Mockito.any(),Mockito.any())).thenReturn(uuid);
when(plugin.getIslands()).thenReturn(im);
// Has team
pm = mock(PlayersManager.class);
when(im.inTeam(Mockito.any(), 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);
// Locales
LocalesManager lm = mock(LocalesManager.class);
when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation");
when(plugin.getLocalesManager()).thenReturn(lm);
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.admin.AdminClearResetsCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteNoTarget() {
AdminClearResetsCommand itl = new AdminClearResetsCommand(ac);
assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>()));
// Show help
Mockito.verify(user).sendMessage(Mockito.eq("commands.help.header"), Mockito.eq("[label]"), Mockito.any());
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.admin.AdminClearResetsCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteUnknownPlayer() {
AdminClearResetsCommand itl = new AdminClearResetsCommand(ac);
String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(null);
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.unknown-player"));
}
/**
* Test method for {@link us.tastybento.bskyblock.commands.admin.AdminClearResetsCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecutePlayerNoIsland() {
AdminClearResetsCommand itl = new AdminClearResetsCommand(ac);
String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(false);
assertFalse(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
Mockito.verify(user).sendMessage(Mockito.eq("general.errors.player-has-no-island"));
}
/**
* Test method for {@link us.AdminClearResetsCommand.tastybento.bskyblock.commands.admin.AdminClearResetCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
*/
@Test
public void testExecuteSuccess() {
String[] name = {"tastybento"};
when(pm.getUUID(Mockito.any())).thenReturn(notUUID);
AdminClearResetsCommand itl = new AdminClearResetsCommand(ac);
assertTrue(itl.execute(user, itl.getLabel(), Arrays.asList(name)));
// Add other verifications
Mockito.verify(user).sendMessage("commands.admin.clearresets.cleared");
Mockito.verify(user).sendMessage(Mockito.eq("general.success"));
}
}