From f0f87b26d7d1d1f80ec9f1ec5f4c672b89f38cb1 Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Fri, 29 Nov 2024 13:33:36 +0100 Subject: [PATCH] Fix 1.21.2 trim material override transition --- .../api/minecraft/data/StructuredDataKey.java | 1 + .../api/minecraft/item/data/ArmorTrim.java | 16 ++++++++ .../item/data/ArmorTrimMaterial.java | 37 +++++++++++++++++++ .../rewriter/ComponentRewriter1_20_5.java | 6 ++- .../BlockItemPacketRewriter1_21_4.java | 4 +- .../v1_21to1_21_2/Protocol1_21To1_21_2.java | 2 +- .../BlockItemPacketRewriter1_21_2.java | 14 +++++++ .../rewriter/StructuredItemRewriter.java | 2 + 8 files changed, 78 insertions(+), 4 deletions(-) diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataKey.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataKey.java index 3e1a99e20..3560ad31a 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataKey.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataKey.java @@ -128,6 +128,7 @@ public record StructuredDataKey(String identifier, Type type) { public static final StructuredDataKey WRITABLE_BOOK_CONTENT = new StructuredDataKey<>("writable_book_content", FilterableString.ARRAY_TYPE); public static final StructuredDataKey WRITTEN_BOOK_CONTENT = new StructuredDataKey<>("written_book_content", WrittenBook.TYPE); public static final StructuredDataKey TRIM1_20_5 = new StructuredDataKey<>("trim", ArmorTrim.TYPE1_20_5); + public static final StructuredDataKey TRIM1_21_2 = new StructuredDataKey<>("trim", ArmorTrim.TYPE1_21_2); public static final StructuredDataKey TRIM1_21_4 = new StructuredDataKey<>("trim", ArmorTrim.TYPE1_21_4); public static final StructuredDataKey DEBUG_STICK_STATE = new StructuredDataKey<>("debug_stick_state", Types.COMPOUND_TAG); public static final StructuredDataKey ENTITY_DATA = new StructuredDataKey<>("entity_data", Types.COMPOUND_TAG); diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrim.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrim.java index 8681fe2f4..e6c276d37 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrim.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrim.java @@ -45,6 +45,22 @@ public record ArmorTrim(Holder material, Holder TYPE1_21_2 = new Type<>(ArmorTrim.class) { + @Override + public ArmorTrim read(final ByteBuf buffer) { + final Holder material = ArmorTrimMaterial.TYPE1_21_2.read(buffer); + final Holder pattern = ArmorTrimPattern.TYPE.read(buffer); + final boolean showInTooltip = buffer.readBoolean(); + return new ArmorTrim(material, pattern, showInTooltip); + } + + @Override + public void write(final ByteBuf buffer, final ArmorTrim value) { + ArmorTrimMaterial.TYPE1_21_2.write(buffer, value.material); + ArmorTrimPattern.TYPE.write(buffer, value.pattern); + buffer.writeBoolean(value.showInTooltip); + } + }; public static final Type TYPE1_21_4 = new Type<>(ArmorTrim.class) { @Override public ArmorTrim read(final ByteBuf buffer) { diff --git a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrimMaterial.java b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrimMaterial.java index 391b4877e..2b9dde7e9 100644 --- a/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrimMaterial.java +++ b/api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrimMaterial.java @@ -38,6 +38,43 @@ public record ArmorTrimMaterial(String assetName, int itemId, float itemModelInd } public static final HolderType TYPE1_20_5 = new HolderType<>() { + // The override key is an int, but given we don't use it at all and that creating a new type is annoying, + // we'll just store it in the string map:tm: + @Override + public ArmorTrimMaterial readDirect(final ByteBuf buffer) { + final String assetName = Types.STRING.read(buffer); + final int item = Types.VAR_INT.readPrimitive(buffer); + final float itemModelIndex = buffer.readFloat(); + + final int overrideArmorMaterialsSize = Types.VAR_INT.readPrimitive(buffer); + final Map overrideArmorMaterials = new Object2ObjectArrayMap<>(overrideArmorMaterialsSize); + for (int i = 0; i < overrideArmorMaterialsSize; i++) { + final int key = Types.VAR_INT.readPrimitive(buffer); + final String value = Types.STRING.read(buffer); + overrideArmorMaterials.put(Integer.toString(key), value); + } + + final Tag description = Types.TAG.read(buffer); + return new ArmorTrimMaterial(assetName, item, itemModelIndex, overrideArmorMaterials, description); + } + + @Override + public void writeDirect(final ByteBuf buffer, final ArmorTrimMaterial value) { + Types.STRING.write(buffer, value.assetName()); + Types.VAR_INT.writePrimitive(buffer, value.itemId()); + buffer.writeFloat(value.itemModelIndex()); + + Types.VAR_INT.writePrimitive(buffer, value.overrideArmorMaterials().size()); + for (final Map.Entry entry : value.overrideArmorMaterials().entrySet()) { + Types.VAR_INT.writePrimitive(buffer, Integer.parseInt(entry.getKey())); + Types.STRING.write(buffer, entry.getValue()); + } + + Types.TAG.write(buffer, value.description()); + } + }; + + public static final HolderType TYPE1_21_2 = new HolderType<>() { @Override public ArmorTrimMaterial readDirect(final ByteBuf buffer) { final String assetName = Types.STRING.read(buffer); 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 48a6d41ed..597753b70 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 @@ -74,6 +74,7 @@ import com.viaversion.viaversion.api.protocol.packet.ClientboundPacketType; import com.viaversion.viaversion.api.type.types.UnsignedByteType; import com.viaversion.viaversion.api.type.types.item.StructuredDataType; import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.Protocol1_20_3To1_20_5; +import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.data.ArmorMaterials1_20_5; import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.data.Attributes1_20_5; import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.data.BannerPatterns1_20_5; import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.data.DyeColors; @@ -692,7 +693,10 @@ public class ComponentRewriter1_20_5 extends Co final CompoundTag overrideArmorMaterialsTag = new CompoundTag(); for (final Map.Entry entry : armorTrimMaterial.overrideArmorMaterials().entrySet()) { - overrideArmorMaterialsTag.putString(entry.getKey(), entry.getValue()); + final String materialKey = ArmorMaterials1_20_5.idToKey(Integer.parseInt(entry.getKey())); + if (materialKey != null) { + overrideArmorMaterialsTag.putString(materialKey, entry.getValue()); + } } materialTag.putString("asset_name", armorTrimMaterial.assetName()); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_21_2to1_21_4/rewriter/BlockItemPacketRewriter1_21_4.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_21_2to1_21_4/rewriter/BlockItemPacketRewriter1_21_4.java index 07f032443..3f1983321 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_21_2to1_21_4/rewriter/BlockItemPacketRewriter1_21_4.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_21_2to1_21_4/rewriter/BlockItemPacketRewriter1_21_4.java @@ -123,7 +123,7 @@ public final class BlockItemPacketRewriter1_21_4 extends StructuredItemRewriter< dataContainer.replaceKey(StructuredDataKey.BUNDLE_CONTENTS1_21_2, StructuredDataKey.BUNDLE_CONTENTS1_21_4); dataContainer.replaceKey(StructuredDataKey.CONTAINER1_21_2, StructuredDataKey.CONTAINER1_21_4); dataContainer.replaceKey(StructuredDataKey.USE_REMAINDER1_21_2, StructuredDataKey.USE_REMAINDER1_21_4); - dataContainer.replaceKey(StructuredDataKey.TRIM1_20_5, StructuredDataKey.TRIM1_21_4); + dataContainer.replaceKey(StructuredDataKey.TRIM1_21_2, StructuredDataKey.TRIM1_21_4); dataContainer.remove(StructuredDataKey.CUSTOM_MODEL_DATA1_20_5); } @@ -133,7 +133,7 @@ public final class BlockItemPacketRewriter1_21_4 extends StructuredItemRewriter< dataContainer.replaceKey(StructuredDataKey.BUNDLE_CONTENTS1_21_4, StructuredDataKey.BUNDLE_CONTENTS1_21_2); dataContainer.replaceKey(StructuredDataKey.CONTAINER1_21_4, StructuredDataKey.CONTAINER1_21_2); dataContainer.replaceKey(StructuredDataKey.USE_REMAINDER1_21_4, StructuredDataKey.USE_REMAINDER1_21_2); - dataContainer.replaceKey(StructuredDataKey.TRIM1_21_4, StructuredDataKey.TRIM1_20_5); + dataContainer.replaceKey(StructuredDataKey.TRIM1_21_4, StructuredDataKey.TRIM1_21_2); dataContainer.remove(StructuredDataKey.CUSTOM_MODEL_DATA1_21_4); } } 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 7dc6c761a..6c6f20687 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 @@ -216,7 +216,7 @@ public final class Protocol1_21To1_21_2 extends AbstractProtocol { + // TODO Rewrite from int to string id via sent registry + if (trim.material().isDirect()) { + trim.material().value().overrideArmorMaterials().clear(); + } + return trim; + }); } public static void downgradeItemData(final Item item) { @@ -624,6 +631,13 @@ public final class BlockItemPacketRewriter1_21_2 extends StructuredItemRewriter< } return new FoodProperties1_20_5(food.nutrition(), food.saturationModifier(), food.canAlwaysEat(), eatSeconds, useRemainderData, foodEffects.toArray(new FoodProperties1_20_5.FoodEffect[0])); }); + dataContainer.replace(StructuredDataKey.TRIM1_21_2, StructuredDataKey.TRIM1_20_5, trim -> { + // TODO + if (trim.material().isDirect()) { + trim.material().value().overrideArmorMaterials().clear(); + } + return trim; + }); dataContainer.replaceKey(StructuredDataKey.CONTAINER1_21_2, StructuredDataKey.CONTAINER1_21); dataContainer.replaceKey(StructuredDataKey.CHARGED_PROJECTILES1_21_2, StructuredDataKey.CHARGED_PROJECTILES1_21); dataContainer.replaceKey(StructuredDataKey.BUNDLE_CONTENTS1_21_2, StructuredDataKey.BUNDLE_CONTENTS1_21); 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 3338332fe..26ea5ccf8 100644 --- a/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java +++ b/common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java @@ -118,6 +118,8 @@ public class StructuredItemRewriter value.rewrite(itemIdRewriter)); + container.replace(StructuredDataKey.TRIM1_21_2, value -> value.rewrite(itemIdRewriter)); + container.replace(StructuredDataKey.TRIM1_21_4, value -> value.rewrite(itemIdRewriter)); container.replace(StructuredDataKey.POT_DECORATIONS, value -> value.rewrite(itemIdRewriter)); container.replace(StructuredDataKey.REPAIRABLE, value -> value.rewrite(itemIdRewriter)); }