From 46d739e37870a103864922d8d7616146ce850820 Mon Sep 17 00:00:00 2001 From: Jeremy Schroeder Date: Wed, 26 Jun 2013 07:20:02 -0400 Subject: [PATCH] Update CooldownCommand to new 0.9 format. --- .../aufdemrand/denizen/objects/Duration.java | 2 + .../denizen/scripts/ScriptEntry.java | 5 +- .../scripts/commands/core/Comparable.java | 19 +- .../commands/core/CooldownCommand.java | 173 +++++++++--------- .../scripts/commands/core/ResetCommand.java | 5 +- 5 files changed, 102 insertions(+), 102 deletions(-) diff --git a/src/main/java/net/aufdemrand/denizen/objects/Duration.java b/src/main/java/net/aufdemrand/denizen/objects/Duration.java index cabe18b86d..b35ab8e1b9 100644 --- a/src/main/java/net/aufdemrand/denizen/objects/Duration.java +++ b/src/main/java/net/aufdemrand/denizen/objects/Duration.java @@ -14,6 +14,8 @@ public class Duration implements dObject { Pattern.compile("(\\d+.\\d+|.\\d+|\\d+)(t|m|s|h|d|)", Pattern.CASE_INSENSITIVE); + final public static Duration ZERO = new Duration(0); + /** * Gets a Duration Object from a dScript argument. Durations must be a positive * number. Can specify the unit of time by using one of the following: T=ticks, M=minutes, diff --git a/src/main/java/net/aufdemrand/denizen/scripts/ScriptEntry.java b/src/main/java/net/aufdemrand/denizen/scripts/ScriptEntry.java index d3286037d2..5f1f6b7399 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/ScriptEntry.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/ScriptEntry.java @@ -129,7 +129,10 @@ public Object getObject(String key) { } public boolean hasObject(String key) { - return objects.containsKey(key.toUpperCase()); + if (objects.containsKey(key.toUpperCase()) + && objects.get(key.toUpperCase()) != null) + return true; + else return false; } public dScript getScript() { diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/Comparable.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/Comparable.java index f636c8407c..949929db8c 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/Comparable.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/Comparable.java @@ -9,27 +9,16 @@ public class Comparable { public static enum Operator { - EQUALS, - MATCHES, - OR_MORE, - OR_LESS, - MORE, - LESS, - CONTAINS, - IS_EMPTY + EQUALS, MATCHES, OR_MORE, OR_LESS, MORE, + LESS, CONTAINS, IS_EMPTY } public static enum Bridge { - OR, - AND, - FIRST, - THEN, - ELSE + OR, AND, FIRST, THEN, ELSE } public static enum Logic { - REGULAR, - NEGATIVE + REGULAR, NEGATIVE } Logic logic = Logic.REGULAR; diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/CooldownCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/CooldownCommand.java index ebd2ad59ff..95e49b78ca 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/CooldownCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/CooldownCommand.java @@ -2,124 +2,123 @@ import net.aufdemrand.denizen.exceptions.CommandExecutionException; import net.aufdemrand.denizen.exceptions.InvalidArgumentsException; -import net.aufdemrand.denizen.objects.dPlayer; -import net.aufdemrand.denizen.objects.dScript; +import net.aufdemrand.denizen.objects.*; import net.aufdemrand.denizen.scripts.ScriptEntry; import net.aufdemrand.denizen.scripts.commands.AbstractCommand; import net.aufdemrand.denizen.utilities.DenizenAPI; -import net.aufdemrand.denizen.objects.Duration; -import net.aufdemrand.denizen.objects.aH; import net.aufdemrand.denizen.utilities.debugging.dB; import net.aufdemrand.denizen.utilities.debugging.dB.Messages; -import org.bukkit.OfflinePlayer; /** *

Sets a 'cooldown' period on a script. Can be per-player or globally.

* - * dScript Usage:
- *
COOLDOWN ({PLAYER}|GLOBAL) (#{60}|DURATION:#) (SCRIPT:script_name)
- * - *
    Arguments: [] - Required, () - Optional, {} - Default
- * - *
    ({PLAYER}|GLOBAL)
    - * The scope of the cooldown. If not specified, it's assumed that the script - * should be cooled down for the PLAYER. If GLOBAL, all players are affected.
- * - *
    (#{60})
    - * The duration of the cooldown period. Worth noting that if not specified, the - * default value is 60 seconds.
- * - *
    (DURATION:#)
    - * Same as using an integer value for the cooldown period, but accepts the dScript - * time format for minutes, hours, days etc. For example: '60m' = 60 minutes. '1d' = 1 day. - * Worth noting: Durations are in real-time, not minecraft time.
- * - *
    [({PLAYER}|GLOBAL)]
    - * The scope of the cooldown. Specifying PLAYER only affects the player, GLOBAL in turn - * will affect ALL players.
- * - *
Example Usage:
- *
    - * - COOLDOWN 100
    - * - COOLDOWN DURATION:18h GLOBAL
    - * - COOLDOWN 'SCRIPT:A Different Script' 10m - *
- * - * * @author Jeremy Schroeder * */ public class CooldownCommand extends AbstractCommand { - private enum Type {GLOBAL, PLAYER} + + private enum Type { GLOBAL, PLAYER } + + + public String getHelp() { + return "Cools down an interact script. While cool, players cannot " + + "run the script. When on cooldown, the script will not pass " + + "requirements allowing the next lowest priority script to " + + "trigger. You can use to " + + "return whether the script is cooled down, and " + + "to get the duration of the cooldown in progress. Cooldown requires" + + "a type (player or default, a script, and a duration. It also requires" + + "a valid link to a dPlayer.\n" + + " \n" + + "Use to keep a player from activating a script for a specified duration. \n" + + "- cooldown bonus_script 11h \n" + + "- cooldown hit_indicator 5s \n" + + "Use the 'global' argument to indicate the script to be on cooldown for all players. \n" + + "- cooldown global daily_treasure_offering 24h \n"; + } + + + public String getUsage() { + return "- cooldown ({player}|global) (script_name) [duration]"; + } + @Override public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException { - // Define necessary fields - dScript script = scriptEntry.getScript(); - Duration duration = null; - Type type = Type.PLAYER; + for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) { - // Parse Arguments - for (String arg : scriptEntry.getArguments()) { + if (!scriptEntry.hasObject("type") + && arg.matchesEnum(Type.values())) + // add Type + scriptEntry.addObject("type", arg.asElement()); - if (aH.matchesDuration(arg) || aH.matchesInteger(arg)) - duration = aH.getDurationFrom(arg); - // Default is PLAYER, but having both to choose from seems to be most straightforward - else if (aH.matchesArg("GLOBAL, PLAYER", arg)) - type = Type.valueOf(arg.toUpperCase()); + else if (!scriptEntry.hasObject("duration") + && arg.matchesArgumentType(Duration.class)) + // add range (for WALKNEAR) + scriptEntry.addObject("duration", arg.asType(Duration.class)); - // Must be an actual script! If the script doesn't exist, matchesScript(...) - // will echo an error. - else if (aH.matchesScript(arg)) - script = aH.getScriptFrom(arg); - else throw new InvalidArgumentsException(Messages.ERROR_UNKNOWN_ARGUMENT, arg); + else if (!scriptEntry.hasObject("script") + && arg.matchesArgumentType(dScript.class)) + // add anchor ID + scriptEntry.addObject("script", arg.asType(dScript.class)); } // Check to make sure required arguments have been filled - if (type == Type.PLAYER && scriptEntry.getPlayer() == null) - throw new InvalidArgumentsException(Messages.ERROR_NO_PLAYER); - if (script == null) + + if (scriptEntry.hasObject("type") + && ((Element) scriptEntry.getObject("type")).identify().equalsIgnoreCase("player") + && scriptEntry.getPlayer() == null) + throw new InvalidArgumentsException("Requires a type, either "); + + if ((!scriptEntry.hasObject("script")) + || (scriptEntry.hasObject("script") + && !((dScript) scriptEntry.getObject("script")).isValid())) throw new InvalidArgumentsException(Messages.ERROR_NO_SCRIPT); - // Store necessary items in the scriptEntry - scriptEntry.addObject("script", script); - scriptEntry.addObject("duration", duration); - scriptEntry.addObject("type", type); + if ((!scriptEntry.hasObject("duration")) + || (scriptEntry.hasObject("script") + && !((dScript) scriptEntry.getObject("script")).isValid())) + throw new InvalidArgumentsException(Messages.ERROR_NO_SCRIPT); } + @Override public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { // Fetch objects dScript script = (dScript) scriptEntry.getObject("script"); Duration duration = (Duration) scriptEntry.getObject("duration"); - Type type = (Type) scriptEntry.getObject("type"); + Element type = (scriptEntry.hasObject("type") ? + (Element) scriptEntry.getObject("type") : new Element("player")); // Report to dB - dB.report(getName(), - aH.debugObj("Type", type.toString()) - + script.debug() - + (type == Type.PLAYER ? aH.debugObj("Player", scriptEntry.getPlayer().getName()) : "") - + duration.debug()); + dB.report(getName(), type.debug() + + script.debug() + + (type.toString().equalsIgnoreCase("player") ? scriptEntry.getPlayer().debug() : "") + + duration.debug()); // Perform cooldown - if (type == Type.PLAYER) - setCooldown(scriptEntry.getPlayer().getName(), - duration.getSecondsAsInt(), - script.getName(), - false); - - else if (type == Type.GLOBAL) - setCooldown(null, - duration.getSecondsAsInt(), - script.getName(), - true); - + Type type_ = Type.valueOf(type.asString().toUpperCase()); + + switch (type_) { + case PLAYER: + setCooldown(scriptEntry.getPlayer().getName(), + duration, + script.getName(), + false); + + case GLOBAL: + setCooldown(null, + duration, + script.getName(), + true); + } } + /** * Checks if a script is cooled-down for a Player. If a cool-down is currently in progress, * its requirements will fail and it will not trigger. If the script is being cooled down @@ -140,11 +139,11 @@ public static boolean checkCooldown(String playerName, String scriptName) { // Check current entry GLOBALLY, reset it if necessary if (DenizenAPI._saves().contains("Global.Scripts." + scriptName + ".Cooldown Time")) { - if (System.currentTimeMillis() < DenizenAPI._saves().getLong("Global.Scripts." + scriptName + ".Cooldown Time")) + if (System.currentTimeMillis() + < DenizenAPI._saves().getLong("Global.Scripts." + scriptName + ".Cooldown Time")) return false; - else { + else DenizenAPI._saves().set("Global.Scripts." + scriptName + ".Cooldown Time", null); - } } // Now check for player-specific cooldowns @@ -154,7 +153,8 @@ public static boolean checkCooldown(String playerName, String scriptName) { return true; // If there is an entry, check against the time - if (System.currentTimeMillis() >= DenizenAPI._saves().getLong("Players." + playerName + ".Scripts." + scriptName + ".Cooldown Time")) { + if (System.currentTimeMillis() + >= DenizenAPI._saves().getLong("Players." + playerName + ".Scripts." + scriptName + ".Cooldown Time")) { DenizenAPI._saves().set("Players." + playerName + ".Scripts." + scriptName + ".Cooldown Time", null); return true; } @@ -162,6 +162,7 @@ public static boolean checkCooldown(String playerName, String scriptName) { return false; } + /** * Sets a cooldown for a Denizen Script. Can be for a specific Player, or GLOBAL. * @@ -174,18 +175,22 @@ public static boolean checkCooldown(String playerName, String scriptName) { * @param global * whether the script should be cooled down globally */ - public static void setCooldown(String playerName, int duration, String scriptName, boolean global) { + public static void setCooldown(String playerName, Duration duration, String scriptName, boolean global) { // I hate case-sensitivity. The positive here outweighs the negative. if (playerName != null) playerName = playerName.toUpperCase(); scriptName = scriptName.toUpperCase(); // Set global cooldown if (global) { - DenizenAPI._saves().set("Global.Scripts." + scriptName + ".Cooldown Time", System.currentTimeMillis() + (duration * 1000)); + DenizenAPI._saves().set("Global.Scripts." + scriptName + ".Cooldown Time", + System.currentTimeMillis() + + (duration.getSecondsAsInt() * 1000)); // or set Player cooldown } else { - DenizenAPI._saves().set("Players." + playerName + ".Scripts." + scriptName + ".Cooldown Time", System.currentTimeMillis() + (duration * 1000)); + DenizenAPI._saves().set("Players." + playerName + ".Scripts." + scriptName + ".Cooldown Time", + System.currentTimeMillis() + + (duration.getSecondsAsInt() * 1000)); } } diff --git a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/ResetCommand.java b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/ResetCommand.java index c221cdd7bb..7b47d45481 100644 --- a/src/main/java/net/aufdemrand/denizen/scripts/commands/core/ResetCommand.java +++ b/src/main/java/net/aufdemrand/denizen/scripts/commands/core/ResetCommand.java @@ -2,6 +2,7 @@ import net.aufdemrand.denizen.exceptions.CommandExecutionException; import net.aufdemrand.denizen.exceptions.InvalidArgumentsException; +import net.aufdemrand.denizen.objects.Duration; import net.aufdemrand.denizen.objects.dScript; import net.aufdemrand.denizen.scripts.ScriptEntry; import net.aufdemrand.denizen.scripts.commands.AbstractCommand; @@ -64,11 +65,11 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException { return; case PLAYER_COOLDOWN: - CooldownCommand.setCooldown(scriptEntry.getPlayer().getName(), 0, script.getName(), false); + CooldownCommand.setCooldown(scriptEntry.getPlayer().getName(), Duration.ZERO, script.getName(), false); return; case GLOBAL_COOLDOWN: - CooldownCommand.setCooldown(scriptEntry.getPlayer().getName(), 0, script.getName(), true); + CooldownCommand.setCooldown(scriptEntry.getPlayer().getName(), Duration.ZERO, script.getName(), true); return; }