-
-
Notifications
You must be signed in to change notification settings - Fork 414
API for custom default values during parsing. #7256
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
Merged
sovdeeth
merged 8 commits into
SkriptLang:dev/feature
from
sovdeeth:feature/default-value-api
Jun 30, 2025
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
e1c1501
add custom default value api
sovdeeth 8c34af2
handle nested custom defaults
sovdeeth 795e726
Merge branch 'dev/feature' into feature/default-value-api
Moderocky 4620668
Merge branch 'dev/feature' into feature/default-value-api
sovdeeth e37a027
requested changes
sovdeeth dd42137
Merge branch 'dev/feature' into feature/default-value-api
sovdeeth 49a7bcd
requested changes
sovdeeth f8e01a2
Merge branch 'dev/feature' into feature/default-value-api
sovdeeth 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
71 changes: 71 additions & 0 deletions
71
src/main/java/ch/njol/skript/lang/parser/DefaultValueData.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,71 @@ | ||
| package ch.njol.skript.lang.parser; | ||
|
|
||
| import ch.njol.skript.lang.DefaultExpression; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| import java.util.ArrayDeque; | ||
| import java.util.Deque; | ||
| import java.util.HashMap; | ||
| import java.util.Map; | ||
|
|
||
| /** | ||
| * A Parser Data that stores custom default values that should be given preference over event-values and other | ||
| * traditional default values. | ||
| * <br> | ||
| * These do not apply to arithmetic or other dynamic default values, they're only supplied during parsing. | ||
| * @see ch.njol.skript.test.runner.SecCustomDefault SecCustomDefault for example usage | ||
| */ | ||
| public class DefaultValueData extends ParserInstance.Data { | ||
|
|
||
| private final Map<Class<?>, Deque<DefaultExpression<?>>> defaults = new HashMap<>(); | ||
|
|
||
| public DefaultValueData(ParserInstance parserInstance) { | ||
| super(parserInstance); | ||
| } | ||
|
|
||
| /** | ||
| * Sets the default value for type to value. | ||
| * <br> | ||
| * Any custom default values should be removed after they go out of scope. | ||
| * It will not be done automatically. | ||
| * | ||
| * @see #removeDefaultValue(Class) | ||
| * | ||
| * @param type The class which this value applies to. | ||
| * @param value The value to use. | ||
| * @param <T> The class of this value. | ||
| */ | ||
| public <T> void addDefaultValue(Class<T> type, DefaultExpression<T> value) { | ||
| defaults.computeIfAbsent(type, (key) -> new ArrayDeque<>()).push(value); | ||
| } | ||
|
|
||
| /** | ||
| * Gets the default value for the given type. | ||
| * @param type The class which this value applies to. | ||
| * @param <T> The class of this value. | ||
| * @return The default value for type, or null if none is set. | ||
| */ | ||
| public <T> @Nullable DefaultExpression<T> getDefaultValue(Class<T> type) { | ||
| Deque<DefaultExpression<?>> stack = defaults.get(type); | ||
| if (stack == null || stack.isEmpty()) | ||
| return null; | ||
| //noinspection unchecked | ||
| return (DefaultExpression<T>) stack.peek(); | ||
| } | ||
|
|
||
| /** | ||
| * Removes a default value from the data. | ||
| * <br> | ||
| * Default values are handled using stacks, so be sure to remove only what you have added. Nothing more, nothing less. | ||
| * @param type Which class to remove the default value of. | ||
| */ | ||
| public void removeDefaultValue(Class<?> type) { | ||
| Deque<DefaultExpression<?>> stack = defaults.get(type); | ||
| if (stack != null && !stack.isEmpty()) { | ||
| stack.pop(); | ||
| } else { | ||
| throw new IllegalStateException("No default value for " + type.getName() + " to remove. Imbalanced add/remove?"); | ||
| } | ||
| } | ||
|
|
||
| } | ||
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
51 changes: 51 additions & 0 deletions
51
src/main/java/ch/njol/skript/test/runner/ExprDefaultNumberValue.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,51 @@ | ||
| package ch.njol.skript.test.runner; | ||
|
|
||
| import ch.njol.skript.Skript; | ||
| import ch.njol.skript.doc.NoDoc; | ||
| import ch.njol.skript.lang.Expression; | ||
| import ch.njol.skript.lang.ExpressionType; | ||
| import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
| import ch.njol.skript.lang.util.SimpleExpression; | ||
| import ch.njol.util.Kleenean; | ||
| import org.bukkit.event.Event; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| @NoDoc | ||
| public class ExprDefaultNumberValue extends SimpleExpression<Number> { | ||
sovdeeth marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| static { | ||
| if (TestMode.ENABLED) | ||
| Skript.registerExpression(ExprDefaultNumberValue.class, Number.class, ExpressionType.PROPERTY, | ||
| "default number [%number%]"); | ||
| } | ||
|
|
||
| Expression<Number> value; | ||
|
|
||
| @Override | ||
| public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
| //noinspection unchecked | ||
| value = (Expression<Number>) expressions[0]; | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected Number @Nullable [] get(Event event) { | ||
| return new Number[]{value.getSingle(event)}; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isSingle() { | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| public Class<? extends Number> getReturnType() { | ||
| return Number.class; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(@Nullable Event event, boolean debug) { | ||
| return "default number"; | ||
| } | ||
|
|
||
| } | ||
70 changes: 70 additions & 0 deletions
70
src/main/java/ch/njol/skript/test/runner/SecCustomDefault.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,70 @@ | ||
| package ch.njol.skript.test.runner; | ||
|
|
||
| import ch.njol.skript.Skript; | ||
| import ch.njol.skript.classes.ClassInfo; | ||
| import ch.njol.skript.config.SectionNode; | ||
| import ch.njol.skript.lang.DefaultExpression; | ||
| import ch.njol.skript.lang.Expression; | ||
| import ch.njol.skript.lang.Literal; | ||
| import ch.njol.skript.lang.Section; | ||
| import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
| import ch.njol.skript.lang.TriggerItem; | ||
| import ch.njol.skript.lang.parser.DefaultValueData; | ||
| 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; | ||
|
|
||
| public class SecCustomDefault extends Section { | ||
|
|
||
| static { | ||
| if (TestMode.ENABLED) { | ||
| Skript.registerSection(SecCustomDefault.class, "run with custom default value %*object% for %*classinfo%"); | ||
| } | ||
| } | ||
|
|
||
| Literal<?> value; | ||
| ClassInfo<?> type; | ||
|
|
||
| @Override | ||
| public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult, | ||
| SectionNode sectionNode, List<TriggerItem> triggerItems) { | ||
| value = (Literal<?>) LiteralUtils.defendExpression(expressions[0]); | ||
| //noinspection unchecked | ||
| this.type = ((Literal<ClassInfo<?>>) expressions[1]).getSingle(); | ||
| Class<?> type = this.type.getC(); | ||
|
|
||
| if (!type.isAssignableFrom(value.getReturnType())) { | ||
| Skript.error("The value expression returns an invalid type: expected " + type.getSimpleName() + | ||
| ", got " + value.getReturnType().getSimpleName()); | ||
| return true; | ||
| } | ||
|
|
||
| DefaultValueData data = getParser().getData(DefaultValueData.class); | ||
|
|
||
| // Add custom default value | ||
| //noinspection rawtypes,unchecked | ||
| data.addDefaultValue((Class) type, (DefaultExpression) value); | ||
|
|
||
| // parse section with custom value | ||
| loadCode(sectionNode); | ||
|
|
||
| // remove custom value | ||
| data.removeDefaultValue(type); | ||
|
|
||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected @Nullable TriggerItem walk(Event event) { | ||
| return walk(event, true); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString(@Nullable Event event, boolean debug) { | ||
| return "run with custom default value " + value.toString(event, debug) + " for " + type.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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| test "custom default values": | ||
| run with custom default value 7 for number: | ||
| assert default number is 7 with "default value was not 7" | ||
| run with custom default value 3 for number: | ||
| assert default number is 3 with "default value was not 3" | ||
| assert default number is 7 with "default value was not 7" | ||
| assert default number is 1 with "default value was not 1" | ||
|
|
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.
Uh oh!
There was an error while loading. Please reload this page.