Commits
md_5 authored 4acd0f49e07
22 22 | import net.minecraft.server.NBTTagString; |
23 23 | import org.bukkit.craftbukkit.util.CraftChatMessage; |
24 24 | |
25 25 | SerializableMeta.class) | (
26 26 | public class CraftMetaBook extends CraftMetaItem implements BookMeta { |
27 27 | static final ItemMetaKey BOOK_TITLE = new ItemMetaKey("title"); |
28 28 | static final ItemMetaKey BOOK_AUTHOR = new ItemMetaKey("author"); |
29 29 | static final ItemMetaKey BOOK_PAGES = new ItemMetaKey("pages"); |
30 30 | static final ItemMetaKey RESOLVED = new ItemMetaKey("resolved"); |
31 31 | static final ItemMetaKey GENERATION = new ItemMetaKey("generation"); |
32 - | static final int MAX_PAGE_LENGTH = Short.MAX_VALUE; // TODO: Check me |
33 - | static final int MAX_TITLE_LENGTH = 0xffff; |
32 + | static final int MAX_PAGES = 50; |
33 + | static final int MAX_PAGE_LENGTH = 256; |
34 + | static final int MAX_TITLE_LENGTH = 16; |
34 35 | |
35 36 | protected String title; |
36 37 | protected String author; |
37 38 | public List<IChatBaseComponent> pages = new ArrayList<IChatBaseComponent>(); |
38 39 | protected Integer generation; |
39 40 | |
40 41 | CraftMetaBook(CraftMetaItem meta) { |
41 42 | super(meta); |
42 43 | |
43 44 | if (meta instanceof CraftMetaBook) { |
61 62 | } |
62 63 | |
63 64 | if (tag.hasKey(BOOK_AUTHOR.NBT)) { |
64 65 | this.author = tag.getString(BOOK_AUTHOR.NBT); |
65 66 | } |
66 67 | |
67 68 | boolean resolved = false; |
68 69 | if (tag.hasKey(RESOLVED.NBT)) { |
69 70 | resolved = tag.getBoolean(RESOLVED.NBT); |
70 71 | } |
71 - | |
72 + | |
72 73 | if (tag.hasKey(GENERATION.NBT)) { |
73 74 | generation = tag.getInt(GENERATION.NBT); |
74 75 | } |
75 76 | |
76 77 | if (tag.hasKey(BOOK_PAGES.NBT) && handlePages) { |
77 78 | NBTTagList pages = tag.getList(BOOK_PAGES.NBT, 8); |
78 79 | |
79 80 | for (int i = 0; i < pages.size(); i++) { |
80 81 | String page = pages.getString(i); |
81 82 | if (resolved) { |
99 100 | setTitle(SerializableMeta.getString(map, BOOK_TITLE.BUKKIT, true)); |
100 101 | |
101 102 | Iterable<?> pages = SerializableMeta.getObject(Iterable.class, map, BOOK_PAGES.BUKKIT, true); |
102 103 | if(pages != null) { |
103 104 | for (Object page : pages) { |
104 105 | if (page instanceof String) { |
105 106 | addPage((String) page); |
106 107 | } |
107 108 | } |
108 109 | } |
109 - | |
110 + | |
110 111 | generation = SerializableMeta.getObject(Integer.class, map, GENERATION.BUKKIT, true); |
111 112 | } |
112 113 | |
113 114 | |
114 115 | void applyToItem(NBTTagCompound itemData) { |
115 116 | applyToItem(itemData, true); |
116 117 | } |
117 118 | |
118 119 | void applyToItem(NBTTagCompound itemData, boolean handlePages) { |
119 120 | super.applyToItem(itemData); |
228 229 | } |
229 230 | |
230 231 | public void setPages(final String... pages) { |
231 232 | this.pages.clear(); |
232 233 | |
233 234 | addPage(pages); |
234 235 | } |
235 236 | |
236 237 | public void addPage(final String... pages) { |
237 238 | for (String page : pages) { |
239 + | if (this.pages.size() >= MAX_PAGES) { |
240 + | return; |
241 + | } |
242 + | |
238 243 | if (page == null) { |
239 244 | page = ""; |
240 245 | } else if (page.length() > MAX_PAGE_LENGTH) { |
241 246 | page = page.substring(0, MAX_PAGE_LENGTH); |
242 247 | } |
243 248 | |
244 249 | this.pages.add(CraftChatMessage.fromString(page, true)[0]); |
245 250 | } |
246 251 | } |
247 252 | |
335 340 | builder.put(BOOK_AUTHOR.BUKKIT, author); |
336 341 | } |
337 342 | |
338 343 | if (hasPages()) { |
339 344 | List<String> pagesString = new ArrayList<String>(); |
340 345 | for (IChatBaseComponent comp : pages) { |
341 346 | pagesString.add(CraftChatMessage.fromComponent(comp)); |
342 347 | } |
343 348 | builder.put(BOOK_PAGES.BUKKIT, pagesString); |
344 349 | } |
345 - | |
350 + | |
346 351 | if (generation != null) { |
347 352 | builder.put(GENERATION.BUKKIT, generation); |
348 353 | } |
349 354 | |
350 355 | return builder; |
351 356 | } |
352 357 | } |