From 22d396d90908c9a7ee95cb7e2a5f22229fee048c Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Fri, 30 Aug 2024 14:19:13 +0200 Subject: [PATCH] Add item hover event handling --- .../minecraft/entities/EntityTypes1_20_5.java | 1 + .../minecraft/item/data/Instrument1_20_5.java | 4 +- .../minecraft/item/data/Instrument1_21_2.java | 8 ++- .../v1_21to1_21_2/Protocol1_21To1_21_2.java | 8 +++ .../BlockItemPacketRewriter1_21_2.java | 3 +- .../rewriter/ComponentRewriter1_21_2.java | 67 +++++++++++++++++++ .../v1_9_3to1_10/Protocol1_9_3To1_10.java | 9 ++- .../rewriter/ComponentRewriter.java | 11 ++- .../rewriter/StructuredItemRewriter.java | 3 +- 9 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/ComponentRewriter1_21_2.java diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/entities/EntityTypes1_20_5.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/entities/EntityTypes1_20_5.java index 5a1cc711e..e33db1dc0 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/entities/EntityTypes1_20_5.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/entities/EntityTypes1_20_5.java @@ -134,6 +134,7 @@ public enum EntityTypes1_20_5 implements EntityType { // Water mobs WATER_ANIMAL(PATHFINDER_MOB, null), + // The following three are ageable water mobs in 1.21.2, but not important enough to create a new enum for it // TODO Change in next enum DOLPHIN(WATER_ANIMAL), SQUID(WATER_ANIMAL), diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Instrument1_20_5.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Instrument1_20_5.java index 3d34f3989..a3675baf2 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Instrument1_20_5.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Instrument1_20_5.java @@ -48,8 +48,8 @@ public record Instrument1_20_5(Holder soundEvent, int useDuration, f } }; - public Instrument rewrite(final Int2IntFunction soundIdRewriteFunction) { + public Instrument1_20_5 rewrite(final Int2IntFunction soundIdRewriteFunction) { final Holder soundEvent = this.soundEvent.updateId(soundIdRewriteFunction); - return soundEvent == this.soundEvent ? this : new Instrument(soundEvent, useDuration, range); + return soundEvent == this.soundEvent ? this : new Instrument1_20_5(soundEvent, useDuration, range); } } diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Instrument1_21_2.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Instrument1_21_2.java index f9f7c8c22..4cca205f5 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Instrument1_21_2.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/Instrument1_21_2.java @@ -28,9 +28,9 @@ import com.viaversion.viaversion.api.minecraft.SoundEvent; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.types.misc.HolderType; import io.netty.buffer.ByteBuf; -import org.checkerframework.checker.nullness.qual.Nullable; +import it.unimi.dsi.fastutil.ints.Int2IntFunction; -public record Instrument1_21_2(Holder soundEvent, float useDuration, float range, @Nullable Tag description) { +public record Instrument1_21_2(Holder soundEvent, float useDuration, float range, Tag description) { public static final HolderType TYPE = new HolderType<>() { @Override @@ -51,4 +51,8 @@ public record Instrument1_21_2(Holder soundEvent, float useDuration, } }; + public Instrument1_21_2 rewrite(final Int2IntFunction soundIdRewriteFunction) { + final Holder soundEvent = this.soundEvent.updateId(soundIdRewriteFunction); + return soundEvent == this.soundEvent ? this : new Instrument1_21_2(soundEvent, useDuration, range, description); + } } diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/Protocol1_21To1_21_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/Protocol1_21To1_21_2.java index 668c5bb53..0e2d6dd5c 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/Protocol1_21To1_21_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/Protocol1_21To1_21_2.java @@ -26,6 +26,7 @@ import com.viaversion.viaversion.api.protocol.AbstractProtocol; import com.viaversion.viaversion.api.protocol.packet.PacketWrapper; import com.viaversion.viaversion.api.protocol.packet.provider.PacketTypesProvider; import com.viaversion.viaversion.api.protocol.packet.provider.SimplePacketTypesProvider; +import com.viaversion.viaversion.api.rewriter.ComponentRewriter; import com.viaversion.viaversion.api.type.Types; import com.viaversion.viaversion.api.type.types.misc.ParticleType; import com.viaversion.viaversion.api.type.types.version.Types1_21_2; @@ -41,6 +42,7 @@ 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.protocols.v1_21to1_21_2.rewriter.BlockItemPacketRewriter1_21_2; +import com.viaversion.viaversion.protocols.v1_21to1_21_2.rewriter.ComponentRewriter1_21_2; import com.viaversion.viaversion.protocols.v1_21to1_21_2.rewriter.EntityPacketRewriter1_21_2; import com.viaversion.viaversion.rewriter.AttributeRewriter; import com.viaversion.viaversion.rewriter.SoundRewriter; @@ -55,6 +57,7 @@ public final class Protocol1_21To1_21_2 extends AbstractProtocol tagRewriter = new TagRewriter<>(this); + private final ComponentRewriter1_21_2 componentRewriter = new ComponentRewriter1_21_2(this); public Protocol1_21To1_21_2() { super(ClientboundPacket1_21.class, ClientboundPacket1_21_2.class, ServerboundPacket1_20_5.class, ServerboundPacket1_21_2.class); @@ -157,6 +160,11 @@ public final class Protocol1_21To1_21_2 extends AbstractProtocol createPacketTypesProvider() { return new SimplePacketTypesProvider<>( diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/BlockItemPacketRewriter1_21_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/BlockItemPacketRewriter1_21_2.java index e8f04f163..855d9ef01 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/BlockItemPacketRewriter1_21_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/BlockItemPacketRewriter1_21_2.java @@ -17,6 +17,7 @@ */ package com.viaversion.viaversion.protocols.v1_21to1_21_2.rewriter; +import com.viaversion.nbt.tag.StringTag; import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.data.MappingData; import com.viaversion.viaversion.api.minecraft.Holder; @@ -268,7 +269,7 @@ public final class BlockItemPacketRewriter1_21_2 extends StructuredItemRewriter< return Holder.of(instrument.id()); } final Instrument1_20_5 value = instrument.value(); - return Holder.of(new Instrument1_21_2(value.soundEvent(), value.useDuration(), value.range(), null)); + return Holder.of(new Instrument1_21_2(value.soundEvent(), value.useDuration(), value.range(), new StringTag(""))); }); dataContainer.replace(StructuredDataKey.FOOD1_21, StructuredDataKey.FOOD1_21_2, food -> { // Just assume the item type default for CONSUMABLE; add USE_REMAINDER from old food properties diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/ComponentRewriter1_21_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/ComponentRewriter1_21_2.java new file mode 100644 index 000000000..74d79188e --- /dev/null +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/ComponentRewriter1_21_2.java @@ -0,0 +1,67 @@ +/* + * 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.viaversion.protocols.v1_21to1_21_2.rewriter; + +import com.viaversion.nbt.tag.CompoundTag; +import com.viaversion.viaversion.api.connection.UserConnection; +import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPacket1_21; +import com.viaversion.viaversion.protocols.v1_21to1_21_2.Protocol1_21To1_21_2; +import com.viaversion.viaversion.rewriter.ComponentRewriter; +import com.viaversion.viaversion.util.SerializerVersion; +import com.viaversion.viaversion.util.TagUtil; + +public final class ComponentRewriter1_21_2 extends ComponentRewriter { + + public ComponentRewriter1_21_2(final Protocol1_21To1_21_2 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; + } + + final CompoundTag instrument = TagUtil.getNamespacedCompoundTag(componentsTag, "instrument"); + if (instrument != null) { + instrument.putString("description", ""); + } + + final CompoundTag food = TagUtil.getNamespacedCompoundTag(componentsTag, "food"); + if (food != null) { + final CompoundTag convertsTo = food.getCompoundTag("using_converts_to"); + if (convertsTo != null) { + food.remove("using_converts_to"); + componentsTag.put("minecraft:use_remainder", convertsTo); + } + food.remove("eat_seconds"); + food.remove("effects"); + } + } + + @Override + protected SerializerVersion inputSerializerVersion() { + return SerializerVersion.V1_20_5; + } + + @Override + protected SerializerVersion outputSerializerVersion() { + return SerializerVersion.V1_20_5; + } +} diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_9_3to1_10/Protocol1_9_3To1_10.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_9_3to1_10/Protocol1_9_3To1_10.java index 42cdb524d..b89fb8d16 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_9_3to1_10/Protocol1_9_3To1_10.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_9_3to1_10/Protocol1_9_3To1_10.java @@ -36,7 +36,6 @@ import com.viaversion.viaversion.protocols.v1_9_1to1_9_3.packet.ServerboundPacke import com.viaversion.viaversion.protocols.v1_9_3to1_10.rewriter.ItemPacketRewriter1_10; import com.viaversion.viaversion.protocols.v1_9_3to1_10.storage.ResourcePackTracker; import java.util.List; -import java.util.concurrent.CopyOnWriteArrayList; public class Protocol1_9_3To1_10 extends AbstractProtocol { @@ -49,12 +48,12 @@ public class Protocol1_9_3To1_10 extends AbstractProtocol, List> TRANSFORM_ENTITY_DATA = new ValueTransformer<>(Types1_9.ENTITY_DATA_LIST) { @Override public List transform(PacketWrapper wrapper, List inputValue) { - List dataList = new CopyOnWriteArrayList<>(inputValue); - for (EntityData data : dataList) { - if (data.id() >= 5) + for (EntityData data : inputValue) { + if (data.id() >= 5) { data.setId(data.id() + 1); + } } - return dataList; + return inputValue; } }; private final ItemPacketRewriter1_10 itemRewriter = new ItemPacketRewriter1_10(this); diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java index 0070a0e74..6c8e3ec52 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java @@ -359,6 +359,10 @@ public class ComponentRewriter implements com.v final CompoundTag componentsTag = contentsTag.getCompoundTag("components"); handleShowItem(connection, contentsTag, componentsTag); if (componentsTag != null) { + final CompoundTag useRemainder = TagUtil.getNamespacedCompoundTag(componentsTag, "use_remainder"); + if (useRemainder != null) { + handleShowItem(connection, useRemainder); + } handleContainerContents(connection, componentsTag); if (inputSerializerVersion() != null) { handleWrittenBookContents(connection, componentsTag); @@ -370,6 +374,10 @@ public class ComponentRewriter implements com.v } } + protected final void handleShowItem(final UserConnection connection, final CompoundTag itemTag) { + handleShowItem(connection, itemTag, itemTag.getCompoundTag("components")); + } + protected void handleShowItem(final UserConnection connection, final CompoundTag itemTag, @Nullable final CompoundTag componentsTag) { final StringTag idTag = itemTag.getStringTag("id"); final String mappedId = protocol.getMappingData().getFullItemMappings().mappedIdentifier(idTag.getValue()); @@ -385,8 +393,7 @@ public class ComponentRewriter implements com.v } for (final CompoundTag entryTag : container) { - final CompoundTag itemTag = entryTag.getCompoundTag("item"); - handleShowItem(connection, itemTag, itemTag.getCompoundTag("components")); + handleShowItem(connection, entryTag.getCompoundTag("item")); } } diff --git a/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java b/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java index d4cdbba98..f7c289a0e 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java @@ -133,7 +133,8 @@ public class StructuredItemRewriter value.isDirect() ? Holder.of(value.value().rewrite(soundIdRewriter)) : value); + container.replace(StructuredDataKey.INSTRUMENT1_20_5, value -> value.isDirect() ? Holder.of(value.value().rewrite(soundIdRewriter)) : value); + container.replace(StructuredDataKey.INSTRUMENT1_21_2, value -> value.isDirect() ? Holder.of(value.value().rewrite(soundIdRewriter)) : value); container.replace(StructuredDataKey.JUKEBOX_PLAYABLE, value -> value.rewrite(soundIdRewriter)); } if (clientbound && protocol.getComponentRewriter() != null) {