Skip to content

Commit

Permalink
add a rate option to the WaitUntil command
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Aug 27, 2019
1 parent 57022c7 commit 5eceac5
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ public void registerCoreCommands() {
registerCoreMember(StopCommand.class, "STOP", "stop", 0);
registerCoreMember(SyncCommand.class, "SYNC", "sync [<commands>]", 0);
registerCoreMember(WaitCommand.class, "WAIT", "wait (<duration>) (queue:<name>)", 0);
registerCoreMember(WaitUntilCommand.class, "WAITUNTIL", "waituntil [<comparisons>]", 0);
registerCoreMember(WaitUntilCommand.class, "WAITUNTIL", "waituntil (rate:<duration>) [<comparisons>]", 1);
registerCoreMember(WebGetCommand.class, "WEBGET", "webget [<url>] (post:<data>) (headers:<header>/<value>|...) (timeout:<duration>/{10s}) (savefile:<path>)", 1);
registerCoreMember(WhileCommand.class, "WHILE", "while [stop/next/<comparison tag>] [<commands>]", 1);
registerCoreMember(YamlCommand.class, "YAML", "yaml [create]/[load:<file> (fix_formatting)]/[loadtext:<text> (fix_formatting)]/[unload]/[savefile:<file>]/[copykey:<source key> <target key> (to_id:<name>)]/[set <key>([<#>])(:<action>):<value>] [id:<name>]", 2);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package com.denizenscript.denizencore.scripts.commands.queue;

import com.denizenscript.denizencore.exceptions.InvalidArgumentsException;
import com.denizenscript.denizencore.objects.Argument;
import com.denizenscript.denizencore.objects.core.DurationTag;
import com.denizenscript.denizencore.utilities.debugging.Debug;
import com.denizenscript.denizencore.utilities.scheduling.RepeatingSchedulable;
import com.denizenscript.denizencore.DenizenCore;
Expand All @@ -17,14 +19,18 @@ public class WaitUntilCommand extends AbstractCommand implements Holdable {

// <--[command]
// @Name WaitUntil
// @Syntax waituntil [<comparisons>]
// @Required 0
// @Syntax waituntil (rate:<duration>) [<comparisons>]
// @Required 1
// @Short Delays a script until the If comparisons return true.
// @Group queue
//
// @Description
// Delays a script until the If comparisons return true.
// Will be checked as often as the queue updates (based on queue speed).
//
// Optionally, specify an update rate (if unset, will update at queue speed).
// The update rate controls how often the tag will be checked. This generally doesn't need to be set,
// unless you're concerned about script efficiency.
// Never set this to faster than queue update rate.
//
// @Tags
// <QueueTag.speed>
Expand All @@ -44,29 +50,50 @@ public void onEnable() {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

scriptEntry.addObject("comparisons", scriptEntry.getArguments());
List<String> arguments = scriptEntry.getArguments();

for (Argument arg : scriptEntry.getProcessedArgs()) {
if (arg.matchesPrefix("rate")) {
scriptEntry.addObject("rate", arg.asType(DurationTag.class));
arguments = new ArrayList<>(arguments);
arguments.remove(0);
}
break;
}

scriptEntry.addObject("comparisons", arguments);
}


@Override
public void execute(ScriptEntry scriptEntry) {

List<String> comparisons = (List<String>) scriptEntry.getObject("comparisons");
DurationTag rate = scriptEntry.getObjectTag("rate");

boolean run = new IfCommand.ArgComparer().compare(new ArrayList<>(comparisons), scriptEntry);

// Report to dB
if (scriptEntry.dbCallShouldDebug()) {
Debug.report(scriptEntry, getName(), ArgumentHelper.debugObj("run_first_check", run));
Debug.report(scriptEntry, getName(), ArgumentHelper.debugObj("run_first_check", run)
+ (rate == null ? "" : rate.debug()));
}

if (run) {
scriptEntry.setFinished(true);
return;
}

final RepeatingSchedulable schedulable = new RepeatingSchedulable(null, scriptEntry.getResidingQueue() instanceof TimedQueue ?
(float)((TimedQueue) scriptEntry.getResidingQueue()).getSpeed().getSeconds(): 0.05f);
if (rate == null) {
if (scriptEntry.getResidingQueue() instanceof TimedQueue) {
rate = ((TimedQueue) scriptEntry.getResidingQueue()).getSpeed();
}
else {
rate = new DurationTag((long) 1);
}
}

final RepeatingSchedulable schedulable = new RepeatingSchedulable(null, (float) rate.getSeconds());
schedulable.run = new Runnable() {
public int counter = 0;
@Override
Expand Down

0 comments on commit 5eceac5

Please sign in to comment.