From b0276eaa7db104542acdd7bc4ee42352c4cfd739 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Fri, 30 Aug 2024 14:32:12 +0200 Subject: [PATCH] Handle item hover events --- .../v1_21_2to1_21/Protocol1_21_2To1_21.java | 4 +- .../rewriter/ComponentRewriter1_21_2.java | 73 +++++++++++++++++++ 2 files changed, 75 insertions(+), 2 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/rewriter/ComponentRewriter1_21_2.java diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/Protocol1_21_2To1_21.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/Protocol1_21_2To1_21.java index 7089b55e..e1ef7871 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/Protocol1_21_2To1_21.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/Protocol1_21_2To1_21.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_21_2to1_21.rewriter.BlockItemPacketRewriter1_21_2; +import com.viaversion.viabackwards.protocol.v1_21_2to1_21.rewriter.ComponentRewriter1_21_2; import com.viaversion.viabackwards.protocol.v1_21_2to1_21.rewriter.EntityPacketRewriter1_21_2; import com.viaversion.viabackwards.protocol.v1_21_2to1_21.storage.InventoryStateIdStorage; import com.viaversion.viabackwards.protocol.v1_21_2to1_21.storage.ItemTagStorage; @@ -44,7 +45,6 @@ import com.viaversion.viaversion.protocols.v1_21to1_21_2.packet.ClientboundPacke import com.viaversion.viaversion.protocols.v1_21to1_21_2.packet.ServerboundPacket1_21_2; import com.viaversion.viaversion.protocols.v1_21to1_21_2.packet.ServerboundPackets1_21_2; import com.viaversion.viaversion.rewriter.AttributeRewriter; -import com.viaversion.viaversion.rewriter.ComponentRewriter.ReadType; import com.viaversion.viaversion.rewriter.StatisticsRewriter; import com.viaversion.viaversion.rewriter.TagRewriter; @@ -55,7 +55,7 @@ public final class Protocol1_21_2To1_21 extends BackwardsProtocol translatableRewriter = new TranslatableRewriter<>(this, ReadType.NBT); + private final TranslatableRewriter translatableRewriter = new ComponentRewriter1_21_2(this); private final TagRewriter tagRewriter = new TagRewriter<>(this); public Protocol1_21_2To1_21() { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/rewriter/ComponentRewriter1_21_2.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/rewriter/ComponentRewriter1_21_2.java new file mode 100644 index 00000000..08a1b2d3 --- /dev/null +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21_2to1_21/rewriter/ComponentRewriter1_21_2.java @@ -0,0 +1,73 @@ +/* + * 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_21_2to1_21.rewriter; + +import com.viaversion.nbt.tag.CompoundTag; +import com.viaversion.viabackwards.api.rewriters.TranslatableRewriter; +import com.viaversion.viabackwards.protocol.v1_21_2to1_21.Protocol1_21_2To1_21; +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.protocols.v1_21to1_21_2.packet.ClientboundPacket1_21_2; +import com.viaversion.viaversion.util.SerializerVersion; +import com.viaversion.viaversion.util.TagUtil; + +public final class ComponentRewriter1_21_2 extends TranslatableRewriter { + + public ComponentRewriter1_21_2(final Protocol1_21_2To1_21 protocol) { + super(protocol, ReadType.NBT); + } + + @Override + protected void handleShowItem(final UserConnection connection, final CompoundTag itemTag, final CompoundTag componentsTag) { + super.handleShowItem(connection, itemTag, componentsTag); + if (componentsTag == null) { + return; + } + + // TODO attribute types + + final CompoundTag instrument = TagUtil.getNamespacedCompoundTag(componentsTag, "instrument"); + if (instrument != null) { + instrument.remove("description"); + } + + final CompoundTag useRemainder = TagUtil.getNamespacedCompoundTag(componentsTag, "use_remainder"); + final CompoundTag food = TagUtil.getNamespacedCompoundTag(componentsTag, "food"); + if (food != null) { + if (useRemainder != null) { + food.put("using_converts_to", useRemainder); + } + food.putFloat("eat_seconds", 1.6F); + } + + TagUtil.removeNamespaced(componentsTag, "repairable"); + TagUtil.removeNamespaced(componentsTag, "enchantable"); + TagUtil.removeNamespaced(componentsTag, "consumable"); + TagUtil.removeNamespaced(componentsTag, "use_remainder"); + TagUtil.removeNamespaced(componentsTag, "use_cooldown"); + } + + @Override + protected SerializerVersion inputSerializerVersion() { + return SerializerVersion.V1_20_5; + } + + @Override + protected SerializerVersion outputSerializerVersion() { + return SerializerVersion.V1_20_5; + } +}