Skip to content

Commit bcebdef

Browse files
authoredAug 16, 2021
Fix ExprXOf CCE (#4261)
1 parent 7a1929a commit bcebdef

File tree

1 file changed

+37
-64
lines changed

1 file changed

+37
-64
lines changed
 

‎src/main/java/ch/njol/skript/expressions/ExprXOf.java

+37-64
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,8 @@
1818
*/
1919
package ch.njol.skript.expressions;
2020

21-
import org.bukkit.event.Event;
22-
import org.bukkit.inventory.ItemStack;
23-
import org.eclipse.jdt.annotation.Nullable;
24-
2521
import ch.njol.skript.Skript;
2622
import ch.njol.skript.aliases.ItemType;
27-
import ch.njol.skript.classes.Converter;
2823
import ch.njol.skript.doc.Description;
2924
import ch.njol.skript.doc.Examples;
3025
import ch.njol.skript.doc.Name;
@@ -36,84 +31,62 @@
3631
import ch.njol.skript.lang.Literal;
3732
import ch.njol.skript.lang.SkriptParser.ParseResult;
3833
import ch.njol.util.Kleenean;
34+
import org.bukkit.event.Event;
35+
import org.bukkit.inventory.ItemStack;
36+
import org.eclipse.jdt.annotation.Nullable;
3937

40-
/**
41-
* @author Peter Güttinger
42-
*/
4338
@Name("X of Item")
4439
@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.")
4540
@Examples("give level of player of pickaxes to the player")
4641
@Since("1.2")
4742
public class ExprXOf extends PropertyExpression<Object, Object> {
43+
4844
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%");
5046
}
51-
52-
@SuppressWarnings("null")
53-
Expression<Number> amount;
54-
55-
@SuppressWarnings({"unchecked", "null"})
47+
48+
private Expression<Number> amount;
49+
5650
@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) {
5853
setExpr(exprs[1]);
5954
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);
6857
}
69-
58+
7059
@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;
9278
}
9379
});
9480
}
95-
96-
@SuppressWarnings("unchecked")
81+
9782
@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();
11285
}
113-
86+
11487
@Override
115-
public String toString(final @Nullable Event e, final boolean debug) {
88+
public String toString(@Nullable Event e, boolean debug) {
11689
return amount.toString(e, debug) + " of " + getExpr().toString(e, debug);
11790
}
118-
91+
11992
}

0 commit comments

Comments
 (0)