-
-
Notifications
You must be signed in to change notification settings - Fork 404
Adds a condition for is divisible by #7104
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
Merged
Merged
Changes from 13 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
c48c195
Create CondIsDivisibleBy.java
EquipableMC 8afc2a5
requested changes + adds tests
EquipableMC 6c64113
requested changes
EquipableMC c4a981f
requested changes
EquipableMC f311bde
Merge branch 'dev/feature' into CondDivisible
EquipableMC 3b678ff
requested changes
EquipableMC 2b8f53c
Update CondIsDivisibleBy.java
EquipableMC 0fbf3b3
requested changes
EquipableMC 4876fd9
requested changes
EquipableMC 041c2e4
Merge branch 'dev/feature' into CondDivisible
EquipableMC c2cc05c
Merge branch 'dev/feature' into CondDivisible
EquipableMC e5ae6f2
requested changes + oops
EquipableMC 0ddd180
Merge branch 'CondDivisible' of https://github.com/EquipableMC/Skript…
EquipableMC fbcc701
requested changes
EquipableMC 531541b
requested changes + fixes tests
EquipableMC 4fa368d
fixes documentation
EquipableMC 2252547
makes "evenly" optional
EquipableMC c083ee2
makes evenly required again
EquipableMC 15fa4e5
requested changes + minor syntax update
EquipableMC e006bbe
Update CondIsDivisibleBy.java
EquipableMC 430e211
requested changes
EquipableMC c31bd37
Merge branch 'dev/feature' into CondDivisible
EquipableMC 0279980
Merge branch 'dev/feature' into CondDivisible
EquipableMC 7833cc5
Merge branch 'dev/feature' into CondDivisible
sovdeeth 7e11828
requested changes
EquipableMC 33b2ce1
requested changes
EquipableMC df9d9e9
Merge branch 'dev/feature' into CondDivisible
EquipableMC c4e8819
Merge branch 'dev/feature' into CondDivisible
sovdeeth 97e1caa
Merge branch 'dev/feature' into CondDivisible
EquipableMC 132f5d7
Merge branch 'dev/feature' into CondDivisible
sovdeeth File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
64 changes: 64 additions & 0 deletions
64
src/main/java/ch/njol/skript/conditions/CondIsDivisibleBy.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
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("Divisible By") | ||
@Description("Check if a number is divisible by another number.") | ||
EquipableMC marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Examples({ | ||
"if 5 is divisible by 5:", | ||
"if 11 cannot be divided by 10:", | ||
}) | ||
@Since("INSERT VERSION") | ||
public class CondIsDivisibleBy extends Condition { | ||
|
||
static { | ||
Skript.registerCondition(CondIsDivisibleBy.class, | ||
"%numbers% (is|are) divisible by %number%", | ||
"%numbers% (isn't|is not|aren't|are not) divisible by %number%", | ||
EquipableMC marked this conversation as resolved.
Show resolved
Hide resolved
|
||
"%numbers% can be evenly divided by %number%", | ||
"%numbers% (can't|cannot|can not) be evenly divided by %number%"); | ||
} | ||
|
||
@SuppressWarnings("null") | ||
private Expression<Number> divisorExpression; | ||
@SuppressWarnings("null") | ||
private Expression<Number> dividendExpression; | ||
|
||
@Override | ||
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { | ||
divisorExpression = (Expression<Number>) exprs[0]; | ||
dividendExpression = (Expression<Number>) exprs[1]; | ||
EquipableMC marked this conversation as resolved.
Show resolved
Hide resolved
|
||
setNegated(matchedPattern == 1 || matchedPattern == 3); | ||
return true; | ||
} | ||
|
||
@Override | ||
public boolean check(Event event) { | ||
Number divisorNumber = dividendExpression.getSingle(event); | ||
EquipableMC marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return divisorExpression.check(event, new Checker<Number>() { | ||
EquipableMC marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@Override | ||
public boolean check(Number dividedNumber) { | ||
double divided = dividedNumber.doubleValue(); | ||
EquipableMC marked this conversation as resolved.
Show resolved
Hide resolved
|
||
double divisor = divisorNumber != null ? divisorNumber.doubleValue() : null; | ||
EquipableMC marked this conversation as resolved.
Show resolved
Hide resolved
|
||
return divided % divisor == 0; | ||
} | ||
}, isNegated()); | ||
} | ||
|
||
@Override | ||
public String toString(@Nullable Event event, boolean debug) { | ||
return divisorExpression.toString(event, debug) + " is divisible by " + dividendExpression.toString(event, debug); | ||
EquipableMC marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
} |
6 changes: 6 additions & 0 deletions
6
src/test/skript/tests/syntaxes/conditions/CondIsDivisibleBy.sk
EquipableMC marked this conversation as resolved.
Show resolved
Hide resolved
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
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 divisible by 0 with "nothing can be divided by 0!" | ||
assert 1964903306 is divisible by 982451653 with "1964903306 is divisible by 982451653!" | ||
assert {_none} is divisible by 10 to fail with "you cannot divide by <none>!" |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.