Save WrittenBooks

Discussion in 'Plugin Development' started by Benni1000, Sep 1, 2012.

Thread Status:
Not open for further replies.
  1. Offline

    Benni1000

    Hello,

    is there a way to virtually save books?
    I tried getting the damage value of the books but no book has a damage value.
    What values must i save to recreate a book out of plain text?

    Thanks
     
  2. Offline

    CorrieKay

    Last i remember, there isnt really a fleshed out api for it yet, so youre gonna have to delve deeper, but heres some code on how to WRITE a book (you can probably figure out how to READ from this code)

    edit: it works similarly to the configuration api, actually.



    ItemStack book = //Get your book somehow
    CraftItemStack stack = (CraftItemStack)book; //gets the minecraft object book
    NBTTagCompound bookData = stack.getHandle().getTag(); //gets the tag compound

    Now, from here, im not sure how to get the array list of pages. Im fairly sure you can get the list via:
    bookData.getList("pages");

    However, i have no idea how to get the pages text from that, as it returns an NBTTagList. Lemme see if i can find anything for ya

    edit2:

    Okay heres what i think ive figured out:

    first up, you can use it similarly to a list of sorts. Since it has a method for figuring out how many indices there are, and a method for returning the value at a specific one, you can do this:

    for(int i = 0; i<bookData.size() ; i++){
    NBTBase base = bookData.get(i);
    }

    Now, and i must stress, this is 100% not tested so far, but, NBTBase's are basically a superclass to all NBT value types ( i think ) as such, in this situation, i believe these NBTBase objects that it returns will be NBTTagStrings. So, try this:

    NBTBase base = bookData.get(i);
    NBTTagString nbtstring = (NBTTagString)base;


    Once you've casted it to an nbt string, you can grab the string from it

    NBTTagString nbtstring = (NBTTagString)base;
    String page = nbtstring.data();

    This page will be the test per page. Also, conviniently enough, at this point, i+1 = page number, so theres that too!

    if you wanted to order the pages into a hashmap, you could do this:

    HashMap<Integer, String> pages;

    then add each page into the hashmap, inside the loop.

    pages.put(i+1,page);


    Alright so thats a lot of stuff. Lets clean up the code, and make it nice and pretty. In the next example, we'll save the entire book to a text file in the plugins data folder.
    Code:
    public void writeBookToFile(ItemStack book){
    //test if book
    if(!book.getType() == Material.WRITTEN_BOOK){
      return;
    }
     
    //get book data
    NBTTagCompound bookData = ((CraftItemStack)book).getHandle().getTag();
     
    //getting actual information!
    String author, title;
    author = bookData.getString("author");
    title = bookData.getString("title");
     
    //we'll store our pages here
    HashMap<Integer,String> pages = new HashMap<Integer,String>();
     
    //heres our pages in the actual object
    NBTTagList tagList = bookData.getList("pages");
     
    //grabbing pages
    for(int i = 0 ; tagList.size() ; i++){
      String page = ((NBTTagString)tagList.get(i)).data
      pages.put(i+1,page);
    }
     
    //creates a new file named by the title of the book. It will override any file that already exists there by that name though. careful.
    File txt = new File(plugin.getDataFolder(),title+".txt");
    txt.createNewFile();
     
    //create our writer
    BufferedWriter out = new BufferedWriter(new FileWriter(txt));
     
    //write stuff
    out.write("The title of this book is: " + title);
    out.newLine();
    out.write("It was written by: "+author);
    out.newLine();
     
    //write pages
    for(int i = 1 ; i<pages.size() ; i++){
      String page = pages.get(i);
      out.write("Page "+i);
      out.newLine();
      out.write(page);
      out.newLine();
    }
     
    //close
    out.close();
     
    }
     
  3. Offline

    Benni1000

    Thank you very much, this was really helpful!
    :)
     
Thread Status:
Not open for further replies.

Share This Page