-
-
Notifications
You must be signed in to change notification settings - Fork 382
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
9e69c9c
commit b1e0a59
Showing
3 changed files
with
105 additions
and
1 deletion.
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,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(); | ||
} | ||
|
||
} |
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,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" |