Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 6 additions & 2 deletions src/main/java/ch/njol/skript/classes/SerializableChecker.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,14 @@
*/
package ch.njol.skript.classes;

import ch.njol.util.Checker;
import org.jetbrains.annotations.ApiStatus;

import java.util.function.Predicate;

/**
* @author Peter Güttinger
*/
@Deprecated
public interface SerializableChecker<T> extends Checker<T> {}
@FunctionalInterface
@ApiStatus.ScheduledForRemoval
public interface SerializableChecker<T> extends ch.njol.util.Checker<T>, Predicate<T> {}
51 changes: 26 additions & 25 deletions src/main/java/ch/njol/skript/conditions/CondCompare.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@
import org.skriptlang.skript.lang.comparator.Comparators;
import ch.njol.skript.util.Patterns;
import ch.njol.skript.util.Utils;
import ch.njol.util.Checker;
import ch.njol.util.Kleenean;
import org.skriptlang.skript.lang.util.Cyclical;

import java.util.function.Predicate;

@Name("Comparison")
@Description({"A very general condition, it simply compares two values. Usually you can only compare for equality (e.g. block is/isn't of &lt;type&gt;), " +
"but some values can also be compared using greater than/less than. In that case you can also test for whether an object is between two others.",
Expand All @@ -64,7 +65,7 @@
"the creature is not an enderman or an ender dragon"})
@Since("1.0")
public class CondCompare extends Condition implements VerboseAssert {

private final static Patterns<Relation> patterns = new Patterns<>(new Object[][]{
{"(1¦neither|) %objects% ((is|are)(|2¦(n't| not|4¦ neither)) ((greater|more|higher|bigger|larger) than|above)|\\>) %objects%", Relation.GREATER},
{"(1¦neither|) %objects% ((is|are)(|2¦(n't| not|4¦ neither)) (greater|more|higher|bigger|larger|above) [than] or (equal to|the same as)|\\>=) %objects%", Relation.GREATER_OR_EQUAL},
Expand All @@ -74,7 +75,7 @@ public class CondCompare extends Condition implements VerboseAssert {
{"(1¦neither|) %objects% (is|are|=) [(equal to|the same as)] %objects%", Relation.EQUAL},
{"(1¦neither|) %objects% (is|are) between %objects% and %objects%", Relation.EQUAL},
{"(1¦neither|) %objects% (2¦)(is not|are not|isn't|aren't) between %objects% and %objects%", Relation.EQUAL},

{"(1¦neither|) %objects@-1% (was|were)(|2¦(n't| not|4¦ neither)) ((greater|more|higher|bigger|larger) than|above) %objects%", Relation.GREATER},
{"(1¦neither|) %objects@-1% (was|were)(|2¦(n't| not|4¦ neither)) (greater|more|higher|bigger|larger|above) [than] or (equal to|the same as) %objects%", Relation.GREATER_OR_EQUAL},
{"(1¦neither|) %objects@-1% (was|were)(|2¦(n't| not|4¦ neither)) ((less|smaller|lower) than|below) %objects%", Relation.SMALLER},
Expand All @@ -83,7 +84,7 @@ public class CondCompare extends Condition implements VerboseAssert {
{"(1¦neither|) %objects@-1% (was|were) [(equal to|the same as)] %objects%", Relation.EQUAL},
{"(1¦neither|) %objects@-1% (was|were) between %objects% and %objects%", Relation.EQUAL},
{"(1¦neither|) %objects@-1% (2¦)(was not|were not|wasn't|weren't) between %objects% and %objects%", Relation.EQUAL},

{"(1¦neither|) %objects@1% (will be|2¦(will (not|4¦neither) be|won't be)) ((greater|more|higher|bigger|larger) than|above) %objects%", Relation.GREATER},
{"(1¦neither|) %objects@1% (will be|2¦(will (not|4¦neither) be|won't be)) (greater|more|higher|bigger|larger|above) [than] or (equal to|the same as) %objects%", Relation.GREATER_OR_EQUAL},
{"(1¦neither|) %objects@1% (will be|2¦(will (not|4¦neither) be|won't be)) ((less|smaller|lower) than|below) %objects%", Relation.SMALLER},
Expand All @@ -93,11 +94,11 @@ public class CondCompare extends Condition implements VerboseAssert {
{"(1¦neither|) %objects@1% will be between %objects% and %objects%", Relation.EQUAL},
{"(1¦neither|) %objects@1% (2¦)(will not be|won't be) between %objects% and %objects%", Relation.EQUAL}
});

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

private Expression<?> first;
private Expression<?> second;

Expand All @@ -109,7 +110,7 @@ public class CondCompare extends Condition implements VerboseAssert {
@Nullable
@SuppressWarnings("rawtypes")
private Comparator comparator;

@Override
public boolean init(final Expression<?>[] vars, final int matchedPattern, final Kleenean isDelayed, final ParseResult parser) {
first = vars[0];
Expand Down Expand Up @@ -152,16 +153,16 @@ public boolean init(final Expression<?>[] vars, final int matchedPattern, final
}
}
}

return true;
}

public static String f(final Expression<?> e) {
if (e.getReturnType() == Object.class)
return e.toString(null, false);
return Classes.getSuperClassInfo(e.getReturnType()).getName().withIndefiniteArticle();
}

@SuppressWarnings("unchecked")
private boolean init(String expr) {
Expression<?> third = this.third;
Expand Down Expand Up @@ -217,11 +218,11 @@ private boolean init(String expr) {
* SkriptParser sees that CondCompare takes two objects. Most of the time,
* this works fine. However, when there are multiple conflicting literals,
* it just picks one of them at random.
*
*
* If our other parameter is not a literal, we can try parsing the other
* explicitly with same return type. This is not guaranteed to succeed,
* but will in work in some cases that were previously ambiguous.
*
*
* Some damage types not working (issue #2184) would be a good example
* of issues that SkriptParser's lack of context can cause.
*/
Expand All @@ -236,7 +237,7 @@ private boolean init(String expr) {
comparator = Comparators.getComparator(first.getReturnType(), secondReturnType);
}
}

}

return comparator != null;
Expand Down Expand Up @@ -273,7 +274,7 @@ private <T> SimpleLiteral<T> reparseLiteral(Class<T> type, Expression<?> express
* For example 'fire' will be VisualEffect without this, but if the user attempts to compare 'fire'
* with a block. This method will see if 'fire' can be compared to the block, and it will find ItemType.
* Essentially solving something a human sees as comparable, but Skript doesn't understand.
*
*
* @param one The UnparsedLiteral expression to attempt to reconstruct.
* @param two any expression to grab the return type from.
* @return The newly formed Literal, will be SimpleLiteral in most cases.
Expand Down Expand Up @@ -307,7 +308,7 @@ private Literal<?> attemptReconstruction(UnparsedLiteral one, Expression<?> two)
/*
* # := condition (e.g. is, is less than, contains, is enchanted with, has permission, etc.)
* !# := not #
*
*
* a and b # x === a # x && b # x
* a or b # x === a # x || b # x
* a # x and y === a # x && a # y
Expand All @@ -318,25 +319,25 @@ private Literal<?> attemptReconstruction(UnparsedLiteral one, Expression<?> two)
* a and b # x or y === a # x or y && b # x or y
* a or b # x and y === a # x and y || b # x and y
* a or b # x or y === a # x or y || b # x or y
*
*
*
*
* a and b !# x === a !# x && b !# x
* neither a nor b # x === a !# x && b !# x // nor = and
* a or b !# x === a !# x || b !# x
*
*
* a !# x and y === a !# x || a !# y // e.g. "player doesn't have 2 emeralds and 5 gold ingots" == "NOT(player has 2 emeralds and 5 gold ingots)" == "player doesn't have 2 emeralds OR player doesn't have 5 gold ingots"
* a # neither x nor y === a !# x && a !# y // nor = or // e.g. "player has neither 2 emeralds nor 5 gold ingots" == "player doesn't have 2 emeralds AND player doesn't have 5 gold ingots"
* a # neither x nor y === a !# x && a !# y // nor = or // e.g. "player is neither the attacker nor the victim" == "player is not the attacker AND player is not the victim"
* a !# x or y === a !# x && a !# y // e.g. "player doesn't have 2 emeralds or 5 gold ingots" == "NOT(player has 2 emeralds or 5 gold ingots)" == "player doesn't have 2 emeralds AND player doesn't have 5 gold ingots"
*
*
* a and b !# x and y === a !# x and y && b !# x and y === (a !# x || a !# y) && (b !# x || b !# y)
* a and b !# x or y === a !# x or y && b !# x or y
* a and b # neither x nor y === a # neither x nor y && b # neither x nor y
*
*
* a or b !# x and y === a !# x and y || b !# x and y
* a or b !# x or y === a !# x or y || b !# x or y
* a or b # neither x nor y === a # neither x nor y || b # neither x nor y
*
*
* neither a nor b # x and y === a !# x and y && b !# x and y // nor = and
* neither a nor b # x or y === a !# x or y && b !# x or y // nor = and
*/
Expand All @@ -351,11 +352,11 @@ public boolean check(final Event event) {
second.getAnd() && !second.isSingle())
return compareLists(event);

return first.check(event, (Checker<Object>) o1 ->
second.check(event, (Checker<Object>) o2 -> {
return first.check(event, (Predicate<Object>) o1 ->
second.check(event, (Predicate<Object>) o2 -> {
if (third == null)
return relation.isImpliedBy(comparator != null ? comparator.compare(o1, o2) : Comparators.compare(o1, o2));
return third.check(event, (Checker<Object>) o3 -> {
return third.check(event, (Predicate<Object>) o3 -> {
boolean isBetween;
if (comparator != null) {
if (o1 instanceof Cyclical<?> && o2 instanceof Cyclical<?> && o3 instanceof Cyclical<?>) {
Expand Down Expand Up @@ -436,5 +437,5 @@ public String toString(final @Nullable Event event, final boolean debug) {
s += " (comparator: " + comparator + ")";
return s;
}

}
19 changes: 10 additions & 9 deletions src/main/java/ch/njol/skript/conditions/CondIsOfType.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,10 @@
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Checker;
import ch.njol.util.Kleenean;

import java.util.function.Predicate;

/**
* @author Peter Güttinger
*/
Expand All @@ -48,16 +49,16 @@
"victim is of type {villager type}"})
@Since("1.4")
public class CondIsOfType extends Condition {

static {
PropertyCondition.register(CondIsOfType.class, "of type[s] %entitytypes/entitydatas%", "itemstacks/entities");
}

@SuppressWarnings("null")
private Expression<?> what;
@SuppressWarnings("null")
private Expression<?> types;

@SuppressWarnings("null")
@Override
public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) {
Expand All @@ -66,12 +67,12 @@ public boolean init(final Expression<?>[] exprs, final int matchedPattern, final
setNegated(matchedPattern == 1);
return true;
}

@Override
public boolean check(final Event e) {
return what.check(e,
(Checker<Object>) o1 -> types.check(e,
(Checker<Object>) o2 -> {
(Predicate<Object>) o1 -> types.check(e,
(Predicate<Object>) o2 -> {
if (o2 instanceof ItemType && o1 instanceof ItemType) {
return ((ItemType) o2).isSupertypeOf((ItemType) o1);
} else if (o2 instanceof EntityData && o1 instanceof Entity) {
Expand All @@ -84,11 +85,11 @@ public boolean check(final Event e) {
}),
isNegated());
}

@Override
public String toString(final @Nullable Event e, final boolean debug) {
return PropertyCondition.toString(this, PropertyType.BE, e, debug, what,
"of " + (types.isSingle() ? "type " : "types") + types.toString(e, debug));
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,10 @@
import ch.njol.skript.lang.Condition;
import ch.njol.skript.lang.Expression;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.util.Checker;
import ch.njol.util.Kleenean;

import java.util.function.Predicate;

/**
* This class can be used for an easier writing of conditions that contain only one type in the pattern,
* and are in one of the following forms:
Expand All @@ -52,7 +53,7 @@
* {@link PropertyCondition#register(Class, String, String)}, be aware that there can only be two patterns -
* the first one needs to be a non-negated one and a negated one.
*/
public abstract class PropertyCondition<T> extends Condition implements Checker<T> {
public abstract class PropertyCondition<T> extends Condition implements ch.njol.util.Checker<T>, Predicate<T> {

/**
* See {@link PropertyCondition} for more info
Expand All @@ -63,13 +64,13 @@ 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
Expand Down Expand Up @@ -145,9 +146,13 @@ public final boolean check(Event event) {
return expr.check(event, this, isNegated());
}

@Override
public abstract boolean check(T value);


@Override
public final boolean test(T value) {
return this.check(value);
}

protected abstract String getPropertyName();

protected PropertyType getPropertyType() {
Expand Down
Loading