Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static final String serialize(ItemStack book) {
- // Require that we get a written book for serialization
- if (book.itemID != Item.writtenBook.itemID) {
- throw new IllegalArgumentException("WrittenBookTag.serialize(ItemStack) must be passed an ItemStack holding a written book");
- }
- NBTTagCompound tag = book.getTagCompound();
- // Play it safe
- if (tag == null) {
- tag = new NBTTagCompound();
- }
- // This is where the serialized content will go
- StringBuffer data = new StringBuffer();
- // Start with the title, then the author
- // 'sep' is a String holding the system line separator
- data.append(tag.getString("title") + sep + tag.getString("author") + sep);
- // Get the pages in the book
- NBTTagList pages = tag.getTagList("pages");
- // Find out how many times we should loop
- int tagCount = pages.tagCount();
- // Debug line, print the number of pages
- err.println("Beginning serialization of " + tagCount + " pages");
- // Start serialization
- for (int i = 0; i < tagCount; i++ ) {
- // Catch exceptions
- try {
- // More debug
- err.println("Getting page " + i);
- // Don't operate on a null page, that'll just break things
- if ((pages.tagAt(i) == null) || (pages.tagAt(i).toString() == null)) {
- err.println("Null page found, terminating loop");
- break;
- }
- // Debug again; current page index (zero-based) and the content
- // This always gives the correct index and the content of the FIRST page,
- // every time! The loop counter increments correctly, but I can't get any
- // page after the first one.
- err.println("Current page (" + i + "): " + pages.tagAt(i).toString());
- }
- // NBTTagList directly uses a List (untyped), and does no bounds checking,
- // so catch Index OOB here, just in case. Never actually happened to me,
- // but better safe than sorry.
- catch (IndexOutOfBoundsException e) {
- err.println("Index " + i + " OOB, terminating");
- break;
- }
- // Same deal, if we get a null pointer, don't explode.
- catch (NullPointerException e) {
- err.println("NPE on index " + i + ", terminating");
- break;
- }
- // Another bit of debug...
- err.println("Appending page to serialized string");
- // The string "--PAGEBREAK--" is used to indicate that a new page should be started.
- // It makes the serialized form a bit more human-readable.
- data.append((i > 0 ? sep + "--PAGEBREAK--" + sep : "") + pages.tagAt(i).toString());
- }
- // End of serialization, print a message, return the result
- err.println("Serialization complete");
- return data.toString();
- }
Advertisement
Add Comment
Please, Sign In to add comment