Skip to content

Commit

Permalink
Add WILL to property condition (#5955)
Browse files Browse the repository at this point in the history
  • Loading branch information
TheLimeGlass authored Oct 7, 2023
1 parent fd23bc0 commit 83a41f6
Showing 1 changed file with 49 additions and 37 deletions.
86 changes: 49 additions & 37 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 @@ -74,100 +74,112 @@ public enum PropertyType {
* Indicates that the condition is in a form of <code>something has/have something</code>,
* also possibly in the negated form
*/
HAVE
HAVE,

/**
* Indicates that the condition is in a form of <code>something will/be something</code>,
* also possibly in the negated form
*/
WILL
}

@SuppressWarnings("null")

private Expression<? extends T> expr;

/**
* @param c the class to register
* @param condition the class to register
* @param property the property name, for example <i>fly</i> in <i>players can fly</i>
* @param type must be plural, for example <i>players</i> in <i>players can fly</i>
*/
public static void register(final Class<? extends Condition> c, final String property, final String type) {
register(c, PropertyType.BE, property, type);
public static void register(Class<? extends Condition> condition, String property, String type) {
register(condition, PropertyType.BE, property, type);
}

/**
* @param c the class to register
* @param condition the class to register
* @param propertyType the property type, see {@link PropertyType}
* @param property the property name, for example <i>fly</i> in <i>players can fly</i>
* @param type must be plural, for example <i>players</i> in <i>players can fly</i>
*/
public static void register(final Class<? extends Condition> c, final PropertyType propertyType, final String property, final String type) {
if (type.contains("%")) {
public static void register(Class<? extends Condition> condition, PropertyType propertyType, String property, String type) {
if (type.contains("%"))
throw new SkriptAPIException("The type argument must not contain any '%'s");
}

switch (propertyType) {
case BE:
Skript.registerCondition(c,
Skript.registerCondition(condition,
"%" + type + "% (is|are) " + property,
"%" + type + "% (isn't|is not|aren't|are not) " + property);
break;
case CAN:
Skript.registerCondition(c,
Skript.registerCondition(condition,
"%" + type + "% can " + property,
"%" + type + "% (can't|cannot|can not) " + property);
break;
case HAVE:
Skript.registerCondition(c,
Skript.registerCondition(condition,
"%" + type + "% (has|have) " + property,
"%" + type + "% (doesn't|does not|do not|don't) have " + property);
break;
case WILL:
Skript.registerCondition(condition,
"%" + type + "% will " + property,
"%" + type + "% (will (not|neither)|won't) " + 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
* @param expr The expression property of this property condition.
*/
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;
case WILL:
return expr.toString(event, debug) + (condition.isNegated() ? " won't " : " will ") + "be " + property;
default:
assert false;
throw new AssertionError();
Expand Down

0 comments on commit 83a41f6

Please sign in to comment.