-
-
Notifications
You must be signed in to change notification settings - Fork 104
/
InventoryHolder.java
224 lines (193 loc) · 7.41 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
223
224
package com.denizenscript.denizen.objects.properties.inventory;
import com.denizenscript.denizen.objects.*;
import com.denizenscript.denizen.utilities.depends.Depends;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.ScriptTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.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(ObjectTag inventory) {
// All inventories should have a holder
return inventory instanceof InventoryTag;
}
public static InventoryHolder getFrom(ObjectTag inventory) {
if (!describes(inventory)) {
return null;
}
return new InventoryHolder((InventoryTag) inventory);
}
public static final String[] handledTags = new String[] {
"id_holder"
};
public static final String[] handledMechs = new String[] {
"holder"
};
///////////////////
// Instance Fields and Methods
/////////////
InventoryTag inventory;
ObjectTag holder;
public InventoryHolder(InventoryTag inventory) {
this.inventory = inventory;
this.holder = getHolder();
}
public ObjectTag getHolder() {
if (inventory.getInventory() == null) {
return null;
}
if (inventory.getIdType() != null
&& (inventory.getIdType().equals("player") || inventory.getIdType().equals("enderchest"))) {
return PlayerTag.valueOf(inventory.getIdHolder());
}
else if (inventory.getIdType() != null && inventory.getIdType().equalsIgnoreCase("script")
&& ScriptTag.matches(inventory.getIdHolder())) {
return ScriptTag.valueOf(inventory.getIdHolder());
}
org.bukkit.inventory.InventoryHolder holder = inventory.getInventory().getHolder();
if (holder != null) {
if (holder instanceof NPCTag) {
return (NPCTag) holder;
}
else if (holder instanceof Player) {
if (Depends.citizens != null && CitizensAPI.getNPCRegistry().isNPC((Player) holder)) {
return new NPCTag(CitizensAPI.getNPCRegistry().getNPC((Player) holder));
}
return new PlayerTag((Player) holder);
}
else if (holder instanceof Entity) {
return new EntityTag((Entity) holder);
}
else if (holder instanceof DoubleChest) {
return new LocationTag(((DoubleChest) holder).getLocation());
}
else if (holder instanceof BlockState) {
return new LocationTag(((BlockState) holder).getLocation());
}
}
else {
return new ElementTag(inventory.getIdHolder());
}
return null;
}
public void setHolder(PlayerTag 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(NPCTag npc) {
inventory.setInventory(npc.getInventory());
}
public void setHolder(EntityTag entity) {
inventory.setInventory(entity.getBukkitInventory());
}
public void setHolder(LocationTag location) {
if (!location.isChunkLoaded()) {
return;
}
inventory.setInventory(location.getBukkitInventory());
}
public void setHolder(ElementTag 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";
}
///////////
// ObjectTag Attributes
////////
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <InventoryTag.id_holder>
// @returns ObjectTag
// @group properties
// @mechanism InventoryTag.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 InventoryTag
// @name holder
// @input ObjectTag
// @description
// Changes the holder of the InventoryTag, therefore completely reconfiguring
// the inventory to that of the holder.
// @tags
// <InventoryTag.id_holder>
// -->
if (mechanism.matches("holder")) {
if (mechanism.getValue().matchesEnum(InventoryType.values())) {
setHolder(mechanism.getValue());
}
else if (mechanism.getValue().matchesType(PlayerTag.class)) {
setHolder(mechanism.valueAsType(PlayerTag.class));
}
else if (Depends.citizens != null && mechanism.getValue().matchesType(NPCTag.class)) {
setHolder(mechanism.valueAsType(NPCTag.class));
}
else if (mechanism.getValue().matchesType(EntityTag.class)) {
setHolder(mechanism.valueAsType(EntityTag.class));
}
else if (mechanism.getValue().matchesType(LocationTag.class)) {
setHolder(mechanism.valueAsType(LocationTag.class));
}
}
}
}