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

Add condition type to fix parsing order #5809

Merged
merged 11 commits into from
Aug 19, 2024
79 changes: 48 additions & 31 deletions src/main/java/ch/njol/skript/Skript.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@
import org.eclipse.jdt.annotation.Nullable;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.skriptlang.skript.lang.comparator.Comparator;
import org.skriptlang.skript.lang.comparator.Comparators;
import org.skriptlang.skript.lang.converter.Converter;
import org.skriptlang.skript.lang.converter.Converters;
import org.skriptlang.skript.lang.entry.EntryValidator;
import org.skriptlang.skript.lang.script.Script;
import org.skriptlang.skript.lang.structure.Structure;
Expand All @@ -86,8 +90,6 @@
import ch.njol.skript.aliases.Aliases;
import ch.njol.skript.bukkitutil.BurgerHelper;
import ch.njol.skript.classes.ClassInfo;
import org.skriptlang.skript.lang.comparator.Comparator;
import org.skriptlang.skript.lang.converter.Converter;
import ch.njol.skript.classes.data.BukkitClasses;
import ch.njol.skript.classes.data.BukkitEventValues;
import ch.njol.skript.classes.data.DefaultComparators;
Expand All @@ -100,6 +102,7 @@
import ch.njol.skript.events.EvtSkript;
import ch.njol.skript.hooks.Hook;
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.Condition.ConditionType;
import ch.njol.skript.lang.Effect;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.ExpressionInfo;
Expand All @@ -124,8 +127,6 @@
import ch.njol.skript.log.SkriptLogger;
import ch.njol.skript.log.Verbosity;
import ch.njol.skript.registrations.Classes;
import org.skriptlang.skript.lang.comparator.Comparators;
import org.skriptlang.skript.lang.converter.Converters;
import ch.njol.skript.registrations.EventValues;
import ch.njol.skript.test.runner.EffObjectives;
import ch.njol.skript.test.runner.SkriptJUnitTest;
Expand Down Expand Up @@ -1326,25 +1327,57 @@ public static SkriptAddon getAddonInstance() {

// ================ CONDITIONS & EFFECTS & SECTIONS ================

private static final Collection<SyntaxElementInfo<? extends Condition>> conditions = new ArrayList<>(50);
private static final Collection<SyntaxElementInfo<? extends Effect>> effects = new ArrayList<>(50);
private static final Collection<SyntaxElementInfo<? extends Statement>> statements = new ArrayList<>(100);
private static final Collection<SyntaxElementInfo<? extends Section>> sections = new ArrayList<>(50);
private static final List<SyntaxElementInfo<? extends Condition>> conditions = new ArrayList<>(50);
private static final List<SyntaxElementInfo<? extends Effect>> effects = new ArrayList<>(50);
private static final List<SyntaxElementInfo<? extends Statement>> statements = new ArrayList<>(100);
private static final List<SyntaxElementInfo<? extends Section>> sections = new ArrayList<>(50);

public static Collection<SyntaxElementInfo<? extends Statement>> getStatements() {
return statements;
}

public static Collection<SyntaxElementInfo<? extends Effect>> getEffects() {
return effects;
}

public static Collection<SyntaxElementInfo<? extends Section>> getSections() {
return sections;
}

// ================ CONDITIONS ================
public static Collection<SyntaxElementInfo<? extends Condition>> getConditions() {
return conditions;
}

private final static int[] conditionTypesStartIndices = new int[ConditionType.values().length];

/**
* registers a {@link Condition}.
*
* @param condition The condition's class
* @param patterns Skript patterns to match this condition
*/
public static <E extends Condition> void registerCondition(Class<E> condition, String... patterns) throws IllegalArgumentException {
registerCondition(condition, ConditionType.COMBINED, patterns);
}

/**
* registers a {@link Condition}.
*
* @param condition The condition's class
* @param type The conditions {@link ConditionType type}. This is used to determine in which order to try to parse conditions.
* @param patterns Skript patterns to match this condition
*/
public static <E extends Condition> void registerCondition(final Class<E> condition, final String... patterns) throws IllegalArgumentException {
public static <E extends Condition> void registerCondition(Class<E> condition, ConditionType type, String... patterns) throws IllegalArgumentException {
checkAcceptRegistrations();
String originClassPath = Thread.currentThread().getStackTrace()[2].getClassName();
final SyntaxElementInfo<E> info = new SyntaxElementInfo<>(patterns, condition, originClassPath);
conditions.add(info);
statements.add(info);
conditions.add(conditionTypesStartIndices[type.ordinal()], info);
statements.add(conditionTypesStartIndices[type.ordinal()], info);
for (int i = type.ordinal(); i < ConditionType.values().length; i++)
conditionTypesStartIndices[i]++;
}

/**
* Registers an {@link Effect}.
*
Expand Down Expand Up @@ -1373,28 +1406,12 @@ public static <E extends Section> void registerSection(Class<E> section, String.
sections.add(info);
}

public static Collection<SyntaxElementInfo<? extends Statement>> getStatements() {
return statements;
}

public static Collection<SyntaxElementInfo<? extends Condition>> getConditions() {
return conditions;
}

public static Collection<SyntaxElementInfo<? extends Effect>> getEffects() {
return effects;
}

public static Collection<SyntaxElementInfo<? extends Section>> getSections() {
return sections;
}

// ================ EXPRESSIONS ================

private final static List<ExpressionInfo<?, ?>> expressions = new ArrayList<>(100);

private final static int[] expressionTypesStartIndices = new int[ExpressionType.values().length];

/**
* Registers an expression.
*
Expand Down
13 changes: 4 additions & 9 deletions src/main/java/ch/njol/skript/conditions/CondCompare.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@

import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.ClassInfo;

import org.skriptlang.skript.lang.comparator.Comparator;
import org.skriptlang.skript.lang.comparator.ComparatorInfo;
import org.skriptlang.skript.lang.comparator.Comparators;
import org.skriptlang.skript.lang.comparator.Relation;
import org.skriptlang.skript.lang.converter.ConverterInfo;
import org.skriptlang.skript.lang.converter.Converters;

import ch.njol.skript.Skript;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.doc.Description;
import ch.njol.skript.doc.Examples;
import ch.njol.skript.doc.Name;
Expand All @@ -46,8 +43,6 @@
import ch.njol.skript.log.RetainingLogHandler;
import ch.njol.skript.log.SkriptLogger;
import ch.njol.skript.registrations.Classes;

import org.skriptlang.skript.lang.comparator.Comparators;
import ch.njol.skript.util.Patterns;
import ch.njol.skript.util.Utils;
import ch.njol.util.Checker;
Expand Down Expand Up @@ -95,7 +90,7 @@ public class CondCompare extends Condition {
});

static {
Skript.registerCondition(CondCompare.class, patterns.getPatterns());
Skript.registerCondition(CondCompare.class, ConditionType.PATTERN_MATCHES_EVERYTHING, patterns.getPatterns());
}

private Expression<?> first;
Expand Down
60 changes: 30 additions & 30 deletions src/main/java/ch/njol/skript/conditions/base/PropertyCondition.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
* the first one needs to be a non-negated one and a negated one.
*/
public abstract class PropertyCondition<T> extends Condition implements Checker<T> {

/**
* See {@link PropertyCondition} for more info
*/
Expand All @@ -63,23 +63,22 @@ public enum PropertyType {
* also possibly in the negated form
*/
BE,

/**
* Indicates that the condition is in a form of <code>something can something</code>,
* also possibly in the negated form
*/
CAN,

/**
* Indicates that the condition is in a form of <code>something has/have something</code>,
* also possibly in the negated form
*/
HAVE
}

@SuppressWarnings("null")

private Expression<? extends T> expr;

/**
* @param c the class to register
* @param property the property name, for example <i>fly</i> in <i>players can fly</i>
Expand All @@ -88,7 +87,7 @@ public enum PropertyType {
public static void register(final Class<? extends Condition> c, final String property, final String type) {
register(c, PropertyType.BE, property, type);
}

/**
* @param c the class to register
* @param propertyType the property type, see {@link PropertyType}
Expand All @@ -101,76 +100,77 @@ public static void register(final Class<? extends Condition> c, final PropertyTy
}
switch (propertyType) {
case BE:
Skript.registerCondition(c,
Skript.registerCondition(c, ConditionType.PROPERTY,
"%" + type + "% (is|are) " + property,
"%" + type + "% (isn't|is not|aren't|are not) " + property);
break;
case CAN:
Skript.registerCondition(c,
Skript.registerCondition(c, ConditionType.PROPERTY,
"%" + type + "% can " + property,
"%" + type + "% (can't|cannot|can not) " + property);
break;
case HAVE:
Skript.registerCondition(c,
Skript.registerCondition(c, ConditionType.PROPERTY,
"%" + type + "% (has|have) " + property,
"%" + type + "% (doesn't|does not|do not|don't) have " + property);
break;
default:
assert false;
}
}

@SuppressWarnings({"unchecked", "null"})

@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
expr = (Expression<? extends T>) exprs[0];
setNegated(matchedPattern == 1);
return true;
}

@Override
public final boolean check(final Event e) {
return expr.check(e, this, isNegated());
public final boolean check(Event event) {
return expr.check(event, this, isNegated());
}

@Override
public abstract boolean check(T t);

protected abstract String getPropertyName();

protected PropertyType getPropertyType() {
return PropertyType.BE;
}

/**
* Sets the expression this condition checks a property of. No reference to the expression should be kept.
*
* @param expr
*/
protected final void setExpr(final Expression<? extends T> expr) {
protected final void setExpr(Expression<? extends T> expr) {
this.expr = expr;
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
return toString(this, getPropertyType(), e, debug, expr, getPropertyName());
public String toString(@Nullable Event event, boolean debug) {
return toString(this, getPropertyType(), event, debug, expr, getPropertyName());
}
public static String toString(Condition condition, PropertyType propertyType, @Nullable Event e,

public static String toString(Condition condition, PropertyType propertyType, @Nullable Event event,
boolean debug, Expression<?> expr, String property) {
switch (propertyType) {
case BE:
return expr.toString(e, debug) + (expr.isSingle() ? " is " : " are ") + (condition.isNegated() ? "not " : "") + property;
return expr.toString(event, debug) + (expr.isSingle() ? " is " : " are ") + (condition.isNegated() ? "not " : "") + property;
case CAN:
return expr.toString(e, debug) + (condition.isNegated() ? " can't " : " can ") + property;
return expr.toString(event, debug) + (condition.isNegated() ? " can't " : " can ") + property;
case HAVE:
if (expr.isSingle())
return expr.toString(e, debug) + (condition.isNegated() ? " doesn't have " : " has ") + property;
return expr.toString(event, debug) + (condition.isNegated() ? " doesn't have " : " has ") + property;
else
return expr.toString(e, debug) + (condition.isNegated() ? " don't have " : " have ") + property;
return expr.toString(event, debug) + (condition.isNegated() ? " don't have " : " have ") + property;
default:
assert false;
throw new AssertionError();
}
}

}
Loading