From 04e4b9a1f5d3b26ab051c2940c5a254dea978dcd Mon Sep 17 00:00:00 2001 From: FlorianMichael Date: Wed, 9 Oct 2024 17:16:40 +0200 Subject: [PATCH] Trim as well --- .../rewriter/ComponentRewriter1_20_5.java | 94 ++++++++++++++----- .../rewriter/ComponentRewriter.java | 3 +- 2 files changed, 71 insertions(+), 26 deletions(-) diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/ComponentRewriter1_20_5.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/ComponentRewriter1_20_5.java index 788d164e2..5204580a2 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/ComponentRewriter1_20_5.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/ComponentRewriter1_20_5.java @@ -97,6 +97,7 @@ import com.viaversion.viaversion.util.Unit; import it.unimi.dsi.fastutil.ints.Int2IntMap; import it.unimi.dsi.fastutil.ints.Int2IntOpenHashMap; import it.unimi.dsi.fastutil.ints.Int2ObjectMap; +import it.unimi.dsi.fastutil.ints.Int2ObjectOpenHashMap; import it.unimi.dsi.fastutil.objects.Reference2ObjectOpenHashMap; import java.util.ArrayList; import java.util.HashMap; @@ -154,7 +155,7 @@ public class ComponentRewriter1_20_5 extends Co register(StructuredDataKey.SUSPICIOUS_STEW_EFFECTS, this::suspiciousStewEffectsToTag, this::suspiciousStewEffectsFromTag); register(StructuredDataKey.WRITABLE_BOOK_CONTENT, this::writableBookContentToTag, this::writableBookContentFromTag); register(StructuredDataKey.WRITTEN_BOOK_CONTENT, this::writtenBookContentToTag, this::writtenBookContentFromTag); - register(StructuredDataKey.TRIM, this::trimToTag); + register(StructuredDataKey.TRIM, this::trimToTag, this::trimFromTag); register(StructuredDataKey.DEBUG_STICK_STATE, this::debugStickRateToTag, this::debugStickRateFromTag); register(StructuredDataKey.ENTITY_DATA, this::entityDataToTag, this::entityDataFromTag); register(StructuredDataKey.BUCKET_ENTITY_DATA, this::bucketEntityDataToTag, this::bucketEntityDataFromTag); @@ -317,28 +318,28 @@ public class ComponentRewriter1_20_5 extends Co return tag; } - public List> toData(final CompoundTag tag) { + public List> toData(final UserConnection connection, final CompoundTag tag) { final List> list = new ArrayList<>(); if (tag != null) { for (final Map.Entry entry : tag.entrySet()) { - final StructuredData data = readFromTag(entry.getKey(), entry.getValue()); + final StructuredData data = readFromTag(connection, entry.getKey(), entry.getValue()); list.add(data); } } return list; } - public StructuredData readFromTag(final String identifier, final Tag tag) { + public StructuredData readFromTag(final UserConnection connection, final String identifier, final Tag tag) { final int id = protocol.getMappingData().getDataComponentSerializerMappings().mappedId(identifier); Preconditions.checkArgument(id != -1, "Unknown data component: %s", identifier); final StructuredDataKey key = structuredDataType.key(id); - return readFromTag(key, id, tag); + return readFromTag(connection, key, id, tag); } - private StructuredData readFromTag(final StructuredDataKey key, final int id, final Tag tag) { + private StructuredData readFromTag(final UserConnection connection, final StructuredDataKey key, final int id, final Tag tag) { final TagConverter converter = tagConverter(key); Preconditions.checkNotNull(converter, "No converter found for: %s", key); - return StructuredData.of(key, converter.convert(tag), id); + return StructuredData.of(key, converter.convert(connection, tag), id); } private String mappedIdentifier(final int id) { @@ -814,18 +815,18 @@ public class ComponentRewriter1_20_5 extends Co return itemArrayToTag(connection, value); } - protected Item[] chargedProjectilesFromTag(final Tag tag) { + protected Item[] chargedProjectilesFromTag(final UserConnection connection, final Tag tag) { final ListTag value = (ListTag) tag; - return itemArrayFromTag(value); + return itemArrayFromTag(connection, value); } protected ListTag bundleContentsToTag(final UserConnection connection, final Item[] value) { return itemArrayToTag(connection, value); } - protected Item[] bundleContentsFromTag(final Tag tag) { + protected Item[] bundleContentsFromTag(final UserConnection connection, final Tag tag) { final ListTag value = (ListTag) tag; - return itemArrayFromTag(value); + return itemArrayFromTag(connection, value); } protected CompoundTag potionContentsToTag(final PotionContents value) { @@ -1033,10 +1034,48 @@ public class ComponentRewriter1_20_5 extends Co return tag; } - protected ArmorTrim trimFromTag(final Tag tag) { + protected ArmorTrim trimFromTag(final UserConnection connection, final Tag tag) { final CompoundTag value = (CompoundTag) tag; - return null; //TODO + final Tag materialTag = value.get("material"); + Holder material; + final ArmorTrimStorage trimStorage = connection.get(ArmorTrimStorage.class); + if (materialTag instanceof StringTag stringTag) { + material = Holder.of(trimStorage.trimMaterials().keyToId(stringTag.getValue())); + } else { + final CompoundTag materialValue = (CompoundTag) materialTag; + final String assetName = identifierFromTag(materialValue.getStringTag("asset_name")); + final int ingredient = Protocol1_20_3To1_20_5.MAPPINGS.getFullItemMappings().mappedId(materialValue.getString("ingredient")); + final float itemModelIndex = materialValue.getFloat("item_model_index"); + final Int2ObjectMap overrideArmorMaterials = new Int2ObjectOpenHashMap<>(); + final CompoundTag overrideArmorMaterialsTag = materialValue.getCompoundTag("override_armor_materials"); + if (overrideArmorMaterialsTag != null) { + for (final Map.Entry entry : overrideArmorMaterialsTag.entrySet()) { + final int materialId = ArmorMaterials1_20_5.keyToId(entry.getKey()); + if (materialId != -1) { + overrideArmorMaterials.put(materialId, ((StringTag) entry.getValue()).getValue()); + } + } + } + final Tag description = materialValue.get("description"); + material = Holder.of(new ArmorTrimMaterial(assetName, ingredient, itemModelIndex, overrideArmorMaterials, description)); + } + + final Tag patternTag = value.get("pattern"); + Holder pattern; + if (patternTag instanceof StringTag stringTag) { + pattern = Holder.of(trimStorage.trimPatterns().keyToId(stringTag.getValue())); + } else { + final CompoundTag patternValue = (CompoundTag) patternTag; + final String assetName = identifierFromTag(patternValue.getStringTag("asset_id")); + final int templateItem = Protocol1_20_3To1_20_5.MAPPINGS.getFullItemMappings().mappedId(patternValue.getString("template_item")); + final Tag description = patternValue.get("description"); + final boolean decal = patternValue.getBoolean("decal", false); + pattern = Holder.of(new ArmorTrimPattern(assetName, templateItem, description, decal)); + } + + final boolean showInTooltip = value.getBoolean("show_in_tooltip", true); + return new ArmorTrim(material, pattern, showInTooltip); } protected CompoundTag debugStickRateToTag(final CompoundTag value) { @@ -1310,12 +1349,12 @@ public class ComponentRewriter1_20_5 extends Co return tag; } - protected Item[] containerFromTag(final Tag tag) { + protected Item[] containerFromTag(final UserConnection connection, final Tag tag) { final ListTag value = (ListTag) tag; final Item[] items = new Item[27]; for (final CompoundTag itemTag : value) { final int slot = itemTag.getInt("slot"); - final Item item = itemFromTag(itemTag.getCompoundTag("item")); + final Item item = itemFromTag(connection, itemTag.getCompoundTag("item")); items[slot] = item; } return items; @@ -1503,10 +1542,10 @@ public class ComponentRewriter1_20_5 extends Co return tag; } - protected Item[] itemArrayFromTag(final ListTag tag) { + protected Item[] itemArrayFromTag(final UserConnection connection, final ListTag tag) { final Item[] items = new Item[tag.size()]; for (int i = 0; i < tag.size(); i++) { - items[i] = itemFromTag(tag.get(i)); + items[i] = itemFromTag(connection, tag.get(i)); } return items; } @@ -1526,10 +1565,10 @@ public class ComponentRewriter1_20_5 extends Co tag.put("components", toTag(connection, components, true)); } - protected Item itemFromTag(final CompoundTag tag) { + protected Item itemFromTag(final UserConnection connection, final CompoundTag tag) { final int id = mappedId(tag.getString("id", "")); final int amount = checkPositiveInt(tag.getInt("count", 1)); - final List> components = toData(tag.getCompoundTag("components")); + final List> components = toData(connection, tag.getCompoundTag("components")); return new StructuredItem(id, amount, new StructuredDataContainer(components.toArray(StructuredData[]::new))); } @@ -1816,15 +1855,16 @@ public class ComponentRewriter1_20_5 extends Co converters.put(key, new ConverterPair<>(null, null)); } - protected void register(final StructuredDataKey key, final DataConverter dataConverter) { // TODO Remove this method - converters.put(key, new ConverterPair<>(dataConverter, null)); - } - protected void register(final StructuredDataKey key, final SimpleDataConverter dataConverter) { final DataConverter converter = ($, value) -> dataConverter.convert(value); converters.put(key, new ConverterPair<>(converter, null)); } + protected void register(final StructuredDataKey key, final SimpleDataConverter dataConverter, final SimpleTagConverter tagConverter) { + final DataConverter converter = ($, value) -> dataConverter.convert(value); + converters.put(key, new ConverterPair<>(converter, (connection, tag) -> tagConverter.convert(tag))); + } + protected void register(final StructuredDataKey key, final SimpleDataConverter dataConverter, final TagConverter tagConverter) { final DataConverter converter = ($, value) -> dataConverter.convert(value); converters.put(key, new ConverterPair<>(converter, tagConverter)); @@ -1863,11 +1903,17 @@ public class ComponentRewriter1_20_5 extends Co } @FunctionalInterface - protected interface TagConverter { + protected interface SimpleTagConverter { T convert(final Tag tag); } + @FunctionalInterface + protected interface TagConverter { + + T convert(UserConnection connection, Tag tag); + } + private record ConverterPair(DataConverter dataConverter, TagConverter tagConverter) { } } 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 5aa31652b..3aeffef77 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/ComponentRewriter.java @@ -39,8 +39,8 @@ import com.viaversion.viaversion.protocols.base.ClientboundLoginPackets; import com.viaversion.viaversion.util.ComponentUtil; import com.viaversion.viaversion.util.SerializerVersion; import com.viaversion.viaversion.util.TagUtil; -import org.checkerframework.checker.nullness.qual.Nullable; import java.util.BitSet; +import org.checkerframework.checker.nullness.qual.Nullable; /** * Handles json and tag components, containing methods to override certain parts of the handling. @@ -355,7 +355,6 @@ public class ComponentRewriter implements com.v return; } - // Until they're properly parsed final CompoundTag componentsTag = contentsTag.getCompoundTag("components"); handleShowItem(connection, contentsTag, componentsTag); if (componentsTag != null) {