From 35fa7e78a0d1d41fa49270674fbf49b79495eee2 Mon Sep 17 00:00:00 2001 From: EnZaXD Date: Mon, 22 Jul 2024 17:42:25 +0200 Subject: [PATCH] Handle attribute_modifiers inside container components in 1.21->1.20.5 (#4035) Closes https://github.com/ViaVersion/ViaVersion/issues/3968 --- .../rewriter/ComponentRewriter1_21.java | 42 ++++++++++++++----- .../viaversion/viaversion/util/TagUtil.java | 5 +++ 2 files changed, 37 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 0c3128037..0c5781b68 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 @@ -34,20 +34,42 @@ public final class ComponentRewriter1_21 extends ComponentRewriter modifiers = attributeModifiers.getListTag("modifiers", CompoundTag.class); - for (final CompoundTag modifier : modifiers) { - final String name = modifier.getString("name"); - final UUID uuid = UUIDUtil.fromIntArray(modifier.getIntArrayTag("uuid").getValue()); - final String id = Protocol1_20_5To1_21.mapAttributeUUID(uuid, name); - modifier.putString("id", id); + private void convertAttributeModifiersComponent(final CompoundTag tag) { + final CompoundTag attributeModifiers = TagUtil.getNamespacedCompoundTag(tag, "minecraft:attribute_modifiers"); + if (attributeModifiers == null) { + return; + } + final ListTag modifiers = attributeModifiers.getListTag("modifiers", CompoundTag.class); + for (final CompoundTag modifier : modifiers) { + final String name = modifier.getString("name"); + final UUID uuid = UUIDUtil.fromIntArray(modifier.getIntArrayTag("uuid").getValue()); + final String id = Protocol1_20_5To1_21.mapAttributeUUID(uuid, name); + modifier.putString("id", id); + } + } + + private void handleContainerComponent(final CompoundTag tag) { + final ListTag container = TagUtil.getNamespacedCompoundTagList(tag, "minecraft:container"); + if (container == null) { + return; + } + for (final CompoundTag entryTag : container) { + final CompoundTag itemTag = entryTag.getCompoundTag("item"); + + final CompoundTag componentsTag = itemTag.getCompoundTag("components"); + if (componentsTag != null) { + convertAttributeModifiersComponent(componentsTag); + handleContainerComponent(componentsTag); } } } + @Override + protected void handleShowItem(final UserConnection connection, final CompoundTag componentsTag) { + convertAttributeModifiersComponent(componentsTag); + handleContainerComponent(componentsTag); + } + @Override protected SerializerVersion inputSerializerVersion() { return SerializerVersion.V1_20_5; 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 c862fb81d..fa51e322e 100644 --- a/common/src/main/java/com/viaversion/viaversion/util/TagUtil.java +++ b/common/src/main/java/com/viaversion/viaversion/util/TagUtil.java @@ -64,6 +64,11 @@ public final class TagUtil { return compoundTag != null ? compoundTag : tag.getCompoundTag(Key.stripMinecraftNamespace(key)); } + public static @Nullable ListTag getNamespacedCompoundTagList(final CompoundTag tag, final String key) { + final ListTag listTag = tag.getListTag(Key.namespaced(key), CompoundTag.class); + return listTag != null ? listTag : tag.getListTag(Key.stripMinecraftNamespace(key), CompoundTag.class); + } + public static Tag handleDeep(final Tag tag, final TagUpdater consumer) { return handleDeep(null, tag, consumer); }