From af8cbaf4394b9fb975cf943b72ed26df06f6b0d9 Mon Sep 17 00:00:00 2001 From: EnZaXD Date: Wed, 30 Oct 2024 10:31:45 +0100 Subject: [PATCH] Add item fixes to component rewriter in 1.21.x protocols as well (#4217) Adds the rarity fix in 1.20.5->1.21 and the item/custom name fix in 1.21->1.21.2 to the component rewriters / chat items. --- .../rewriter/ComponentRewriter1_21.java | 30 ++++++++++++------- .../rewriter/ComponentRewriter1_21_2.java | 22 ++++++++++++++ .../viaversion/viaversion/util/TagUtil.java | 4 +++ 3 files changed, 46 insertions(+), 10 deletions(-) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_5to1_21/rewriter/ComponentRewriter1_21.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_5to1_21/rewriter/ComponentRewriter1_21.java index 409895843..031bf9529 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_5to1_21/rewriter/ComponentRewriter1_21.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_5to1_21/rewriter/ComponentRewriter1_21.java @@ -19,10 +19,12 @@ package com.viaversion.viaversion.protocols.v1_20_5to1_21.rewriter; import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.ListTag; +import com.viaversion.nbt.tag.StringTag; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ClientboundPacket1_20_5; import com.viaversion.viaversion.protocols.v1_20_5to1_21.Protocol1_20_5To1_21; import com.viaversion.viaversion.rewriter.ComponentRewriter; +import com.viaversion.viaversion.util.Key; import com.viaversion.viaversion.util.SerializerVersion; import com.viaversion.viaversion.util.TagUtil; import com.viaversion.viaversion.util.UUIDUtil; @@ -34,8 +36,24 @@ public final class ComponentRewriter1_21 extends ComponentRewriter { @@ -62,6 +64,26 @@ public final class ComponentRewriter1_21_2 extends ComponentRewriter(Collections.singletonList(nameTag))); + + componentsTag.put("minecraft:custom_name", new StringTag(output.toString(output.toComponent(name)))); + } } public static void convertAttributes(final CompoundTag componentsTag, final FullMappings mappings) { diff --git a/common/src/main/java/com/viaversion/viaversion/util/TagUtil.java b/common/src/main/java/com/viaversion/viaversion/util/TagUtil.java index ac34fe676..bd20aa0f5 100644 --- a/common/src/main/java/com/viaversion/viaversion/util/TagUtil.java +++ b/common/src/main/java/com/viaversion/viaversion/util/TagUtil.java @@ -60,6 +60,10 @@ public final class TagUtil { return tag.remove(Key.namespaced(key)) != null || tag.remove(Key.stripMinecraftNamespace(key)) != null; } + public static boolean containsNamespaced(final CompoundTag tag, final String key) { + return tag.contains(Key.namespaced(key)) || tag.contains(Key.stripMinecraftNamespace(key)); + } + public static @Nullable CompoundTag getNamespacedCompoundTag(final CompoundTag tag, final String key) { final CompoundTag compoundTag = tag.getCompoundTag(Key.namespaced(key)); return compoundTag != null ? compoundTag : tag.getCompoundTag(Key.stripMinecraftNamespace(key));