/
ItemNBT.java
97 lines (81 loc) · 2.79 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
package net.aufdemrand.denizen.objects.properties.item;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.utilities.nbt.CustomNBT;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.Mechanism;
import net.aufdemrand.denizencore.objects.dList;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.tags.Attribute;
public class ItemNBT implements Property {
public static boolean describes(dObject item) {
return item instanceof dItem;
}
public static ItemNBT getFrom(dObject item) {
if (!describes(item)) {
return null;
}
else {
return new ItemNBT((dItem) item);
}
}
private ItemNBT(dItem item) {
this.item = item;
}
dItem item;
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <i@item.has_nbt[<key>]>
// @returns Element(Boolean)
// @group properties
// @description
// Returns whether this item has the specified NBT key.
// -->
if (attribute.startsWith("has_nbt")) {
return new Element(CustomNBT.hasCustomNBT(item.getItemStack(), attribute.getContext(1)))
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.nbt_keys[<filter>]>
// @returns dList
// @group properties
// @description
// Returns all of this item's NBT keys as a dList. Optionally, specify
// a filter for the start of the keys.
// -->
if (attribute.startsWith("nbt_keys")) {
String filter = attribute.hasContext(1) ? attribute.getContext(1) : "";
return new dList(CustomNBT.listNBT(item.getItemStack(), filter))
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.nbt[<key>]>
// @returns Element
// @group properties
// @description
// Returns the value of this item's NBT key as a string Element as best it can.
// -->
if (attribute.startsWith("nbt")) {
return new Element(CustomNBT.getCustomNBT(item.getItemStack(), attribute.getContext(1)))
.getAttribute(attribute.fulfill(1));
}
return null;
}
@Override
public String getPropertyString() {
// TODO: list Denizen command-created NBT if/when that is added?
return null;
}
@Override
public String getPropertyId() {
return "nbt";
}
@Override
public void adjust(Mechanism mechanism) {
// Do nothing
}
}