Skip to content

Commit

Permalink
WIP - clear reset all command
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Jul 25, 2018
1 parent 342387c commit 541ee35
Show file tree
Hide file tree
Showing 14 changed files with 303 additions and 84 deletions.
19 changes: 19 additions & 0 deletions src/main/java/us/tastybento/bskyblock/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -438,6 +438,10 @@ public class Settings implements DataObject, WorldSettings {
private boolean closePanelOnClickOutside = true;

//---------------------------------------------------------------------------------------/
@ConfigComment("These settings should not be edited")
@ConfigEntry(path = "do-not-edit-these-settings.reset-epoch")
private long resetEpoch = 0;

private String uniqueId = "config";

// Getters and setters
Expand Down Expand Up @@ -680,6 +684,7 @@ public int getPurgeMaxIslandLevel() {
/**
* @return the resetLimit
*/
@Override
public int getResetLimit() {
return resetLimit;
}
Expand Down Expand Up @@ -1479,6 +1484,20 @@ public List<String> getGeoLimitSettings() {
public void setGeoLimitSettings(List<String> geoLimitSettings) {
this.geoLimitSettings = geoLimitSettings;
}
/**
* @return the resetEpoch
*/
@Override
public long getResetEpoch() {
return resetEpoch;
}
/**
* @param resetEpoch the resetEpoch to set
*/
@Override
public void setResetEpoch(long resetEpoch) {
this.resetEpoch = resetEpoch;
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -235,4 +235,20 @@ public interface WorldSettings {
* @return list of entity types that should not exit the island limits
*/
List<String> getGeoLimitSettings();

/**
* @return reset limit for world
*/
int getResetLimit();


/**
* Get the island reset time stamp. Any player who last logged in before this time will have resets zeroed
*/
long getResetEpoch();

/**
* Set the island reset time stamp. Any player who last logged in before this time will have resets zeroed
*/
void setResetEpoch(long timestamp);
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +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.AdminGetRankCommand;
import us.tastybento.bskyblock.commands.admin.AdminInfoCommand;
import us.tastybento.bskyblock.commands.admin.AdminRegisterCommand;
Expand Down Expand Up @@ -54,6 +56,9 @@ public void setup() {
new AdminUnregisterCommand(this);
// Range
new AdminRangeCommand(this);
// Resets
new AdminClearResetCommand(this);
new AdminClearResetAllCommand(this);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
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));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package us.tastybento.bskyblock.commands.admin;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

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 AdminClearResetCommand(CompositeCommand parent) {
super(parent, "clearreset");
}

@Override
public void setup() {
setPermission("admin.clearreset");
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.size() != 1) {
showHelp(this, user);
return false;
}
// Get target
UUID targetUUID = getPlayers().getUUID(args.get(0));
if (targetUUID == null) {
user.sendMessage("general.errors.unknown-player");
return false;
}
if (!getIslands().hasIsland(getWorld(), targetUUID)) {
user.sendMessage("general.errors.player-has-no-island");
return false;
}
// Clear resets
user.sendMessage("commands.admin.clearreset.cleared");
getPlayers().setResets(getWorld(), user.getUniqueId(), 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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,14 @@ public boolean execute(User user, String label, List<String> args) {
user.sendMessage("commands.island.reset.must-remove-members");
return false;
}
if (getSettings().getResetLimit() >= 0 ) {
if (getPlayers().getResetsLeft(user.getUniqueId()) == 0) {
if (getIWM().getResetLimit(getWorld()) >= 0 ) {
int resetsLeft = getIWM().getResetLimit(getWorld()) - getPlayers().getResets(getWorld(), user.getUniqueId());
if (resetsLeft <= 0) {
user.sendMessage("commands.island.reset.none-left");
return false;
} else {
// Notify how many resets are left
user.sendMessage("commands.island.reset.resets-left", TextVariables.NUMBER, String.valueOf(getPlayers().getResetsLeft(user.getUniqueId())));
user.sendMessage("commands.island.reset.resets-left", TextVariables.NUMBER, String.valueOf(resetsLeft));
}
}
// Request confirmation
Expand Down Expand Up @@ -88,6 +89,8 @@ private boolean resetIsland(User user) {
if (getSettings().isUseEconomy() && getIWM().isOnLeaveResetMoney(getWorld())) {
// TODO: needs Vault
}
// Add a reset
getPlayers().addReset(getWorld(), user.getUniqueId());
// Create new island and then delete the old one
try {
NewIsland.builder()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,7 +600,7 @@ public boolean showInfo(BSkyBlock plugin, User user, World world) {
user.sendMessage("commands.admin.info.last-login","[date]", d.toString());

user.sendMessage("commands.admin.info.deaths", "[number]", String.valueOf(plugin.getPlayers().getDeaths(owner)));
String resets = String.valueOf(plugin.getPlayers().getResetsLeft(owner));
String resets = String.valueOf(plugin.getPlayers().getResets(world, owner));
String total = plugin.getSettings().getResetLimit() < 0 ? "Unlimited" : String.valueOf(plugin.getSettings().getResetLimit());
user.sendMessage("commands.admin.info.resets-left", "[number]", resets, "[total]", total);
// Show team members
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public class Players implements DataObject {
@Expose
private String playerName;
@Expose
private int resetsLeft;
private Map<String, Integer> resets = new HashMap<>();
@Expose
private String locale = "";
@Expose
Expand All @@ -52,7 +52,6 @@ public Players() {}
public Players(BSkyBlock plugin, UUID uniqueId) {
this.uniqueId = uniqueId.toString();
homeLocations = new HashMap<>();
resetsLeft = plugin.getSettings().getResetLimit();
locale = "";
kickedList = new HashMap<>();
// Try to get player's name
Expand Down Expand Up @@ -142,18 +141,35 @@ public String getPlayerName() {
}

/**
* Get number of resets done in this world
* @param world - world
* @return the resetsLeft
*/
public int getResetsLeft() {
return resetsLeft;
public int getResets(World world) {
resets.putIfAbsent(world.getName(), 0);
return resets.get(world.getName());
}

/**
* @return the resets
*/
public Map<String, Integer> getResets() {
return resets;
}

/**
* @param resets the resets to set
*/
public void setResets(Map<String, Integer> resets) {
this.resets = resets;
}

/**
* @param resetsLeft
* the resetsLeft to set
* @param resets
* the resets to set
*/
public void setResetsLeft(int resetsLeft) {
this.resetsLeft = resetsLeft;
public void setResets(World world, int resets) {
this.resets.put(world.getName(), resets);
}

/**
Expand Down Expand Up @@ -281,4 +297,12 @@ public void setUniqueId(String uniqueId) {
this.uniqueId = uniqueId;
}

/**
* Increments the reset counter for player in world
* @param world - world
*/
public void addReset(World world) {
resets.merge(world.getName(), 1, Integer::sum);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -39,11 +39,10 @@ public void onPlayerJoin(final PlayerJoinEvent event) {
if (plugin.getPlayers().isKnown(playerUUID)) {
// Load player
players.addPlayer(playerUUID);

// Reset resets if the admin changes it to or from unlimited
if (plugin.getSettings().getResetLimit() < players.getResetsLeft(playerUUID) || (plugin.getSettings().getResetLimit() >= 0 && players.getResetsLeft(playerUUID) < 0)) {
players.setResetsLeft(playerUUID, plugin.getSettings().getResetLimit());
}
// Reset island resets if required
plugin.getIWM().getOverWorlds().stream()
.filter(w -> event.getPlayer().getLastPlayed() < plugin.getIWM().getResetEpoch(w))
.forEach(w -> players.setResets(w, playerUUID, 0));
// Set the player's name (it may have changed), but only if it isn't empty
if (!user.getName().isEmpty()) {
players.setPlayerName(user);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -734,4 +734,30 @@ public boolean isWaterNotSafe(World world) {
public List<String> getGeoLimitSettings(World world) {
return worldSettings.get(Util.getWorld(world)).getGeoLimitSettings();
}

/**
* Get the reset limit for this world
* @param world - world
* @return number of resets allowed. -1 = unlimited
*/
public int getResetLimit(World world) {
return worlds.containsKey(Util.getWorld(world)) ? worldSettings.get(Util.getWorld(world)).getResetLimit() : -1;
}


/**
* Gets the time stamp for when all player resets were zeroed
* @param world
*/
public long getResetEpoch(World world) {
return worldSettings.get(Util.getWorld(world)).getResetEpoch();
}

/**
* Sets the time stamp for when all player resets were zeroed
* @param world
*/
public void setResetEpoch(World world) {
worldSettings.get(Util.getWorld(world)).setResetEpoch(System.currentTimeMillis());
}
}
Loading

0 comments on commit 541ee35

Please sign in to comment.