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

Adds a condition for is divisible by #7104

Merged
merged 30 commits into from
Oct 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
c48c195
Create CondIsDivisibleBy.java
EquipableMC Sep 21, 2024
8afc2a5
requested changes + adds tests
EquipableMC Sep 21, 2024
6c64113
requested changes
EquipableMC Sep 21, 2024
c4a981f
requested changes
EquipableMC Sep 21, 2024
f311bde
Merge branch 'dev/feature' into CondDivisible
EquipableMC Sep 21, 2024
3b678ff
requested changes
EquipableMC Sep 21, 2024
2b8f53c
Update CondIsDivisibleBy.java
EquipableMC Sep 21, 2024
0fbf3b3
requested changes
EquipableMC Sep 21, 2024
4876fd9
requested changes
EquipableMC Sep 22, 2024
041c2e4
Merge branch 'dev/feature' into CondDivisible
EquipableMC Sep 22, 2024
c2cc05c
Merge branch 'dev/feature' into CondDivisible
EquipableMC Sep 23, 2024
e5ae6f2
requested changes + oops
EquipableMC Sep 23, 2024
0ddd180
Merge branch 'CondDivisible' of https://github.com/EquipableMC/Skript…
EquipableMC Sep 23, 2024
fbcc701
requested changes
EquipableMC Sep 24, 2024
531541b
requested changes + fixes tests
EquipableMC Sep 24, 2024
4fa368d
fixes documentation
EquipableMC Sep 25, 2024
2252547
makes "evenly" optional
EquipableMC Sep 25, 2024
c083ee2
makes evenly required again
EquipableMC Sep 25, 2024
15fa4e5
requested changes + minor syntax update
EquipableMC Sep 27, 2024
e006bbe
Update CondIsDivisibleBy.java
EquipableMC Sep 27, 2024
430e211
requested changes
EquipableMC Sep 27, 2024
c31bd37
Merge branch 'dev/feature' into CondDivisible
EquipableMC Oct 1, 2024
0279980
Merge branch 'dev/feature' into CondDivisible
EquipableMC Oct 1, 2024
7833cc5
Merge branch 'dev/feature' into CondDivisible
sovdeeth Oct 3, 2024
7e11828
requested changes
EquipableMC Oct 3, 2024
33b2ce1
requested changes
EquipableMC Oct 12, 2024
df9d9e9
Merge branch 'dev/feature' into CondDivisible
EquipableMC Oct 13, 2024
c4e8819
Merge branch 'dev/feature' into CondDivisible
sovdeeth Oct 13, 2024
97e1caa
Merge branch 'dev/feature' into CondDivisible
EquipableMC Oct 20, 2024
132f5d7
Merge branch 'dev/feature' into CondDivisible
sovdeeth Oct 26, 2024
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
60 changes: 60 additions & 0 deletions src/main/java/ch/njol/skript/conditions/CondIsDivisibleBy.java
Original file line number Diff line number Diff line change
@@ -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;

EquipableMC marked this conversation as resolved.
Show resolved Hide resolved
@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<Number> dividend;
@SuppressWarnings("null")
private Expression<Number> divisor;

@Override
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
dividend = (Expression<Number>) exprs[0];
divisor = (Expression<Number>) 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);
}

}
16 changes: 16 additions & 0 deletions src/test/skript/tests/syntaxes/conditions/CondIsDivisibleBy.sk
EquipableMC marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
test "is divisible":
EquipableMC marked this conversation as resolved.
Show resolved Hide resolved
EquipableMC marked this conversation as resolved.
Show resolved Hide resolved
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 <none>!"
EquipableMC marked this conversation as resolved.
Show resolved Hide resolved
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!"