/
ItemNBT.java
192 lines (171 loc) · 6.36 KB
/
ItemNBT.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
package com.denizenscript.denizen.objects.properties.item;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.utilities.nbt.CustomNBT;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.core.EscapeTagBase;
import com.denizenscript.denizencore.utilities.Deprecations;
import org.bukkit.Material;
import org.bukkit.inventory.ItemStack;
import java.util.List;
public class ItemNBT implements Property {
public static boolean describes(ObjectTag item) {
return item instanceof ItemTag;
}
public static ItemNBT getFrom(ObjectTag item) {
if (!describes(item)) {
return null;
}
else {
return new ItemNBT((ItemTag) item);
}
}
public static final String[] handledTags = new String[] {
"has_nbt", "nbt_keys", "nbt"
};
public static final String[] handledMechs = new String[] {
"remove_nbt", "nbt"
};
private ItemNBT(ItemTag item) {
this.item = item;
}
ItemTag item;
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <ItemTag.has_nbt[<key>]>
// @returns ElementTag(Boolean)
// @mechanism ItemTag.nbt
// @group properties
// @deprecated Use has_flag[...] instead.
// @description
// Deprecated: use <@link tag FlaggableObject.has_flag> instead.
// -->
if (attribute.startsWith("has_nbt")) {
Deprecations.itemNbt.warn(attribute.context);
return new ElementTag(CustomNBT.hasCustomNBT(item.getItemStack(), attribute.getContext(1), CustomNBT.KEY_DENIZEN))
.getObjectAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("nbt_keys")) {
Deprecations.itemNbt.warn(attribute.context);
return new ListTag(CustomNBT.listNBT(item.getItemStack(), CustomNBT.KEY_DENIZEN))
.getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ItemTag.nbt[<key>]>
// @returns ElementTag
// @mechanism ItemTag.nbt
// @group properties
// @deprecated Use flag[...] instead.
// @description
// Deprecated: use <@link tag FlaggableObject.flag> instead.
// -->
if (attribute.matches("nbt")) {
Deprecations.itemNbt.warn(attribute.context);
if (!attribute.hasContext(1)) {
ListTag list = getNBTDataList();
if (list == null) {
return null;
}
return list.getObjectAttribute(attribute.fulfill(1));
}
String res = CustomNBT.getCustomNBT(item.getItemStack(), attribute.getContext(1), CustomNBT.KEY_DENIZEN);
if (res == null) {
return null;
}
return new ElementTag(res)
.getObjectAttribute(attribute.fulfill(1));
}
return null;
}
public ListTag getNBTDataList() {
ItemStack itemStack = item.getItemStack();
List<String> nbtKeys = CustomNBT.listNBT(itemStack, CustomNBT.KEY_DENIZEN);
if (nbtKeys != null && !nbtKeys.isEmpty()) {
ListTag list = new ListTag();
for (String key : nbtKeys) {
list.add(EscapeTagBase.escape(key) + "/" + EscapeTagBase.escape(CustomNBT.getCustomNBT(itemStack, key, CustomNBT.KEY_DENIZEN)));
}
return list;
}
return null;
}
@Override
public String getPropertyString() {
ListTag list = getNBTDataList();
if (list == null) {
return null;
}
return list.identify();
}
@Override
public String getPropertyId() {
return "nbt";
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object ItemTag
// @name remove_nbt
// @input ListTag
// @deprecated Use 'flag' instead.
// @description
// Deprecated: use <@link mechanism ItemTag.flag> instead.
// @tags
// <ItemTag.nbt>
// <ItemTag.has_nbt>
// -->
if (mechanism.matches("remove_nbt")) {
Deprecations.itemNbt.warn(mechanism.context);
if (item.getMaterial().getMaterial() == Material.AIR) {
Debug.echoError("Cannot apply NBT to AIR!");
return;
}
ItemStack itemStack = item.getItemStack();
List<String> list;
if (mechanism.hasValue()) {
list = mechanism.valueAsType(ListTag.class);
}
else {
list = CustomNBT.listNBT(itemStack, CustomNBT.KEY_DENIZEN);
}
for (String string : list) {
itemStack = CustomNBT.removeCustomNBT(itemStack, string, CustomNBT.KEY_DENIZEN);
}
item.setItemStack(itemStack);
}
// <--[mechanism]
// @object ItemTag
// @name nbt
// @input ListTag
// @deprecated Use 'flag' instead.
// @description
// Deprecated: use <@link mechanism ItemTag.flag> instead.
// @tags
// <ItemTag.nbt>
// <ItemTag.has_nbt>
// -->
if (mechanism.matches("nbt")) {
Deprecations.itemNbt.warn(mechanism.context);
if (item.getMaterial().getMaterial() == Material.AIR) {
Debug.echoError("Cannot apply NBT to AIR!");
return;
}
ListTag list = mechanism.valueAsType(ListTag.class);
ItemStack itemStack = item.getItemStack();
for (String string : list) {
String[] split = string.split("/", 2);
itemStack = CustomNBT.addCustomNBT(itemStack, EscapeTagBase.unEscape(split[0]), EscapeTagBase.unEscape(split[1]), CustomNBT.KEY_DENIZEN);
}
item.setItemStack(itemStack);
}
}
}