From 8bc51d45ca5b9831c6d2d03201675af1fd053e3d Mon Sep 17 00:00:00 2001 From: FlorianMichael Date: Sat, 14 Dec 2024 23:24:25 +0100 Subject: [PATCH] Add chat item rewriting to 1.20.5->1.20.3 --- .../Protocol1_20_5To1_20_3.java | 4 +- .../rewriter/ComponentRewriter1_20_5.java | 82 +++++++++++++++++++ gradle.properties | 2 +- gradle/libs.versions.toml | 2 +- 4 files changed, 86 insertions(+), 4 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_5to1_20_3/rewriter/ComponentRewriter1_20_5.java 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 ac2f7d9e..db1f7b31 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 @@ -23,6 +23,7 @@ import com.viaversion.viabackwards.api.rewriters.SoundRewriter; import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; import com.viaversion.viabackwards.protocol.v1_20_5to1_20_3.provider.TransferProvider; import com.viaversion.viabackwards.protocol.v1_20_5to1_20_3.rewriter.BlockItemPacketRewriter1_20_5; +import com.viaversion.viabackwards.protocol.v1_20_5to1_20_3.rewriter.ComponentRewriter1_20_5; import com.viaversion.viabackwards.protocol.v1_20_5to1_20_3.rewriter.EntityPacketRewriter1_20_5; import com.viaversion.viabackwards.protocol.v1_20_5to1_20_3.storage.CookieStorage; import com.viaversion.viabackwards.protocol.v1_20_5to1_20_3.storage.RegistryDataStorage; @@ -57,7 +58,6 @@ import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ServerboundPac import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.storage.ArmorTrimStorage; import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.storage.BannerPatternStorage; import com.viaversion.viaversion.protocols.v1_20to1_20_2.packet.ServerboundConfigurationPackets1_20_2; -import com.viaversion.viaversion.rewriter.ComponentRewriter.ReadType; import com.viaversion.viaversion.rewriter.ParticleRewriter; import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.TagRewriter; @@ -70,7 +70,7 @@ public final class Protocol1_20_5To1_20_3 extends BackwardsProtocol particleRewriter = new ParticleRewriter<>(this); - private final TranslatableRewriter translatableRewriter = new TranslatableRewriter<>(this, ReadType.NBT); + private final TranslatableRewriter translatableRewriter = new ComponentRewriter1_20_5(this); private final TagRewriter tagRewriter = new TagRewriter<>(this); public Protocol1_20_5To1_20_3() { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_5to1_20_3/rewriter/ComponentRewriter1_20_5.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_5to1_20_3/rewriter/ComponentRewriter1_20_5.java new file mode 100644 index 00000000..89fbb92b --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_5to1_20_3/rewriter/ComponentRewriter1_20_5.java @@ -0,0 +1,82 @@ +/* + * This file is part of ViaBackwards - https://github.com/ViaVersion/ViaBackwards + * 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_20_5to1_20_3.rewriter; + +import com.viaversion.nbt.tag.CompoundTag; +import com.viaversion.nbt.tag.StringTag; +import com.viaversion.viabackwards.api.BackwardsProtocol; +import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.api.minecraft.data.StructuredData; +import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer; +import com.viaversion.viaversion.api.minecraft.item.Item; +import com.viaversion.viaversion.api.minecraft.item.StructuredItem; +import com.viaversion.viaversion.api.type.types.version.Types1_20_5; +import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ClientboundPacket1_20_5; +import com.viaversion.viaversion.util.SerializerVersion; +import java.util.List; +import org.checkerframework.checker.nullness.qual.Nullable; + +public final class ComponentRewriter1_20_5 extends TranslatableRewriter { + + private final com.viaversion.viaversion.protocols.v1_20_3to1_20_5.rewriter.ComponentRewriter1_20_5 vvRewriter; + + public ComponentRewriter1_20_5(final BackwardsProtocol protocol) { + super(protocol, ReadType.NBT); + vvRewriter = new com.viaversion.viaversion.protocols.v1_20_3to1_20_5.rewriter.ComponentRewriter1_20_5<>(protocol, Types1_20_5.STRUCTURED_DATA); + } + + @Override + protected void handleShowItem(final UserConnection connection, final CompoundTag itemTag, @Nullable final CompoundTag componentsTag) { + super.handleShowItem(connection, itemTag, componentsTag); + if (componentsTag == null) { + return; + } + + final StringTag idTag = itemTag.getStringTag("id"); + if (idTag == null) { + return; + } + + final List> data = vvRewriter.toData(connection, componentsTag); + if (data.isEmpty()) { + return; + } + + final int identifier = this.protocol.getMappingData().getFullItemMappings().id(idTag.getValue()); + + final StructuredItem structuredItem = new StructuredItem(identifier, 1, new StructuredDataContainer(data.toArray(StructuredData[]::new))); + final Item dataItem = protocol.getItemRewriter().handleItemToClient(connection, structuredItem); + if (dataItem.tag() == null) { + return; + } + + itemTag.remove("components"); + itemTag.put("tag", new StringTag(outputSerializerVersion().toSNBT(dataItem.tag()))); + } + + @Override + protected @Nullable SerializerVersion inputSerializerVersion() { + return SerializerVersion.V1_20_5; + } + + @Override + protected @Nullable SerializerVersion outputSerializerVersion() { + return SerializerVersion.V1_20_3; + } +} diff --git a/gradle.properties b/gradle.properties index 03b192fd..c9a04e01 100644 --- a/gradle.properties +++ b/gradle.properties @@ -1,4 +1,4 @@ -projectVersion=5.2.0 +projectVersion=5.2.1-SNAPSHOT # Smile emoji mcVersions=1.21.4,1.21.3,1.21.2,1.21.1,1.21,1.20.6,1.20.5,1.20.4, 1.20.3, 1.20.2, 1.20.1, 1.20, 1.19.4, 1.19.3, 1.19.2, 1.19.1, 1.19, 1.18.2, 1.18.1, 1.18, 1.17.1, 1.17, 1.16.5, 1.16.4, 1.16.3, 1.16.2, 1.16.1, 1.16, 1.15.2, 1.15.1, 1.15, 1.14.4, 1.14.3, 1.14.2, 1.14.1, 1.14, 1.13.2, 1.13.1, 1.13, 1.12.2, 1.12.1, 1.12, 1.11.2, 1.11.1, 1.11, 1.10.2, 1.10.1, 1.10 diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 0ea59547..b1c7d3c1 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -3,7 +3,7 @@ metadata.format.version = "1.1" [versions] # ViaVersion -viaver = "5.2.0" +viaver = "5.2.1-SNAPSHOT" # Common provided netty = "4.0.20.Final"