Solved How to make a Written Book

Discussion in 'Plugin Development' started by Jogy34, Oct 10, 2012.

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


    I was wondering how I would go about making a Written book including how to set the title, author, and what is written inside of it. I looked but I couldn't find anything in the JavaDocs on it.
  2. -- Removed everything that was written here b4 --

    Here's a plugin which does exactly what you want:
    1. package de.V10lator.BookTest;
    3. import net.minecraft.server.NBTTagList;
    4. import net.minecraft.server.NBTTagString;
    6. import org.bukkit.Material;
    7. import org.bukkit.command.Command;
    8. import org.bukkit.command.CommandSender;
    9. import org.bukkit.craftbukkit.inventory.CraftItemStack;
    10. import org.bukkit.entity.Player;
    11. import;
    13. public class BookTest extends JavaPlugin
    14. {
    15. public boolean onCommand(CommandSender sender, Command command,
    16. String label, String[] args)
    17. {
    18. if(!(sender instanceof Player))
    19. return true;
    20. CraftItemStack is = new CraftItemStack(Material.WRITTEN_BOOK);
    21. //NMS code start
    22. net.minecraft.server.ItemStack nmsItemStack = is.getHandle();
    23. net.minecraft.server.NBTTagCompound nbttc = new net.minecraft.server.NBTTagCompound();
    24. nbttc.setString("title", "Test Book");
    25. nbttc.setString("author", "V10lator");
    26. NBTTagList nbttaglist = new NBTTagList(); //This is where the pages have to be. Not sure how to add them...
    27. nbttaglist.add(new NBTTagString("page1", "This is page 1"));
    28. nbttaglist.add(new NBTTagString("page2", "This is page 2"));
    29. nbttaglist.add(new NBTTagString("page3", "This is page 3"));
    30. nbttc.set("pages", nbttaglist);
    31. nmsItemStack.setTag(nbttc);
    32. // NMS code end...
    33. ((Player)sender).getInventory().addItem(is);
    34. return true;
    35. }
    36. }
  3. Offline


    I tried a few different things and couldn't get it to work. Thanks for that though.
  4. Jogy34 The code is copy&pasteable, I have successfully compiled and used a plugin with this code. So what exactly is the problem?
  5. Offline


    I got the written book as an item it just doesn't have the title, author, or pages
  6. Jogy34 That's weird. When I tested it it worked just fine. :(
    Did you change anything at the codes (especially at the NMS part) ?

    //EDIT: Also did you make sure the title is < 16 characters?

    //EDIT²: Not sure if there are character limits for autor and text on a page. Please try it with as little text as possible first.
  7. Offline


    Turns out my problem was that I was dropping the book on the ground instead of giving it directly to a player

    Thanks it works now. Also, do you know how to get the information about a written book. Like a player has a book in his hand and I want to get the title, author, and pages
  8. Offline


    I found this cool API someone made. It is easy to use, very efficient, and lets you go beyond the soft-limits on book data.

    I used it to make a plugin that gives every new player a book describing the rules and available commands. It works wonderfully. :3
  9. Offline

    Ammar Askar

    For grabbing info out of the book you can do the following
    net.minecraft.server.NBTTagCompound nbttc = yourCraftItemStack.getTag();
    String title = nbttagcompound.getString("title");
    String author = nbttagcompound.getString("author");
    NBTTagList list = nbttc.getList("pages");
    List<String> pages = new ArrayList<String>();
    for(int i=0; i<list.size(); i++) {
        pages.add(((NBTTagString) list.get(i)).data);
  10. Offline


    How would that get information from out of a book? It looks like it's just making a new NBTTagCompound object then attempting to get a title, author, and pages from out of it
  11. Offline


    Here's what I use for pulling info out of a book:

    public Book(ItemStack book){
            NBTTagCompound tags = ((CraftItemStack) book).getHandle().tag;
            try {
       = tags.getString("author");
            } catch( NullPointerException e){
       = "Dear Mother";
            try {
                this.title = tags.getString("title");
            } catch( NullPointerException e){
                this.title = "Hearts and Kisses";
            try {
                NBTTagList tagPages = tags.getList("pages");
                for(int i = 0;i<tagPages.size();i++){
            } catch( NullPointerException e){
                this.pages.add("[This Page Left Intentionally Blank]");
    (Tinkered together a whole class:


    I have also found that I can't drop a book that I've created--it ends up empty and blank. I can place it directly into an inventory, though... Somebody suggested that I create *and drop* the book first, and then write the NBT to it. Which might work, but I haven't tested it yet.
  12. Offline


    Thanks I'll try that
  13. Offline


    Do you have Spout on your server?
  14. Offline


    No. The book looses it's data only when I drop it on the ground, instead of giving it directly to a player, with the world.dropItem() method
  15. Offline


    Interesting. I hit the same problem a couple of weeks back, and found it (the book going blank) only happened when SpoutPlugin was present. I was manually dropping the book though (pressing 'Q', or dying).
  16. Offline


    desht Jogy34

    I've found the same problem--but on my test server (which has only a single plugin, the plugin in question), so I don't think that it's a plugin incompatibility. Going to Eclipse to check a couple of other things...
  17. I re-checked it also: If you use world.spawnItem(Naturally) the book is empty. If you give it directly to the player it's no problem to drop it (Q) and take it back up.

    But I also didn't test what happens if you first use spawnItem, then set the content...
  18. Offline


  19. Offline


    Excuse me for the slight necromance, but thought some might appreciate this for simple things:

    If you need to copy an item from inventory to an item on the ground, you just need this small portion or similar:

    NBTTagCompound newBookData = ((CraftItemStackitem).getHandle().tag;
    Item droppedBook p.getWorld().dropItemNaturally(p.getLocation(), new ItemStack(Material.WRITTEN_BOOK));
  20. Offline


    Thank you so much
Thread Status:
Not open for further replies.

Share This Page