/
ItemBook.java
236 lines (210 loc) · 8.14 KB
/
ItemBook.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
package net.aufdemrand.denizen.objects.properties.item;
import net.aufdemrand.denizen.objects.dItem;
import net.aufdemrand.denizen.utilities.debugging.dB;
import net.aufdemrand.denizencore.objects.*;
import net.aufdemrand.denizencore.objects.properties.Property;
import net.aufdemrand.denizencore.tags.Attribute;
import net.aufdemrand.denizencore.tags.core.EscapeTags;
import org.bukkit.Material;
import org.bukkit.inventory.meta.BookMeta;
import java.util.ArrayList;
public class ItemBook implements Property {
public static boolean describes(dObject item) {
return item instanceof dItem
&& (
((dItem) item).getItemStack().getType().equals(Material.WRITTEN_BOOK)
|| ((dItem) item).getItemStack().getType().equals(Material.BOOK_AND_QUILL)
);
}
public static ItemBook getFrom(dObject _item) {
if (!describes(_item)) {
return null;
}
else {
return new ItemBook((dItem) _item);
}
}
private ItemBook(dItem _item) {
item = _item;
}
dItem item;
@Override
public String getAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
if (attribute.startsWith("book")) {
BookMeta bookInfo = (BookMeta) item.getItemStack().getItemMeta();
attribute = attribute.fulfill(1);
if (item.getItemStack().getType() == Material.WRITTEN_BOOK) {
// <--[tag]
// @attribute <i@item.book.author>
// @returns Element
// @mechanism dItem.book
// @group properties
// @description
// Returns the author of the book.
// -->
if (attribute.startsWith("author")) {
return new Element(bookInfo.getAuthor())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.book.title>
// @returns Element
// @mechanism dItem.book
// @group properties
// @description
// Returns the title of the book.
// -->
if (attribute.startsWith("title")) {
return new Element(bookInfo.getTitle())
.getAttribute(attribute.fulfill(1));
}
}
// <--[tag]
// @attribute <i@item.book.page_count>
// @returns Element(Number)
// @mechanism dItem.book
// @group properties
// @description
// Returns the number of pages in the book.
// -->
if (attribute.startsWith("page_count")) {
return new Element(bookInfo.getPageCount())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.book.get_page[<#>]>
// @returns Element
// @mechanism dItem.book
// @group properties
// @description
// Returns the page specified from the book as an element.
// -->
if (attribute.startsWith("get_page") && aH.matchesInteger(attribute.getContext(1))) {
return new Element(bookInfo.getPage(attribute.getIntContext(1)))
.getAttribute(attribute.fulfill(1));
}
// Deprecated in favor of pages.escape_contents
if (attribute.startsWith("pages.escaped")) {
StringBuilder output = new StringBuilder();
for (String page : bookInfo.getPages()) {
output.append(EscapeTags.Escape(page)).append("|");
}
return new dList(output.length() > 0 ?
output.substring(0, output.length() - 1) : output.toString())
.getAttribute(attribute.fulfill(2));
}
// <--[tag]
// @attribute <i@item.book.pages>
// @returns dList
// @mechanism dItem.book
// @group properties
// @description
// Returns the pages of the book as a dList.
// -->
if (attribute.startsWith("pages")) {
return new dList(bookInfo.getPages())
.getAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <i@item.book>
// @returns Element
// @mechanism dItem.book
// @group properties
// @description
// Returns full information on the book item, in the format
// author|AUTHOR|title|TITLE|pages|PAGE_ONE|PAGE_TWO|...
// or as pages|PAGE_ONE|PAGE_TWO|...
// Pre-escaped to prevent issues.
// See <@link language Property Escaping>
// -->
String output = getPropertyString();
if (output == null) {
output = "null";
}
return new Element(output)
.getAttribute(attribute);
}
return null;
}
@Override
public String getPropertyString() {
StringBuilder output = new StringBuilder();
BookMeta bookInfo = (BookMeta) item.getItemStack().getItemMeta();
if (item.getItemStack().getType().equals(Material.WRITTEN_BOOK)
&& bookInfo.hasAuthor() && bookInfo.hasTitle()) {
output.append("author|").append(EscapeTags.Escape(bookInfo.getAuthor()))
.append("|title|").append(EscapeTags.Escape(bookInfo.getTitle())).append("|");
}
output.append("pages|");
if (bookInfo.hasPages()) {
for (String page : bookInfo.getPages()) {
output.append(EscapeTags.Escape(page)).append("|");
}
}
if (output.length() <= 6) {
return null;
}
else {
return output.substring(0, output.length() - 1);
}
}
@Override
public String getPropertyId() {
return "book";
}
@Override
public void adjust(Mechanism mechanism) {
// <--[mechanism]
// @object dItem
// @name book
// @input Element
// @description
// Changes the information on a book item.
// See <@link language Property Escaping>
// @tags
// <i@item.is_book>
// <i@item.book.author>
// <i@item.book.title>
// <i@item.book.page_count>
// <i@item.book.get_page[<#>]>
// <i@item.book.pages>
// <i@item.book>
// -->
if (mechanism.matches("book")) {
BookMeta meta = (BookMeta) item.getItemStack().getItemMeta();
dList data = mechanism.getValue().asType(dList.class);
if (data.size() < 2) {
dB.echoError("Invalid book input!");
}
else {
if (data.size() > 4 && data.get(0).equalsIgnoreCase("author")
&& data.get(2).equalsIgnoreCase("title")) {
if (!item.getItemStack().getType().equals(Material.WRITTEN_BOOK)) {
dB.echoError("That type of book cannot have title or author!");
}
else {
meta.setAuthor(EscapeTags.unEscape(data.get(1)));
meta.setTitle(EscapeTags.unEscape(data.get(3)));
for (int i = 0; i < 4; i++) {
data.remove(0); // No .removeRange?
}
}
}
if (!data.get(0).equalsIgnoreCase("pages")) {
dB.echoError("Invalid book input!");
}
else {
ArrayList<String> newPages = new ArrayList<String>();
for (int i = 1; i < data.size(); i++) {
newPages.add(EscapeTags.unEscape(data.get(i)));
}
meta.setPages(newPages);
}
item.getItemStack().setItemMeta(meta);
}
}
}
}