/
CustomNBT.java
430 lines (350 loc) · 14.9 KB
/
CustomNBT.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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
package com.denizenscript.denizen.utilities.nbt;
import com.denizenscript.denizen.nms.NMSHandler;
import com.denizenscript.denizen.nms.util.jnbt.*;
import com.denizenscript.denizen.objects.properties.entity.EntityDisabledSlots.Action;
import com.denizenscript.denizencore.utilities.CoreUtilities;
import org.bukkit.Material;
import org.bukkit.entity.Entity;
import org.bukkit.inventory.EquipmentSlot;
import org.bukkit.inventory.ItemStack;
import java.util.*;
public class CustomNBT {
public static final String KEY_DENIZEN = "Denizen NBT";
public static final String KEY_ATTRIBUTES = "AttributeModifiers";
public static final String KEY_CAN_PLACE_ON = "CanPlaceOn";
public static final String KEY_CAN_DESTROY = "CanDestroy";
public static final String KEY_DISABLED_SLOTS = "DisabledSlots";
public static MapOfEnchantments getEnchantments(ItemStack item) {
return new MapOfEnchantments(item);
}
private static final Map<EquipmentSlot, Integer> slotMap;
static {
slotMap = new HashMap<>();
slotMap.put(EquipmentSlot.HAND, 0);
slotMap.put(EquipmentSlot.FEET, 1);
slotMap.put(EquipmentSlot.LEGS, 2);
slotMap.put(EquipmentSlot.CHEST, 3);
slotMap.put(EquipmentSlot.HEAD, 4);
slotMap.put(EquipmentSlot.OFF_HAND, 5);
}
/*
* Some static methods for dealing with Minecraft NBT data, which is used to store
* custom NBT.
*/
public static class AttributeReturn {
public String attr;
public String slot;
public int op;
public double amt;
public long uuidMost;
public long uuidLeast;
}
public static List<AttributeReturn> getAttributes(ItemStack itemStack) {
if (itemStack == null || itemStack.getType() == Material.AIR) {
return null;
}
CompoundTag compoundTag = NMSHandler.getItemHelper().getNbtData(itemStack);
List<CompoundTag> attribs = new ArrayList<>();
if (compoundTag.getValue().containsKey(KEY_ATTRIBUTES)) {
List<Tag> temp = (List<Tag>) compoundTag.getValue().get(KEY_ATTRIBUTES).getValue();
for (Tag tag : temp) {
attribs.add((CompoundTag) tag);
}
}
List<AttributeReturn> attrs = new ArrayList<>();
for (int i = 0; i < attribs.size(); i++) {
CompoundTag ct = attribs.get(i);
AttributeReturn atr = new AttributeReturn();
atr.attr = (String) ct.getValue().get("AttributeName").getValue();
atr.slot = ct.getValue().get("Slot") == null ? "mainhand" : (String) ct.getValue().get("Slot").getValue();
atr.op = (Integer) ct.getValue().get("Operation").getValue();
Tag t = ct.getValue().get("Amount");
if (t instanceof IntTag) {
atr.amt = (Integer) t.getValue();
}
else if (t instanceof LongTag) {
atr.amt = (Long) t.getValue();
}
else if (t instanceof DoubleTag) {
atr.amt = (Double) t.getValue();
}
else {
/// ????
atr.amt = 0;
}
t = ct.getValue().get("UUIDMost");
if (t instanceof LongTag) {
atr.uuidMost = (Long) t.getValue();
}
else if (t instanceof IntTag) {
atr.uuidMost = (Integer) t.getValue();
}
t = ct.getValue().get("UUIDLeast");
if (t instanceof LongTag) {
atr.uuidLeast = (Long) t.getValue();
}
else if (t instanceof IntTag) {
atr.uuidLeast = (Integer) t.getValue();
}
attrs.add(atr);
}
return attrs;
}
public static long uuidChoice(ItemStack its) {
String mat = CoreUtilities.toLowerCase(its.getType().name());
if (mat.contains("boots")) {
return 1000;
}
else if (mat.contains("legging")) {
return 100000;
}
else if (mat.contains("helmet")) {
return 10000000;
}
else if (mat.contains("chestp")) {
return 1000000000;
}
else {
return 1;
}
}
public static ItemStack addAttribute(ItemStack itemStack, String attr, String slot, int op, double amt) {
if (itemStack == null || itemStack.getType() == Material.AIR) {
return null;
}
CompoundTag compoundTag = NMSHandler.getItemHelper().getNbtData(itemStack);
List<CompoundTag> attribs = new ArrayList<>();
if (compoundTag.getValue().containsKey(KEY_ATTRIBUTES)) {
List<Tag> temp = (List<Tag>) compoundTag.getValue().get(KEY_ATTRIBUTES).getValue();
for (Tag tag : temp) {
attribs.add((CompoundTag) tag);
}
}
HashMap<String, Tag> tmap = new HashMap<>();
tmap.put("AttributeName", new StringTag(attr));
tmap.put("Name", new StringTag(attr));
tmap.put("Slot", new StringTag(slot));
tmap.put("Operation", new IntTag(op));
tmap.put("Amount", new DoubleTag(amt));
long uuidhelp = uuidChoice(itemStack);
tmap.put("UUIDMost", new LongTag(uuidhelp + 88512 + attribs.size()));
tmap.put("UUIDLeast", new LongTag(uuidhelp * 2 + 1250025L + attribs.size()));
CompoundTag ct = NMSHandler.getInstance().createCompoundTag(tmap);
attribs.add(ct);
ListTag lt = new ListTag(CompoundTag.class, attribs);
compoundTag = compoundTag.createBuilder().put(KEY_ATTRIBUTES, lt).build();
return NMSHandler.getItemHelper().setNbtData(itemStack, compoundTag);
}
public static List<Material> getNBTMaterials(ItemStack itemStack, String key) {
if (itemStack == null || itemStack.getType() == Material.AIR) {
return null;
}
CompoundTag compoundTag = NMSHandler.getItemHelper().getNbtData(itemStack);
List<Material> materials = new ArrayList<>();
if (compoundTag.getValue().containsKey(key)) {
List<StringTag> temp = (List<StringTag>) compoundTag.getValue().get(key).getValue();
for (StringTag tag : temp) {
materials.add(NMSHandler.getItemHelper().getMaterialFromInternalName(tag.getValue()));
}
}
return materials;
}
public static ItemStack setNBTMaterials(ItemStack itemStack, String key, List<Material> materials) {
if (itemStack == null || itemStack.getType() == Material.AIR) {
return null;
}
CompoundTag compoundTag = NMSHandler.getItemHelper().getNbtData(itemStack);
compoundTag = compoundTag.createBuilder().remove(key).build();
if (materials.isEmpty()) {
return NMSHandler.getItemHelper().setNbtData(itemStack, compoundTag);
}
List<StringTag> internalMaterials = new ArrayList<>();
for (Material material : materials) {
internalMaterials.add(new StringTag(NMSHandler.getItemHelper().getInternalNameFromMaterial(material)));
}
ListTag lt = new ListTag(StringTag.class, internalMaterials);
compoundTag = compoundTag.createBuilder().put(key, lt).build();
return NMSHandler.getItemHelper().setNbtData(itemStack, compoundTag);
}
public static ItemStack addCustomNBT(ItemStack itemStack, String key, String value, String basekey) {
if (itemStack == null || itemStack.getType() == Material.AIR) {
return null;
}
CompoundTag compoundTag = NMSHandler.getItemHelper().getNbtData(itemStack);
CompoundTag denizenTag;
if (compoundTag.getValue().containsKey(basekey)) {
denizenTag = (CompoundTag) compoundTag.getValue().get(basekey);
}
else {
denizenTag = NMSHandler.getInstance().createCompoundTag(new HashMap<>());
}
// Add custom NBT
denizenTag = denizenTag.createBuilder().putString(CoreUtilities.toLowerCase(key), value).build();
compoundTag = compoundTag.createBuilder().put(basekey, denizenTag).build();
// Write tag back
return NMSHandler.getItemHelper().setNbtData(itemStack, compoundTag);
}
public static ItemStack clearNBT(ItemStack itemStack, String key) {
if (itemStack == null || itemStack.getType() == Material.AIR) {
return null;
}
CompoundTag compoundTag = NMSHandler.getItemHelper().getNbtData(itemStack);
compoundTag = compoundTag.createBuilder().remove(key).build();
// Write tag back
return NMSHandler.getItemHelper().setNbtData(itemStack, compoundTag);
}
public static ItemStack removeCustomNBT(ItemStack itemStack, String key, String basekey) {
if (itemStack == null || itemStack.getType() == Material.AIR) {
return null;
}
CompoundTag compoundTag = NMSHandler.getItemHelper().getNbtData(itemStack);
CompoundTag denizenTag;
if (compoundTag.getValue().containsKey(basekey)) {
denizenTag = (CompoundTag) compoundTag.getValue().get(basekey);
}
else {
return itemStack;
}
// Remove custom NBT
denizenTag = denizenTag.createBuilder().remove(CoreUtilities.toLowerCase(key)).build();
compoundTag = compoundTag.createBuilder().put(basekey, denizenTag).build();
// Write tag back
return NMSHandler.getItemHelper().setNbtData(itemStack, compoundTag);
}
public static boolean hasCustomNBT(ItemStack itemStack, String key, String basekey) {
if (itemStack == null || itemStack.getType() == Material.AIR) {
return false;
}
CompoundTag compoundTag = NMSHandler.getItemHelper().getNbtData(itemStack);
CompoundTag denizenTag;
if (compoundTag.getValue().containsKey(basekey)) {
denizenTag = (CompoundTag) compoundTag.getValue().get(basekey);
}
else {
return false;
}
return denizenTag.getValue().containsKey(CoreUtilities.toLowerCase(key));
}
public static String getCustomNBT(ItemStack itemStack, String key, String basekey) {
if (itemStack == null || itemStack.getType() == Material.AIR || key == null) {
return null;
}
CompoundTag compoundTag = NMSHandler.getItemHelper().getNbtData(itemStack);
if (compoundTag.getValue().containsKey(basekey)) {
CompoundTag denizenTag = (CompoundTag) compoundTag.getValue().get(basekey);
String lowerKey = CoreUtilities.toLowerCase(key);
if (denizenTag.containsKey(lowerKey)) {
return denizenTag.getString(lowerKey);
}
}
return null;
}
public static List<String> listNBT(ItemStack itemStack, String basekey) {
List<String> nbt = new ArrayList<>();
if (itemStack == null || itemStack.getType() == Material.AIR) {
return nbt;
}
CompoundTag compoundTag = NMSHandler.getItemHelper().getNbtData(itemStack);
if (compoundTag.getValue().containsKey(basekey)) {
CompoundTag denizenTag = (CompoundTag) compoundTag.getValue().get(basekey);
nbt.addAll(denizenTag.getValue().keySet());
}
return nbt;
}
public static void addCustomNBT(Entity entity, String key, String value) {
if (entity == null) {
return;
}
CompoundTag compoundTag = NMSHandler.getEntityHelper().getNbtData(entity);
// Add custom NBT
compoundTag = compoundTag.createBuilder().putString(key, value).build();
// Write tag back
NMSHandler.getEntityHelper().setNbtData(entity, compoundTag);
}
public static void addCustomNBT(Entity entity, String key, int value) {
if (entity == null) {
return;
}
CompoundTag compoundTag = NMSHandler.getEntityHelper().getNbtData(entity);
// Add custom NBT
compoundTag = compoundTag.createBuilder().putInt(key, value).build();
// Write tag back
NMSHandler.getEntityHelper().setNbtData(entity, compoundTag);
}
public static void removeCustomNBT(Entity entity, String key) {
if (entity == null) {
return;
}
CompoundTag compoundTag = NMSHandler.getEntityHelper().getNbtData(entity);
// Remove custom NBT
compoundTag = compoundTag.createBuilder().remove(key).build();
// Write tag back
NMSHandler.getEntityHelper().setNbtData(entity, compoundTag);
}
public static boolean hasCustomNBT(Entity entity, String key) {
if (entity == null) {
return false;
}
CompoundTag compoundTag = NMSHandler.getEntityHelper().getNbtData(entity);
// Check for key
return compoundTag.getValue().containsKey(key);
}
public static String getCustomNBT(Entity entity, String key) {
if (entity == null) {
return null;
}
CompoundTag compoundTag = NMSHandler.getEntityHelper().getNbtData(entity);
// Return contents of the tag
return compoundTag.getString(key);
}
public static int getCustomIntNBT(Entity entity, String key) {
if (entity == null) {
return 0;
}
CompoundTag compoundTag = NMSHandler.getEntityHelper().getNbtData(entity);
// Return contents of the tag
return compoundTag.getInt(key);
}
public static void setDisabledSlots(Entity entity, Map<EquipmentSlot, Set<Action>> map) {
int sum = 0;
for (Map.Entry<EquipmentSlot, Set<Action>> entry : map.entrySet()) {
if (!slotMap.containsKey(entry.getKey())) {
continue;
}
for (Action action : entry.getValue()) {
sum += 1 << (slotMap.get(entry.getKey()) + action.getId());
}
}
addCustomNBT(entity, KEY_DISABLED_SLOTS, sum);
}
public static Map<EquipmentSlot, Set<Action>> getDisabledSlots(Entity entity) {
if (entity == null) {
return null;
}
Map<EquipmentSlot, Set<Action>> map = new HashMap<>();
CompoundTag compoundTag = NMSHandler.getEntityHelper().getNbtData(entity);
int disabledSlots = compoundTag.getInt(KEY_DISABLED_SLOTS);
if (disabledSlots == 0) {
return map;
}
slotLoop:
for (EquipmentSlot slot : slotMap.keySet()) {
for (Action action : Action.values()) {
int matchedSlot = disabledSlots & 1 << slotMap.get(slot) + action.getId();
if (matchedSlot != 0) {
Set<Action> set = map.get(slot);
if (set == null) {
set = new HashSet<>();
map.put(slot, set);
}
set.add(action);
disabledSlots -= matchedSlot;
if (disabledSlots == 0) {
break slotLoop;
}
}
}
}
return map;
}
}