-
-
Notifications
You must be signed in to change notification settings - Fork 103
/
ItemBook.java
376 lines (347 loc) · 14.8 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
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
package com.denizenscript.denizen.objects.properties.item;
import com.denizenscript.denizen.utilities.FormattedTextHelper;
import com.denizenscript.denizen.utilities.debugging.Debug;
import com.denizenscript.denizen.objects.ItemTag;
import com.denizenscript.denizencore.objects.core.ElementTag;
import com.denizenscript.denizencore.objects.Mechanism;
import com.denizenscript.denizencore.objects.core.ListTag;
import com.denizenscript.denizencore.objects.ObjectTag;
import com.denizenscript.denizencore.objects.core.MapTag;
import com.denizenscript.denizencore.objects.properties.Property;
import com.denizenscript.denizencore.tags.Attribute;
import com.denizenscript.denizencore.tags.core.EscapeTagBase;
import com.denizenscript.denizencore.utilities.Deprecations;
import net.md_5.bungee.api.chat.BaseComponent;
import net.md_5.bungee.chat.ComponentSerializer;
import org.bukkit.Material;
import org.bukkit.inventory.meta.BookMeta;
import java.util.ArrayList;
import java.util.List;
public class ItemBook implements Property {
public static boolean describes(ObjectTag item) {
Material material = ((ItemTag) item).getItemStack().getType();
return (material == Material.WRITTEN_BOOK || material == Material.WRITABLE_BOOK);
}
public static ItemBook getFrom(ObjectTag _item) {
if (!describes(_item)) {
return null;
}
else {
return new ItemBook((ItemTag) _item);
}
}
public static final String[] handledTags = new String[] {
"book", "book_author", "book_title", "book_pages", "book_map"
};
public static final String[] handledMechs = new String[] {
"book", "book_raw_pages", "book_pages", "book_author", "book_title"
};
private ItemBook(ItemTag _item) {
item = _item;
}
public BookMeta getBookInfo() {
return (BookMeta) item.getItemMeta();
}
ItemTag item;
@Override
public ObjectTag getObjectAttribute(Attribute attribute) {
if (attribute == null) {
return null;
}
// <--[tag]
// @attribute <ItemTag.book_author>
// @returns ElementTag
// @mechanism ItemTag.book
// @group properties
// @description
// Returns the author of the book.
// -->
if (attribute.startsWith("book_author") && item.getItemStack().getType() == Material.WRITTEN_BOOK) {
return new ElementTag(getBookInfo().getAuthor())
.getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ItemTag.book_title>
// @returns ElementTag
// @mechanism ItemTag.book
// @group properties
// @description
// Returns the title of the book.
// -->
if (attribute.startsWith("book_title") && item.getItemStack().getType() == Material.WRITTEN_BOOK) {
return new ElementTag(getBookInfo().getTitle())
.getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ItemTag.book_pages>
// @returns ListTag
// @mechanism ItemTag.book
// @group properties
// @description
// Returns the plain-text pages of the book as a ListTag.
// -->
if (attribute.startsWith("book_pages")) {
ListTag output = new ListTag();
for (BaseComponent[] page : getBookInfo().spigot().getPages()) {
output.add(FormattedTextHelper.stringify(page));
}
return output.getObjectAttribute(attribute.fulfill(1));
}
// <--[tag]
// @attribute <ItemTag.book_map>
// @returns MapTag
// @mechanism ItemTag.book
// @group properties
// @description
// Returns a MapTag of data about the book, with keys "pages" (a ListTag), and when available, "author" and "title".
// -->
if (attribute.startsWith("book_map")) {
return getBookMap().getObjectAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("book")) {
Deprecations.itemBookTags.warn(attribute.context);
BookMeta bookInfo = (BookMeta) item.getItemMeta();
attribute = attribute.fulfill(1);
if (item.getItemStack().getType() == Material.WRITTEN_BOOK) {
if (attribute.startsWith("author")) {
return new ElementTag(bookInfo.getAuthor())
.getObjectAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("title")) {
return new ElementTag(bookInfo.getTitle())
.getObjectAttribute(attribute.fulfill(1));
}
}
if (attribute.startsWith("page_count")) {
return new ElementTag(bookInfo.getPageCount())
.getObjectAttribute(attribute.fulfill(1));
}
if ((attribute.startsWith("page") || attribute.startsWith("get_page")) && attribute.hasContext(1)) {
return new ElementTag(FormattedTextHelper.stringify(bookInfo.spigot().getPage(attribute.getIntContext(1))))
.getObjectAttribute(attribute.fulfill(1));
}
if ((attribute.startsWith("raw_page") || attribute.startsWith("get_raw_page")) && attribute.hasContext(1)) {
Deprecations.bookItemRawTags.warn(attribute.context);
return new ElementTag(ComponentSerializer.toString(bookInfo.spigot().getPage(attribute.getIntContext(1))))
.getObjectAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("pages")) {
ListTag output = new ListTag();
for (BaseComponent[] page : bookInfo.spigot().getPages()) {
output.add(FormattedTextHelper.stringify(page));
}
return output.getObjectAttribute(attribute.fulfill(1));
}
if (attribute.startsWith("raw_pages")) {
Deprecations.bookItemRawTags.warn(attribute.context);
ListTag output = new ListTag();
for (BaseComponent[] page : bookInfo.spigot().getPages()) {
output.add(ComponentSerializer.toString(page));
}
return output.getObjectAttribute(attribute.fulfill(1));
}
String output = getOutputString();
if (output == null) {
output = "null";
}
return new ElementTag(output)
.getObjectAttribute(attribute);
}
return null;
}
@Override
public String getPropertyString() {
MapTag map = getBookMap();
return map.toString();
}
public MapTag getBookMap() {
MapTag outMap = new MapTag();
BookMeta bookInfo = (BookMeta) item.getItemMeta();
if (item.getItemStack().getType().equals(Material.WRITTEN_BOOK) && bookInfo.hasAuthor() && bookInfo.hasTitle()) {
outMap.putObject("author", new ElementTag(bookInfo.getAuthor()));
outMap.putObject("title", new ElementTag(bookInfo.getTitle()));
}
if (bookInfo.hasPages()) {
List<BaseComponent[]> pages = bookInfo.spigot().getPages();
ListTag pageList = new ListTag(pages.size());
for (BaseComponent[] page : pages) {
pageList.addObject(new ElementTag(FormattedTextHelper.stringify(page)));
}
outMap.putObject("pages", pageList);
}
return outMap;
}
@Deprecated
public String getOutputString() {
StringBuilder output = new StringBuilder(128);
BookMeta bookInfo = (BookMeta) item.getItemMeta();
if (item.getItemStack().getType().equals(Material.WRITTEN_BOOK) && bookInfo.hasAuthor() && bookInfo.hasTitle()) {
output.append("author|").append(EscapeTagBase.escape(bookInfo.getAuthor()))
.append("|title|").append(EscapeTagBase.escape(bookInfo.getTitle())).append("|");
}
output.append("pages|");
if (bookInfo.hasPages()) {
for (BaseComponent[] page : bookInfo.spigot().getPages()) {
output.append(EscapeTagBase.escape(FormattedTextHelper.stringify(page))).append("|");
}
}
return output.substring(0, output.length() - 1);
}
@Override
public String getPropertyId() {
return "book";
}
@Override
public void adjust(Mechanism mechanism) {
if (mechanism.matches("book_raw_pages")) {
Deprecations.bookItemRawTags.warn(mechanism.context);
BookMeta meta = (BookMeta) item.getItemMeta();
ListTag data = mechanism.valueAsType(ListTag.class);
ArrayList<BaseComponent[]> newPages = new ArrayList<>();
for (String str : data) {
newPages.add(ComponentSerializer.parse(EscapeTagBase.unEscape(str)));
}
meta.spigot().setPages(newPages);
item.setItemMeta(meta);
}
// <--[mechanism]
// @object ItemTag
// @name book_pages
// @input ListTag
// @description
// Changes the plain-text pages of a book item.
// See <@link language Property Escaping>
// @tags
// <ItemTag.book_pages>
// -->
if (mechanism.matches("book_pages")) {
BookMeta meta = (BookMeta) item.getItemMeta();
ListTag data = mechanism.valueAsType(ListTag.class);
ArrayList<BaseComponent[]> newPages = new ArrayList<>();
for (String str : data) {
newPages.add(FormattedTextHelper.parse(EscapeTagBase.unEscape(str)));
}
meta.spigot().setPages(newPages);
item.setItemMeta(meta);
}
// <--[mechanism]
// @object ItemTag
// @name book_author
// @input ElementTag
// @description
// Changes the author of a book item.
// @tags
// <ItemTag.book_author>
// -->
if (mechanism.matches("book_author")) {
if (!item.getItemStack().getType().equals(Material.WRITTEN_BOOK)) {
Debug.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
}
else {
BookMeta meta = (BookMeta) item.getItemMeta();
meta.setAuthor(mechanism.getValue().asString());
item.setItemMeta(meta);
}
}
// <--[mechanism]
// @object ItemTag
// @name book_title
// @input ElementTag
// @description
// Changes the title of a book item.
// @tags
// <ItemTag.book_title>
// -->
if (mechanism.matches("book_title")) {
if (!item.getItemStack().getType().equals(Material.WRITTEN_BOOK)) {
Debug.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
}
else {
BookMeta meta = (BookMeta) item.getItemMeta();
meta.setTitle(mechanism.getValue().asString());
item.setItemMeta(meta);
}
}
// <--[mechanism]
// @object ItemTag
// @name book
// @input MapTag
// @description
// Changes the information on a book item.
// Should have keys "pages" (a ListTag), and optionally "title" and "author".
// @tags
// <ItemTag.is_book>
// <ItemTag.book_title>
// <ItemTag.book_author>
// <ItemTag.book_pages>
// -->
if (mechanism.matches("book")) {
BookMeta meta = (BookMeta) item.getItemMeta();
if (mechanism.getValue().asString().startsWith("map@")) {
MapTag mapData = mechanism.valueAsType(MapTag.class);
ObjectTag author = mapData.getObject("author");
ObjectTag title = mapData.getObject("title");
if (author != null && title != null) {
if (!item.getItemStack().getType().equals(Material.WRITTEN_BOOK)) {
Debug.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
}
else {
meta.setAuthor(author.toString());
meta.setTitle(title.toString());
}
}
ObjectTag pages = mapData.getObject("pages");
if (pages != null) {
ListTag pageList = ListTag.getListFor(pages, mechanism.context);
ArrayList<BaseComponent[]> newPages = new ArrayList<>(pageList.size());
for (int i = 1; i < pageList.size(); i++) {
newPages.add(FormattedTextHelper.parse(pageList.get(i)));
}
meta.spigot().setPages(newPages);
}
item.setItemMeta(meta);
return;
}
ListTag data = mechanism.valueAsType(ListTag.class);
if (data.size() < 1) {
Debug.echoError("Invalid book input!");
return;
}
if (data.size() < 2) {
// Nothing to do, but not necessarily invalid.
return;
}
if (data.size() > 4 && data.get(0).equalsIgnoreCase("author")
&& data.get(2).equalsIgnoreCase("title")) {
if (!item.getItemStack().getType().equals(Material.WRITTEN_BOOK)) {
Debug.echoError("Only WRITTEN_BOOK (not WRITABLE_BOOK) can have a title or author!");
}
else {
meta.setAuthor(EscapeTagBase.unEscape(data.get(1)));
meta.setTitle(EscapeTagBase.unEscape(data.get(3)));
for (int i = 0; i < 4; i++) {
data.removeObject(0); // No .removeRange?
}
}
}
if (data.get(0).equalsIgnoreCase("raw_pages")) {
ArrayList<BaseComponent[]> newPages = new ArrayList<>(data.size());
for (int i = 1; i < data.size(); i++) {
newPages.add(ComponentSerializer.parse(EscapeTagBase.unEscape(data.get(i))));
}
meta.spigot().setPages(newPages);
}
else if (data.get(0).equalsIgnoreCase("pages")) {
ArrayList<BaseComponent[]> newPages = new ArrayList<>(data.size());
for (int i = 1; i < data.size(); i++) {
newPages.add(FormattedTextHelper.parse(EscapeTagBase.unEscape(data.get(i))));
}
meta.spigot().setPages(newPages);
}
else {
Debug.echoError("Invalid book input!");
}
item.setItemMeta(meta);
}
}
}