forked from Mwexim/skript-parser
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathExprBooleanOperators.java
More file actions
94 lines (87 loc) · 3.24 KB
/
ExprBooleanOperators.java
File metadata and controls
94 lines (87 loc) · 3.24 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
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.base.ConditionalExpression;
import io.github.syst3ms.skriptparser.parsing.ParseContext;
import org.jetbrains.annotations.Nullable;
/**
* Basic boolean operators. It is possible to use conditions inside the operators.
*
* @author Syst3ms
* @name Boolean Operators
* @pattern not %=boolean%
* @pattern %=boolean% (or|\|\|) %=boolean%
* @pattern %=boolean% (and|&&) %=boolean%
* @since ALPHA
*/
public class ExprBooleanOperators extends ConditionalExpression {
static {
Parser.getMainRegistration().newExpression(
ExprBooleanOperators.class, Boolean.class, true,
"not %=boolean%",
"%=boolean% (or|\\|\\|) %=boolean%",
"%=boolean% (and|&&) %=boolean%")
.name("Boolean Operators")
.description("Performs boolean operations on booleans.")
.examples("if {_boolean} and {_boolean2}:",
"if {_boolean} or {_boolean2}:",
"if not {_boolean}:")
.since("1.0.0")
.register();
}
private int pattern;
private Expression<Boolean> first;
@Nullable
private Expression<Boolean> second;
@SuppressWarnings("unchecked")
@Override
public boolean init(Expression<?>[] expressions, int matchedPattern, ParseContext parseContext) {
pattern = matchedPattern;
first = (Expression<Boolean>) expressions[0];
if (expressions.length > 1) {
second = (Expression<Boolean>) expressions[1];
}
setNegated(matchedPattern == 0);
return true;
}
// Leaving this here in case this was a bad idea to change to ConditionalExpression
// @Override
// public Boolean[] getValues(TriggerContext ctx) {
// assert second != null || pattern == 0;
// return first.getSingle(ctx)
// .flatMap(f -> pattern == 0
// ? Optional.of(!f)
// : second.getSingle(ctx).map(s -> pattern == 1 ? f || s : f && s)
// )
// .map(val -> new Boolean[]{val})
// .orElse(new Boolean[0]);
// }
@Override
public boolean check(TriggerContext ctx) {
return this.first.check(ctx, firstBoolean -> {
if (this.second == null) {
return firstBoolean;
}
return this.second.check(ctx, secondBoolean -> {
if (this.pattern == 1) {
return firstBoolean || secondBoolean;
}
return firstBoolean && secondBoolean;
});
}, isNegated());
}
@Override
public String toString(TriggerContext ctx, boolean debug) {
if (pattern == 0) {
return "not " + first.toString(ctx, debug);
} else {
assert second != null;
if (pattern == 1) {
return first.toString(ctx, debug) + " or " + second.toString(ctx, debug);
} else {
return first.toString(ctx, debug) + " and " + second.toString(ctx, debug);
}
}
}
}