-
-
Notifications
You must be signed in to change notification settings - Fork 55
/
InventoryMenuSlot.java
122 lines (110 loc) · 3.82 KB
/
InventoryMenuSlot.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package net.citizensnpcs.api.gui;
import java.util.Arrays;
import java.util.Collection;
import java.util.EnumSet;
import java.util.Set;
import org.bukkit.Material;
import org.bukkit.event.Event.Result;
import org.bukkit.event.inventory.ClickType;
import org.bukkit.event.inventory.InventoryAction;
import org.bukkit.event.inventory.InventoryClickEvent;
import org.bukkit.inventory.Inventory;
import org.bukkit.inventory.ItemFlag;
import org.bukkit.inventory.ItemStack;
import org.bukkit.inventory.meta.ItemMeta;
import net.citizensnpcs.api.util.Colorizer;
import net.citizensnpcs.api.util.Messaging;
/**
* Represents a single inventory slot in a {@link InventoryMenu}.
*/
public class InventoryMenuSlot {
private Set<InventoryAction> actionFilter = EnumSet.allOf(InventoryAction.class);
private final int index;
private final Inventory inventory;
InventoryMenuSlot(MenuContext menu, int i) {
this.inventory = menu.getInventory();
this.index = i;
}
@Override
public boolean equals(Object obj) {
if (this == obj) {
return true;
}
if (obj == null || getClass() != obj.getClass()) {
return false;
}
InventoryMenuSlot other = (InventoryMenuSlot) obj;
if (index != other.index) {
return false;
}
if (inventory == null) {
if (other.inventory != null) {
return false;
}
} else if (!inventory.equals(other.inventory)) {
return false;
}
return true;
}
/**
* @return The set of {@link InventoryAction}s that will be allowed
*/
public Collection<InventoryAction> getFilter() {
return actionFilter;
}
@Override
public int hashCode() {
int result = 31 + index;
return 31 * result + ((inventory == null) ? 0 : inventory.hashCode());
}
void initialise(MenuSlot data) {
ItemStack defaultItem = null;
if (data.compatMaterial().length == 2) {
Material mat = Material.getMaterial(data.compatMaterial()[0]);
if (mat == null) {
mat = Material.getMaterial(data.compatMaterial()[1]);
}
defaultItem = new ItemStack(mat, data.amount());
} else if (data.material() != null) {
defaultItem = new ItemStack(data.material(), data.amount());
}
if (defaultItem != null) {
ItemMeta meta = defaultItem.getItemMeta();
meta.addItemFlags(ItemFlag.HIDE_ATTRIBUTES);
if (!data.lore().equals("EMPTY")) {
meta.setLore(Arrays.asList(Colorizer.parseColors(Messaging.tryTranslate(data.lore())).split("\\n|\n")));
}
if (!data.title().equals("EMPTY")) {
meta.setDisplayName(Colorizer.parseColors(Messaging.tryTranslate(data.title())));
}
defaultItem.setItemMeta(meta);
}
inventory.setItem(index, defaultItem);
setFilter(Arrays.asList(data.filter()));
}
void onClick(InventoryClickEvent event) {
if (!actionFilter.contains(event.getAction())) {
event.setCancelled(true);
event.setResult(Result.DENY);
}
}
/**
* Sets a new {@link ClickType} filter that will only accept clicks with the given type. An empty set is equivalent
* to allowing all click types.
*
* @param filter
* The new filter
*/
public void setFilter(Collection<InventoryAction> filter) {
this.actionFilter = filter == null || filter.isEmpty() ? EnumSet.allOf(InventoryAction.class)
: EnumSet.copyOf(filter);
}
/**
* Manually set the {@link ItemStack} for this slot
*
* @param stack
*/
public void setItemStack(ItemStack stack) {
inventory.setItem(index, stack);
}
}