-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathCondExprIsDivisible.java
More file actions
59 lines (52 loc) · 2.04 KB
/
CondExprIsDivisible.java
File metadata and controls
59 lines (52 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
package io.github.syst3ms.skriptparser.expressions;
import io.github.syst3ms.skriptparser.Parser;
import io.github.syst3ms.skriptparser.lang.Expression;
import io.github.syst3ms.skriptparser.lang.TriggerContext;
import io.github.syst3ms.skriptparser.lang.properties.ConditionalType;
import io.github.syst3ms.skriptparser.lang.properties.PropertyConditional;
import io.github.syst3ms.skriptparser.parsing.ParseContext;
import io.github.syst3ms.skriptparser.util.math.BigDecimalMath;
import java.math.BigInteger;
/**
* Check if a given number is divisible by another number.
* Note that when the number is a decimal number,
* the check will automatically fail.
*
* @name Is Divisible
* @type CONDITION
* @pattern %numbers% (is|are)[ not|n't] divisible by %integer%
* @since ALPHA
* @author Mwexim
*/
public class CondExprIsDivisible extends PropertyConditional<Number> {
static {
Parser.getMainRegistration().addPropertyConditional(
CondExprIsDivisible.class,
"numbers",
ConditionalType.BE,
"divisible by %integer%"
);
}
private Expression<BigInteger> divider;
@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, ParseContext parseContext) {
divider = (Expression<BigInteger>) expressions[1];
return super.init(expressions, matchedPattern, parseContext);
}
@Override
public boolean check(TriggerContext ctx) {
return getOwner().check(
ctx,
performer -> divider.getSingle(ctx)
.filter(__ -> BigDecimalMath.isIntValue(BigDecimalMath.getBigDecimal(performer)))
.filter(val -> BigDecimalMath.getBigInteger(performer).mod(val).equals(BigInteger.ZERO))
.isPresent(),
isNegated()
);
}
@Override
public String toString(TriggerContext ctx, boolean debug) {
return toString(ctx, debug, "divisible by " + divider.toString(ctx, debug));
}
}