/
InventoryContents.java
218 lines (194 loc) · 7.72 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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package com.denizenscript.denizen.objects.properties.inventory;
import com.denizenscript.denizen.objects.InventoryTag;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.objects.properties.PropertyParser;
import org.bukkit.ChatColor;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
public class InventoryContents implements Property {
public static boolean describes(ObjectTag inventory) {
// All inventories should have contents
return inventory instanceof InventoryTag;
}
public static InventoryContents getFrom(ObjectTag inventory) {
if (!describes(inventory)) {
return null;
}
return new InventoryContents((InventoryTag) inventory);
}
public static final String[] handledMechs = new String[] {
"contents"
};
InventoryTag inventory;
public InventoryContents(InventoryTag inventory) {
this.inventory = inventory;
}
public ListTag getContents(boolean simple) {
if (inventory.getInventory() == null) {
return null;
}
int lastNonAir = -1;
ListTag contents = new ListTag();
for (ItemStack item : inventory.getInventory().getContents()) {
if (item != null && item.getType() != Material.AIR) {
lastNonAir = contents.size();
if (simple) {
contents.add(new ItemTag(item).identifySimple());
}
else {
contents.addObject(new ItemTag(item));
}
}
else {
contents.addObject(new ItemTag(Material.AIR));
}
}
lastNonAir++;
while (contents.size() > lastNonAir) {
contents.remove(lastNonAir);
}
return contents;
}
public ListTag getContentsWithLore(String lore, boolean simple) {
if (inventory.getInventory() == null) {
return null;
}
ListTag contents = new ListTag();
lore = ChatColor.stripColor(lore);
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 ItemTag(item).identifySimple());
}
else {
contents.addObject(new ItemTag(item));
}
break;
}
}
}
}
}
return contents;
}
@Override
public String getPropertyString() {
if (!inventory.getIdType().equals("generic") && !inventory.isUnique()) {
return null;
}
ListTag contents = getContents(false);
if (contents == null || contents.isEmpty()) {
return null;
}
else {
return contents.identify();
}
}
@Override
public String getPropertyId() {
return "contents";
}
public static void registerTags() {
// <--[tag]
// @attribute <InventoryTag.map_slots>
// @returns MapTag
// @group properties
// @description
// Returns a map of inventory slots to the items in those slots (excludes air).
// -->
PropertyParser.<InventoryContents>registerTag("map_slots", (attribute, contents) -> {
MapTag map = new MapTag();
ItemStack[] items = contents.inventory.getContents();
for (int i = 0; i < items.length; i++) {
if (items[i] == null || items[i].getType() == Material.AIR) {
continue;
}
map.putObject(String.valueOf(i + 1), new ItemTag(items[i]));
}
return map;
});
// <--[tag]
// @attribute <InventoryTag.list_contents>
// @returns ListTag(ItemTag)
// @group properties
// @mechanism InventoryTag.contents
// @description
// Returns a list of all items in the inventory.
// -->
PropertyParser.<InventoryContents>registerTag("list_contents", (attribute, contents) -> {
// <--[tag]
// @attribute <InventoryTag.list_contents.simple>
// @returns ListTag(ItemTag)
// @group properties
// @mechanism InventoryTag.contents
// @description
// Returns a list of all items in the inventory, without item properties.
// -->
if (attribute.startsWith("simple", 2)) {
attribute.fulfill(1);
return contents.getContents(true);
}
// <--[tag]
// @attribute <InventoryTag.list_contents.with_lore[<element>]>
// @returns ListTag(ItemTag)
// @group properties
// @mechanism InventoryTag.contents
// @description
// Returns a list of all items in the inventory with the specified
// lore. Color codes are ignored.
// -->
if (attribute.startsWith("with_lore", 2)) {
attribute.fulfill(1);
// Must specify lore to check
if (!attribute.hasContext(1)) {
return null;
}
String lore = attribute.getContext(1);
attribute.fulfill(1);
// <--[tag]
// @attribute <InventoryTag.list_contents.with_lore[<element>].simple>
// @returns ListTag(ItemTag)
// @group properties
// @mechanism InventoryTag.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", 2)) {
attribute.fulfill(1);
return contents.getContentsWithLore(lore, true);
}
return contents.getContentsWithLore(lore, false);
}
return contents.getContents(false);
});
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object InventoryTag
// @name contents
// @input ListTag(ItemTag)
// @description
// Sets the contents of the inventory.
// @tags
// <InventoryTag.list_contents>
// <InventoryTag.list_contents.simple>
// <InventoryTag.list_contents.with_lore[<lore>]>
// <InventoryTag.list_contents.with_lore[<lore>].simple>
// -->
if (mechanism.matches("contents") && inventory.getIdType().equals("generic")) {
inventory.setContents(mechanism.valueAsType(ListTag.class), mechanism.context);
}
}
}