Skip to content

Commit

Permalink
Added progressive reset messages
Browse files Browse the repository at this point in the history
- Added progressive reset messages sent to users every so often to update them how the reset is doing
- Added more options to the config for how often it is
- Completed tab completion of commands
  • Loading branch information
StrangeOne101 committed Aug 18, 2020
1 parent 6511cfe commit 77df6b5
Show file tree
Hide file tree
Showing 6 changed files with 84 additions and 23 deletions.
61 changes: 51 additions & 10 deletions src/com/strangeone101/platinumarenas/Arena.java
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ public void addKeys(Collection<BlockData> keys) {
//PlatinumArenas.INSTANCE.getLogger().info("Key took " + (System.currentTimeMillis() - time) + "ms");
}

public void reset(int resetSpeed, CommandSender sender, Runnable... callback) {
public void reset(int resetSpeed, CommandSender sender) {
if (getSections().size() == 0) return;

this.beingReset = true;
Expand All @@ -144,20 +144,23 @@ public void reset(int resetSpeed, CommandSender sender, Runnable... callback) {
data.maxBlocksThisTick = resetSpeed;
data.speed = resetSpeed;
data.sender = sender;
data.startTime = System.currentTimeMillis();
data.totalBlocksToReset = getTotalBlocks();
for (Section s : getSections()) {
int sectionAmount = (int)((double)resetSpeed / (double)getTotalBlocks() * (double)s.getTotalBlocks());
if (sectionAmount <= 0) sectionAmount = 1; //Do AT LEAST one block per tick in each section
data.sections.put(s.getID(), sectionAmount); //Store the amount of blocks each section should reset per tick
data.sectionIDs.add(s.getID());
}

loopyReset(data, callback);
loopyReset(data, sender);
}

/**
* Reset with recursion until complete, with each layer adding delay to the last
* @param data The reset data, including sections and the amounts they reset, etc
*/
private void loopyReset(ResetLoopinData data, Runnable... callbacks) {
private void loopyReset(ResetLoopinData data, CommandSender sender) {
if (cancelReset) {
beingReset = false;
cancelReset = false;
Expand All @@ -171,14 +174,15 @@ private void loopyReset(ResetLoopinData data, Runnable... callbacks) {
int sectionsRemovedThisTick = 0;

for (int sectionsIterated = 0; sectionsIterated < data.sections.size(); sectionsIterated++) {
int id = (data.sections.keySet().toArray(new Integer[data.sections.size()])[(sectionsIterated + data.currentSectionResetting) % data.sections.size()]) % getSections().size(); //Get number x in list + offset, and wrap around with %
int id = data.sectionIDs.get((sectionsIterated + data.currentSectionResetting) % data.sections.size()) % getSections().size(); //Get number x in list + offset, and wrap around with %
Section s = getSections().get(id);
long t = System.nanoTime();
boolean reset = s.reset(data.sections.get(id));
data.resetMicroseconds += System.nanoTime() - t;
if (reset) {
t = System.nanoTime();
data.sections.remove(id);
data.sectionIDs.remove((Object)id); //Remove object and not the index
sectionsIterated--;
sectionsRemovedThisTick++;

Expand All @@ -199,6 +203,7 @@ private void loopyReset(ResetLoopinData data, Runnable... callbacks) {
data.calculateMicroseconds += System.nanoTime() - t;
}
data.blocksThisTick += s.getBlocksResetThisTick();
data.totalBlocksReset += s.getBlocksResetThisTick();

//If we have gone over the max, set the section we should start at next tick
if (data.blocksThisTick > data.maxBlocksThisTick) {
Expand All @@ -209,22 +214,44 @@ private void loopyReset(ResetLoopinData data, Runnable... callbacks) {

}

long t = System.nanoTime();

//Update the user on the progress. Only check this every 5 ticks to reduce workload.
if (System.currentTimeMillis() > data.lastUpdate + ConfigManager.RESET_UPDATE_INTERVAL * 1000 && data.tick % 5 == 0) {
double percentageDone = (double)data.totalBlocksReset / (double)data.totalBlocksToReset * 100;

if (percentageDone > data.lastPerUpdate + ConfigManager.RESET_UPDATE_PERCENTAGE) {
data.lastUpdate = System.currentTimeMillis();
data.lastPerUpdate = (float) percentageDone;

updatePlayerReset(sender, this, (float) percentageDone);
}
}

data.tick++;
data.calculateMicroseconds += System.nanoTime() - t;

if (data.sections.size() == 0) {
for (Runnable r : callbacks) r.run();
beingReset = false;

double resetMs = (double)(data.resetMicroseconds / 1000) / 1000;
double calcMs = (double)(data.calculateMicroseconds / 1000) / 1000;
String s = "Reset took " + resetMs + "ms" + "\n" + "Reset calculations took " + calcMs + "ms";
DebugCommand.debugString = s;

if (sender != null && (!(sender instanceof Player) || ((Player)sender).isOnline())) {
long took = System.currentTimeMillis() - data.startTime;
String tookS = took < 1000 ? took + "ms" : (took > 1000 * 120 ? took / 60000 + "m" : took / 1000 + "s");
sender.sendMessage(PlatinumArenas.PREFIX + ChatColor.GREEN + " Arena \"" + getName() + "\" reset complete (took " + tookS + ")!");
}

return;
}

new BukkitRunnable() {
@Override
public void run() {
loopyReset(data, callbacks);
loopyReset(data, sender);
}
}.runTaskLater(PlatinumArenas.INSTANCE, 1L);
}
Expand Down Expand Up @@ -395,6 +422,7 @@ private static class CreationLoopinData {

private static class ResetLoopinData {
Map<Integer, Integer> sections = new HashMap<>();
List<Integer> sectionIDs = new ArrayList<>(); //Having another list instead of making an array from the map each tick is faster
int currentSectionResetting;
int blocksThisTick = 0;
int maxBlocksThisTick;
Expand All @@ -403,6 +431,12 @@ private static class ResetLoopinData {
long overloadWarning;
long calculateMicroseconds;
long resetMicroseconds;
long startTime;
long lastUpdate = System.currentTimeMillis() - (ConfigManager.RESET_UPDATE_INTERVAL / 2 * 1000);
float lastPerUpdate = 0F;
int tick = 0;
int totalBlocksReset = 0;
int totalBlocksToReset;
}

/**
Expand Down Expand Up @@ -471,7 +505,7 @@ private static void loopyCreate(CreationLoopinData data, final int amount, Playe
if (keyList.size() > data.arena.keys.length) data.arena.addKeys(keyList);
data.totalBlocks++;
data.index++;
updatePlayer(player, data);
updatePlayerCreate(player, data);
continue;
}

Expand All @@ -481,7 +515,7 @@ private static void loopyCreate(CreationLoopinData data, final int amount, Playe
if (keyList.size() > data.arena.keys.length) data.arena.addKeys(keyList);
data.index++;
data.totalBlocks++;
updatePlayer(player, data);
updatePlayerCreate(player, data);
continue;
}

Expand All @@ -493,7 +527,7 @@ private static void loopyCreate(CreationLoopinData data, final int amount, Playe
data.index++;
data.totalBlocks++;

updatePlayer(player, data);
updatePlayerCreate(player, data);
}
data.tick++;
if (keyList.size() > data.arena.keys.length) data.arena.addKeys(keyList);
Expand All @@ -507,7 +541,7 @@ public void run() {
}.runTaskLater(PlatinumArenas.INSTANCE, 1L);
}

private static void updatePlayer(Player player, CreationLoopinData data) {
private static void updatePlayerCreate(Player player, CreationLoopinData data) {
if (System.currentTimeMillis() - data.lastUpdate > 10 * 1000) {
double perc = ((double)data.totalBlocks / (double)data.maxBlocks);
NumberFormat format = NumberFormat.getPercentInstance();
Expand All @@ -521,6 +555,13 @@ private static void updatePlayer(Player player, CreationLoopinData data) {
}
}

private static void updatePlayerReset(CommandSender sender, Arena arena, float percentage) {
NumberFormat format = NumberFormat.getPercentInstance();
format.setMinimumFractionDigits(2);
String percS = format.format(percentage / 100);
sender.sendMessage(ChatColor.GREEN + "Arena \"" + arena.getName() + "\" reset " + percS + " complete");
}



public BlockData[] getKeys() {
Expand Down
10 changes: 8 additions & 2 deletions src/com/strangeone101/platinumarenas/ArenaCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

public abstract class ArenaCommand {

Expand Down Expand Up @@ -102,9 +103,14 @@ static TabCompleter getTabCompleter() {
if (args.length > 1) {
if (subcommands.containsKey(args[0].toLowerCase())) {
List<String> listArgs = new ArrayList<>(Arrays.asList(Arrays.copyOfRange(args, 1, args.length)));
if (listArgs.size() > 0 && listArgs.get(0).equalsIgnoreCase("")) listArgs.remove(0);
return subcommands.get(args[0].toLowerCase()).getTabCompletion(sender, listArgs);
//if (listArgs.size() > 0 && listArgs.get(listArgs.size() - 1).equalsIgnoreCase("")) listArgs.remove(listArgs.size() - 1);
return subcommands.get(args[0].toLowerCase()).getTabCompletion(sender, listArgs).stream()
.filter(s -> s.startsWith(listArgs.get(listArgs.size() - 1))).collect(Collectors.toList());
}
} else {
return subcommands.values().stream().filter(
(subcmd) -> !subcmd.isHidden() && sender.hasPermission("platinumarenas." + subcmd.getCommand()))
.map((subcmd) -> subcmd.getCommand()).filter(s -> s.startsWith(args[args.length - 1])).collect(Collectors.toList());
}
return new ArrayList<>();
};
Expand Down
9 changes: 9 additions & 0 deletions src/com/strangeone101/platinumarenas/ConfigManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ public class ConfigManager {
public static int BLOCKS_RESET_PER_SECOND_VERYFAST = 5000 * 20;
public static int BLOCKS_RESET_PER_SECOND_EXTREME = 10000 * 20;

public static int RESET_UPDATE_INTERVAL = 10;
public static float RESET_UPDATE_PERCENTAGE = 15F;

private static YamlConfiguration config;

public static boolean setup() {
Expand All @@ -50,6 +53,12 @@ public static boolean setup() {
BLOCKS_RESET_PER_SECOND_VERYFAST = config.getInt("Speeds.VeryFast", BLOCKS_RESET_PER_SECOND_VERYFAST);
BLOCKS_RESET_PER_SECOND_EXTREME = config.getInt("Speeds.Extreme", BLOCKS_RESET_PER_SECOND_EXTREME);

RESET_UPDATE_INTERVAL = config.getInt("ResetUpdate.Interval", RESET_UPDATE_INTERVAL);
RESET_UPDATE_PERCENTAGE = (float)config.getDouble("ResetUpdate.Percent", RESET_UPDATE_PERCENTAGE);

if (RESET_UPDATE_INTERVAL < 0) RESET_UPDATE_INTERVAL = 1;
if (RESET_UPDATE_PERCENTAGE > 100) RESET_UPDATE_PERCENTAGE = 100F;

return true;
} catch (IOException e) {
PlatinumArenas.INSTANCE.getLogger().severe("Failed to load config.yml!");
Expand Down
16 changes: 6 additions & 10 deletions src/com/strangeone101/platinumarenas/commands/ResetCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -78,14 +78,7 @@ public void run() {
private void resetArena(Arena arena, ResetSpeed speed, CommandSender sender) {
sender.sendMessage(PlatinumArenas.PREFIX + ChatColor.GREEN + " Resetting arena \"" + arena.getName() + "\"!");
long time = System.currentTimeMillis();
arena.reset(speed.getAmount() / 20, sender, () -> {
if (sender != null && (!(sender instanceof Player) || ((Player)sender).isOnline())) {
long took = System.currentTimeMillis() - time;
String tookS = took < 1000 ? took + "ms" : (took > 1000 * 120 ? took / 60000 + "m" : took / 1000 + "s");
sender.sendMessage(PlatinumArenas.PREFIX + ChatColor.GREEN + " Arena \"" + arena.getName() + "\" reset complete (took " + tookS + ")!");
}

});
arena.reset(speed.getAmount() / 20, sender);
}


Expand Down Expand Up @@ -124,11 +117,14 @@ public static ResetSpeed getSpeed(String string) {
@Override
protected List<String> getTabCompletion(CommandSender sender, List<String> args) {
List<String> completions = new ArrayList<>();
if (args.size() == 0) {
if (args.size() <= 1) {
completions.addAll(Arena.arenas.keySet());
completions.sort(Comparator.naturalOrder());
} else if (args.size() == 1) {
} else if (args.size() == 2) {
completions.addAll(Arrays.asList(new String[] {"veryslow", "slow", "normal", "fast", "veryfast", "extreme"}));
if (sender.hasPermission("platinumarenas.reset.instant")) {
completions.add("instant");
}
}

return completions;
Expand Down
10 changes: 9 additions & 1 deletion src/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,12 @@ Speeds:
Normal: 10000
Fast: 40000
VeryFast: 100000
Extreme: 200000
Extreme: 200000

# How often the game will update you on the reset progress of arenas.
# The interval is how long until it can update you again (in seconds),
# while the percentage is how much it must achieve since it last
# notified you before it can do so again.
ResetUpdate:
Interval: 10
Percent: 15
1 change: 1 addition & 0 deletions src/plugin.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ permissions:
platinumarenas.cancel: true
platinumarenas.border: true
platinumarenas.remove: true
platinumarenas.debug: true
description: Use the platinumarenas command
platinumarenas.reset.instant:
description: Reset arenas instantly
Expand Down

0 comments on commit 77df6b5

Please sign in to comment.