Skip to content

Commit

Permalink
Added clear resets and clear reset all admin commands.
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
tastybento committed Jul 25, 2018
1 parent 541ee35 commit 9c41ceb
Show file tree
Hide file tree
Showing 7 changed files with 356 additions and 64 deletions.
8 changes: 7 additions & 1 deletion locales/en-US.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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>"
Expand Down Expand Up @@ -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]"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@
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;
Expand Down Expand Up @@ -57,8 +57,8 @@ public void setup() {
// Range
new AdminRangeCommand(this);
// Resets
new AdminClearResetCommand(this);
new AdminClearResetAllCommand(this);
new AdminClearResetsCommand(this);
new AdminClearResetsAllCommand(this);
}

@Override
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -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;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,17 @@
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
Expand All @@ -40,8 +40,8 @@ public boolean execute(User user, String label, List<String> args) {
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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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());
}

}
Loading

0 comments on commit 9c41ceb

Please sign in to comment.