Skip to content
Permalink
Browse files

add foreach as:(name)

  • Loading branch information...
mcmonkey4eva committed Feb 22, 2019
1 parent 43c3825 commit 58409c5bcf8c403871621e019fe4b7b12374b92d
@@ -343,7 +343,7 @@ public void registerCoreCommands() {

// <--[command]
// @Name Foreach
// @Syntax foreach [stop/next/<object>|...] [<commands>]
// @Syntax foreach [stop/next/<object>|...] [<commands>] (as:<name>)
// @Required 1
// @Short Loops through a dList, running a set of commands for each item.
// @Group core
@@ -353,6 +353,8 @@ public void registerCoreCommands() {
// Loops through a dList of any type. For each item in the dList, the specified commands will be ran for
// that list entry. To call the value of the entry while in the loop, you can use <def[value]>.
//
// Optionally, specify "as:<name>" to change the definition name to something other than "value".
//
// To end a foreach loop, do - foreach stop
//
// To jump immediately to the next entry in the loop, do - foreach next
@@ -374,7 +376,7 @@ public void registerCoreCommands() {

// -->
registerCoreMember(ForeachCommand.class,
"foreach", "foreach [stop/next/<object>|...] [<commands>]", 1);
"foreach", "foreach [stop/next/<object>|...] [<commands>] (as:<name>)", 1);


// <--[command]
@@ -28,45 +28,47 @@ 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("stop")
&& arg.matchesOne("stop")) {
scriptEntry.addObject("stop", Element.TRUE);
break;
if (!handled
&& arg.matches("stop")) {
scriptEntry.addObject("stop", new Element(true));
handled = true;
}

else if (!scriptEntry.hasObject("next")
&& arg.matchesOne("next")) {
scriptEntry.addObject("next", Element.TRUE);
break;
else if (!handled
&& arg.matches("next")) {
scriptEntry.addObject("next", new Element(true));
handled = true;
}

else if (!scriptEntry.hasObject("callback")
&& arg.matchesOne("\0CALLBACK")) {
scriptEntry.addObject("callback", Element.TRUE);
break;
else if (!handled
&& arg.matches("\0CALLBACK")) {
scriptEntry.addObject("callback", new Element(true));
handled = true;
}
else if (!scriptEntry.hasObject("as_name")
&& arg.matchesOnePrefix("as")) {
scriptEntry.addObject("as_name", arg.asElement());
}

else if (!scriptEntry.hasObject("list")) {
else if (!handled) {
scriptEntry.addObject("list", dList.valueOf(arg.raw_value));
scriptEntry.addObject("braces", getBracedCommands(scriptEntry));
break;
handled = true;
}

else {
arg.reportUnhandled();
break;
}

}

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

scriptEntry.defaultObject("as_name", "value");
}

@SuppressWarnings("unchecked")
@@ -77,6 +79,7 @@ public void execute(ScriptEntry scriptEntry) throws CommandExecutionException {
Element next = scriptEntry.getElement("next");
Element callback = scriptEntry.getElement("callback");
dList list = (dList) scriptEntry.getObject("list");
Element as_name = scriptEntry.getElement("as_name");

if (stop != null && stop.asBoolean()) {
// Report to dB
@@ -146,7 +149,7 @@ else if (callback != null && callback.asBoolean()) {
if (data.index <= data.list.size()) {
dB.echoDebug(scriptEntry, DebugElement.Header, "Foreach loop " + data.index);
scriptEntry.getResidingQueue().addDefinition("loop_index", String.valueOf(data.index));
scriptEntry.getResidingQueue().addDefinition("value", String.valueOf(data.list.get(data.index - 1)));
scriptEntry.getResidingQueue().addDefinition(as_name.asString(), String.valueOf(data.list.get(data.index - 1)));
List<ScriptEntry> bracedCommands = BracedCommand.getBracedCommands(scriptEntry.getOwner()).get(0).value;
ScriptEntry callbackEntry = null;
try {
@@ -192,7 +195,7 @@ else if (callback != null && callback.asBoolean()) {

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

int target = list.size();
@@ -215,7 +218,7 @@ else if (callback != null && callback.asBoolean()) {
}
callbackEntry.setOwner(scriptEntry);
bracedCommandsList.add(callbackEntry);
scriptEntry.getResidingQueue().addDefinition("value", list.get(0));
scriptEntry.getResidingQueue().addDefinition(as_name.asString(), list.get(0));
scriptEntry.getResidingQueue().addDefinition("loop_index", "1");
for (int i = 0; i < bracedCommandsList.size(); i++) {
bracedCommandsList.get(i).setInstant(true);

0 comments on commit 58409c5

Please sign in to comment.
You can’t perform that action at this time.