-
-
Notifications
You must be signed in to change notification settings - Fork 415
Expand the key API to allow for nested keys #8243
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
Open
UnderscoreTud
wants to merge
4
commits into
dev/feature
Choose a base branch
from
feature/recursive-key-api
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.
+525
−164
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 hidden or 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 hidden or 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 hidden or 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
109 changes: 109 additions & 0 deletions
109
src/main/java/ch/njol/skript/expressions/ExprRecursive.java
This file contains hidden or 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,109 @@ | ||
| package ch.njol.skript.expressions; | ||
|
|
||
| import ch.njol.skript.Skript; | ||
| import ch.njol.skript.SkriptAPIException; | ||
| import ch.njol.skript.doc.*; | ||
| import ch.njol.skript.expressions.base.WrapperExpression; | ||
| import ch.njol.skript.lang.Expression; | ||
| import ch.njol.skript.lang.ExpressionType; | ||
| import ch.njol.skript.lang.KeyProviderExpression; | ||
| import ch.njol.skript.lang.KeyedValue; | ||
| import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
| import ch.njol.util.Kleenean; | ||
| import org.bukkit.event.Event; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import java.util.Iterator; | ||
|
|
||
| @Name("Recursive") | ||
| @Description("Returns all values of an expression, including those in nested structures such as lists of lists.") | ||
| @Example(""" | ||
| on load: | ||
| set {_data::a::b::c} to "value1" | ||
| set {_data::a::b::d} to "value2" | ||
| set {_data::a::e} to "value3" | ||
| set {_data::f} to "value4" | ||
|
|
||
| broadcast recursive {_data::*} | ||
| # broadcasts "value1", "value2", "value3", "value4" | ||
|
|
||
| broadcast recursive indices of {_data::*} | ||
| # broadcasts "a::b::c", "a::b::d", "a::e", "f" | ||
| """) | ||
| @Since("INSERT VERSION") | ||
| @Keywords({"deep", "nested"}) | ||
| public class ExprRecursive extends WrapperExpression<Object> implements KeyProviderExpression<Object> { | ||
|
|
||
| static { | ||
| Skript.registerExpression(ExprRecursive.class, Object.class, ExpressionType.COMBINED, "recursive %~objects%"); | ||
| } | ||
|
|
||
| private boolean returnsKeys; | ||
|
|
||
| @Override | ||
| public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
| if (!expressions[0].allowNestedStructures()) { | ||
| Skript.error(expressions[0] + " does not support nested structures."); | ||
| return false; | ||
| } | ||
| setExpr(expressions[0]); | ||
| returnsKeys = KeyProviderExpression.canReturnKeys(getExpr()); | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public Object[] getAll(Event event) { | ||
| return getExpr().getAll(event); | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull String @NotNull [] getArrayKeys(Event event) throws IllegalStateException { | ||
| if (!returnsKeys) | ||
| throw new IllegalStateException(); | ||
| return ((KeyProviderExpression<?>) getExpr()).getArrayKeys(event); | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull String @NotNull [] getAllKeys(Event event) { | ||
| if (!returnsKeys) | ||
| throw new IllegalStateException(); | ||
| return ((KeyProviderExpression<?>) getExpr()).getAllKeys(event); | ||
| } | ||
|
|
||
| @Override | ||
| public Iterator<KeyedValue<Object>> keyedIterator(Event event) { | ||
| if (!returnsKeys) | ||
| throw new IllegalStateException(); | ||
| //noinspection unchecked | ||
| return ((KeyProviderExpression<Object>) getExpr()).keyedIterator(event); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean canReturnKeys() { | ||
| return returnsKeys; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean areKeysRecommended() { | ||
| return KeyProviderExpression.areKeysRecommended(getExpr()); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isIndexLoop(String input) { | ||
| if (!returnsKeys) | ||
| throw new IllegalStateException(); | ||
| return ((KeyProviderExpression<?>) getExpr()).isIndexLoop(input); | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isLoopOf(String input) { | ||
| return getExpr().isLoopOf(input); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(@Nullable Event event, boolean debug) { | ||
| return "recursive " + getExpr().toString(event, debug); | ||
| } | ||
|
|
||
| } |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -14,6 +14,15 @@ | |
| */ | ||
| public interface KeyReceiverExpression<T> extends Expression<T> { | ||
|
|
||
| /** | ||
| * Returns whether this expression's changer supports nested structures. | ||
| * | ||
| * @return true if nested structures are supported, false otherwise | ||
| */ | ||
| default boolean acceptsNestedStructures() { | ||
|
Member
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. Does this need to be used in EffChange's key handling? |
||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * An alternative changer method that provides a set of keys as well as a set of values. | ||
| * This is only ever called for {@link ChangeMode#supportsKeyedChange()} safe change modes, | ||
|
|
||
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
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.
Should it be possible to disallow nested structures? This method changing state on the Expression is slightly odd to me, but not necessarily a major issue.