-
-
Notifications
You must be signed in to change notification settings - Fork 20
Add expression to parse conditions. #907
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
Fusezion
wants to merge
4
commits into
ShaneBeee:dev/feature
Choose a base branch
from
Fusezion:enhancement/parse-condition
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 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
e5827c7
ExprParseCondition.java - Add expression to parse conditions
Fusezion 8c92d6f
#1 Punctuation Hater
Fusezion 53c5a83
ExprParseCondition.java - Use proper single quote
Fusezion d64f2cc
Merge remote-tracking branch 'origin/enhancement/parse-condition' int…
Fusezion 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
74 changes: 74 additions & 0 deletions
74
src/main/java/com/shanebeestudios/skbee/elements/other/expressions/ExprParseCondition.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,74 @@ | ||
| package com.shanebeestudios.skbee.elements.other.expressions; | ||
|
|
||
| import ch.njol.skript.Skript; | ||
| 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.Condition; | ||
| import ch.njol.skript.lang.Expression; | ||
| import ch.njol.skript.lang.ExpressionType; | ||
| import ch.njol.skript.lang.SkriptParser.ParseResult; | ||
| import ch.njol.skript.lang.TriggerItem; | ||
| import ch.njol.skript.lang.util.SimpleExpression; | ||
| import ch.njol.util.Kleenean; | ||
| import org.bukkit.event.Event; | ||
| import org.jetbrains.annotations.NotNull; | ||
| import org.jetbrains.annotations.Nullable; | ||
|
|
||
| @Name("Parse Condition - With Return") | ||
| @Description({"This will parse a string as a condition and then check it and returns whether or not it's valid.", | ||
| "If you provide a command sender it works the same as Skript's 'effect commands'.", | ||
| "Otherwise it runs using the current event allowing you to use event-values", | ||
| "", | ||
| "**NOTE:** This is handled very differently from the parse effect expression in addition to behaving differently than Skript's `whether <condition>`.", | ||
| "If you have no good reason to use this, please check out the other two.", | ||
| "*tip: when running into invalid null states try replacing parts with variables*"}) | ||
| @Examples({"command /parse <string>:", | ||
| "\ttrigger:", | ||
| "\t\tif parse condition arg-1 is false:", | ||
| "\t\t\tsend \"&4:ERROR&c %arg-1% did not pass\""}) | ||
| @Since("2.15.0") | ||
| public class ExprParseCondition extends SimpleExpression<Boolean> { | ||
|
|
||
| static { | ||
| Skript.registerExpression(ExprParseCondition.class, Boolean.class, ExpressionType.COMBINED, | ||
| "parse condition[s] %strings%"); | ||
| } | ||
|
|
||
| private Expression<String> conditions; | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
| @Override | ||
| public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
| this.conditions = (Expression<String>) exprs[0]; | ||
| return true; | ||
| } | ||
|
|
||
| @Override | ||
| protected Boolean @Nullable [] get(Event event) { | ||
| String[] conditions = this.conditions.getArray(event); | ||
| Boolean[] booleans = new Boolean[conditions.length]; | ||
| for (int i = 0; i < conditions.length; i++) { | ||
| Condition condition = Condition.parse(conditions[i], null); | ||
| booleans[i] = condition != null && TriggerItem.walk(condition, event); | ||
| } | ||
| return booleans; | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isSingle() { | ||
| return this.conditions.isSingle(); | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull Class<? extends Boolean> getReturnType() { | ||
| return Boolean.class; | ||
| } | ||
|
|
||
| @Override | ||
| public @NotNull String toString(@Nullable Event e, boolean d) { | ||
| return "parse condition `" + this.conditions.toString(e, d) + "'"; | ||
| } | ||
|
|
||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.