Skip to content

Commit f858c4a

Browse files
authored
Adds Explicit ItemStack Support to ExprItemAmount, fixing issue with item amount of variables. (#5513)
1 parent 157b896 commit f858c4a

File tree

2 files changed

+76
-42
lines changed

2 files changed

+76
-42
lines changed

src/main/java/ch/njol/skript/expressions/ExprItemAmount.java

Lines changed: 49 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919
package ch.njol.skript.expressions;
2020

2121
import ch.njol.skript.aliases.ItemType;
22-
import ch.njol.skript.classes.Changer;
2322
import ch.njol.skript.classes.Changer.ChangeMode;
2423
import ch.njol.skript.doc.Description;
2524
import ch.njol.skript.doc.Examples;
@@ -29,71 +28,80 @@
2928
import ch.njol.skript.util.slot.Slot;
3029
import ch.njol.util.coll.CollectionUtils;
3130
import org.bukkit.event.Event;
31+
import org.bukkit.inventory.ItemStack;
3232
import org.eclipse.jdt.annotation.Nullable;
3333

3434
@Name("Item Amount")
3535
@Description("The amount of an <a href='classes.html#itemstack'>item stack</a>.")
3636
@Examples("send \"You have got %item amount of player's tool% %player's tool% in your hand!\" to player")
3737
@Since("2.2-dev24")
3838
public class ExprItemAmount extends SimplePropertyExpression<Object, Long> {
39-
40-
static {
41-
register(ExprItemAmount.class, Long.class, "item[[ ]stack] (amount|size|number)", "slots/itemtypes");
42-
}
4339

44-
40+
static {
41+
register(ExprItemAmount.class, Long.class, "item[[ ]stack] (amount|size|number)", "slots/itemtypes/itemstacks");
42+
}
43+
4544
@Override
4645
public Long convert(final Object item) {
47-
return (long) (item instanceof ItemType ? ((ItemType) item).getAmount() : ((Slot) item).getAmount());
46+
if (item instanceof ItemType) {
47+
return (long) ((ItemType) item).getAmount();
48+
} else if (item instanceof Slot) {
49+
return (long) ((Slot) item).getAmount();
50+
} else {
51+
return (long) ((ItemStack) item).getAmount();
52+
}
4853
}
49-
54+
5055
@Override
51-
public @Nullable Class<?>[] acceptChange(Changer.ChangeMode mode) {
52-
return (mode != ChangeMode.REMOVE_ALL) ? CollectionUtils.array(Number.class) : null;
53-
}
56+
@Nullable
57+
public Class<?>[] acceptChange(ChangeMode mode) {
58+
switch (mode) {
59+
case SET:
60+
case ADD:
61+
case RESET:
62+
case DELETE:
63+
case REMOVE:
64+
return CollectionUtils.array(Long.class);
65+
}
66+
return null;
67+
}
5468

55-
@Override
56-
public void change(Event event, @Nullable Object[] delta, Changer.ChangeMode mode) {
57-
int amount = delta != null ? ((Number) delta[0]).intValue() : 0;
58-
switch (mode) {
59-
case ADD:
60-
for (Object obj : getExpr().getArray(event))
69+
@Override
70+
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
71+
int amount = delta != null ? ((Number) delta[0]).intValue() : 0;
72+
switch (mode) {
73+
case REMOVE:
74+
amount = -amount;
75+
// fall through
76+
case ADD:
77+
for (Object obj : getExpr().getArray(event))
6178
if (obj instanceof ItemType) {
6279
ItemType item = ((ItemType) obj);
6380
item.setAmount(item.getAmount() + amount);
64-
} else {
81+
} else if (obj instanceof Slot) {
6582
Slot slot = ((Slot) obj);
6683
slot.setAmount(slot.getAmount() + amount);
84+
} else {
85+
ItemStack item = ((ItemStack) obj);
86+
item.setAmount(item.getAmount() + amount);
6787
}
68-
break;
69-
case SET:
88+
break;
89+
case RESET:
90+
case DELETE:
91+
amount = 1;
92+
// fall through
93+
case SET:
7094
for (Object obj : getExpr().getArray(event))
71-
if (obj instanceof ItemType)
95+
if (obj instanceof ItemType) {
7296
((ItemType) obj).setAmount(amount);
73-
else
97+
} else if (obj instanceof Slot) {
7498
((Slot) obj).setAmount(amount);
75-
break;
76-
case REMOVE:
77-
for (Object obj : getExpr().getArray(event))
78-
if (obj instanceof ItemType) {
79-
ItemType item = ((ItemType) obj);
80-
item.setAmount(item.getAmount() - amount);
8199
} else {
82-
Slot slot = ((Slot) obj);
83-
slot.setAmount(slot.getAmount() - amount);
100+
((ItemStack) obj).setAmount(amount);
84101
}
85-
break;
86-
case REMOVE_ALL:
87-
case RESET:
88-
case DELETE:
89-
for (Object obj : getExpr().getArray(event))
90-
if (obj instanceof ItemType)
91-
((ItemType) obj).setAmount(1);
92-
else
93-
((Slot) obj).setAmount(1);
94102
break;
95-
}
96-
}
103+
}
104+
}
97105

98106
@Override
99107
public Class<? extends Long> getReturnType() {
@@ -104,5 +112,4 @@ public Class<? extends Long> getReturnType() {
104112
protected String getPropertyName() {
105113
return "item[[ ]stack] (amount|size|number)";
106114
}
107-
108115
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
test "item amount of itemstack":
2+
set {_loc} to spawn of world "world"
3+
set {_old-block} to type of block at {_loc}
4+
set block at {_loc} to chest
5+
add 10 stone to inventory of block at {_loc}
6+
set {_item} to slot 0 of inventory of block at {_loc}
7+
assert item amount of {_item} is 10 with "Amount of itemstack is not 10: %{_amount}%"
8+
remove 1 from item amount of {_item}
9+
assert item amount of {_item} is 9 with "Amount of itemstack is not 9: %{_amount}%"
10+
set block at {_loc} to {_old-block}
11+
12+
test "item amount of itemtype":
13+
set {_item} to 10 stone
14+
set {_amount} to item amount of {_item}
15+
assert item amount of {_item} is 10 with "Amount of itemtype is not 10: %{_amount}%"
16+
remove 1 from item amount of {_item}
17+
assert item amount of {_item} is 9 with "Amount of itemtype is not 9: %{_amount}%"
18+
19+
test "item amount of slot":
20+
set {_loc} to spawn of world "world"
21+
set {_old-block} to type of block at {_loc}
22+
set block at {_loc} to chest
23+
add 10 stone to inventory of block at {_loc}
24+
assert item amount of (slot 0 of inventory of block at {_loc}) is 10 with "Amount of slot is not 10: %{_amount}%"
25+
remove 1 from item amount of (slot 0 of inventory of block at {_loc})
26+
assert item amount of (slot 0 of inventory of block at {_loc}) is 9 with "Amount of slot is not 9: %{_amount}%"
27+
set block at {_loc} to {_old-block}

0 commit comments

Comments
 (0)