Problem/Bug Need Help by NBTTagCompound for Book

Discussion in 'Bukkit Help' started by AaaGamer1, Jul 14, 2016.

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

    AaaGamer1

    Hi i have a Question how i format a String so that he match an NBT Tag.

    Here the Method how I create the NBTTagCompound

    public TecBook(String Author,String Name,NBTTagString... page) {
    ItemStack i = CraftItemStack.asNMSCopy(new org.bukkit.inventory.ItemStack(Material.WRITTEN_BOOK, 1, (short)0));
    NBTTagCompound c = new NBTTagCompound();
    c.setString("author", Author);
    c.setString("title", Name);
    NBTTagList l = new NBTTagList();
    for(int i3 = 0; i3 < page.length; i3++){
    l.add(page[i3]);
    }
    Bukkit.broadcastMessage(l.toString());
    c.set("pages", l);
    i.setTag(c);
    h = CraftItemStack.asBukkitCopy(i);
    }

    I use as Parameters


    TecBook t = new TecBook("Hallo", "Hallo",new NBTTagString("{\"text\":\"Test\",\"bold\":\"true\",\"italic\":\"true\",\"underlined\":\"true\",\"color\":\"dark_blue\",\"hoverEvent\":{\"action\":\"show_text\",\"value\":{\"text\":\"Hallo!\"}},\"clickEvent\":{\"action\":\"run_command\",\"value\":\"/say hi\"}}"));
    p.getInventory().addItem(t.h);

    But there stand always in the book * Invalid book tag *


    Please help.
     
  2. Offline

    I Al Istannen

    @AaaGamer1
    I did something like this a while back for a Plugin called "Novel Books", which allowed more pages in books.

    In the version I used:

    The field "pages" is a List of type "ChatCompontText".
    This means, you just add new ChatComponentsText to it.

    You first get the ItemMeta of the bukkit book ItemStack and cast it to "BookMeta". Save it in a variable (in my case "meta").

    Done by getting the list of pages (Field "pages"):
    "List<Object> list = ReflectionUtils.getListFromBookMeta(meta);" (As you did, ReflectionUtils is a class of mine)
    Notice we get the pages from the meta, not directly from the itemstack!

    Then converting all the Strings you want the book to display to ChatComponentText (The class has a constructor taking a String. This string is a normal chat messages, what you get after calling ChatColor#translateAlternateColorCodes.

    And then adding this to the List "list".

    In my case:
    Code:
    <String stream with the colored lines>.map(ReflectionUtils::getChatComponentText).forEach(list::add);
    Getting the ChatComponentText with reflection is a bit more difficult, I can explain it too if you want. For testing you can just import it and use it directly. In this case you just write "new ChatComponentText(String <colored text>)"

    Now you have changed the list. Great. But you need to apply it to the item!"
    bookItemStack.setItemMeta(meta);" ==> Done
    This works as the pages is saved in the ItemMeta and Bukkit does the rest for you!

    And you are done. Nice, isn't it?
    As I probably explained this quite badly, just shout and name the part you didn't understand!
     
  3. Offline

    AaaGamer1

    Thanks really,

    for only Colors it is easier to use just the '§' Letter, the ChatComponentText is nice for working, thanks for information :)

    I have not understand all right....
    when i get the List its a list of Strings and not of Objects....
    the other is how i get the stream of colored lines?
    that is what I've tried

    ItemStack item = new ItemStack(Material.WRITTEN_BOOK, 1);
    BookMeta bm = (BookMeta) item.getItemMeta();
    ChatComponentText c = new ChatComponentText("Test");
    c.setChatModifier(new ChatModifier().setChatClickable(new ChatClickable(EnumClickAction.RUN_COMMAND, "/say hi")).setColor(EnumChatFormat.AQUA).setInsertion("Bye"));
    String[] s = new String[1];
    s[0] = c.toString();
    bm.addPage(s);
    item.setItemMeta(bm);
    p.getInventory().addItem(item);


    and how i can make code lines?
    Code:
    Test
    You can send raw Code I will understanding it better than subscribing I think

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: Jul 14, 2016
  4. Offline

    I Al Istannen

    @AaaGamer1
    While the § may be easier, don't use it :) Make a color method instead, using ChatColor#translateAlternateColorCodes('&', text) instead. Then just use "color("&a&lLight green, bold");" instead.

    This has two main reasons:
    1. On different computers and file encodings, the unicode char "§" may break.
    2. In the future the character might be changed, rendering your plugin inable to print colors.
    Good luck with it :)
     
Thread Status:
Not open for further replies.

Share This Page