Skip to content

Commit 9c41ceb

Browse files
committed
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).
1 parent 541ee35 commit 9c41ceb

File tree

7 files changed

+356
-64
lines changed

7 files changed

+356
-64
lines changed

locales/en-US.yml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,12 @@ commands:
5252
help:
5353
parameters: ""
5454
description: "admin command"
55+
clearresets:
56+
parameters: "<player>"
57+
description: "clears player reset count for this world"
58+
cleared: "&2Resets cleared"
59+
clearresetsall:
60+
description: "clears all player reset counts for this world"
5561
team:
5662
add:
5763
parameters: "<leader> <player>"
@@ -115,7 +121,7 @@ commands:
115121
owner: "Owner: [owner] ([uuid])"
116122
last-login: "Last login: [date]"
117123
deaths: "Deaths: [number]"
118-
resets-left: "Resets left: [number]/[total]"
124+
resets-left: "Resets: [number] (Max: [total])"
119125
team-members-title: "Team members:"
120126
team-owner-format: "&a[name] [rank]"
121127
team-member-format: "&b[name] [rank]"

src/main/java/us/tastybento/bskyblock/commands/AdminCommand.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
import us.tastybento.bskyblock.api.commands.CompositeCommand;
66
import us.tastybento.bskyblock.api.localization.TextVariables;
77
import us.tastybento.bskyblock.api.user.User;
8-
import us.tastybento.bskyblock.commands.admin.AdminClearResetAllCommand;
9-
import us.tastybento.bskyblock.commands.admin.AdminClearResetCommand;
8+
import us.tastybento.bskyblock.commands.admin.AdminClearResetsAllCommand;
9+
import us.tastybento.bskyblock.commands.admin.AdminClearResetsCommand;
1010
import us.tastybento.bskyblock.commands.admin.AdminGetRankCommand;
1111
import us.tastybento.bskyblock.commands.admin.AdminInfoCommand;
1212
import us.tastybento.bskyblock.commands.admin.AdminRegisterCommand;
@@ -57,8 +57,8 @@ public void setup() {
5757
// Range
5858
new AdminRangeCommand(this);
5959
// Resets
60-
new AdminClearResetCommand(this);
61-
new AdminClearResetAllCommand(this);
60+
new AdminClearResetsCommand(this);
61+
new AdminClearResetsAllCommand(this);
6262
}
6363

6464
@Override

src/main/java/us/tastybento/bskyblock/commands/admin/AdminClearResetAllCommand.java

Lines changed: 0 additions & 52 deletions
This file was deleted.
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package us.tastybento.bskyblock.commands.admin;
2+
3+
import java.util.List;
4+
5+
import org.bukkit.Bukkit;
6+
import org.bukkit.entity.Player;
7+
8+
import us.tastybento.bskyblock.api.commands.CompositeCommand;
9+
import us.tastybento.bskyblock.api.user.User;
10+
11+
public class AdminClearResetsAllCommand extends CompositeCommand {
12+
13+
public AdminClearResetsAllCommand(CompositeCommand parent) {
14+
super(parent, "clearresetsall");
15+
}
16+
17+
@Override
18+
public void setup() {
19+
setPermission("admin.clearresetsall");
20+
setDescription("commands.admin.clearresetsall.description");
21+
}
22+
23+
@Override
24+
public boolean execute(User user, String label, List<String> args) {
25+
// If args are not right, show help
26+
if (!args.isEmpty()) {
27+
showHelp(this, user);
28+
return false;
29+
}
30+
this.askConfirmation(user, () -> {
31+
// Set the reset epoch to now
32+
getIWM().setResetEpoch(getWorld());
33+
// Reset all current players
34+
Bukkit.getOnlinePlayers().stream().map(Player::getUniqueId).filter(getPlayers()::isKnown).forEach(u -> getPlayers().setResets(getWorld(), u, 0));
35+
user.sendMessage("general.success");
36+
});
37+
return false;
38+
}
39+
40+
}

src/main/java/us/tastybento/bskyblock/commands/admin/AdminClearResetCommand.java renamed to src/main/java/us/tastybento/bskyblock/commands/admin/AdminClearResetsCommand.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@
99
import us.tastybento.bskyblock.api.user.User;
1010
import us.tastybento.bskyblock.util.Util;
1111

12-
public class AdminClearResetCommand extends CompositeCommand {
12+
public class AdminClearResetsCommand extends CompositeCommand {
1313

14-
public AdminClearResetCommand(CompositeCommand parent) {
15-
super(parent, "clearreset");
14+
public AdminClearResetsCommand(CompositeCommand parent) {
15+
super(parent, "clearresets");
1616
}
1717

1818
@Override
1919
public void setup() {
2020
setPermission("admin.clearreset");
21-
setParameters("commands.admin.clearreset.parameters");
22-
setDescription("commands.admin.clearreset.description");
21+
setParameters("commands.admin.clearresets.parameters");
22+
setDescription("commands.admin.clearresets.description");
2323
}
2424

2525
@Override
@@ -40,8 +40,8 @@ public boolean execute(User user, String label, List<String> args) {
4040
return false;
4141
}
4242
// Clear resets
43-
user.sendMessage("commands.admin.clearreset.cleared");
44-
getPlayers().setResets(getWorld(), user.getUniqueId(), 0);
43+
user.sendMessage("commands.admin.clearresets.cleared");
44+
getPlayers().setResets(getWorld(), targetUUID, 0);
4545
user.sendMessage("general.success");
4646
return true;
4747
}
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
package us.tastybento.bskyblock.commands.admin;
2+
3+
import static org.junit.Assert.assertFalse;
4+
import static org.mockito.Mockito.mock;
5+
import static org.mockito.Mockito.when;
6+
7+
import java.util.ArrayList;
8+
import java.util.HashMap;
9+
import java.util.UUID;
10+
11+
import org.bukkit.Bukkit;
12+
import org.bukkit.World;
13+
import org.bukkit.entity.Player;
14+
import org.bukkit.scheduler.BukkitScheduler;
15+
import org.junit.Before;
16+
import org.junit.Test;
17+
import org.junit.runner.RunWith;
18+
import org.mockito.Mockito;
19+
import org.powermock.api.mockito.PowerMockito;
20+
import org.powermock.core.classloader.annotations.PrepareForTest;
21+
import org.powermock.modules.junit4.PowerMockRunner;
22+
import org.powermock.reflect.Whitebox;
23+
24+
import us.tastybento.bskyblock.BSkyBlock;
25+
import us.tastybento.bskyblock.Settings;
26+
import us.tastybento.bskyblock.api.user.User;
27+
import us.tastybento.bskyblock.commands.AdminCommand;
28+
import us.tastybento.bskyblock.managers.CommandsManager;
29+
import us.tastybento.bskyblock.managers.IslandWorldManager;
30+
import us.tastybento.bskyblock.managers.IslandsManager;
31+
import us.tastybento.bskyblock.managers.LocalesManager;
32+
import us.tastybento.bskyblock.managers.PlayersManager;
33+
34+
@RunWith(PowerMockRunner.class)
35+
@PrepareForTest({Bukkit.class, BSkyBlock.class, User.class })
36+
public class AdminClearResetsAllCommandTest {
37+
38+
private AdminCommand ac;
39+
private User user;
40+
private IslandsManager im;
41+
private PlayersManager pm;
42+
private UUID notUUID;
43+
44+
/**
45+
* @throws java.lang.Exception
46+
*/
47+
@Before
48+
public void setUp() throws Exception {
49+
// Set up plugin
50+
BSkyBlock plugin = mock(BSkyBlock.class);
51+
Whitebox.setInternalState(BSkyBlock.class, "instance", plugin);
52+
53+
// Command manager
54+
CommandsManager cm = mock(CommandsManager.class);
55+
when(plugin.getCommandsManager()).thenReturn(cm);
56+
57+
// Settings
58+
Settings s = mock(Settings.class);
59+
when(s.getResetWait()).thenReturn(0L);
60+
when(s.getResetLimit()).thenReturn(3);
61+
when(plugin.getSettings()).thenReturn(s);
62+
63+
// Player
64+
Player p = mock(Player.class);
65+
// Sometimes use Mockito.withSettings().verboseLogging()
66+
user = mock(User.class);
67+
when(user.isOp()).thenReturn(false);
68+
UUID uuid = UUID.randomUUID();
69+
notUUID = UUID.randomUUID();
70+
while(notUUID.equals(uuid)) {
71+
notUUID = UUID.randomUUID();
72+
}
73+
when(user.getUniqueId()).thenReturn(uuid);
74+
when(user.getPlayer()).thenReturn(p);
75+
when(user.getName()).thenReturn("tastybento");
76+
User.setPlugin(plugin);
77+
78+
// Parent command has no aliases
79+
ac = mock(AdminCommand.class);
80+
when(ac.getSubCommandAliases()).thenReturn(new HashMap<>());
81+
82+
// Island World Manager
83+
IslandWorldManager iwm = mock(IslandWorldManager.class);
84+
World world = mock(World.class);
85+
when(iwm.getBSBIslandWorld()).thenReturn(world);
86+
when(plugin.getIWM()).thenReturn(iwm);
87+
88+
89+
// Player has island to begin with
90+
im = mock(IslandsManager.class);
91+
when(im.hasIsland(Mockito.any(), Mockito.any(UUID.class))).thenReturn(true);
92+
when(im.hasIsland(Mockito.any(), Mockito.any(User.class))).thenReturn(true);
93+
when(im.isOwner(Mockito.any(),Mockito.any())).thenReturn(true);
94+
when(im.getTeamLeader(Mockito.any(),Mockito.any())).thenReturn(uuid);
95+
when(plugin.getIslands()).thenReturn(im);
96+
97+
// Has team
98+
pm = mock(PlayersManager.class);
99+
when(im.inTeam(Mockito.any(), Mockito.eq(uuid))).thenReturn(true);
100+
101+
when(plugin.getPlayers()).thenReturn(pm);
102+
103+
// Server & Scheduler
104+
BukkitScheduler sch = mock(BukkitScheduler.class);
105+
PowerMockito.mockStatic(Bukkit.class);
106+
when(Bukkit.getScheduler()).thenReturn(sch);
107+
108+
// Locales
109+
LocalesManager lm = mock(LocalesManager.class);
110+
when(lm.get(Mockito.any(), Mockito.any())).thenReturn("mock translation");
111+
when(plugin.getLocalesManager()).thenReturn(lm);
112+
}
113+
114+
/**
115+
* Test method for {@link us.tastybento.bskyblock.commands.admin.AdminClearResetsAllCommand#execute(us.tastybento.bskyblock.api.user.User, java.util.List)}.
116+
*/
117+
@Test
118+
public void testExecuteCheckConfirm() {
119+
AdminClearResetsAllCommand itl = new AdminClearResetsAllCommand(ac);
120+
assertFalse(itl.execute(user, itl.getLabel(), new ArrayList<>()));
121+
Mockito.verify(user).sendMessage(Mockito.eq("general.confirm"), Mockito.eq("[seconds]"), Mockito.any());
122+
}
123+
124+
}

0 commit comments

Comments
 (0)