-
-
Notifications
You must be signed in to change notification settings - Fork 380
/
Copy pathExprItem.java
101 lines (91 loc) · 2.6 KB
/
ExprItem.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
package ch.njol.skript.expressions;
import org.bukkit.entity.Item;
import org.bukkit.event.Event;
import org.bukkit.inventory.ItemStack;
import org.jetbrains.annotations.Nullable;
import ch.njol.skript.aliases.ItemType;
import ch.njol.skript.classes.Changer.ChangeMode;
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.expressions.base.EventValueExpression;
import ch.njol.skript.util.slot.Slot;
@Name("Item")
@Description("The item involved in an event, e.g. in a drop, dispense, pickup or craft event.")
@Examples({
"on dispense:",
"\titem is a clock",
"\tset the time to 6:00"
})
@Since("<i>unknown</i> (before 2.1)")
public class ExprItem extends EventValueExpression<ItemStack> {
static {
register(ExprItem.class, ItemStack.class, "item");
}
public ExprItem() {
super(ItemStack.class);
}
@Nullable
private EventValueExpression<Item> item;
@Nullable
private EventValueExpression<Slot> slot;
@Override
@Nullable
public Class<?>[] acceptChange(ChangeMode mode) {
if (mode == ChangeMode.RESET)
return null;
item = new EventValueExpression<>(Item.class);
if (item.init())
return new Class[] {ItemType.class};
item = null;
slot = new EventValueExpression<>(Slot.class);
if (slot.init())
return new Class[] {ItemType.class};
slot = null;
return null;
}
@Override
public void change(Event event, @Nullable Object[] delta, ChangeMode mode) {
assert mode != ChangeMode.RESET;
ItemType itemType = delta == null ? null : (ItemType) delta[0];
Item item = this.item != null ? this.item.getSingle(event) : null;
Slot slot = this.slot != null ? this.slot.getSingle(event) : null;
if (item == null && slot == null)
return;
ItemStack itemstack = item != null ? item.getItemStack() : slot != null ? slot.getItem() : null;
switch (mode) {
case SET:
assert itemType != null;
itemstack = itemType.getRandom();
break;
case ADD:
case REMOVE:
case REMOVE_ALL:
assert itemType != null;
if (itemType.isOfType(itemstack)) {
if (mode == ChangeMode.ADD)
itemstack = itemType.addTo(itemstack);
else if (mode == ChangeMode.REMOVE)
itemstack = itemType.removeFrom(itemstack);
else
itemstack = itemType.removeAll(itemstack);
}
break;
case DELETE:
itemstack = null;
if (item != null)
item.remove();
break;
case RESET:
assert false;
}
if (item != null && itemstack != null) {
item.setItemStack(itemstack);
} else if (slot != null) {
slot.setItem(itemstack);
} else {
assert false;
}
}
}