/
ItemMap.java
131 lines (109 loc) · 3.51 KB
/
ItemMap.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
package net.aufdemrand.denizen.objects.properties.item;
import net.aufdemrand.denizen.nms.NMSHandler;
import net.aufdemrand.denizen.nms.NMSVersion;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizencore.objects.Element;
import net.aufdemrand.denizencore.objects.Mechanism;
import net.aufdemrand.denizencore.objects.dObject;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.tags.Attribute;
import org.bukkit.Material;
import org.bukkit.inventory.meta.MapMeta;
public class ItemMap implements Property {
public static Material MAP_MATERIAL = NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2) ? Material.FILLED_MAP : Material.MAP;
public static boolean describes(dObject item) {
return item instanceof dItem
&& (((dItem) item).getItemStack().getType() == MAP_MATERIAL);
}
public static ItemMap getFrom(dObject _item) {
if (!describes(_item)) {
return null;
}
else {
return new ItemMap((dItem) _item);
}
}
public static final String[] handledTags = new String[]{
"map"
};
public static final String[] handledMechs = new String[] {
"map"
};
private ItemMap(dItem _item) {
item = _item;
}
dItem item;
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <i@item.map>
// @returns Element(Number)
// @group properties
// @mechanism dItem.map
// @description
// Returns the ID number of the map item's map.
// -->
if (attribute.startsWith("map")) {
if (!hasMapId()) {
return null;
}
return new Element(getMapId())
.getAttribute(attribute.fulfill(1));
}
return null;
}
public boolean hasMapId() {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
MapMeta map = (MapMeta) item.getItemStack().getItemMeta();
return map.hasMapId();
}
return true;
}
public int getMapId() {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
MapMeta map = (MapMeta) item.getItemStack().getItemMeta();
if (!map.hasMapId()) {
return 0;
}
return map.getMapId();
}
return item.getItemStack().getDurability();
}
public void setMapId(int id) {
if (NMSHandler.getVersion().isAtLeast(NMSVersion.v1_13_R2)) {
MapMeta map = (MapMeta) item.getItemStack().getItemMeta();
map.setMapId(id);
item.getItemStack().setItemMeta(map);
}
item.getItemStack().setDurability((short) (id));
}
@Override
public String getPropertyString() {
if (!hasMapId()) {
return null;
}
return String.valueOf(getMapId());
}
@Override
public String getPropertyId() {
return "map";
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object dItem
// @name map
// @input Element(Number)
// @description
// Changes what map ID number a map item uses.
// @tags
// <i@item.map>
// -->
if (mechanism.matches("map") && mechanism.requireInteger()) {
setMapId(mechanism.getValue().asInt());
}
}
}