From 1a9cda05798294b5bfc4589420e53eae75998fab Mon Sep 17 00:00:00 2001 From: EnZaXD Date: Thu, 10 Oct 2024 22:42:21 +0200 Subject: [PATCH] Handle WRITTEN_BOOK_CONTENT component in rewriters as well (#4175) Closes https://github.com/ViaVersion/ViaVersion/issues/4144 --- .../rewriter/ComponentRewriter.java | 38 ++++++++++++++++++- .../rewriter/StructuredItemRewriter.java | 12 ++++++ 2 files changed, 49 insertions(+), 1 deletion(-) diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java index 5aa31652b..59dd5a010 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java @@ -39,8 +39,8 @@ import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets; import com.viaversion.viaversion.util.ComponentUtil; import com.viaversion.viaversion.util.SerializerVersion; import com.viaversion.viaversion.util.TagUtil; -import org.checkerframework.checker.nullness.qual.Nullable; import java.util.BitSet; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Handles json and tag components, containing methods to override certain parts of the handling. @@ -360,6 +360,8 @@ public class ComponentRewriter implements com.v handleShowItem(connection, contentsTag, componentsTag); if (componentsTag != null) { handleContainerContents(connection, componentsTag); + handleWrittenBookContents(connection, componentsTag); + handleItemArrayContents(connection, componentsTag, "bundle_contents"); handleItemArrayContents(connection, componentsTag, "charged_projectiles"); } @@ -386,6 +388,40 @@ public class ComponentRewriter implements com.v } } + protected void handleWrittenBookContents(final UserConnection connection, final CompoundTag tag) { + final CompoundTag book = TagUtil.getNamespacedCompoundTag(tag, "minecraft:written_book_content"); + if (book == null) { + return; + } + + final ListTag pagesTag = book.getListTag("pages", CompoundTag.class); + if (pagesTag == null) { + return; + } + + for (final CompoundTag compoundTag : pagesTag) { + final StringTag raw = compoundTag.getStringTag("raw"); + processJsonString(connection, raw); + + final StringTag filtered = compoundTag.getStringTag("filtered"); + processJsonString(connection, filtered); + } + } + + private void processJsonString(final UserConnection connection, final StringTag tag) { + if (tag == null) { + return; + } + + final var input = inputSerializerVersion(); + final var output = outputSerializerVersion(); + + final Tag asTag = input.toTag(input.toComponent(tag.getValue())); + processTag(connection, asTag); + + tag.setValue(output.toString(output.toComponent(asTag))); + } + protected void handleItemArrayContents(final UserConnection connection, final CompoundTag tag, final String key) { final ListTag container = TagUtil.getNamespacedCompoundTagList(tag, key); if (container == null) { diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java index fe3b72e63..8294702ba 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java @@ -28,6 +28,8 @@ import com.viaversion.viaversion.api.minecraft.data.StructuredData; import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer; import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey; import com.viaversion.viaversion.api.minecraft.item.Item; +import com.viaversion.viaversion.api.minecraft.item.data.FilterableComponent; +import com.viaversion.viaversion.api.minecraft.item.data.WrittenBook; import com.viaversion.viaversion.api.protocol.Protocol; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; @@ -138,6 +140,16 @@ public class StructuredItemRewriter