From 22df29225833bd65bb9137829a57e490258ed045 Mon Sep 17 00:00:00 2001 From: Thinkofdeath Date: Sun, 30 Nov 2014 15:06:14 +0000 Subject: [PATCH] Add a somewhat hacky fix for plugins converting between book types --- .../craftbukkit/inventory/CraftItemStack.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java index b49c2dca22..59be14b766 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java +++ b/src/main/java/org/bukkit/craftbukkit/inventory/CraftItemStack.java @@ -20,6 +20,10 @@ import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.meta.ItemMeta; import com.google.common.collect.ImmutableMap; +import net.minecraft.server.ChatSerializer; +import net.minecraft.server.Items; +import net.minecraft.server.NBTTagString; +import org.bukkit.craftbukkit.util.CraftChatMessage; @DelegateDeserialization(ItemStack.class) public final class CraftItemStack extends ItemStack { @@ -374,6 +378,42 @@ public final class CraftItemStack extends ItemStack { item.setTag(tag); ((CraftMetaItem) itemMeta).applyToItem(tag); + + // Hacky fix for books + // TODO: Not use a hacky fix for books + if (tag.getBoolean(CraftMetaBook.RESOLVED.NBT) && item.getItem() == Items.WRITABLE_BOOK) { + if (tag.hasKey(CraftMetaBook.BOOK_PAGES.NBT)) { + NBTTagList pages = tag.getList(CraftMetaBook.BOOK_PAGES.NBT, 8); + + for (int i = 0; i < pages.size(); i++) { + String page = pages.getString(i); + page = CraftChatMessage.fromComponent(ChatSerializer.a(page)); + pages.a(i, new NBTTagString(page)); + } + tag.set(CraftMetaBook.BOOK_PAGES.NBT, pages); + } + tag.setBoolean(CraftMetaBook.RESOLVED.NBT, false); + } else if (!tag.getBoolean(CraftMetaBook.RESOLVED.NBT) && item.getItem() == Items.WRITTEN_BOOK) { + if (tag.hasKey(CraftMetaBook.BOOK_PAGES.NBT)) { + NBTTagList pages = tag.getList(CraftMetaBook.BOOK_PAGES.NBT, 8); + + for (int i = 0; i < pages.size(); i++) { + String page = pages.getString(i); + page = ChatSerializer.a(CraftChatMessage.fromString(page, true)[0]); + pages.a(i, new NBTTagString(page)); + } + tag.set(CraftMetaBook.BOOK_PAGES.NBT, pages); + } + + tag.setBoolean(CraftMetaBook.RESOLVED.NBT, true); + if (!tag.hasKey(CraftMetaBook.BOOK_TITLE.NBT)) { + tag.setString(CraftMetaBook.BOOK_TITLE.NBT, ""); + } + if (!tag.hasKey(CraftMetaBook.BOOK_AUTHOR.NBT)) { + tag.setString(CraftMetaBook.BOOK_AUTHOR.NBT, ""); + } + } + return true; }