diff --git a/src/main/java/ch/njol/skript/conditions/CondIsDivisibleBy.java b/src/main/java/ch/njol/skript/conditions/CondIsDivisibleBy.java new file mode 100644 index 00000000000..42131fb7069 --- /dev/null +++ b/src/main/java/ch/njol/skript/conditions/CondIsDivisibleBy.java @@ -0,0 +1,60 @@ +package ch.njol.skript.conditions; + +import ch.njol.skript.Skript; +import ch.njol.skript.doc.Description; +import ch.njol.skript.doc.Examples; +import ch.njol.skript.doc.Name; +import ch.njol.skript.doc.Since; +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 org.bukkit.event.Event; +import org.jetbrains.annotations.Nullable; + +@Name("Is Evenly Divisible By") +@Description("Check if a number is evenly divisible by another number.") +@Examples({ + "if 5 is evenly divisible by 5:", + "if 11 cannot be evenly divided by 10:", +}) +@Since("INSERT VERSION") +public class CondIsDivisibleBy extends Condition { + + static { + Skript.registerCondition(CondIsDivisibleBy.class, + "%numbers% (is|are) evenly divisible by %number%", + "%numbers% (isn't|is not|aren't|are not) evenly divisible by %number%", + "%numbers% can be evenly divided by %number%", + "%numbers% (can't|can[ ]not) be evenly divided by %number%"); + } + @SuppressWarnings("null") + private Expression dividend; + @SuppressWarnings("null") + private Expression divisor; + + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + dividend = (Expression) exprs[0]; + divisor = (Expression) exprs[1]; + setNegated(matchedPattern == 1 || matchedPattern == 3); + return true; + } + + @Override + public boolean check(Event event) { + Number divisorNumber = divisor.getSingle(event); + if (divisorNumber == null) + return isNegated(); + double divisor = divisorNumber.doubleValue(); + return dividend.check(event, dividendNumber -> (dividendNumber.doubleValue() % divisor == 0), isNegated()); + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return dividend.toString(event, debug) + " is " + (isNegated() ? "not " : "") + + "evenly divisible by " + divisor.toString(event, debug); + } + +} diff --git a/src/test/skript/tests/syntaxes/conditions/CondIsDivisibleBy.sk b/src/test/skript/tests/syntaxes/conditions/CondIsDivisibleBy.sk new file mode 100644 index 00000000000..90a258a400b --- /dev/null +++ b/src/test/skript/tests/syntaxes/conditions/CondIsDivisibleBy.sk @@ -0,0 +1,16 @@ +test "is divisible": + assert 5 can be evenly divided by 5 with "5 can be divided by 5!" + assert 5 can not be evenly divided by 10 with "5 cannot be divided by 10!" + assert 5 isn't evenly divisible by 0 with "nothing can be divided by 0!" + assert 1964903306 is evenly divisible by 982451653 with "1964903306 is divisible by 982451653!" + assert {_none} is evenly divisible by 10 to fail with "you cannot divide by !" + assert 5, 10, and 15 can be evenly divided by 5 with "5, 10, and 15 can be evenly divided by 5!" + assert 3, 5, and 7 cannot be evenly divided by 2 with "3, 5, and 7 cannot be evenly divided by 2!" + assert 5 or 10 or 15 can be evenly divided by 5 with "5 or 10 or 15 can be evenly divided by 5!" + assert 3 or 5 or 7 cannot be evenly divided by 2 with "3 or 5 or 7 cannot be evenly divided by 2!" + assert -5 is evenly divisible by 5 with "-5 is evenly divisible by 5!" + assert -5, -10, and -15 can be evenly divided by 5 with "5, 10, and 15 can be evenly divided by 5!" + set {_num} to "5" + assert {_num} can be divided by 5 to fail with "You cannot divide by a string!" + set {_numlist::*} to "5", "10", "15" + assert {_numlist::*} can be divided by 5 to fail with "You cannot divide by a string!"