From c48c195a3ad1251496016f912d83b11dc1673ea5 Mon Sep 17 00:00:00 2001 From: EquipableMC <66171067+EquipableMC@users.noreply.github.com> Date: Sat, 21 Sep 2024 08:25:30 -0400 Subject: [PATCH] Create CondIsDivisibleBy.java --- .../skript/conditions/CondIsDivisibleBy.java | 54 +++++++++++++++++++ 1 file changed, 54 insertions(+) create mode 100644 src/main/java/ch/njol/skript/conditions/CondIsDivisibleBy.java 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..fdbb50ba0e4 --- /dev/null +++ b/src/main/java/ch/njol/skript/conditions/CondIsDivisibleBy.java @@ -0,0 +1,54 @@ +package ch.njol.skript.conditions; + +import ch.njol.skript.Skript; +import ch.njol.skript.lang.Condition; +import ch.njol.skript.lang.Expression; +import ch.njol.skript.lang.SkriptParser.ParseResult; +import ch.njol.util.Kleenean; +import org.bukkit.event.Event; +import org.jetbrains.annotations.Nullable; + +public class CondIsDivisibleBy extends Condition { + + static { + Skript.registerCondition(CondIsDivisibleBy.class, + "%number% is divisible by %number%", + "%number% can be divisible by %number%"); + } + + @SuppressWarnings("null") + private Expression num1; + @SuppressWarnings("null") + private Expression num2; + + @Override + public boolean init(Expression[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { + num1 = (Expression) exprs[0]; + num2 = (Expression) exprs[1]; + return true; + } + + @Override + public boolean check(Event event) { + Number number1 = num1.getSingle(event); + Number number2 = num2.getSingle(event); + + if (number1 == null || number2 == null) { + return false; // Prevent NullPointerException + } + + double divisor = number2.doubleValue(); + + if (divisor == 0) { + return false; // Avoid division by zero + } + + return number1.doubleValue() % divisor == 0; + } + + @Override + public String toString(@Nullable Event event, boolean debug) { + return "is divisible by " + num1.toString(event, debug) + num2.toString(event, debug); + } + +}