-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
InventoryHolder.java
222 lines (191 loc) · 7.14 KB
/
InventoryHolder.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
package net.aufdemrand.denizen.objects.properties.inventory;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizen.utilities.depends.Depends;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.Mechanism;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.objects.dScript;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.tags.Attribute;
import net.citizensnpcs.api.CitizensAPI;
import org.bukkit.Bukkit;
import org.bukkit.block.BlockState;
import org.bukkit.block.DoubleChest;
import org.bukkit.entity.Entity;
import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryType;
import org.bukkit.inventory.CraftingInventory;
import org.bukkit.inventory.Inventory;
public class InventoryHolder implements Property {
public static boolean describes(dObject inventory) {
// All inventories should have a holder
return inventory instanceof dInventory;
}
public static InventoryHolder getFrom(dObject inventory) {
if (!describes(inventory)) {
return null;
}
return new InventoryHolder((dInventory) inventory);
}
public static final String[] handledTags = new String[]{
"id_holder"
};
public static final String[] handledMechs = new String[] {
"holder"
};
///////////////////
// Instance Fields and Methods
/////////////
dInventory inventory;
dObject holder;
public InventoryHolder(dInventory inventory) {
this.inventory = inventory;
this.holder = getHolder();
}
public dObject getHolder() {
if (inventory.getInventory() == null) {
return null;
}
if (inventory.getIdType() != null
&& (inventory.getIdType().equals("player") || inventory.getIdType().equals("enderchest"))) {
return dPlayer.valueOf(inventory.getIdHolder());
}
else if (inventory.getIdType() != null && inventory.getIdType().equalsIgnoreCase("script")
&& dScript.matches(inventory.getIdHolder())) {
return dScript.valueOf(inventory.getIdHolder());
}
org.bukkit.inventory.InventoryHolder holder = inventory.getInventory().getHolder();
if (holder != null) {
if (holder instanceof dNPC) {
return (dNPC) holder;
}
else if (holder instanceof Player) {
if (Depends.citizens != null && CitizensAPI.getNPCRegistry().isNPC((Player) holder)) {
return new dNPC(CitizensAPI.getNPCRegistry().getNPC((Player) holder));
}
return new dPlayer((Player) holder);
}
else if (holder instanceof Entity) {
return new dEntity((Entity) holder);
}
else if (holder instanceof DoubleChest) {
return new dLocation(((DoubleChest) holder).getLocation());
}
else if (holder instanceof BlockState) {
return new dLocation(((BlockState) holder).getLocation());
}
}
else {
return new Element(inventory.getIdHolder());
}
return null;
}
public void setHolder(dPlayer player) {
if (inventory.getIdType().equals("enderchest")) {
inventory.setInventory(player.getBukkitEnderChest(), player);
}
else if (inventory.getIdType().equals("workbench")) {
inventory.setInventory(player.getBukkitWorkbench(), player);
}
else if (inventory.getIdType().equals("crafting")) {
Inventory opened = player.getPlayerEntity().getOpenInventory().getTopInventory();
if (opened instanceof CraftingInventory) {
inventory.setInventory(opened, player);
}
else {
inventory.setIdType("player");
inventory.setInventory(player.getBukkitInventory(), player);
}
}
else {
inventory.setInventory(player.getBukkitInventory(), player);
}
}
public void setHolder(dNPC npc) {
inventory.setInventory(npc.getInventory());
}
public void setHolder(dEntity entity) {
inventory.setInventory(entity.getBukkitInventory());
}
public void setHolder(dLocation location) {
inventory.setInventory(location.getBukkitInventory());
}
public void setHolder(Element element) {
if (element.matchesEnum(InventoryType.values())) {
inventory.setInventory(Bukkit.getServer().createInventory(null,
InventoryType.valueOf(element.asString().toUpperCase())));
}
}
/////////
// Property Methods
///////
@Override
public String getPropertyString() {
if (holder == null || (inventory.getIdType().equals("generic")
&& inventory.getIdHolder().equals("CHEST"))) {
return null;
}
else {
return holder.identify();
}
}
@Override
public String getPropertyId() {
return "holder";
}
///////////
// dObject Attributes
////////
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <in@inventory.id_holder>
// @returns dObject
// @group properties
// @mechanism dInventory.holder
// @description
// Returns Denizen's holder ID for this inventory. (p@aufdemrand, l@123,321,123, etc.)
// -->
if (attribute.startsWith("id_holder")) {
if (holder == null) {
return null;
}
return holder.getAttribute(attribute.fulfill(1));
}
return null;
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object dInventory
// @name holder
// @input dObject
// @description
// Changes the holder of the dInventory, therefore completely reconfiguring
// the inventory to that of the holder.
// @tags
// <in@inventory.id_holder>
// -->
if (mechanism.matches("holder")) {
Element value = mechanism.getValue();
if (value.matchesEnum(InventoryType.values())) {
setHolder(value);
}
else if (value.matchesType(dPlayer.class)) {
setHolder(value.asType(dPlayer.class));
}
else if (Depends.citizens != null && value.matchesType(dNPC.class)) {
setHolder(value.asType(dNPC.class));
}
else if (value.matchesType(dEntity.class)) {
setHolder(value.asType(dEntity.class));
}
else if (value.matchesType(dLocation.class)) {
setHolder(value.asType(dLocation.class));
}
}
}
}