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 WILL to property condition #5955

Merged
merged 3 commits into from
Oct 7, 2023
Merged
Changes from all commits
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
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