From a9b9c48b26aea30af82d2dc8d298e1ce2e031356 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Wed, 29 May 2024 19:50:40 +0200 Subject: [PATCH] Handle item changes in show item text components --- .../viabackwards/api/BackwardsProtocol.java | 3 +- .../api/rewriters/BackwardsItemRewriter.java | 111 ++---------------- .../rewriters/BackwardsItemRewriterBase.java | 34 ------ .../BackwardsStructuredItemRewriter.java | 91 +++++++------- .../api/rewriters/EntityRewriterBase.java | 4 +- .../StructuredEnchantmentRewriter.java | 14 +-- .../v1_14to1_13_2/Protocol1_14To1_13_2.java | 2 +- .../v1_15to1_14_4/Protocol1_15To1_14_4.java | 2 +- .../Protocol1_16_2To1_16_1.java | 2 +- .../v1_16to1_15_2/Protocol1_16To1_15_2.java | 2 +- .../rewriter/BlockItemPacketRewriter1_16.java | 2 +- .../rewriter/EntityPacketRewriter1_16.java | 6 +- .../v1_17to1_16_4/Protocol1_17To1_16_4.java | 2 +- .../v1_18to1_17_1/Protocol1_18To1_17_1.java | 2 +- .../v1_19_1to1_19/Protocol1_19_1To1_19.java | 2 +- .../Protocol1_19_3To1_19_1.java | 2 +- .../Protocol1_19_4To1_19_3.java | 2 +- .../BlockItemPacketRewriter1_19_4.java | 3 +- .../rewriter/EntityPacketRewriter1_19_4.java | 2 +- .../v1_19to1_18_2/Protocol1_19To1_18_2.java | 2 +- .../Protocol1_20_3To1_20_2.java | 2 +- .../Protocol1_20_5To1_20_3.java | 2 +- .../v1_20to1_19_4/Protocol1_20To1_19_4.java | 2 +- .../v1_21to1_20_5/Protocol1_21To1_20_5.java | 6 +- .../rewriter/ComponentRewriter1_21.java | 64 ++++++++++ .../template/protocol/Protocol1_98To1_99.java | 2 +- 26 files changed, 157 insertions(+), 211 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/rewriter/ComponentRewriter1_21.java diff --git a/common/src/main/java/com/viaversion/viabackwards/api/BackwardsProtocol.java b/common/src/main/java/com/viaversion/viabackwards/api/BackwardsProtocol.java index db3dc5ca..64ade316 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/BackwardsProtocol.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/BackwardsProtocol.java @@ -65,7 +65,8 @@ public abstract class BackwardsProtocol getTranslatableRewriter() { + @Override + public @Nullable TranslatableRewriter getComponentRewriter() { return null; } } diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsItemRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsItemRewriter.java index 43c527e8..db9ba4e1 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsItemRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsItemRewriter.java @@ -17,16 +17,6 @@ */ package com.viaversion.viabackwards.api.rewriters; -import com.viaversion.viabackwards.api.BackwardsProtocol; -import com.viaversion.viabackwards.api.data.MappedItem; -import com.viaversion.viaversion.api.connection.UserConnection; -import com.viaversion.viaversion.api.minecraft.item.Item; -import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; -import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; -import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; -import com.viaversion.viaversion.api.type.Type; -import com.viaversion.viaversion.api.type.Types; -import com.viaversion.viaversion.libs.gson.JsonElement; import com.viaversion.nbt.tag.ByteTag; import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.IntTag; @@ -34,6 +24,13 @@ import com.viaversion.nbt.tag.ListTag; import com.viaversion.nbt.tag.NumberTag; import com.viaversion.nbt.tag.StringTag; import com.viaversion.nbt.tag.Tag; +import com.viaversion.viabackwards.api.BackwardsProtocol; +import com.viaversion.viabackwards.api.data.MappedItem; +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.item.Item; +import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; +import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; +import com.viaversion.viaversion.api.type.Type; import org.checkerframework.checker.nullness.qual.Nullable; public class BackwardsItemRewriter { - wrapper.passthrough(Types.BOOLEAN); // Reset/clear - final int size = wrapper.passthrough(Types.VAR_INT); // Mapping size - for (int i = 0; i < size; i++) { - wrapper.passthrough(Types.STRING); // Identifier - wrapper.passthrough(Types.OPTIONAL_STRING); // Parent - - // Display data - if (wrapper.passthrough(Types.BOOLEAN)) { - final JsonElement title = wrapper.passthrough(Types.COMPONENT); - final JsonElement description = wrapper.passthrough(Types.COMPONENT); - final TranslatableRewriter translatableRewriter = protocol.getTranslatableRewriter(); - if (translatableRewriter != null) { - translatableRewriter.processText(wrapper.user(), title); - translatableRewriter.processText(wrapper.user(), description); - } - - final Item icon = handleItemToClient(wrapper.user(), wrapper.read(itemType())); - wrapper.write(mappedItemType(), icon); - - wrapper.passthrough(Types.VAR_INT); // Frame type - int flags = wrapper.passthrough(Types.INT); // Flags - if ((flags & 1) != 0) { - wrapper.passthrough(Types.STRING); // Background texture - } - wrapper.passthrough(Types.FLOAT); // X - wrapper.passthrough(Types.FLOAT); // Y - } - - wrapper.passthrough(Types.STRING_ARRAY); // Criteria - - final int arrayLength = wrapper.passthrough(Types.VAR_INT); - for (int array = 0; array < arrayLength; array++) { - wrapper.passthrough(Types.STRING_ARRAY); // String array - } - } - }); - } - }); - } - - @Override - public void registerAdvancements1_20_3(final C packetType) { - // Insert translatable rewriter - protocol.registerClientbound(packetType, wrapper -> { - wrapper.passthrough(Types.BOOLEAN); // Reset/clear - final int size = wrapper.passthrough(Types.VAR_INT); // Mapping size - for (int i = 0; i < size; i++) { - wrapper.passthrough(Types.STRING); // Identifier - wrapper.passthrough(Types.OPTIONAL_STRING); // Parent - - // Display data - if (wrapper.passthrough(Types.BOOLEAN)) { - final Tag title = wrapper.passthrough(Types.TAG); - final Tag description = wrapper.passthrough(Types.TAG); - final TranslatableRewriter translatableRewriter = protocol.getTranslatableRewriter(); - if (translatableRewriter != null) { - translatableRewriter.processTag(wrapper.user(), title); - translatableRewriter.processTag(wrapper.user(), description); - } - - final Item icon = handleItemToClient(wrapper.user(), wrapper.read(itemType())); - wrapper.write(mappedItemType(), icon); - - wrapper.passthrough(Types.VAR_INT); // Frame type - final int flags = wrapper.passthrough(Types.INT); - if ((flags & 1) != 0) { - wrapper.passthrough(Types.STRING); // Background texture - } - wrapper.passthrough(Types.FLOAT); // X - wrapper.passthrough(Types.FLOAT); // Y - } - - final int requirements = wrapper.passthrough(Types.VAR_INT); - for (int array = 0; array < requirements; array++) { - wrapper.passthrough(Types.STRING_ARRAY); - } - - wrapper.passthrough(Types.BOOLEAN); // Send telemetry - } - }); - } } diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsItemRewriterBase.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsItemRewriterBase.java index 6de9d1fb..abfeaaf0 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsItemRewriterBase.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsItemRewriterBase.java @@ -75,29 +75,6 @@ public abstract class BackwardsItemRewriterBase original, String name) { - // List tags cannot contain tags of different types, so we have to store them a bit more awkwardly as an indexed compound tag - String backupName = nbtTagName(name); - if (!tag.contains(backupName)) { - CompoundTag output = new CompoundTag(); - for (int i = 0; i < original.size(); i++) { - output.put(Integer.toString(i), original.get(i)); - } - tag.put(backupName, output); - } - } - - protected List removeGenericTagList(CompoundTag tag, String name) { - String backupName = nbtTagName(name); - CompoundTag data = tag.getCompoundTag(backupName); - if (data == null) { - return null; - } - - tag.remove(backupName); - return new ArrayList<>(data.values()); - } - protected void restoreDisplayTag(Item item) { if (item.tag() == null) return; @@ -129,17 +106,6 @@ public abstract class BackwardsItemRewriterBase @Nullable ListTag removeListTag(CompoundTag tag, String tagName, Class tagType) { - String backupName = nbtTagName(tagName); - ListTag data = tag.getListTag(backupName, tagType); - if (data == null) { - return null; - } - - tag.remove(backupName); - return data; - } - @Override public String nbtTagName() { return "VB|" + protocol.getClass().getSimpleName(); diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsStructuredItemRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsStructuredItemRewriter.java index f328641e..9b24c27f 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsStructuredItemRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsStructuredItemRewriter.java @@ -19,6 +19,7 @@ package com.viaversion.viabackwards.api.rewriters; import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.IntTag; +import com.viaversion.nbt.tag.ListTag; import com.viaversion.nbt.tag.NumberTag; import com.viaversion.nbt.tag.Tag; import com.viaversion.viabackwards.api.BackwardsProtocol; @@ -33,12 +34,13 @@ import com.viaversion.viaversion.api.minecraft.item.Item; import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.protocol.packet.ServerboundPacketType; import com.viaversion.viaversion.api.type.Type; +import com.viaversion.viaversion.rewriter.StructuredItemRewriter; +import java.util.ArrayList; +import java.util.List; import org.checkerframework.checker.nullness.qual.Nullable; -import static com.viaversion.viaversion.rewriter.StructuredItemRewriter.updateItemComponents; - public class BackwardsStructuredItemRewriter> extends BackwardsItemRewriter { + T extends BackwardsProtocol> extends StructuredItemRewriter { public BackwardsStructuredItemRewriter(final T protocol, final Type itemType, final Type itemArrayType) { super(protocol, itemType, itemArrayType); @@ -62,12 +64,12 @@ public class BackwardsStructuredItemRewriter customNameData = dataContainer.getNonEmpty(StructuredDataKey.CUSTOM_NAME); if (customNameData != null) { final Tag originalName = customNameData.value().copy(); - protocol.getTranslatableRewriter().processTag(connection, customNameData.value()); + protocol.getComponentRewriter().processTag(connection, customNameData.value()); if (!customNameData.value().equals(originalName)) { saveTag(createCustomTag(item), originalName, "Name"); } @@ -76,7 +78,7 @@ public class BackwardsStructuredItemRewriter loreData = dataContainer.getNonEmpty(StructuredDataKey.LORE); if (loreData != null) { for (final Tag tag : loreData.value()) { - protocol.getTranslatableRewriter().processTag(connection, tag); + protocol.getComponentRewriter().processTag(connection, tag); } } } @@ -141,7 +143,7 @@ public class BackwardsStructuredItemRewriter customData = data.getNonEmpty(StructuredDataKey.CUSTOM_DATA); - if (customData != null) { - return customData.value(); + protected void saveListTag(CompoundTag tag, ListTag original, String name) { + // Multiple places might try to backup data + String backupName = nbtTagName(name); + if (!tag.contains(backupName)) { + tag.put(backupName, original.copy()); + } + } + + public @Nullable ListTag removeListTag(CompoundTag tag, String tagName, Class tagType) { + String backupName = nbtTagName(tagName); + ListTag data = tag.getListTag(backupName, tagType); + if (data == null) { + return null; } - final CompoundTag tag = new CompoundTag(); - data.set(StructuredDataKey.CUSTOM_DATA, tag); - return tag; + tag.remove(backupName); + return data; + } + + protected void saveGenericTagList(CompoundTag tag, List original, String name) { + // List tags cannot contain tags of different types, so we have to store them a bit more awkwardly as an indexed compound tag + String backupName = nbtTagName(name); + if (!tag.contains(backupName)) { + CompoundTag output = new CompoundTag(); + for (int i = 0; i < original.size(); i++) { + output.put(Integer.toString(i), original.get(i)); + } + tag.put(backupName, output); + } + } + + protected List removeGenericTagList(CompoundTag tag, String name) { + String backupName = nbtTagName(name); + CompoundTag data = tag.getCompoundTag(backupName); + if (data == null) { + return null; + } + + tag.remove(backupName); + return new ArrayList<>(data.values()); } @Override - protected void restoreDisplayTag(final Item item) { - final StructuredDataContainer data = item.dataContainer(); - final StructuredData customData = data.getNonEmpty(StructuredDataKey.CUSTOM_DATA); - if (customData == null) { - return; - } - - // Remove custom name - if (customData.value().remove(nbtTagName("customName")) != null) { - data.remove(StructuredDataKey.CUSTOM_NAME); - } else { - final Tag name = removeBackupTag(customData.value(), "Name"); - if (name != null) { - data.set(StructuredDataKey.CUSTOM_NAME, name); - } - } - } - - protected void saveTag(CompoundTag customData, Tag tag, String name) { - String backupName = nbtTagName(name); - if (!customData.contains(backupName)) { - customData.put(backupName, tag); - } - } - - protected @Nullable Tag removeBackupTag(CompoundTag customData, String tagName) { - return customData.remove(nbtTagName(tagName)); + public String nbtTagName() { + return "VB|" + protocol.getClass().getSimpleName(); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriterBase.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriterBase.java index 8ba94aa4..be17ad74 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriterBase.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/EntityRewriterBase.java @@ -187,7 +187,7 @@ public abstract class EntityRewriterBase itemRewriter; + protected final BackwardsStructuredItemRewriter itemRewriter; private boolean rewriteIds = true; - public StructuredEnchantmentRewriter(final BackwardsItemRewriter itemRewriter) { + public StructuredEnchantmentRewriter(final BackwardsStructuredItemRewriter itemRewriter) { this.itemRewriter = itemRewriter; } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_14to1_13_2/Protocol1_14To1_13_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_14to1_13_2/Protocol1_14To1_13_2.java index 9d3abd78..7af0a097 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_14to1_13_2/Protocol1_14To1_13_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_14to1_13_2/Protocol1_14To1_13_2.java @@ -191,7 +191,7 @@ public class Protocol1_14To1_13_2 extends BackwardsProtocol getTranslatableRewriter() { + public TranslatableRewriter getComponentRewriter() { return translatableRewriter; } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_15to1_14_4/Protocol1_15To1_14_4.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_15to1_14_4/Protocol1_15To1_14_4.java index 0ca7a7c1..eedaa693 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_15to1_14_4/Protocol1_15To1_14_4.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_15to1_14_4/Protocol1_15To1_14_4.java @@ -127,7 +127,7 @@ public class Protocol1_15To1_14_4 extends BackwardsProtocol getTranslatableRewriter() { + public TranslatableRewriter getComponentRewriter() { return translatableRewriter; } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16_2to1_16_1/Protocol1_16_2To1_16_1.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16_2to1_16_1/Protocol1_16_2To1_16_1.java index 16e5c001..a78d15c5 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16_2to1_16_1/Protocol1_16_2To1_16_1.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16_2to1_16_1/Protocol1_16_2To1_16_1.java @@ -127,7 +127,7 @@ public class Protocol1_16_2To1_16_1 extends BackwardsProtocol getTranslatableRewriter() { + public TranslatableRewriter getComponentRewriter() { return translatableRewriter; } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16to1_15_2/Protocol1_16To1_15_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16to1_15_2/Protocol1_16To1_15_2.java index 97d7a0ff..7db56ae5 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16to1_15_2/Protocol1_16To1_15_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16to1_15_2/Protocol1_16To1_15_2.java @@ -177,7 +177,7 @@ public class Protocol1_16To1_15_2 extends BackwardsProtocol pagesTag = tag.getListTag("pages", StringTag.class); if (pagesTag != null) { for (StringTag page : pagesTag) { - JsonElement jsonElement = protocol.getTranslatableRewriter().processText(connection, page.getValue()); + JsonElement jsonElement = protocol.getComponentRewriter().processText(connection, page.getValue()); page.setValue(jsonElement.toString()); } } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16to1_15_2/rewriter/EntityPacketRewriter1_16.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16to1_15_2/rewriter/EntityPacketRewriter1_16.java index 67ea59a7..b4e8192e 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16to1_15_2/rewriter/EntityPacketRewriter1_16.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_16to1_15_2/rewriter/EntityPacketRewriter1_16.java @@ -215,14 +215,14 @@ public class EntityPacketRewriter1_16 extends EntityRewriter getTranslatableRewriter() { + public TranslatableRewriter getComponentRewriter() { return translatableRewriter; } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_18to1_17_1/Protocol1_18To1_17_1.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_18to1_17_1/Protocol1_18To1_17_1.java index e4ec84db..42c4fc24 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_18to1_17_1/Protocol1_18To1_17_1.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_18to1_17_1/Protocol1_18To1_17_1.java @@ -152,7 +152,7 @@ public final class Protocol1_18To1_17_1 extends BackwardsProtocol getTranslatableRewriter() { + public TranslatableRewriter getComponentRewriter() { return translatableRewriter; } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19_1to1_19/Protocol1_19_1To1_19.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19_1to1_19/Protocol1_19_1To1_19.java index 53428007..89a89324 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19_1to1_19/Protocol1_19_1To1_19.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19_1to1_19/Protocol1_19_1To1_19.java @@ -395,7 +395,7 @@ public final class Protocol1_19_1To1_19 extends BackwardsProtocol getTranslatableRewriter() { + public TranslatableRewriter getComponentRewriter() { return translatableRewriter; } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19_3to1_19_1/Protocol1_19_3To1_19_1.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19_3to1_19_1/Protocol1_19_3To1_19_1.java index e54d20d4..6b057636 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19_3to1_19_1/Protocol1_19_3To1_19_1.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19_3to1_19_1/Protocol1_19_3To1_19_1.java @@ -403,7 +403,7 @@ public final class Protocol1_19_3To1_19_1 extends BackwardsProtocol getTranslatableRewriter() { + public TranslatableRewriter getComponentRewriter() { return translatableRewriter; } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19_4to1_19_3/Protocol1_19_4To1_19_3.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19_4to1_19_3/Protocol1_19_4To1_19_3.java index 7abf857c..b7f73bc5 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19_4to1_19_3/Protocol1_19_4To1_19_3.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19_4to1_19_3/Protocol1_19_4To1_19_3.java @@ -136,7 +136,7 @@ public final class Protocol1_19_4To1_19_3 extends BackwardsProtocol getTranslatableRewriter() { + public TranslatableRewriter getComponentRewriter() { return translatableRewriter; } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19_4to1_19_3/rewriter/BlockItemPacketRewriter1_19_4.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19_4to1_19_3/rewriter/BlockItemPacketRewriter1_19_4.java index c1024f67..ea85e512 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19_4to1_19_3/rewriter/BlockItemPacketRewriter1_19_4.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19_4to1_19_3/rewriter/BlockItemPacketRewriter1_19_4.java @@ -21,7 +21,6 @@ import com.viaversion.viabackwards.api.rewriters.BackwardsItemRewriter; import com.viaversion.viabackwards.protocol.v1_19_4to1_19_3.Protocol1_19_4To1_19_3; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.remapper.PacketHandlers; -import com.viaversion.viaversion.api.type.Type; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.types.chunk.ChunkType1_18; import com.viaversion.viaversion.protocols.v1_19_1to1_19_3.packet.ServerboundPackets1_19_3; @@ -60,7 +59,7 @@ public final class BlockItemPacketRewriter1_19_4 extends BackwardsItemRewriter { // TODO Maybe spawn an extra entity to ride the armor stand for blocks and items diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19to1_18_2/Protocol1_19To1_18_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19to1_18_2/Protocol1_19To1_18_2.java index 8889231a..de77de02 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19to1_18_2/Protocol1_19To1_18_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_19to1_18_2/Protocol1_19To1_18_2.java @@ -372,7 +372,7 @@ public final class Protocol1_19To1_18_2 extends BackwardsProtocol getTranslatableRewriter() { + public TranslatableRewriter getComponentRewriter() { return translatableRewriter; } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_3to1_20_2/Protocol1_20_3To1_20_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_3to1_20_2/Protocol1_20_3To1_20_2.java index 2c0593a9..40b72057 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_3to1_20_2/Protocol1_20_3To1_20_2.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_3to1_20_2/Protocol1_20_3To1_20_2.java @@ -398,7 +398,7 @@ public final class Protocol1_20_3To1_20_2 extends BackwardsProtocol getTranslatableRewriter() { + public TranslatableRewriter getComponentRewriter() { return translatableRewriter; } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_5to1_20_3/Protocol1_20_5To1_20_3.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_5to1_20_3/Protocol1_20_5To1_20_3.java index b6d41538..d5b403ff 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_5to1_20_3/Protocol1_20_5To1_20_3.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_5to1_20_3/Protocol1_20_5To1_20_3.java @@ -254,7 +254,7 @@ public final class Protocol1_20_5To1_20_3 extends BackwardsProtocol getTranslatableRewriter() { + public TranslatableRewriter getComponentRewriter() { return translatableRewriter; } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20to1_19_4/Protocol1_20To1_19_4.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20to1_19_4/Protocol1_20To1_19_4.java index 03f760d3..9daa8576 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20to1_19_4/Protocol1_20To1_19_4.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20to1_19_4/Protocol1_20To1_19_4.java @@ -113,7 +113,7 @@ public final class Protocol1_20To1_19_4 extends BackwardsProtocol getTranslatableRewriter() { + public TranslatableRewriter getComponentRewriter() { return translatableRewriter; } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/Protocol1_21To1_20_5.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/Protocol1_21To1_20_5.java index c9bf8e1f..87248a46 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/Protocol1_21To1_20_5.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/Protocol1_21To1_20_5.java @@ -22,6 +22,7 @@ import com.viaversion.viabackwards.api.data.BackwardsMappingData; import com.viaversion.viabackwards.api.rewriters.SoundRewriter; import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; import com.viaversion.viabackwards.protocol.v1_21to1_20_5.rewriter.BlockItemPacketRewriter1_21; +import com.viaversion.viabackwards.protocol.v1_21to1_20_5.rewriter.ComponentRewriter1_21; import com.viaversion.viabackwards.protocol.v1_21to1_20_5.rewriter.EntityPacketRewriter1_21; import com.viaversion.viabackwards.protocol.v1_21to1_20_5.storage.EnchantmentsPaintingsStorage; import com.viaversion.viaversion.api.connection.UserConnection; @@ -42,7 +43,6 @@ import com.viaversion.viaversion.protocols.v1_20_5to1_21.Protocol1_20_5To1_21; import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundConfigurationPackets1_21; import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPacket1_21; import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPackets1_21; -import com.viaversion.viaversion.rewriter.ComponentRewriter.ReadType; import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.TagRewriter; @@ -53,7 +53,7 @@ public final class Protocol1_21To1_20_5 extends BackwardsProtocol translatableRewriter = new TranslatableRewriter<>(this, ReadType.NBT); + private final TranslatableRewriter translatableRewriter = new ComponentRewriter1_21(this); private final TagRewriter tagRewriter = new TagRewriter<>(this); public Protocol1_21To1_20_5() { @@ -198,7 +198,7 @@ public final class Protocol1_21To1_20_5 extends BackwardsProtocol getTranslatableRewriter() { + public TranslatableRewriter getComponentRewriter() { return translatableRewriter; } diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/rewriter/ComponentRewriter1_21.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/rewriter/ComponentRewriter1_21.java new file mode 100644 index 00000000..d2e8a3c6 --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/rewriter/ComponentRewriter1_21.java @@ -0,0 +1,64 @@ +/* + * This file is part of ViaVersion - https://github.com/ViaVersion/ViaVersion + * Copyright (C) 2016-2024 ViaVersion and contributors + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package com.viaversion.viabackwards.protocol.v1_21to1_20_5.rewriter; + +import com.viaversion.nbt.tag.CompoundTag; +import com.viaversion.nbt.tag.IntArrayTag; +import com.viaversion.nbt.tag.ListTag; +import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; +import com.viaversion.viabackwards.protocol.v1_21to1_20_5.Protocol1_21To1_20_5; +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.protocols.v1_20_5to1_21.Protocol1_20_5To1_21; +import com.viaversion.viaversion.protocols.v1_20_5to1_21.data.AttributeModifierMappings1_21; +import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPacket1_21; +import com.viaversion.viaversion.util.SerializerVersion; +import com.viaversion.viaversion.util.TagUtil; +import com.viaversion.viaversion.util.UUIDUtil; +import java.util.UUID; + +public final class ComponentRewriter1_21 extends TranslatableRewriter { + + public ComponentRewriter1_21(final Protocol1_21To1_20_5 protocol) { + super(protocol, ReadType.NBT); + } + + @Override + protected void handleShowItem(final UserConnection connection, final CompoundTag componentsTag) { + final CompoundTag attributeModifiers = TagUtil.getNamespacedCompoundTag(componentsTag, "attribute_modifiers"); + if (attributeModifiers != null) { + final ListTag modifiers = attributeModifiers.getListTag("modifiers", CompoundTag.class); + for (final CompoundTag modifier : modifiers) { + final String id = modifier.getString("id"); + final UUID uuid = Protocol1_20_5To1_21.mapAttributeId(id); + final String name = AttributeModifierMappings1_21.idToName(id); + modifier.put("uuid", new IntArrayTag(UUIDUtil.toIntArray(uuid))); + modifier.putString("name", name != null ? name : id); + } + } + } + + @Override + protected SerializerVersion inputSerializerVersion() { + return SerializerVersion.V1_20_5; + } + + @Override + protected SerializerVersion outputSerializerVersion() { + return SerializerVersion.V1_20_5; + } +} diff --git a/template/src/main/java/com/viaversion/viabackwards/template/protocol/Protocol1_98To1_99.java b/template/src/main/java/com/viaversion/viabackwards/template/protocol/Protocol1_98To1_99.java index 36888203..b82eabd9 100644 --- a/template/src/main/java/com/viaversion/viabackwards/template/protocol/Protocol1_98To1_99.java +++ b/template/src/main/java/com/viaversion/viabackwards/template/protocol/Protocol1_98To1_99.java @@ -113,7 +113,7 @@ public final class Protocol1_98To1_99 extends BackwardsProtocol getTranslatableRewriter() { + public TranslatableRewriter getComponentRewriter() { return translatableRewriter; }