From 68db0c9853994929472dda746d5021de25705d30 Mon Sep 17 00:00:00 2001 From: Bjarne Koll <32834385+lynxplay@users.noreply.github.com> Date: Mon, 9 Aug 2021 00:57:20 +0200 Subject: [PATCH] Support complex components in written book builder (#6337) The previously existing toBuilder method defined on the CraftMetaBook would, no matter if called on a writable or written book, return a builder targetting a writable book, in which complex components, such as hover or click text are not allowed. The builer hence serializes the page components using LEGACY_UXRC to ensure only colour may be passed. While this works as intented for writable books, the builder fails to fully support the complex components that may be used in a written book. This commit implemements a child class of the CraftMetaBookBuilder, the CraftMetaBookSignedBuilder, which builds to a CraftMetaBookSigned instance and hence serializes the pages to json. This builder instance is automatically supplied when calling toBuilder on a CraftMetaBookSigned instance. Resolves: #6296 --- patches/server/Adventure.patch | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/patches/server/Adventure.patch b/patches/server/Adventure.patch index 70d8e8da93..7ed080d7a3 100644 --- a/patches/server/Adventure.patch +++ b/patches/server/Adventure.patch @@ -2816,7 +2816,7 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + this.pages = pages.subList(0, Math.min(MAX_PAGES, pages.size())).stream().map(io.papermc.paper.adventure.PaperAdventure.LEGACY_SECTION_UXRC::serialize).collect(java.util.stream.Collectors.toList()); + } + -+ static final class CraftMetaBookBuilder implements BookMetaBuilder { ++ static class CraftMetaBookBuilder implements BookMetaBuilder { + private net.kyori.adventure.text.Component title = null; + private net.kyori.adventure.text.Component author = null; + private final List<net.kyori.adventure.text.Component> pages = new java.util.ArrayList<>(); @@ -2853,6 +2853,10 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 + + @Override + public BookMeta build() { ++ return this.build(title, author, pages); ++ } ++ ++ protected BookMeta build(net.kyori.adventure.text.Component title, net.kyori.adventure.text.Component author, java.util.List<net.kyori.adventure.text.Component> pages) { + return new CraftMetaBook(title, author, pages); + } + } @@ -2896,6 +2900,28 @@ index 0000000000000000000000000000000000000000..00000000000000000000000000000000 super.serialize(builder); return builder; } ++ ++ // Paper start - adventure ++ private CraftMetaBookSigned(net.kyori.adventure.text.Component title, net.kyori.adventure.text.Component author, java.util.List<net.kyori.adventure.text.Component> pages) { ++ super((org.bukkit.craftbukkit.inventory.CraftMetaItem) org.bukkit.Bukkit.getItemFactory().getItemMeta(Material.WRITABLE_BOOK)); ++ this.title = title == null ? null : io.papermc.paper.adventure.PaperAdventure.LEGACY_SECTION_UXRC.serialize(title); ++ this.author = author == null ? null : io.papermc.paper.adventure.PaperAdventure.LEGACY_SECTION_UXRC.serialize(author); ++ this.pages = io.papermc.paper.adventure.PaperAdventure.asJson(pages.subList(0, Math.min(MAX_PAGES, pages.size()))); ++ } ++ ++ static final class CraftMetaBookSignedBuilder extends CraftMetaBookBuilder { ++ @Override ++ protected BookMeta build(net.kyori.adventure.text.Component title, net.kyori.adventure.text.Component author, java.util.List<net.kyori.adventure.text.Component> pages) { ++ return new CraftMetaBookSigned(title, author, pages); ++ } ++ } ++ ++ @Override ++ public BookMetaBuilder toBuilder() { ++ return new CraftMetaBookSignedBuilder(); ++ } ++ // Paper end + } diff --git a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java b/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java index 0000000000000000000000000000000000000000..0000000000000000000000000000000000000000 100644 --- a/src/main/java/org/bukkit/craftbukkit/inventory/CraftMetaItem.java