-
-
Notifications
You must be signed in to change notification settings - Fork 368
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
For each index & value loop. #6562
Open
Moderocky
wants to merge
4
commits into
SkriptLang:dev/feature
Choose a base branch
from
Moderocky:for-loop
base: dev/feature
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,154 @@ | ||||||
package ch.njol.skript.sections; | ||||||
|
||||||
import ch.njol.skript.Skript; | ||||||
import ch.njol.skript.SkriptAPIException; | ||||||
import ch.njol.skript.classes.Changer; | ||||||
import ch.njol.skript.config.SectionNode; | ||||||
import ch.njol.skript.doc.Description; | ||||||
import ch.njol.skript.doc.Examples; | ||||||
import ch.njol.skript.doc.Name; | ||||||
import ch.njol.skript.doc.Since; | ||||||
import ch.njol.skript.lang.Expression; | ||||||
import ch.njol.skript.lang.SkriptParser.ParseResult; | ||||||
import ch.njol.skript.lang.TriggerItem; | ||||||
import ch.njol.skript.lang.Variable; | ||||||
import ch.njol.skript.lang.util.ContainerExpression; | ||||||
import ch.njol.skript.registrations.Feature; | ||||||
import ch.njol.skript.util.Container; | ||||||
import ch.njol.skript.util.Container.ContainerType; | ||||||
import ch.njol.skript.util.LiteralUtils; | ||||||
import ch.njol.util.Kleenean; | ||||||
import org.bukkit.event.Event; | ||||||
import org.jetbrains.annotations.Nullable; | ||||||
|
||||||
import java.util.List; | ||||||
import java.util.Map; | ||||||
|
||||||
@Name("For Each Loop (Experimental)") | ||||||
@Description(""" | ||||||
A specialised loop section run for each element in a list. | ||||||
Unlike the basic loop, this is designed for extracting the key & value from pairs. | ||||||
The loop element's key/index and value can be stored in a variable for convenience. | ||||||
|
||||||
When looping a simple (non-indexed) set of values, e.g. all players, the index will be the loop counter number.""" | ||||||
) | ||||||
@Examples({ | ||||||
"for each {_player} in players:", | ||||||
"\tsend \"Hello %{_player}%!\" to {_player}", | ||||||
"", | ||||||
"loop {_item} in {list of items::*}:", | ||||||
"\tbroadcast {_item}'s name", | ||||||
"", | ||||||
"for each key {_index} in {list of items::*}:", | ||||||
"\tbroadcast {_index}", | ||||||
"", | ||||||
"loop key {_index} and value {_value} in {list of items::*}:", | ||||||
"\tbroadcast \"%{_index}% = %{_value}%\"", | ||||||
"", | ||||||
"for each {_index} = {_value} in {my list::*}:", | ||||||
"\tbroadcast \"%{_index}% = %{_value}%\"", | ||||||
}) | ||||||
@Since("INSERT VERSION") | ||||||
public class SecFor extends SecLoop { | ||||||
|
||||||
static { | ||||||
Skript.registerSection(SecFor.class, | ||||||
"(for [each]|loop) [value] %~object% in %objects%", | ||||||
"(for [each]|loop) (key|index) %~object% in %objects%", | ||||||
"(for [each]|loop) [key|index] %~object% (=|and) [value] %~object% in %objects%" | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure about this |
||||||
); | ||||||
} | ||||||
|
||||||
private @Nullable Expression<?> keyStore, valueStore; | ||||||
|
||||||
@Override | ||||||
@SuppressWarnings("unchecked") | ||||||
public boolean init(Expression<?>[] exprs, | ||||||
int matchedPattern, | ||||||
Kleenean isDelayed, | ||||||
ParseResult parseResult, | ||||||
SectionNode sectionNode, | ||||||
List<TriggerItem> triggerItems) { | ||||||
if (!this.getParser().hasExperiment(Feature.FOR_EACH_LOOPS)) | ||||||
return false; | ||||||
//<editor-fold desc="Set the key/value expressions based on the pattern" defaultstate="collapsed"> | ||||||
switch (matchedPattern) { | ||||||
case 0: | ||||||
this.valueStore = exprs[0]; | ||||||
this.expression = LiteralUtils.defendExpression(exprs[1]); | ||||||
break; | ||||||
case 1: | ||||||
this.keyStore = exprs[0]; | ||||||
this.expression = LiteralUtils.defendExpression(exprs[1]); | ||||||
break; | ||||||
default: | ||||||
this.keyStore = exprs[0]; | ||||||
this.valueStore = exprs[1]; | ||||||
this.expression = LiteralUtils.defendExpression(exprs[2]); | ||||||
} | ||||||
//</editor-fold> | ||||||
Moderocky marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
//<editor-fold desc="Check our input expressions are safe/correct" defaultstate="collapsed"> | ||||||
if (!(keyStore instanceof Variable || keyStore == null)) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this would be less confusing to instead write as
Suggested change
Same for below |
||||||
Skript.error("The 'key' input for a for-loop must be a variable to store the value."); | ||||||
return false; | ||||||
} | ||||||
if (!(valueStore instanceof Variable || valueStore == null)) { | ||||||
Skript.error("The 'value' input for a for-loop must be a variable to store the value."); | ||||||
return false; | ||||||
} | ||||||
if (!LiteralUtils.canInitSafely(expression)) { | ||||||
Skript.error("Can't understand this loop: '" + parseResult.expr + "'"); | ||||||
return false; | ||||||
} | ||||||
if (Container.class.isAssignableFrom(expression.getReturnType())) { | ||||||
ContainerType type = expression.getReturnType().getAnnotation(ContainerType.class); | ||||||
if (type == null) | ||||||
throw new SkriptAPIException(expression.getReturnType() | ||||||
.getName() + " implements Container but is missing the required @ContainerType annotation"); | ||||||
this.expression = new ContainerExpression((Expression<? extends Container<?>>) expression, type.value()); | ||||||
} | ||||||
if (expression.isSingle()) { | ||||||
Skript.error("Can't loop '" + expression + "' because it's only a single value"); | ||||||
return false; | ||||||
} | ||||||
//</editor-fold> | ||||||
this.loadOptionalCode(sectionNode); | ||||||
super.setNext(this); | ||||||
return true; | ||||||
} | ||||||
|
||||||
@Override | ||||||
protected void store(Event event, Object next) { | ||||||
super.store(event, next); | ||||||
//<editor-fold desc="Store the loop index/value in the variables" defaultstate="collapsed"> | ||||||
if (next instanceof Map.Entry) { | ||||||
@SuppressWarnings("unchecked") Map.Entry<String, Object> entry = (Map.Entry<String, Object>) next; | ||||||
if (keyStore != null) | ||||||
this.keyStore.change(event, new Object[] {entry.getKey()}, Changer.ChangeMode.SET); | ||||||
if (valueStore != null) | ||||||
this.valueStore.change(event, new Object[] {entry.getValue()}, Changer.ChangeMode.SET); | ||||||
} else { | ||||||
if (keyStore != null) | ||||||
this.keyStore.change(event, new Object[] {this.getLoopCounter(event)}, Changer.ChangeMode.SET); | ||||||
if (valueStore != null) | ||||||
this.valueStore.change(event, new Object[] {next}, Changer.ChangeMode.SET); | ||||||
} | ||||||
//</editor-fold> | ||||||
} | ||||||
|
||||||
@Override | ||||||
public String toString(@Nullable Event event, boolean debug) { | ||||||
if (keyStore != null && valueStore != null) { | ||||||
return "for each key " + keyStore.toString(event, debug) | ||||||
+ " and value " + valueStore.toString(event, debug) + " in " | ||||||
+ super.expression.toString(event, debug); | ||||||
} else if (keyStore != null) { | ||||||
return "for each key " + keyStore.toString(event, debug) | ||||||
+ " in " + super.expression.toString(event, debug); | ||||||
} | ||||||
assert valueStore != null : "How did we get here?"; | ||||||
return "for each value " + valueStore.toString(event, debug) | ||||||
+ " in " + super.expression.toString(event, debug); | ||||||
} | ||||||
|
||||||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
using for each loops | ||
|
||
test "for section": | ||
|
||
set {_list::*} to 1, 5, and 10 | ||
set {_expect} to 0 | ||
for {_value} in {_list::*}: | ||
assert {_value} is greater than 0 with "Expected value > 0, found %{_value}%" | ||
set {_expect} to {_value} | ||
|
||
assert {_value} is 10 with "Expected value = 10, found %{_value}%" | ||
assert {_expect} is 10 with "Expected expect = 10, found %{_expect}%" | ||
|
||
delete {_key} | ||
delete {_value} | ||
|
||
for {_key} = {_value} in {_list::*}: | ||
assert {_key} is greater than 0 with "Expected key > 0, found %{_key}%" | ||
assert {_key} is less than 4 with "Expected key < 4, found %{_key}%" | ||
assert {_value} is greater than 0 with "Expected value > 0, found %{_value}%" | ||
|
||
assert {_key} is 3 with "Expected key = 3, found %{_key}%" | ||
assert {_value} is 10 with "Expected value = 10, found %{_value}%" | ||
|
||
delete {_key} | ||
delete {_value} | ||
|
||
|
||
for key {_key} and value {_value} in {_list::*}: | ||
assert {_key} is greater than 0 with "Expected key > 0, found %{_key}%" | ||
assert {_key} is less than 4 with "Expected key < 4, found %{_key}%" | ||
assert {_value} is greater than 0 with "Expected value > 0, found %{_value}%" | ||
|
||
assert {_key} is 3 with "Expected key = 3, found %{_key}%" | ||
assert {_value} is 10 with "Expected value = 10, found %{_value}%" | ||
|
||
delete {_key} | ||
delete {_value} | ||
|
||
for {_key} and {_value} in {_list::*}: | ||
assert {_key} is greater than 0 with "Expected key > 0, found %{_key}%" | ||
assert {_key} is less than 4 with "Expected key < 4, found %{_key}%" | ||
assert {_value} is greater than 0 with "Expected value > 0, found %{_value}%" | ||
|
||
assert {_key} is 3 with "Expected key = 3, found %{_key}%" | ||
assert {_value} is 10 with "Expected value = 10, found %{_value}%" | ||
|
||
delete {_key} | ||
delete {_value} | ||
|
||
# 'loop' syntax alternative | ||
loop {_key} and {_value} in {_list::*}: | ||
assert {_key} is greater than 0 with "Expected key > 0, found %{_key}%" | ||
assert {_key} is less than 4 with "Expected key < 4, found %{_key}%" | ||
assert {_value} is greater than 0 with "Expected value > 0, found %{_value}%" | ||
|
||
assert {_key} is 3 with "Expected key = 3, found %{_key}%" | ||
assert {_value} is 10 with "Expected value = 10, found %{_value}%" | ||
|
||
delete {_key} | ||
delete {_value} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think
using experiment for
is great. I would either requireeach
or do something likefor (each [loop[s]]|loop[s])
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
for [each] loop[s] would be my preference