From 8500da5cedd44230fbea35deef4dbf42c62fadfc Mon Sep 17 00:00:00 2001 From: TPGamesNL Date: Sat, 20 Mar 2021 17:40:00 +0100 Subject: [PATCH 1/2] Update files --- src/main/java/ch/njol/skript/Skript.java | 2 + .../skript/conditions/CondExpression.java | 59 +++++++++ .../java/ch/njol/skript/lang/Condition.java | 114 +++++++++++++++++- 3 files changed, 170 insertions(+), 5 deletions(-) create mode 100644 src/main/java/ch/njol/skript/conditions/CondExpression.java diff --git a/src/main/java/ch/njol/skript/Skript.java b/src/main/java/ch/njol/skript/Skript.java index 4b61582fc55..f39048cb480 100644 --- a/src/main/java/ch/njol/skript/Skript.java +++ b/src/main/java/ch/njol/skript/Skript.java @@ -1247,6 +1247,8 @@ public static void registerCondition(final Class condit final SyntaxElementInfo info = new SyntaxElementInfo<>(patterns, condition, originClassPath); conditions.add(info); statements.add(info); + // PATTERN_MATCHES_EVERYTHING makes the expression be attempted last + registerExpression(condition, Boolean.class, ExpressionType.PATTERN_MATCHES_EVERYTHING, patterns); } /** diff --git a/src/main/java/ch/njol/skript/conditions/CondExpression.java b/src/main/java/ch/njol/skript/conditions/CondExpression.java new file mode 100644 index 00000000000..1d7802385b9 --- /dev/null +++ b/src/main/java/ch/njol/skript/conditions/CondExpression.java @@ -0,0 +1,59 @@ +/** + * This file is part of Skript. + * + * Skript is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * Skript is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with Skript. If not, see . + * + * Copyright Peter Güttinger, SkriptLang team and contributors + */ +package ch.njol.skript.conditions; + +import java.util.Objects; + +import org.bukkit.event.Event; +import org.eclipse.jdt.annotation.Nullable; + +import ch.njol.skript.lang.Condition; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser; +import ch.njol.util.Kleenean; + +public class CondExpression extends Condition { + + private final Expression expression; + + public CondExpression(Expression expression) { + Objects.requireNonNull(expression); + this.expression = expression; + } + + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { + throw new IllegalStateException(); + } + + @Override + public boolean check(Event e) { + return expression.check(e, o -> o, false); + } + + @Override + public String toString(@Nullable Event e, boolean debug) { + return expression.toString(e, debug); + } + + public Expression getExpression() { + return expression; + } + +} diff --git a/src/main/java/ch/njol/skript/lang/Condition.java b/src/main/java/ch/njol/skript/lang/Condition.java index 4d2f6173b9e..2c9ce1b69ae 100644 --- a/src/main/java/ch/njol/skript/lang/Condition.java +++ b/src/main/java/ch/njol/skript/lang/Condition.java @@ -24,8 +24,13 @@ import org.eclipse.jdt.annotation.Nullable; import ch.njol.skript.Skript; +import ch.njol.skript.classes.Changer; +import ch.njol.skript.conditions.CondExpression; +import ch.njol.skript.lang.util.ConvertedExpression; import ch.njol.skript.lang.util.SimpleExpression; import ch.njol.util.Checker; +import ch.njol.util.coll.CollectionUtils; +import ch.njol.util.coll.iterator.SingleItemIterator; /** * A condition which must be fulfilled for the trigger to continue. If the condition is in a section the behaviour depends on the section. @@ -33,7 +38,7 @@ * @author Peter Güttinger * @see Skript#registerCondition(Class, String...) */ -public abstract class Condition extends Statement { +public abstract class Condition extends Statement implements Expression { private boolean negated = false; @@ -57,8 +62,6 @@ public final boolean run(final Event e) { /** * Sets the negation state of this condition. This will change the behaviour of {@link Expression#check(Event, Checker, boolean)}. - * - * @param invert */ protected final void setNegated(final boolean invert) { negated = invert; @@ -71,13 +74,114 @@ public final boolean isNegated() { return negated; } - @SuppressWarnings({"rawtypes", "unchecked", "null"}) + @SuppressWarnings({"ConstantConditions", "unchecked", "rawtypes"}) @Nullable public static Condition parse(String s, final String defaultError) { s = s.trim(); while (s.startsWith("(") && SkriptParser.next(s, 0, ParseContext.DEFAULT) == s.length()) s = s.substring(1, s.length() - 1); - return (Condition) SkriptParser.parse(s, (Iterator) Skript.getConditions().iterator(), defaultError); + + Expression expression = new SkriptParser(s).parseExpression(Boolean.class); + + if (expression == null) + return null; + + return expression instanceof Condition ? (Condition) expression : new CondExpression(expression); + } + + @Override + public Boolean getSingle(Event e) { + return check(e); + } + + @Override + public Boolean[] getArray(Event e) { + return new Boolean[] {check(e)}; + } + + @Override + public Boolean[] getAll(Event e) { + return new Boolean[] {check(e)}; + } + + @Override + public boolean isSingle() { + return true; + } + + @Override + public boolean check(Event e, Checker c, boolean negated) { + return SimpleExpression.check(getAll(e), c, negated, getAnd()); + } + + @Override + public boolean check(Event e, Checker c) { + return SimpleExpression.check(getAll(e), c, false, getAnd()); + } + + @SuppressWarnings("unchecked") + @Override + public Expression getConvertedExpression(Class... to) { + if (CollectionUtils.containsSuperclass(to, Boolean.class)) + return (Expression) this; + return ConvertedExpression.newInstance(this, to); + } + + @Override + public Class getReturnType() { + return Boolean.class; + } + + @Override + public boolean getAnd() { + return true; + } + + @Override + public boolean setTime(int time) { + return false; + } + + @Override + public int getTime() { + return 0; + } + + @Override + public boolean isDefault() { + return false; + } + + @Nullable + @Override + public Iterator iterator(Event e) { + return new SingleItemIterator<>(check(e)); + } + + @Override + public boolean isLoopOf(String s) { + return false; + } + + @Override + public Expression getSource() { + return this; + } + + @Override + public Expression simplify() { + return this; + } + + @Nullable + @Override + public Class[] acceptChange(Changer.ChangeMode mode) { + return null; + } + + @Override + public void change(Event e, @Nullable Object[] delta, Changer.ChangeMode mode) { + throw new UnsupportedOperationException(); } } From 3f6bd4b69ec0816cfa90533e9456a46d5f862e08 Mon Sep 17 00:00:00 2001 From: TPGamesNL Date: Sat, 20 Mar 2021 18:45:50 +0100 Subject: [PATCH 2/2] Update Condition.java --- .../java/ch/njol/skript/lang/Condition.java | 21 ++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/src/main/java/ch/njol/skript/lang/Condition.java b/src/main/java/ch/njol/skript/lang/Condition.java index 2c9ce1b69ae..341949d5bc2 100644 --- a/src/main/java/ch/njol/skript/lang/Condition.java +++ b/src/main/java/ch/njol/skript/lang/Condition.java @@ -28,6 +28,8 @@ import ch.njol.skript.conditions.CondExpression; import ch.njol.skript.lang.util.ConvertedExpression; import ch.njol.skript.lang.util.SimpleExpression; +import ch.njol.skript.log.ParseLogHandler; +import ch.njol.skript.log.SkriptLogger; import ch.njol.util.Checker; import ch.njol.util.coll.CollectionUtils; import ch.njol.util.coll.iterator.SingleItemIterator; @@ -74,17 +76,26 @@ public final boolean isNegated() { return negated; } - @SuppressWarnings({"ConstantConditions", "unchecked", "rawtypes"}) + @SuppressWarnings({"ConstantConditions", "unchecked"}) @Nullable public static Condition parse(String s, final String defaultError) { s = s.trim(); while (s.startsWith("(") && SkriptParser.next(s, 0, ParseContext.DEFAULT) == s.length()) s = s.substring(1, s.length() - 1); - Expression expression = new SkriptParser(s).parseExpression(Boolean.class); - - if (expression == null) - return null; + Expression expression; + ParseLogHandler logHandler = SkriptLogger.startParseLogHandler(); + try { + expression = new SkriptParser(s).parseExpression(Boolean.class); + if (expression == null) { + logHandler.printError(defaultError); + return null; + } + + logHandler.printLog(); + } finally { + logHandler.stop(); + } return expression instanceof Condition ? (Condition) expression : new CondExpression(expression); }