/
ItemLore.java
151 lines (129 loc) · 4.58 KB
/
ItemLore.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
package net.aufdemrand.denizen.objects.properties.item;
import net.aufdemrand.denizen.Settings;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.scripts.containers.core.ItemScriptHelper;
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;
import net.aufdemrand.denizencore.tags.core.EscapeTags;
import org.bukkit.inventory.meta.ItemMeta;
import java.util.ArrayList;
import java.util.List;
public class ItemLore implements Property {
public static boolean describes(dObject item) {
// Technically, all items can hold lore
return item instanceof dItem;
}
public static ItemLore getFrom(dObject _item) {
if (!describes(_item)) {
return null;
}
else {
return new ItemLore((dItem) _item);
}
}
public boolean hasLore() {
return item.getItemStack().hasItemMeta()
&& (item.getItemStack().getItemMeta().hasLore());
}
private ItemLore(dItem _item) {
item = _item;
}
dItem item;
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// deprecated in favor of .escape_contents
if (attribute.startsWith("lore.escaped")) {
if (hasLore()) {
return new Element(getPropertyString()).getAttribute(attribute.fulfill(2));
}
}
// <--[tag]
// @attribute <i@item.lore>
// @returns dList
// @mechanism dItem.lore
// @group properties
// @description
// Returns lore as a dList. Excludes the custom-script-id lore.
// To get that information, use <i@item.scriptname>.
// -->
if (attribute.startsWith("lore")) {
if (hasLore()) {
List<String> loreList = new ArrayList<String>();
for (String itemLore : item.getItemStack().getItemMeta().getLore()) {
if (!itemLore.startsWith(dItem.itemscriptIdentifier)
&& !itemLore.startsWith(ItemScriptHelper.ItemScriptHashID)) {
loreList.add(itemLore);
}
}
return new dList(loreList).getAttribute(attribute.fulfill(1));
}
}
// <--[tag]
// @attribute <i@item.has_lore>
// @returns Element(Boolean)
// @mechanism dItem.lore
// @group properties
// @description
// Returns whether the item has lore set on it.
// -->
if (attribute.startsWith("has_lore")) {
return new Element(hasLore())
.getAttribute(attribute.fulfill(1));
}
return null;
}
@Override
public String getPropertyString() {
if (hasLore()) {
StringBuilder output = new StringBuilder();
for (String itemLore : item.getItemStack().getItemMeta().getLore()) {
if (!itemLore.startsWith(dItem.itemscriptIdentifier)
&& !itemLore.startsWith(ItemScriptHelper.ItemScriptHashID)) {
output.append(EscapeTags.Escape(itemLore)).append("|");
}
}
return (output.length() == 0) ? null : output.substring(0, output.length() - 1);
}
else {
return null;
}
}
@Override
public String getPropertyId() {
return "lore";
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object dItem
// @name lore
// @input dList
// @description
// Sets the item's lore.
// See <@link language Property Escaping>
// @tags
// <i@item.lore>
// -->
if (mechanism.matches("lore")) {
ItemMeta meta = item.getItemStack().getItemMeta();
dList lore = mechanism.getValue().asType(dList.class);
if (item.isItemscript()) {
if (!Settings.packetInterception()) {
lore.add(0, ItemScriptHelper.createItemScriptID(item.getScriptName()));
}
}
for (int i = 0; i < lore.size(); i++) {
lore.set(i, EscapeTags.unEscape(lore.get(i)));
}
meta.setLore(lore);
item.getItemStack().setItemMeta(meta);
}
}
}