Skip to content
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

Parse Effect #7520

Open
wants to merge 17 commits into
base: dev/feature
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/ch/njol/skript/classes/Changer.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ public boolean supportsKeyedChange() {

/**
* @param what The objects to change
* @param delta An array with one or more instances of one or more of the the classes returned by {@link #acceptChange(ChangeMode)} for the given change mode (null for
* @param delta An array with one or more instances of one or more of the classes returned by {@link #acceptChange(ChangeMode)} for the given change mode (null for
* {@link ChangeMode#DELETE} and {@link ChangeMode#RESET}). <b>This can be a Object[], thus casting is not allowed.</b>
* @param mode The {@link ChangeMode} to test.
* @throws UnsupportedOperationException (optional) if this method was called on an unsupported ChangeMode.
Expand Down
79 changes: 79 additions & 0 deletions src/main/java/ch/njol/skript/effects/EffParse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package ch.njol.skript.effects;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.Changer.ChangeMode;
import ch.njol.skript.classes.Changer.ChangerUtils;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.classes.Parser;
import ch.njol.skript.lang.*;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.registrations.Classes;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.jetbrains.annotations.Nullable;

public class EffParse extends Effect {

static {
Skript.registerEffect(EffParse.class, "parse %~strings% as %*classinfo%");
}

private Expression<String> toParse;
private ClassInfo<?> classInfo;

@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] expressions, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
toParse = (Expression<String>) expressions[0];
classInfo = ((Literal<ClassInfo<?>>) expressions[1]).getSingle();

if (classInfo.getC() == String.class) {
Skript.error("Parsing as text is useless as only things that are already text may be parsed");
return false;
}

if (toParse == null) {
return false;
}

Parser<?> parser = classInfo.getParser();
if (parser == null || !parser.canParse(ParseContext.PARSE)) {
Skript.error("Text cannot be parsed as " + classInfo.getName().withIndefiniteArticle());
return false;
}

if (toParse instanceof ExpressionList<String> toParseExpressions) {
for (int i = 0; i < toParseExpressions.getAllExpressions().size(); i++) {
Expression<String> expression = (Expression<String>) toParseExpressions.getAllExpressions().get(i);
if (!ChangerUtils.acceptsChange(expression, ChangeMode.SET, classInfo.getC())) {
Skript.error(toParse + " can't be set to " + classInfo.getName().withIndefiniteArticle());
return false;
}
}
} else if (!ChangerUtils.acceptsChange(toParse, ChangeMode.SET, classInfo.getC())) {
Skript.error(toParse + " can't be set to " + classInfo.getName().withIndefiniteArticle());
return false;
}

return true;
}

@Override
@SuppressWarnings("unchecked")
protected void execute(Event event) {
if (toParse instanceof ExpressionList<String> toParseExpressions) {
for (int i = 0; i < toParseExpressions.getAllExpressions().size(); i++) {
Expression<String> expression = (Expression<String>) toParseExpressions.getAllExpressions().get(i);
expression.changeInPlace(event, (stringToParse) -> Classes.parseSimple(stringToParse, classInfo.getC(), ParseContext.PARSE));
}
} else {
toParse.changeInPlace(event, (stringToParse) -> Classes.parseSimple(stringToParse, classInfo.getC(), ParseContext.PARSE));
}
}

@Override
public String toString(@Nullable Event event, boolean debug) {
return "parse " + toParse + " as " + classInfo.getName().withIndefiniteArticle();
}

}
25 changes: 25 additions & 0 deletions src/test/skript/tests/syntaxes/effects/EffParse.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
test "parse effect":
# test single value
set {_value} to "5"
parse {_value} as int
assert {_value} = 5 with "couldn't parse a single value"

set {_value} to "a"
parse {_value} as int
assert {_value} isn't set with "failed parse didn't delete the value"

# test lists
set {_values::*} to "1", "2a", "3"
parse {_values::*} as int
assert {_values::1} = 1 with "couldn't parse a list - 1"
assert {_values::2} isn't set with "couldn't parse a list - 2"
assert {_values::3} = 3 with "couldn't parse a list - 3"

# test multiple expressions
set {_a} to "3"
set {_b} to "hello"
set {_c} to "5"
parse {_a}, {_b} and {_c} as int
assert {_a} = 3 with "couldn't parse multiple expressions - 1"
assert {_b} isn't set with "couldn't parse multiple expressions - 2"
assert {_c} = 5 with "couldn't parse multiple expressions - 3"
Loading