Implement Command Cooldowns. Resolves #110

This commit is contained in:
Ali Moghnieh 2016-06-28 03:40:47 +01:00 committed by Trent Hensler
parent 864dadab80
commit 09acbcdb05
30 changed files with 302 additions and 21 deletions

View file

@ -16,10 +16,13 @@ import org.bukkit.inventory.ItemStack;
import java.io.File;
import java.math.BigDecimal;
import java.util.*;
import java.util.Map.Entry;
import java.util.logging.Level;
import java.util.logging.Logger;
import static com.earth2me.essentials.I18n.tl;
import java.util.regex.Pattern;
import java.util.regex.PatternSyntaxException;
public class Settings implements net.ess3.api.ISettings {
@ -532,6 +535,7 @@ public class Settings implements net.ess3.api.ISettings {
customQuitMessage = _getCustomQuitMessage();
isCustomQuitMessage = !customQuitMessage.equals("none");
muteCommands = _getMuteCommands();
commandCooldowns = _getCommandCooldowns();
}
private List<Integer> itemSpawnBl = new ArrayList<Integer>();
@ -1173,4 +1177,89 @@ public class Settings implements net.ess3.api.ISettings {
public boolean isTeleportToCenterLocation() {
return config.getBoolean("teleport-to-center", true);
}
private Map<Pattern, Long> commandCooldowns;
private Map<Pattern, Long> _getCommandCooldowns() {
if (!config.isConfigurationSection("command-cooldowns")) {
return null;
}
ConfigurationSection section = config.getConfigurationSection("command-cooldowns");
Map<Pattern, Long> result = new LinkedHashMap<>();
for (String cmdEntry : section.getKeys(false)) {
Pattern pattern = null;
/* ================================
* >> Regex
* ================================ */
if (cmdEntry.startsWith("^")) {
try {
pattern = Pattern.compile(cmdEntry.substring(1));
} catch (PatternSyntaxException e) {
ess.getLogger().warning("Command cooldown error: " + e.getMessage());
}
} else {
// Escape above Regex
if (cmdEntry.startsWith("\\^")) {
cmdEntry = cmdEntry.substring(1);
}
String cmd = cmdEntry
.replaceAll("\\*", ".*"); // Wildcards are accepted as asterisk * as known universally.
pattern = Pattern.compile(cmd + "( .*)?"); // This matches arguments, if present, to "ignore" them from the feature.
}
/* ================================
* >> Process cooldown value
* ================================ */
Object value = section.get(cmdEntry);
if (!(value instanceof Number) && value instanceof String) {
try {
value = Double.parseDouble(value.toString());
} catch (NumberFormatException ignored) {
}
}
if (!(value instanceof Number)) {
ess.getLogger().warning("Command cooldown error: '" + value + "' is not a valid cooldown");
continue;
}
double cooldown = ((Number) value).doubleValue();
if (cooldown < 1) {
ess.getLogger().warning("Command cooldown with very short " + cooldown + " cooldown.");
}
result.put(pattern, (long) cooldown * 1000); // convert to milliseconds
}
return result;
}
@Override
public boolean isCommandCooldownsEnabled() {
return commandCooldowns != null;
}
@Override
public long getCommandCooldownMs(String label) {
Entry<Pattern, Long> result = getCommandCooldownEntry(label);
return result != null ? result.getValue() : -1; // return cooldown in milliseconds
}
@Override
public Entry<Pattern, Long> getCommandCooldownEntry(String label) {
if (isCommandCooldownsEnabled()) {
for (Entry<Pattern, Long> entry : this.commandCooldowns.entrySet()) {
// Check if label matches current pattern (command-cooldown in config)
if (entry.getKey().matcher(label).matches()) {
return entry;
}
}
}
return null;
}
@Override
public boolean isCommandCooldownPersistent(String label) {
// TODO: enable per command cooldown specification for persistence.
return config.getBoolean("command-cooldown-persistence", true);
}
}