Skip to content

Commit

Permalink
add repeat as:(name)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcmonkey4eva committed Feb 22, 2019
1 parent f0216a1 commit 5e69357
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
Expand Up @@ -3036,7 +3036,7 @@ public void registerCoreMembers() {

// <--[command]
// @Name Repeat
// @Syntax repeat [stop/next/<amount>] [<commands>]
// @Syntax repeat [stop/next/<amount>] [<commands>] (as:<name>)
// @Required 1
// @Short Runs a series of braced commands several times.
// @Group core
Expand All @@ -3046,6 +3046,8 @@ public void registerCoreMembers() {
// Loops through a series of braced commands a specified number of times.
// To get the number of loops so far, you can use <def[value]>.
//
// Optionally, specify "as:<name>" to change the definition name to something other than "value".
//
// To stop a repeat loop, do - repeat stop
//
// To jump immediately to the next number in the loop, do - repeat next
Expand All @@ -3060,7 +3062,7 @@ public void registerCoreMembers() {
// }
// -->
registerCoreMember(RepeatCommand.class,
"REPEAT", "repeat [stop/next/<amount>] [<commands>]", 1);
"REPEAT", "repeat [stop/next/<amount>] [<commands>] (as:<name>)", 1);


// <--[command]
Expand Down
Expand Up @@ -28,39 +28,45 @@ public void onEnable() {
@Override
public void parseArgs(ScriptEntry scriptEntry) throws InvalidArgumentsException {

boolean handled = false;

for (aH.Argument arg : aH.interpret(scriptEntry.getArguments())) {

if (!scriptEntry.hasObject("qty")
if (!handled
&& arg.matchesPrimitive(aH.PrimitiveType.Integer)) {
scriptEntry.addObject("qty", arg.asElement());
break;
scriptEntry.addObject("braces", getBracedCommands(scriptEntry));
handled = true;
}
else if (!scriptEntry.hasObject("stop")
else if (!handled
&& arg.matches("stop")) {
scriptEntry.addObject("stop", Element.TRUE);
break;
scriptEntry.addObject("stop", new Element(true));
handled = true;
}
else if (!scriptEntry.hasObject("next")
else if (!handled
&& arg.matches("next")) {
scriptEntry.addObject("next", Element.TRUE);
break;
scriptEntry.addObject("next", new Element(true));
handled = true;
}
else if (!scriptEntry.hasObject("callback")
else if (!handled
&& arg.matches("\0CALLBACK")) {
scriptEntry.addObject("callback", Element.TRUE);
break;
scriptEntry.addObject("callback", new Element(true));
handled = true;
}
else if (!scriptEntry.hasObject("as_name")
&& arg.matchesOnePrefix("as")) {
scriptEntry.addObject("as_name", arg.asElement());
}
else {
arg.reportUnhandled();
break;
}
}

if (!scriptEntry.hasObject("qty") && !scriptEntry.hasObject("stop") && !scriptEntry.hasObject("next") && !scriptEntry.hasObject("callback")) {
if (!handled) {
throw new InvalidArgumentsException("Must specify a quantity or 'stop' or 'next'!");
}

scriptEntry.addObject("braces", getBracedCommands(scriptEntry));
scriptEntry.defaultObject("as_name", "value");

}

Expand All @@ -72,6 +78,7 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element next = scriptEntry.getElement("next");
Element callback = scriptEntry.getElement("callback");
Element quantity = scriptEntry.getElement("qty");
Element as_name = scriptEntry.getElement("as_name");

if (stop != null && stop.asBoolean()) {
// Report to dB
Expand Down Expand Up @@ -140,7 +147,7 @@ else if (callback != null && callback.asBoolean()) {
data.index++;
if (data.index <= data.target) {
dB.echoDebug(scriptEntry, DebugElement.Header, "Repeat loop " + data.index);
scriptEntry.getResidingQueue().addDefinition("value", String.valueOf(data.index));
scriptEntry.getResidingQueue().addDefinition(as_name.asString(), String.valueOf(data.index));
List<ScriptEntry> bracedCommands = BracedCommand.getBracedCommands(scriptEntry.getOwner()).get(0).value;
ScriptEntry callbackEntry = null;
try {
Expand Down Expand Up @@ -183,7 +190,7 @@ else if (callback != null && callback.asBoolean()) {

// Report to dB
if (scriptEntry.dbCallShouldDebug()) {
dB.report(scriptEntry, getName(), quantity.debug());
dB.report(scriptEntry, getName(), quantity.debug() + as_name.debug());
}

int target = quantity.asInt();
Expand All @@ -197,7 +204,7 @@ else if (callback != null && callback.asBoolean()) {
scriptEntry.setData(datum);
ScriptEntry callbackEntry = null;
try {
callbackEntry = new ScriptEntry("REPEAT", new String[] {"\0CALLBACK"},
callbackEntry = new ScriptEntry("REPEAT", new String[] {"\0CALLBACK", "as:" + as_name.asString()},
(scriptEntry.getScript() != null ? scriptEntry.getScript().getContainer() : null));
callbackEntry.copyFrom(scriptEntry);
}
Expand All @@ -206,7 +213,7 @@ else if (callback != null && callback.asBoolean()) {
}
callbackEntry.setOwner(scriptEntry);
bracedCommandsList.add(callbackEntry);
scriptEntry.getResidingQueue().addDefinition("value", "1");
scriptEntry.getResidingQueue().addDefinition(as_name.asString(), "1");
for (int i = 0; i < bracedCommandsList.size(); i++) {
bracedCommandsList.get(i).setInstant(true);
bracedCommandsList.get(i).addObject("reqid", scriptEntry.getObject("reqid"));
Expand Down

0 comments on commit 5e69357

Please sign in to comment.