-
-
Notifications
You must be signed in to change notification settings - Fork 353
/
ExprDifference.java
194 lines (164 loc) · 6.46 KB
/
ExprDifference.java
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
/**
* This file is part of Skript.
*
* Skript is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Skript is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Skript. If not, see <http://www.gnu.org/licenses/>.
*
* Copyright Peter Güttinger, SkriptLang team and contributors
*/
package ch.njol.skript.expressions;
import ch.njol.skript.Skript;
import ch.njol.skript.classes.Arithmetic;
import ch.njol.skript.classes.ClassInfo;
import ch.njol.skript.conditions.CondCompare;
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.Expression;
import ch.njol.skript.lang.ExpressionType;
import ch.njol.skript.lang.SkriptParser.ParseResult;
import ch.njol.skript.lang.util.SimpleExpression;
import ch.njol.skript.registrations.Classes;
import ch.njol.skript.util.LiteralUtils;
import ch.njol.skript.util.Utils;
import ch.njol.util.Kleenean;
import org.bukkit.event.Event;
import org.eclipse.jdt.annotation.Nullable;
import org.skriptlang.skript.lang.arithmetic.Arithmetics;
import org.skriptlang.skript.lang.arithmetic.DifferenceInfo;
import java.lang.reflect.Array;
@Name("Difference")
@Description({
"The difference between two values",
"Supported types include <a href='./classes.html#number'>numbers</a>, <a href='./classes/#date'>dates</a> and <a href='./classes/#time'>times</a>."
})
@Examples({
"if difference between {command::%player%::lastuse} and now is smaller than a minute:",
"\tmessage \"You have to wait a minute before using this command again!\""
})
@Since("1.4")
public class ExprDifference extends SimpleExpression<Object> {
static {
Skript.registerExpression(ExprDifference.class, Object.class, ExpressionType.COMBINED,
"difference (between|of) %object% and %object%"
);
}
@SuppressWarnings("NotNullFieldNotInitialized")
private Expression<?> first, second;
@Nullable
@SuppressWarnings("rawtypes")
private DifferenceInfo differenceInfo;
@Override
@SuppressWarnings("unchecked")
public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) {
Expression<?> first = LiteralUtils.defendExpression(exprs[0]);
Expression<?> second = LiteralUtils.defendExpression(exprs[1]);
if (!LiteralUtils.canInitSafely(first, second)) {
return false;
}
Class<?> firstReturnType = first.getReturnType();
Class<?> secondReturnType = second.getReturnType();
Class<?> superType = Utils.getSuperType(firstReturnType, secondReturnType);
boolean fail = false;
if (superType == Object.class && (firstReturnType != Object.class || secondReturnType != Object.class)) {
// We may not have a way to obtain the difference between these two values. Further checks needed.
// These two types are unrelated, meaning conversion is needed
if (firstReturnType != Object.class && secondReturnType != Object.class) {
// We will work our way out of failure
fail = true;
// Attempt to use first type's math
differenceInfo = Arithmetics.getDifferenceInfo(firstReturnType);
if (differenceInfo != null) { // Try to convert second to first
Expression<?> secondConverted = second.getConvertedExpression(firstReturnType);
if (secondConverted != null) {
second = secondConverted;
fail = false;
}
}
if (fail) { // First type won't work, try second type
differenceInfo = Arithmetics.getDifferenceInfo(secondReturnType);
if (differenceInfo != null) { // Try to convert first to second
Expression<?> firstConverted = first.getConvertedExpression(secondReturnType);
if (firstConverted != null) {
first = firstConverted;
fail = false;
}
}
}
} else { // It may just be the case that the type of one of our values cannot be known at parse time
Expression<?> converted;
if (firstReturnType == Object.class) {
converted = first.getConvertedExpression(secondReturnType);
if (converted != null) { // This may fail if both types are Object
first = converted;
}
} else { // This is an else statement to avoid X->Object conversions
converted = second.getConvertedExpression(firstReturnType);
if (converted != null) {
second = converted;
}
}
if (converted == null) { // It's unlikely that these two can be compared
fail = true;
} else { // Attempt to resolve a better class info
superType = Utils.getSuperType(first.getReturnType(), second.getReturnType());
}
}
}
if (superType != Object.class && (differenceInfo = Arithmetics.getDifferenceInfo(superType)) == null) {
fail = true;
}
if (fail) {
Skript.error("Can't get the difference of " + CondCompare.f(first) + " and " + CondCompare.f(second));
return false;
}
this.first = first;
this.second = second;
return true;
}
@Override
@Nullable
@SuppressWarnings("unchecked")
protected Object[] get(Event event) {
Object first = this.first.getSingle(event);
Object second = this.second.getSingle(event);
if (first == null || second == null) {
return new Object[0];
}
DifferenceInfo differenceInfo = this.differenceInfo;
if (differenceInfo == null) { // Try to determine now that actual types are known
Class<?> superType = Utils.getSuperType(first.getClass(), second.getClass());
differenceInfo = Arithmetics.getDifferenceInfo(superType);
if (differenceInfo == null) { // User did something stupid, just return <none> for them
return new Object[0];
}
}
assert differenceInfo != null; // it cannot be null here
Object[] one = (Object[]) Array.newInstance(differenceInfo.getReturnType(), 1);
one[0] = differenceInfo.getOperation().calculate(first, second);
return one;
}
@Override
public boolean isSingle() {
return true;
}
@Override
public Class<?> getReturnType() {
return differenceInfo == null ? Object.class : differenceInfo.getReturnType();
}
@Override
public String toString(@Nullable Event event, boolean debug) {
return "difference between " + first.toString(event, debug) + " and " + second.toString(event, debug);
}
}