1.15.2 json bookmeta

Discussion in 'Spigot Plugin Development' started by Bennooo, Feb 7, 2020.

  1. Hello spigot,

    Is there a way to use json for book meta?
    I already tried 2 methods.
    Code (Text):
    public void openBook(Player player, String configName){
            ItemStack book = new ItemStack(Material.WRITTEN_BOOK, 1);
            BookMeta bookMeta = (BookMeta) book.getItemMeta();
            if(bookMeta != null){
                String title = BookMenu.getInstance().getConfig().getString(configName + ".title");
                String author = BookMenu.getInstance().getConfig().getString(configName + ".author");
                bookMeta.addPage("This would be the JSON Text");
    After some research I found this:
    Code (Text):
    public void openBook(Player player, String configName){
            ItemStack book = new ItemStack(Material.BOOK);
            net.minecraft.server.v1_15_R1.ItemStack stack = CraftItemStack.asNMSCopy(book);
            NBTTagCompound tag = new NBTTagCompound();
            tag.setString("title", "someBookTitle");
            tag.setString("author", "someAuthor");
            NBTTagList pages = new NBTTagList();
            pages.add(new NBTTagString("{text:\"Hello there!\",color:blue}"));
            pages.add(new NBTTagString("{text:\"Another page :O\"}"));
            tag.set("pages", pages);
            ItemStack is = CraftItemStack.asCraftMirror(stack);
    But this doesn't work cuz of: http://prntscr.com/qz25mw
    Thanks for reading.
  2. If you really want to use NBTTagString, you could use reflection. With that, you can use the constructor, whether it's private or not.
  3. Using spigot, you can use BookMeta#spigot and then work with ChatComponents (and I'm pretty sure you can serialize/deserialize them from JSON).
    If you have still have to use NBT, an NBTTagString is constructed using the static method
    • Agree Agree x 1
  4. Idk, but I don't find anything to serialize
  5. You can use Gson for that but you're going to have to work with Json objects, arrays, etc...
  6. Look at BaseComponentSerializer
  7. Can't be used for the bookmeta.
  8. But you can use BaseComponents. And you can serialize those or am i wrong?
    • Agree Agree x 1
  9. Sorry, u were completely right! It worked, but when I want to add a new line with a comma between 2 json texts it generated this error:

    Code (Text):
    Caused by: com.google.gson.stream.MalformedJsonException: Use JsonReader.setLenient(true) to accept malformed JSON at line 1 column 72 path $
        at com.google.gson.stream.JsonReader.syntaxError(JsonReader.java:1559) ~[patched_1.15.2.jar:git-Paper-93]
        at com.google.gson.stream.JsonReader.checkLenient(JsonReader.java:1401) ~[patched_1.15.2.jar:git-Paper-93]
        at com.google.gson.stream.JsonReader.doPeek(JsonReader.java:542) ~[patched_1.15.2.jar:git-Paper-93]
        at com.google.gson.stream.JsonReader.peek(JsonReader.java:425) ~[patched_1.15.2.jar:git-Paper-93]
        at com.google.gson.JsonParser.parse(JsonParser.java:60) ~[patched_1.15.2.jar:git-Paper-93]
        at com.google.gson.JsonParser.parse(JsonParser.java:45) ~[patched_1.15.2.jar:git-Paper-93]
        at net.md_5.bungee.chat.ComponentSerializer.parse(ComponentSerializer.java:37) ~[patched_1.15.2.jar:git-Paper-93]
        at me.absurd.bookmenu.commands.BookMenuCMD.openBook(BookMenuCMD.java:56) ~[?:?]
        at me.absurd.bookmenu.commands.BookMenuCMD.onCommand(BookMenuCMD.java:34) ~[?:?]
        at org.bukkit.command.PluginCommand.execute(PluginCommand.java:45) ~[patched_1.15.2.jar:git-Paper-93]
        ... 17 more
    It generates the error on:

    EDIT: It's because a comma isn't between {}, so I know what the bug is, but not how to fix it.
  10. Can you post your JSON-String?