-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
InventoryContents.java
201 lines (173 loc) · 7.03 KB
/
InventoryContents.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
package net.aufdemrand.denizen.objects.properties.inventory;
import net.aufdemrand.denizen.objects.*;
import net.aufdemrand.denizencore.objects.*;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.tags.Attribute;
public class InventoryContents implements Property {
public static boolean describes(dObject inventory) {
// All inventories should have contents
return inventory instanceof dInventory;
}
public static InventoryContents getFrom(dObject inventory) {
if (!describes(inventory)) return null;
return new InventoryContents((dInventory) inventory);
}
///////////////////
// Instance Fields and Methods
/////////////
dInventory inventory;
public InventoryContents(dInventory inventory) {
this.inventory = inventory;
}
public dList getContents(int simpleOrFull) {
if (inventory.getInventory() == null)
return null;
dList contents = new dList();
boolean containsNonAir = false;
for (ItemStack item : inventory.getInventory().getContents()) {
if (item != null && item.getType() != Material.AIR) {
containsNonAir = true;
if (simpleOrFull == 1)
contents.add(new dItem(item).identifySimple());
else if (simpleOrFull == 2)
contents.add(new dItem(item).getFullString());
else
contents.add(new dItem(item).identify());
}
else
contents.add("i@air");
}
if (!containsNonAir)
contents.clear();
else
contents = dList.valueOf(contents.identify().replaceAll("(\\|i@air)*$", ""));
return contents;
}
public dList getContentsWithLore(String lore, boolean simple) {
if (inventory.getInventory() == null)
return null;
dList contents = new dList();
for (ItemStack item : inventory.getInventory().getContents()) {
if (item != null && item.getType() != Material.AIR) {
if (item.hasItemMeta() && item.getItemMeta().hasLore()) {
for (String line : item.getItemMeta().getLore()) {
// Add the item to the list if it contains the lore specified in
// the context
if (ChatColor.stripColor(line).equalsIgnoreCase(lore)) {
if (simple)
contents.add(new dItem(item).identifySimple());
else
contents.add(new dItem(item).identify());
break;
}
}
}
}
}
return contents;
}
/////////
// Property Methods
///////
@Override
public String getPropertyString() {
if (!inventory.getIdType().equals("generic") && !inventory.isUnique())
return null;
dList contents = getContents(0);
if (contents == null || contents.isEmpty())
return null;
else
return contents.identify();
}
@Override
public String getPropertyId() {
return "contents";
}
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) return "null";
// <--[tag]
// @attribute <in@inventory.list_contents>
// @returns dList(dItem)
// @group properties
// @mechanism dInventory.contents
// @description
// Returns a list of all items in the inventory.
// -->
if (attribute.startsWith("list_contents")) {
attribute.fulfill(1);
// <--[tag]
// @attribute <in@inventory.list_contents.simple>
// @returns dList(dItem)
// @group properties
// @mechanism dInventory.contents
// @description
// Returns a list of all items in the inventory, without item properties.
// -->
if (attribute.startsWith("simple"))
return getContents(1).getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <in@inventory.list_contents.full>
// @returns dList(dItem)
// @group properties
// @mechanism dInventory.contents
// @description
// Returns a list of all items in the inventory, without with the tag item.full used.
// -->
if (attribute.startsWith("full"))
return getContents(2).getAttribute(attribute.fulfill(1));
// <--[tag]
// @attribute <in@inventory.list_contents.with_lore[<element>]>
// @returns dList(dItem)
// @group properties
// @mechanism dInventory.contents
// @description
// Returns a list of all items in the inventory with the specified
// lore. Color codes are ignored.
// -->
if (attribute.startsWith("with_lore")) {
// Must specify lore to check
if (!attribute.hasContext(1)) return null;
String lore = attribute.getContext(1);
attribute.fulfill(1);
// <--[tag]
// @attribute <in@inventory.list_contents.with_lore[<element>].simple>
// @returns dList(dItem)
// @group properties
// @mechanism dInventory.contents
// @description
// Returns a list of all items in the inventory with the specified
// lore, without item properties. Color codes are ignored.
// -->
if (attribute.startsWith("simple"))
return getContentsWithLore(lore, true)
.getAttribute(attribute.fulfill(1));
return getContentsWithLore(lore, false)
.getAttribute(attribute);
}
return getContents(0).getAttribute(attribute);
}
return null;
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object dInventory
// @name contents
// @input dList(dItem)
// @description
// Sets the contents of the inventory.
// @tags
// <in@inventory.list_contents>
// <in@inventory.list_contents.simple>
// <in@inventory.list_contents.with_lore[<lore>]>
// <in@inventory.list_contents.with_lore[<lore>].simple>
// -->
if (mechanism.matches("contents") && inventory.getIdType().equals("generic")) {
inventory.setContents(mechanism.getValue().asType(dList.class));
}
}
}