Add item fixes to component rewriter in 1.21.x protocols as well (#4217)

Adds the rarity fix in 1.20.5->1.21 and the item/custom name fix in 1.21->1.21.2 to the component rewriters / chat items.
This commit is contained in:
EnZaXD 2024-10-30 10:31:45 +01:00 committed by GitHub
parent ad83d01fad
commit af8cbaf439
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 46 additions and 10 deletions

View File

@ -19,10 +19,12 @@ package com.viaversion.viaversion.protocols.v1_20_5to1_21.rewriter;
import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.ListTag;
import com.viaversion.nbt.tag.StringTag;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.packet.ClientboundPacket1_20_5;
import com.viaversion.viaversion.protocols.v1_20_5to1_21.Protocol1_20_5To1_21;
import com.viaversion.viaversion.rewriter.ComponentRewriter;
import com.viaversion.viaversion.util.Key;
import com.viaversion.viaversion.util.SerializerVersion;
import com.viaversion.viaversion.util.TagUtil;
import com.viaversion.viaversion.util.UUIDUtil;
@ -34,8 +36,24 @@ public final class ComponentRewriter1_21 extends ComponentRewriter<ClientboundPa
super(protocol, ReadType.NBT);
}
private void convertAttributeModifiersComponent(final CompoundTag tag) {
final CompoundTag attributeModifiers = TagUtil.getNamespacedCompoundTag(tag, "minecraft:attribute_modifiers");
@Override
protected void handleShowItem(final UserConnection connection, final CompoundTag itemTag, CompoundTag componentsTag) {
super.handleShowItem(connection, itemTag, componentsTag);
final String identifier = Key.stripMinecraftNamespace(itemTag.getString("id"));
if (identifier.equals("trident") || identifier.equals("piglin_banner_pattern")) {
if (componentsTag == null) {
itemTag.put("components", componentsTag = new CompoundTag());
}
if (!TagUtil.containsNamespaced(componentsTag, "rarity")) {
componentsTag.put("minecraft:rarity", new StringTag("common"));
}
}
if (componentsTag == null) {
return;
}
final CompoundTag attributeModifiers = TagUtil.getNamespacedCompoundTag(componentsTag, "attribute_modifiers");
if (attributeModifiers == null) {
return;
}
@ -48,14 +66,6 @@ public final class ComponentRewriter1_21 extends ComponentRewriter<ClientboundPa
}
}
@Override
protected void handleShowItem(final UserConnection connection, final CompoundTag itemTag, final CompoundTag componentsTag) {
super.handleShowItem(connection, itemTag, componentsTag);
if (componentsTag != null) {
convertAttributeModifiersComponent(componentsTag);
}
}
@Override
protected SerializerVersion inputSerializerVersion() {
return SerializerVersion.V1_20_5;

View File

@ -20,6 +20,7 @@ package com.viaversion.viaversion.protocols.v1_21to1_21_2.rewriter;
import com.viaversion.nbt.tag.CompoundTag;
import com.viaversion.nbt.tag.ListTag;
import com.viaversion.nbt.tag.StringTag;
import com.viaversion.nbt.tag.Tag;
import com.viaversion.viaversion.api.connection.UserConnection;
import com.viaversion.viaversion.api.data.FullMappings;
import com.viaversion.viaversion.protocols.v1_20_5to1_21.packet.ClientboundPacket1_21;
@ -27,6 +28,7 @@ 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;
import java.util.Collections;
import java.util.Iterator;
public final class ComponentRewriter1_21_2 extends ComponentRewriter<ClientboundPacket1_21> {
@ -62,6 +64,26 @@ public final class ComponentRewriter1_21_2 extends ComponentRewriter<Clientbound
TagUtil.removeNamespaced(componentsTag, "fire_resistant");
TagUtil.removeNamespaced(componentsTag, "lock");
final StringTag customName = TagUtil.getNamespacedStringTag(componentsTag, "custom_name");
final StringTag itemName = TagUtil.getNamespacedStringTag(componentsTag, "item_name");
if (customName != null || itemName == null) {
return;
}
final int identifier = protocol.getMappingData().getFullItemMappings().mappedId(itemTag.getString("id"));
if (identifier == 952 || identifier == 1147 || identifier == 1039 || identifier == 1203 || identifier == 1200 || identifier == 1204 || identifier == 1202) {
final var input = inputSerializerVersion();
final var output = outputSerializerVersion();
final CompoundTag name = new CompoundTag();
name.putBoolean("italic", false);
name.putString("text", "");
final Tag nameTag = input.toTag(input.toComponent(itemName.getValue()));
name.put("extra", new ListTag<>(Collections.singletonList(nameTag)));
componentsTag.put("minecraft:custom_name", new StringTag(output.toString(output.toComponent(name))));
}
}
public static void convertAttributes(final CompoundTag componentsTag, final FullMappings mappings) {

View File

@ -60,6 +60,10 @@ public final class TagUtil {
return tag.remove(Key.namespaced(key)) != null || tag.remove(Key.stripMinecraftNamespace(key)) != null;
}
public static boolean containsNamespaced(final CompoundTag tag, final String key) {
return tag.contains(Key.namespaced(key)) || tag.contains(Key.stripMinecraftNamespace(key));
}
public static @Nullable CompoundTag getNamespacedCompoundTag(final CompoundTag tag, final String key) {
final CompoundTag compoundTag = tag.getCompoundTag(Key.namespaced(key));
return compoundTag != null ? compoundTag : tag.getCompoundTag(Key.stripMinecraftNamespace(key));