Skip to content

Commit

Permalink
clean up cooldown mess a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Dec 10, 2020
1 parent e2d9402 commit 536bb55
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 64 deletions.
Expand Up @@ -3008,6 +3008,8 @@ public void adjust(Mechanism mechanism) {
// @input None
// @description
// Marks the entity as visible to players by default (if it was hidden).
// See also <@link mechanism EntityTag.hide_from_players>.
// To show to only one player, see <@link mechanism PlayerTag.show_entity>.
// Works with offline players.
// -->
if (mechanism.matches("show_to_players")) {
Expand All @@ -3020,6 +3022,8 @@ public void adjust(Mechanism mechanism) {
// @input None
// @description
// Hides the entity from players by default.
// See also <@link mechanism EntityTag.show_to_players>.
// To hide for only one player, see <@link mechanism PlayerTag.hide_entity>.
// Works with offline players.
// -->
if (mechanism.matches("hide_from_players")) {
Expand Down
Expand Up @@ -2898,6 +2898,8 @@ public void adjust(Mechanism mechanism) {
// @input EntityTag
// @description
// Shows the player a previously hidden entity.
// To show for everyone, use <@link mechanism EntityTag.show_to_players>.
// See also <@link mechanism PlayerTag.hide_entity>.
// -->
if (mechanism.matches("show_entity") && mechanism.requireObject(EntityTag.class)) {
NMSHandler.getEntityHelper().unhideEntity(getPlayerEntity(), mechanism.valueAsType(EntityTag.class).getBukkitEntity());
Expand All @@ -2909,6 +2911,8 @@ public void adjust(Mechanism mechanism) {
// @input EntityTag
// @description
// Hides an entity from the player.
// To hide from everyone, use <@link mechanism EntityTag.hide_from_players>.
// See also <@link mechanism PlayerTag.show_entity>.
// -->
if (mechanism.matches("hide_entity")) {
if (!mechanism.getValue().asString().isEmpty()) {
Expand Down
Expand Up @@ -62,42 +62,24 @@ private enum Type {GLOBAL, PLAYER}

@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

// Defaults are Type.PLAYER and the attached Script
scriptEntry.addObject("type", Type.PLAYER);
scriptEntry.addObject("script", scriptEntry.getScript());

// Parse arguments.. we need a type, duration, and script.

for (Argument arg : scriptEntry.getProcessedArgs()) {

// Type may be PLAYER or GLOBAL.. must not have a prefix.
if (!arg.hasPrefix() && arg.matchesEnum(Type.values())) {
if (arg.matchesPrefix("script", "s")
&& arg.matchesArgumentType(ScriptTag.class)) {
scriptEntry.addObject("script", arg.asType(ScriptTag.class));
}
else if (arg.matchesEnum(Type.values())) {
scriptEntry.addObject("type", Type.valueOf(arg.getValue().toUpperCase()));
}

// DurationTag does not need a prefix, but is required.
else if (!scriptEntry.hasObject("duration")
&& arg.matchesArgumentType(DurationTag.class)) {
scriptEntry.addObject("duration", arg.asType(DurationTag.class));
}

// Require a prefix on the script, since it's optional.
else if (arg.matchesPrefix("script", "s")) {
// Check matchesArgumentType afterwards so we don't default
// to the attached script unintentionally.
if (arg.matchesArgumentType(ScriptTag.class)) {
scriptEntry.addObject("script", arg.asType(ScriptTag.class));
}
else {
throw new InvalidArgumentsException("Specified an invalid script!");
}
}
else {
arg.reportUnhandled();
}
}

scriptEntry.defaultObject("type", Type.PLAYER);
scriptEntry.defaultObject("script", scriptEntry.getScript());
if (!scriptEntry.hasObject("duration")) {
throw new InvalidArgumentsException("Requires a valid duration!");
}
Expand All @@ -107,41 +89,23 @@ else if (arg.matchesPrefix("script", "s")) {
public void execute(ScriptEntry scriptEntry) {
ScriptTag script = scriptEntry.getObjectTag("script");
DurationTag duration = scriptEntry.getObjectTag("duration");
Type type = (scriptEntry.hasObject("type") ?
(Type) scriptEntry.getObject("type") : Type.PLAYER);

Type type = (scriptEntry.hasObject("type") ? (Type) scriptEntry.getObject("type") : Type.PLAYER);
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), ArgumentHelper.debugObj("Type", type.name())
+ script.debug()
+ (type.name().equalsIgnoreCase("player") ? Utilities.getEntryPlayer(scriptEntry).debug() : "")
+ duration.debug());
}

// Perform cooldown
switch (type) {
case PLAYER:
setCooldown(Utilities.getEntryPlayer(scriptEntry),
duration,
script.getName(),
false);
setCooldown(Utilities.getEntryPlayer(scriptEntry), duration, script.getName(), false);
break;

case GLOBAL:
setCooldown(null,
duration,
script.getName(),
true);
setCooldown(null, duration, script.getName(), true);
break;
}
}

/**
* Gets the duration of a script cool-down.
*
* @param player the Player to check, null if only checking Global.
* @param scriptName the name of the script to check
* @return a DurationTag of the time remaining
*/
public static DurationTag getCooldownDuration(PlayerTag player, String scriptName) {
TimeTag expires = Denizen.getInstance().serverFlagMap.getFlagExpirationTime("__interact_cooldown." + scriptName);
if (expires != null) {
Expand All @@ -157,31 +121,14 @@ public static DurationTag getCooldownDuration(PlayerTag player, String scriptNam
return new DurationTag(0);
}

/**
* Checks if a script is cooled-down. If a cool-down is currently in progress,
* its requirements will fail and it will not trigger. If the script is being cooled down
* globally, this will also return false.
*
* @param player the Player to check, null if only checking Global.
* @param scriptName the name of the script to check
* @return true if the script is cool
*/
public static boolean checkCooldown(PlayerTag player, String scriptName) {
DurationTag cooldown = getCooldownDuration(player, scriptName);
if (cooldown.getSeconds() <= 0) {
if (cooldown.getSeconds() > 0) {
return false;
}
return true;
}

/**
* Sets a cooldown for a Denizen Script. Can be for a specific Player, or GLOBAL.
*
* @param player if not a global cooldown, the Player to set the cooldown for
* @param duration the duration of the cooldown period, in seconds
* @param scriptName the name of the script to cooldown
* @param global whether the script should be cooled down globally
*/
public static void setCooldown(PlayerTag player, DurationTag duration, String scriptName, boolean global) {
TimeTag cooldownTime = new TimeTag(TimeTag.now().millis() + duration.getMillis());
if (global) {
Expand Down

0 comments on commit 536bb55

Please sign in to comment.