|
18 | 18 | */
|
19 | 19 | package ch.njol.skript.expressions;
|
20 | 20 |
|
21 |
| -import org.bukkit.event.Event; |
22 |
| -import org.bukkit.inventory.ItemStack; |
23 |
| -import org.eclipse.jdt.annotation.Nullable; |
24 |
| - |
25 | 21 | import ch.njol.skript.Skript;
|
26 | 22 | import ch.njol.skript.aliases.ItemType;
|
27 |
| -import ch.njol.skript.classes.Converter; |
28 | 23 | import ch.njol.skript.doc.Description;
|
29 | 24 | import ch.njol.skript.doc.Examples;
|
30 | 25 | import ch.njol.skript.doc.Name;
|
|
36 | 31 | import ch.njol.skript.lang.Literal;
|
37 | 32 | import ch.njol.skript.lang.SkriptParser.ParseResult;
|
38 | 33 | import ch.njol.util.Kleenean;
|
| 34 | +import org.bukkit.event.Event; |
| 35 | +import org.bukkit.inventory.ItemStack; |
| 36 | +import org.eclipse.jdt.annotation.Nullable; |
39 | 37 |
|
40 |
| -/** |
41 |
| - * @author Peter Güttinger |
42 |
| - */ |
43 | 38 | @Name("X of Item")
|
44 | 39 | @Description("An expression to be able to use a certain amount of items where the amount can be any expression. Please note that this expression is not stable and might be replaced in the future.")
|
45 | 40 | @Examples("give level of player of pickaxes to the player")
|
46 | 41 | @Since("1.2")
|
47 | 42 | public class ExprXOf extends PropertyExpression<Object, Object> {
|
| 43 | + |
48 | 44 | static {
|
49 |
| - Skript.registerExpression(ExprXOf.class, Object.class, ExpressionType.PATTERN_MATCHES_EVERYTHING, "%number% of %itemstacks/entitytype%"); |
| 45 | + Skript.registerExpression(ExprXOf.class, Object.class, ExpressionType.PATTERN_MATCHES_EVERYTHING, "%number% of %itemstacks/itemtypes/entitytype%"); |
50 | 46 | }
|
51 |
| - |
52 |
| - @SuppressWarnings("null") |
53 |
| - Expression<Number> amount; |
54 |
| - |
55 |
| - @SuppressWarnings({"unchecked", "null"}) |
| 47 | + |
| 48 | + private Expression<Number> amount; |
| 49 | + |
56 | 50 | @Override
|
57 |
| - public boolean init(final Expression<?>[] exprs, final int matchedPattern, final Kleenean isDelayed, final ParseResult parseResult) { |
| 51 | + @SuppressWarnings("unchecked") |
| 52 | + public boolean init(Expression<?>[] exprs, int matchedPattern, Kleenean isDelayed, ParseResult parseResult) { |
58 | 53 | setExpr(exprs[1]);
|
59 | 54 | amount = (Expression<Number>) exprs[0];
|
60 |
| - if (amount instanceof Literal && getExpr() instanceof Literal)// "x of y" is also an ItemType syntax |
61 |
| - return false; |
62 |
| - return true; |
63 |
| - } |
64 |
| - |
65 |
| - @Override |
66 |
| - public Class<? extends Object> getReturnType() { |
67 |
| - return getExpr().getReturnType(); |
| 55 | + // "x of y" is also an ItemType syntax |
| 56 | + return !(amount instanceof Literal) || !(getExpr() instanceof Literal); |
68 | 57 | }
|
69 |
| - |
| 58 | + |
70 | 59 | @Override
|
71 |
| - protected Object[] get(final Event e, final Object[] source) { |
72 |
| - return get(source, new Converter<Object, Object>() { |
73 |
| - @Override |
74 |
| - @Nullable |
75 |
| - public Object convert(final Object o) { |
76 |
| - final Number a = amount.getSingle(e); |
77 |
| - if (a == null) |
78 |
| - return null; |
79 |
| - if (o instanceof ItemStack) { |
80 |
| - final ItemStack is = ((ItemStack) o).clone(); |
81 |
| - is.setAmount(a.intValue()); |
82 |
| - return is; |
83 |
| - } else if (o instanceof ItemType) { |
84 |
| - ItemType type = ((ItemType) o).clone(); |
85 |
| - type.setAmount(a.intValue()); |
86 |
| - return type; |
87 |
| - } else { |
88 |
| - final EntityType t = ((EntityType) o).clone(); |
89 |
| - t.amount = a.intValue(); |
90 |
| - return t; |
91 |
| - } |
| 60 | + protected Object[] get(Event e, Object[] source) { |
| 61 | + Number a = amount.getSingle(e); |
| 62 | + if (a == null) |
| 63 | + return new Object[0]; |
| 64 | + |
| 65 | + return get(source, o -> { |
| 66 | + if (o instanceof ItemStack) { |
| 67 | + ItemStack is = ((ItemStack) o).clone(); |
| 68 | + is.setAmount(a.intValue()); |
| 69 | + return is; |
| 70 | + } else if (o instanceof ItemType) { |
| 71 | + ItemType type = ((ItemType) o).clone(); |
| 72 | + type.setAmount(a.intValue()); |
| 73 | + return type; |
| 74 | + } else { |
| 75 | + EntityType t = ((EntityType) o).clone(); |
| 76 | + t.amount = a.intValue(); |
| 77 | + return t; |
92 | 78 | }
|
93 | 79 | });
|
94 | 80 | }
|
95 |
| - |
96 |
| - @SuppressWarnings("unchecked") |
| 81 | + |
97 | 82 | @Override
|
98 |
| - @Nullable |
99 |
| - public <R> Expression<? extends R> getConvertedExpression(Class<R>... to) { |
100 |
| - // Make sure we get converted expression from Variables etc. correctly |
101 |
| - // Then, wrap it so that our 'X' is properly applied |
102 |
| - // See #1747 for issue that was caused by failure to do this |
103 |
| - |
104 |
| - Expression<? extends R> converted = getExpr().getConvertedExpression(to); |
105 |
| - if (converted == null) // Can't create converted expression |
106 |
| - return null; |
107 |
| - |
108 |
| - ExprXOf wrapped = new ExprXOf(); |
109 |
| - wrapped.setExpr(converted); |
110 |
| - wrapped.amount = amount; |
111 |
| - return (Expression<? extends R>) wrapped; |
| 83 | + public Class<?> getReturnType() { |
| 84 | + return getExpr().getReturnType(); |
112 | 85 | }
|
113 |
| - |
| 86 | + |
114 | 87 | @Override
|
115 |
| - public String toString(final @Nullable Event e, final boolean debug) { |
| 88 | + public String toString(@Nullable Event e, boolean debug) { |
116 | 89 | return amount.toString(e, debug) + " of " + getExpr().toString(e, debug);
|
117 | 90 | }
|
118 |
| - |
| 91 | + |
119 | 92 | }
|
0 commit comments