-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
Item.java
330 lines (263 loc) · 10.8 KB
/
Item.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
package net.aufdemrand.denizen.utilities.arguments;
import net.aufdemrand.denizen.interfaces.dScriptArgument;
import net.aufdemrand.denizen.npc.dNPC;
import net.aufdemrand.denizen.scripts.ScriptRegistry;
import net.aufdemrand.denizen.scripts.containers.core.ItemScriptContainer;
import net.aufdemrand.denizen.tags.Attribute;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizen.utilities.nbt.NBTItem;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.craftbukkit.v1_5_R2.inventory.CraftItemStack;
import org.bukkit.entity.Player;
import org.bukkit.inventory.ItemStack;
import java.lang.reflect.Constructor;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Item extends ItemStack implements dScriptArgument {
/////////////////////
// STATIC METHODS
/////////////////
// Patterns used in valueOf...
// TODO: Make prettier.. maybe an array of Patterns isn't even necessary anymore.
// Seperate them out instead?
final static Pattern[] getItemPtrn = {
Pattern.compile("(?:(?:.+?:)|)(\\d+):(\\d+)"),
Pattern.compile("(?:(?:.+?:)|)(\\d+)"),
Pattern.compile("(?:(?:.+?:)|)([a-zA-Z\\x5F]+?):(\\d+)"),
Pattern.compile("(?:(?:.+?:)|)([a-zA-Z\\x5F]+)"),
Pattern.compile("(?:(?:.+?:)|)item\\.(.+)", Pattern.CASE_INSENSITIVE),
Pattern.compile("(?:(?:.+?:)|)(.+)"),
};
/**
* Gets a Item Object from a string form.
*
* @param string the string or dScript argument String
* @return an Item, or null if incorrectly formatted
*
*/
public static Item valueOf(String string) {
return valueOf(string, null, null);
}
public static Item valueOf(String string, Player player, dNPC npc) {
if (string == null) return null;
Matcher[] m = new Matcher[4];
Item stack = null;
// Check if a saved item instance from NEW
m[0] = getItemPtrn[4].matcher(string);
if (m[0].matches()) {
// TODO: Finish NEW command.
}
// Check traditional item patterns.
m[0] = getItemPtrn[0].matcher(string);
m[1] = getItemPtrn[1].matcher(string);
m[2] = getItemPtrn[2].matcher(string);
m[3] = getItemPtrn[3].matcher(string);
try {
// Match 'ItemId:Data'
if (m[0].matches()) {
stack = new Item(Integer.valueOf(m[0].group(1)));
stack.setDurability(Short.valueOf(m[0].group(2)));
return stack.setId(stack.getType().name());
// Match 'ItemId'
} else if (m[1].matches()) {
stack = new Item(Integer.valueOf(m[1].group(1)));
stack.setId(stack.getType().name());
return stack;
// Match 'Material:Data'
} else if (m[2].matches()) {
stack = new Item(Material.valueOf(m[2].group(1).toUpperCase()));
stack.setDurability(Short.valueOf(m[2].group(2)));
return stack.setId(stack.getType().name());
// Match 'Material'
} else if (m[3].matches()) {
stack = new Item(Material.valueOf(m[3].group(1).toUpperCase()));
stack.setId(stack.getType().name());
return stack;
}
} catch (Exception e) {
// Just a catch, might be an item script...
}
// Check custom item script
m[0] = getItemPtrn[5].matcher(string);
if (m[0].matches() && ScriptRegistry.containsScript(m[0].group(1), ItemScriptContainer.class)) {
// Get item from script
return ScriptRegistry.getScriptContainerAs(m[0].group(1), ItemScriptContainer.class).getItemFrom(player, npc);
}
// No match.
// dB.echoError("Invalid item! Failed to find a matching Item type.");
return stack;
}
/////////////////////
// INSTANCE METHODS
/////////////////
private String id;
private String prefix = "Item";
public Item(Material material) {
super(material);
setId(getType().name());
}
public Item(int itemId) {
super(itemId);
setId(getType().name());
}
public Item(Material material, int qty) {
super(material, qty);
setId(getType().name());
}
public Item(int type, int qty) {
super(type, qty);
setId(getType().name());
}
public Item(ItemStack item) {
super(item);
setId(getType().name());
}
public ItemStack toCraftBukkit() {
if (!((ItemStack) this instanceof CraftItemStack)) {
try {
// Use reflection to grant access to CraftItemStack constructer
// which is not public
Constructor<CraftItemStack> con = CraftItemStack.class
.getDeclaredConstructor(org.bukkit.inventory.ItemStack.class);
con.setAccessible(true);
return con.newInstance((ItemStack) this);
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
public Item setId(String id) {
if (NBTItem.hasCustomNBT(this, "denizen-item-id"))
this.id = NBTItem.getCustomNBT(this, "denizen-custom-item-id");
else {
this.id = id.toUpperCase();
NBTItem.addCustomNBT(this, "denizen-item-id", id);
}
return this;
}
public boolean matches(ItemStack item) {
return matches(new Item(item));
}
public boolean matches(Item item) {
// Check IDs
if (!id.equalsIgnoreCase(item.id))
return false;
return true;
}
//////////////////////////////
// DSCRIPT ARGUMENT METHODS
/////////////////////////
@Override
public String getDefaultPrefix() {
return prefix;
}
@Override
public String debug() {
return "<G>" + prefix + "='<Y>" + id + " seconds<G>' ";
}
@Override
public String as_dScriptArg() {
return prefix + ":" + id;
}
public String dScriptArgValue() {
return getDefaultPrefix().toLowerCase() + ":" + as_dScriptArg();
}
@Override
public String toString() {
return serialize().toString();
}
@Override
public dScriptArgument setPrefix(String prefix) {
this.prefix = prefix;
return this;
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return null;
// Desensitize the attribute for comparison
String id = this.id.toLowerCase();
if (attribute.startsWith("qty"))
return new Element(String.valueOf(getAmount()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("id"))
return new Element(id)
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("typeid"))
return new Element(String.valueOf(getTypeId()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("max_stack"))
return new Element(String.valueOf(getMaxStackSize()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("data"))
return new Element(String.valueOf(getData().getData()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("durability"))
return new Element(String.valueOf(getDurability()))
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("material.formatted")) {
if (id.equals("air"))
return new Element("nothing")
.getAttribute(attribute.fulfill(2));
if (id.equals("ice") || id.equals("dirt"))
return new Element(id)
.getAttribute(attribute.fulfill(2));
if (getAmount() > 1) {
if (id.equals("cactus"))
return new Element("cactuses")
.getAttribute(attribute.fulfill(2));
if (id.endsWith("y"))
return new Element(id.substring(0, id.length() - 1) + "ies")
.getAttribute(attribute.fulfill(2)); // ex: lily -> lilies
if (id.endsWith("s"))
return new Element(id)
.getAttribute(attribute.fulfill(2)); // ex: shears -> shears
// else
return new Element(id + "s")
.getAttribute(attribute.fulfill(2)); // iron sword -> iron swords
} else {
if (id.equals("cactus")) return new Element("a cactus").getAttribute(attribute.fulfill(2));
if (id.endsWith("s")) return new Element(id).getAttribute(attribute.fulfill(2));
if (id.startsWith("a") || id.startsWith("e") || id.startsWith("i")
|| id.startsWith("o") || id.startsWith("u"))
return new Element("an " + id)
.getAttribute(attribute.fulfill(2));// ex: emerald -> an emerald
// else
return new Element("a " + id)
.getAttribute(attribute.fulfill(2));// ex: diamond -> a diamond
}
}
if (attribute.startsWith("material"))
return new Element(getType().toString())
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("display"))
if (hasItemMeta() && getItemMeta().hasDisplayName())
return new Element(getItemMeta().getDisplayName())
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("enchantments")) {
}
if (attribute.startsWith("lore")) {
if (hasItemMeta() && getItemMeta().hasLore())
return new List(getItemMeta().getLore()).getAttribute(attribute.fulfill(1));
else return new List("Empty List", "").getAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("prefix"))
return new Element(prefix)
.getAttribute(attribute.fulfill(1));
if (attribute.startsWith("debug.log")) {
dB.log(debug());
return new Element(Boolean.TRUE.toString())
.getAttribute(attribute.fulfill(2));
}
if (attribute.startsWith("debug.no_color")) {
return new Element(ChatColor.stripColor(debug()))
.getAttribute(attribute.fulfill(2));
}
if (attribute.startsWith("debug")) {
return new Element(debug())
.getAttribute(attribute.fulfill(1));
}
return new Element(dScriptArgValue()).getAttribute(attribute.fulfill(1));
}
}