JadeSkies

Written Book Serialization (Destructive Copy Version)

Mar 8th, 2014
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.72 KB | None | 0 0
  1. public static final String serialize(ItemStack book) {
  2.     // Require that we get a written book for serialization
  3.     if (book.itemID != Item.writtenBook.itemID) {
  4.         throw new IllegalArgumentException("WrittenBookTag.serialize(ItemStack) must be passed an ItemStack holding a written book");
  5.     }
  6.     NBTTagCompound tag = book.getTagCompound();
  7.     // Play it safe
  8.     if (tag == null) {
  9.         tag = new NBTTagCompound();
  10.     }
  11.     // This is where the serialized content will go
  12.     StringBuffer data = new StringBuffer();
  13.     // Start with the title, then the author
  14.     // 'sep' is a String holding the system line separator
  15.     data.append(tag.getString("title") + sep + tag.getString("author") + sep);
  16.     // Make a copy of the tags, so we don't mess around with the actual book
  17.     NBTTagList pages = (NBTTagList) tag.getTagList("pages").copy();
  18.     // Find out how many times we should loop
  19.     int tagCount = pages.tagCount();
  20.     // Debug line, print the number of pages
  21.     err.println("Beginning serialization of " + tagCount + " pages");
  22.     // Start serialization
  23.     for (int i = 0; i < tagCount; i++ ) {
  24.         // Catch exceptions
  25.         try {
  26.             // More debug
  27.             err.println("Getting page " + i);
  28.             // Don't operate on a null page, that'll just break things
  29.             if ((pages.tagAt(0) == null) || (pages.tagAt(0).toString() == null)) {
  30.                 err.println("Null page found, terminating loop");
  31.                 break;
  32.             }
  33.             // Debug again; current page index (zero-based) and the content
  34.             // This always gives the correct index and the content of the FIRST page,
  35.             // every time! The loop counter increments correctly, but I can't get any
  36.             // page after the first one.
  37.             err.println("Current page (" + i + "): " + pages.tagAt(0).toString());
  38.         }
  39.         // NBTTagList directly uses a List (untyped), and does no bounds checking,
  40.         // so catch Index OOB here, just in case. Never actually happened to me,
  41.         // but better safe than sorry.
  42.         catch (IndexOutOfBoundsException e) {
  43.             err.println("Index " + i + " OOB, terminating");
  44.             break;
  45.         }
  46.         // Same deal, if we get a null pointer, don't explode.
  47.         catch (NullPointerException e) {
  48.             err.println("NPE on index " + i + ", terminating");
  49.             break;
  50.         }
  51.         // Another bit of debug...
  52.         err.println("Appending page to serialized string");
  53.         // The string "--PAGEBREAK--" is used to indicate that a new page should be started.
  54.         // It makes the serialized form a bit more human-readable.
  55.         data.append((i > 0 ? sep + "--PAGEBREAK--" + sep : "") + pages.tagAt(0).toString());
  56.         // This is a DEstructive version, so remove the first page (calls ".remove(0)" on
  57.         // the list holding NBT tag internally)
  58.         pages.removeTag(0);
  59.     }
  60.     // End of serialization, print a message, return the result
  61.     err.println("Serialization complete");
  62.     return data.toString();
  63. }
Advertisement
Add Comment
Please, Sign In to add comment