Add item hover event handling

This commit is contained in:
Nassim Jahnke 2024-08-30 14:19:13 +02:00
parent 39b28e21ca
commit 22d396d909
No known key found for this signature in database
GPG Key ID: EF6771C01F6EF02F
9 changed files with 101 additions and 13 deletions

View File

@ -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),

View File

@ -48,8 +48,8 @@ public record Instrument1_20_5(Holder<SoundEvent> soundEvent, int useDuration, f
}
};
public Instrument rewrite(final Int2IntFunction soundIdRewriteFunction) {
public Instrument1_20_5 rewrite(final Int2IntFunction soundIdRewriteFunction) {
final Holder<SoundEvent> 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);
}
}

View File

@ -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> soundEvent, float useDuration, float range, @Nullable Tag description) {
public record Instrument1_21_2(Holder<SoundEvent> soundEvent, float useDuration, float range, Tag description) {
public static final HolderType<Instrument1_21_2> TYPE = new HolderType<>() {
@Override
@ -51,4 +51,8 @@ public record Instrument1_21_2(Holder<SoundEvent> soundEvent, float useDuration,
}
};
public Instrument1_21_2 rewrite(final Int2IntFunction soundIdRewriteFunction) {
final Holder<SoundEvent> soundEvent = this.soundEvent.updateId(soundIdRewriteFunction);
return soundEvent == this.soundEvent ? this : new Instrument1_21_2(soundEvent, useDuration, range, description);
}
}

View File

@ -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<ClientboundPack
private final EntityPacketRewriter1_21_2 entityRewriter = new EntityPacketRewriter1_21_2(this);
private final BlockItemPacketRewriter1_21_2 itemRewriter = new BlockItemPacketRewriter1_21_2(this);
private final TagRewriter<ClientboundPacket1_21> 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<ClientboundPack
return tagRewriter;
}
@Override
public ComponentRewriter getComponentRewriter() {
return componentRewriter;
}
@Override
protected PacketTypesProvider<ClientboundPacket1_21, ClientboundPacket1_21_2, ServerboundPacket1_20_5, ServerboundPacket1_21_2> createPacketTypesProvider() {
return new SimplePacketTypesProvider<>(

View File

@ -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

View File

@ -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 <http://www.gnu.org/licenses/>.
*/
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<ClientboundPacket1_21> {
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;
}
}

View File

@ -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<ClientboundPackets1_9_3, ClientboundPackets1_9_3, ServerboundPackets1_9_3, ServerboundPackets1_9_3> {
@ -49,12 +48,12 @@ public class Protocol1_9_3To1_10 extends AbstractProtocol<ClientboundPackets1_9_
public static final ValueTransformer<List<EntityData>, List<EntityData>> TRANSFORM_ENTITY_DATA = new ValueTransformer<>(Types1_9.ENTITY_DATA_LIST) {
@Override
public List<EntityData> transform(PacketWrapper wrapper, List<EntityData> inputValue) {
List<EntityData> 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);

View File

@ -359,6 +359,10 @@ public class ComponentRewriter<C extends ClientboundPacketType> 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<C extends ClientboundPacketType> 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<C extends ClientboundPacketType> 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"));
}
}

View File

@ -133,7 +133,8 @@ public class StructuredItemRewriter<C extends ClientboundPacketType, S extends S
}
if (mappingData.getSoundMappings() != null) {
final Int2IntFunction soundIdRewriter = clientbound ? mappingData::getNewSoundId : mappingData::getOldSoundId;
container.replace(StructuredDataKey.INSTRUMENT, value -> 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) {