|
9 | 9 | import ch.njol.skript.lang.ExpressionType; |
10 | 10 | import ch.njol.skript.lang.SkriptParser; |
11 | 11 | import ch.njol.skript.lang.util.SimpleExpression; |
12 | | -import org.skriptlang.skript.lang.converter.Converters; |
13 | 12 | import ch.njol.skript.util.LiteralUtils; |
14 | 13 | import ch.njol.skript.util.Utils; |
15 | 14 | import ch.njol.util.Kleenean; |
16 | 15 | import org.bukkit.event.Event; |
17 | | -import org.jetbrains.annotations.Nullable; |
18 | 16 |
|
19 | | -import java.lang.reflect.Array; |
| 17 | +import java.util.Arrays; |
| 18 | +import java.util.Collections; |
| 19 | +import java.util.HashSet; |
| 20 | +import java.util.Set; |
20 | 21 |
|
21 | 22 | @Name("Default Value") |
22 | 23 | @Description("A shorthand expression for giving things a default value. If the first thing isn't set, the second thing will be returned.") |
23 | 24 | @Examples({"broadcast {score::%player's uuid%} otherwise \"%player% has no score!\""}) |
24 | 25 | @Since("2.2-dev36") |
25 | | -@SuppressWarnings("null") |
26 | | -public class ExprDefaultValue<T> extends SimpleExpression<T> { |
| 26 | +public class ExprDefaultValue extends SimpleExpression<Object> { |
27 | 27 |
|
28 | 28 | static { |
29 | 29 | Skript.registerExpression(ExprDefaultValue.class, Object.class, ExpressionType.COMBINED, |
30 | 30 | "%objects% (otherwise|?) %objects%"); |
31 | 31 | } |
32 | 32 |
|
33 | | - private final ExprDefaultValue<?> source; |
34 | | - private final Class<? extends T>[] types; |
35 | | - private final Class<T> superType; |
36 | | - @Nullable |
37 | | - private Expression<Object> first; |
38 | | - @Nullable |
39 | | - private Expression<Object> second; |
| 33 | + private Class<?>[] types; |
| 34 | + private Class<?> superType; |
40 | 35 |
|
41 | | - @SuppressWarnings("unchecked") |
42 | | - public ExprDefaultValue() { |
43 | | - this(null, (Class<? extends T>) Object.class); |
44 | | - } |
45 | | - |
46 | | - @SuppressWarnings("unchecked") |
47 | | - private ExprDefaultValue(ExprDefaultValue<?> source, Class<? extends T>... types) { |
48 | | - this.source = source; |
49 | | - if (source != null) { |
50 | | - this.first = source.first; |
51 | | - this.second = source.second; |
52 | | - } |
53 | | - this.types = types; |
54 | | - this.superType = (Class<T>) Utils.getSuperType(types); |
55 | | - } |
| 36 | + private Expression<Object> values; |
| 37 | + private Expression<Object> defaultValues; |
56 | 38 |
|
57 | 39 | @Override |
58 | 40 | public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, SkriptParser.ParseResult parseResult) { |
59 | | - first = LiteralUtils.defendExpression(exprs[0]); |
60 | | - second = LiteralUtils.defendExpression(exprs[1]); |
61 | | - return LiteralUtils.canInitSafely(first, second); |
62 | | - } |
63 | | - |
64 | | - @Override |
65 | | - @SuppressWarnings("unchecked") |
66 | | - protected T[] get(Event e) { |
67 | | - Object[] first = this.first.getArray(e); |
68 | | - Object values[] = first.length != 0 ? first : second.getArray(e); |
69 | | - try { |
70 | | - return Converters.convert(values, types, superType); |
71 | | - } catch (ClassCastException e1) { |
72 | | - return (T[]) Array.newInstance(superType, 0); |
| 41 | + values = LiteralUtils.defendExpression(exprs[0]); |
| 42 | + defaultValues = LiteralUtils.defendExpression(exprs[1]); |
| 43 | + if (!LiteralUtils.canInitSafely(values, defaultValues)) { |
| 44 | + return false; |
73 | 45 | } |
| 46 | + |
| 47 | + Set<Class<?>> types = new HashSet<>(); |
| 48 | + Collections.addAll(types, values.possibleReturnTypes()); |
| 49 | + Collections.addAll(types, defaultValues.possibleReturnTypes()); |
| 50 | + this.types = types.toArray(new Class<?>[0]); |
| 51 | + this.superType = Utils.getSuperType(this.types); |
| 52 | + |
| 53 | + return true; |
74 | 54 | } |
75 | 55 |
|
76 | 56 | @Override |
77 | | - public <R> Expression<? extends R> getConvertedExpression(Class<R>... to) { |
78 | | - return new ExprDefaultValue<>(this, to); |
| 57 | + protected Object[] get(Event event) { |
| 58 | + Object[] values = this.values.getArray(event); |
| 59 | + if (values.length != 0) { |
| 60 | + return values; |
| 61 | + } |
| 62 | + return defaultValues.getArray(event); |
79 | 63 | } |
80 | 64 |
|
81 | 65 | @Override |
82 | | - public Expression<?> getSource() { |
83 | | - return source == null ? this : source; |
| 66 | + public boolean isSingle() { |
| 67 | + return values.isSingle() && defaultValues.isSingle(); |
84 | 68 | } |
85 | 69 |
|
86 | 70 | @Override |
87 | | - public Class<? extends T> getReturnType() { |
| 71 | + public Class<?> getReturnType() { |
88 | 72 | return superType; |
89 | 73 | } |
90 | 74 |
|
91 | 75 | @Override |
92 | | - public boolean isSingle() { |
93 | | - return first.isSingle() && second.isSingle(); |
| 76 | + public Class<?>[] possibleReturnTypes() { |
| 77 | + return Arrays.copyOf(types, types.length); |
94 | 78 | } |
95 | 79 |
|
96 | 80 | @Override |
97 | | - public String toString(Event e, boolean debug) { |
98 | | - return first.toString(e, debug) + " or else " + second.toString(e, debug); |
| 81 | + public String toString(Event event, boolean debug) { |
| 82 | + return values.toString(event, debug) + " or else " + defaultValues.toString(event, debug); |
99 | 83 | } |
100 | 84 |
|
101 | 85 | } |
0 commit comments