Skip to content

Commit f0f87b2

Browse files
committed
Fix 1.21.2 trim material override transition
1 parent 45a55a8 commit f0f87b2

File tree

8 files changed

+78
-4
lines changed

8 files changed

+78
-4
lines changed

api/src/main/java/com/viaversion/viaversion/api/minecraft/data/StructuredDataKey.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ public record StructuredDataKey<T>(String identifier, Type<T> type) {
128128
public static final StructuredDataKey<FilterableString[]> WRITABLE_BOOK_CONTENT = new StructuredDataKey<>("writable_book_content", FilterableString.ARRAY_TYPE);
129129
public static final StructuredDataKey<WrittenBook> WRITTEN_BOOK_CONTENT = new StructuredDataKey<>("written_book_content", WrittenBook.TYPE);
130130
public static final StructuredDataKey<ArmorTrim> TRIM1_20_5 = new StructuredDataKey<>("trim", ArmorTrim.TYPE1_20_5);
131+
public static final StructuredDataKey<ArmorTrim> TRIM1_21_2 = new StructuredDataKey<>("trim", ArmorTrim.TYPE1_21_2);
131132
public static final StructuredDataKey<ArmorTrim> TRIM1_21_4 = new StructuredDataKey<>("trim", ArmorTrim.TYPE1_21_4);
132133
public static final StructuredDataKey<CompoundTag> DEBUG_STICK_STATE = new StructuredDataKey<>("debug_stick_state", Types.COMPOUND_TAG);
133134
public static final StructuredDataKey<CompoundTag> ENTITY_DATA = new StructuredDataKey<>("entity_data", Types.COMPOUND_TAG);

api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrim.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,22 @@ public void write(final ByteBuf buffer, final ArmorTrim value) {
4545
buffer.writeBoolean(value.showInTooltip);
4646
}
4747
};
48+
public static final Type<ArmorTrim> TYPE1_21_2 = new Type<>(ArmorTrim.class) {
49+
@Override
50+
public ArmorTrim read(final ByteBuf buffer) {
51+
final Holder<ArmorTrimMaterial> material = ArmorTrimMaterial.TYPE1_21_2.read(buffer);
52+
final Holder<ArmorTrimPattern> pattern = ArmorTrimPattern.TYPE.read(buffer);
53+
final boolean showInTooltip = buffer.readBoolean();
54+
return new ArmorTrim(material, pattern, showInTooltip);
55+
}
56+
57+
@Override
58+
public void write(final ByteBuf buffer, final ArmorTrim value) {
59+
ArmorTrimMaterial.TYPE1_21_2.write(buffer, value.material);
60+
ArmorTrimPattern.TYPE.write(buffer, value.pattern);
61+
buffer.writeBoolean(value.showInTooltip);
62+
}
63+
};
4864
public static final Type<ArmorTrim> TYPE1_21_4 = new Type<>(ArmorTrim.class) {
4965
@Override
5066
public ArmorTrim read(final ByteBuf buffer) {

api/src/main/java/com/viaversion/viaversion/api/minecraft/item/data/ArmorTrimMaterial.java

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,43 @@ public ArmorTrimMaterial(final String assetName, final int itemId, final Map<Str
3838
}
3939

4040
public static final HolderType<ArmorTrimMaterial> TYPE1_20_5 = new HolderType<>() {
41+
// The override key is an int, but given we don't use it at all and that creating a new type is annoying,
42+
// we'll just store it in the string map:tm:
43+
@Override
44+
public ArmorTrimMaterial readDirect(final ByteBuf buffer) {
45+
final String assetName = Types.STRING.read(buffer);
46+
final int item = Types.VAR_INT.readPrimitive(buffer);
47+
final float itemModelIndex = buffer.readFloat();
48+
49+
final int overrideArmorMaterialsSize = Types.VAR_INT.readPrimitive(buffer);
50+
final Map<String, String> overrideArmorMaterials = new Object2ObjectArrayMap<>(overrideArmorMaterialsSize);
51+
for (int i = 0; i < overrideArmorMaterialsSize; i++) {
52+
final int key = Types.VAR_INT.readPrimitive(buffer);
53+
final String value = Types.STRING.read(buffer);
54+
overrideArmorMaterials.put(Integer.toString(key), value);
55+
}
56+
57+
final Tag description = Types.TAG.read(buffer);
58+
return new ArmorTrimMaterial(assetName, item, itemModelIndex, overrideArmorMaterials, description);
59+
}
60+
61+
@Override
62+
public void writeDirect(final ByteBuf buffer, final ArmorTrimMaterial value) {
63+
Types.STRING.write(buffer, value.assetName());
64+
Types.VAR_INT.writePrimitive(buffer, value.itemId());
65+
buffer.writeFloat(value.itemModelIndex());
66+
67+
Types.VAR_INT.writePrimitive(buffer, value.overrideArmorMaterials().size());
68+
for (final Map.Entry<String, String> entry : value.overrideArmorMaterials().entrySet()) {
69+
Types.VAR_INT.writePrimitive(buffer, Integer.parseInt(entry.getKey()));
70+
Types.STRING.write(buffer, entry.getValue());
71+
}
72+
73+
Types.TAG.write(buffer, value.description());
74+
}
75+
};
76+
77+
public static final HolderType<ArmorTrimMaterial> TYPE1_21_2 = new HolderType<>() {
4178
@Override
4279
public ArmorTrimMaterial readDirect(final ByteBuf buffer) {
4380
final String assetName = Types.STRING.read(buffer);

common/src/main/java/com/viaversion/viaversion/protocols/v1_20_3to1_20_5/rewriter/ComponentRewriter1_20_5.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
import com.viaversion.viaversion.api.type.types.UnsignedByteType;
7575
import com.viaversion.viaversion.api.type.types.item.StructuredDataType;
7676
import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.Protocol1_20_3To1_20_5;
77+
import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.data.ArmorMaterials1_20_5;
7778
import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.data.Attributes1_20_5;
7879
import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.data.BannerPatterns1_20_5;
7980
import com.viaversion.viaversion.protocols.v1_20_3to1_20_5.data.DyeColors;
@@ -692,7 +693,10 @@ protected CompoundTag convertTrim(final UserConnection connection, final ArmorTr
692693

693694
final CompoundTag overrideArmorMaterialsTag = new CompoundTag();
694695
for (final Map.Entry<String, String> entry : armorTrimMaterial.overrideArmorMaterials().entrySet()) {
695-
overrideArmorMaterialsTag.putString(entry.getKey(), entry.getValue());
696+
final String materialKey = ArmorMaterials1_20_5.idToKey(Integer.parseInt(entry.getKey()));
697+
if (materialKey != null) {
698+
overrideArmorMaterialsTag.putString(materialKey, entry.getValue());
699+
}
696700
}
697701

698702
materialTag.putString("asset_name", armorTrimMaterial.assetName());

common/src/main/java/com/viaversion/viaversion/protocols/v1_21_2to1_21_4/rewriter/BlockItemPacketRewriter1_21_4.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ public static void updateItemData(final Item item) {
123123
dataContainer.replaceKey(StructuredDataKey.BUNDLE_CONTENTS1_21_2, StructuredDataKey.BUNDLE_CONTENTS1_21_4);
124124
dataContainer.replaceKey(StructuredDataKey.CONTAINER1_21_2, StructuredDataKey.CONTAINER1_21_4);
125125
dataContainer.replaceKey(StructuredDataKey.USE_REMAINDER1_21_2, StructuredDataKey.USE_REMAINDER1_21_4);
126-
dataContainer.replaceKey(StructuredDataKey.TRIM1_20_5, StructuredDataKey.TRIM1_21_4);
126+
dataContainer.replaceKey(StructuredDataKey.TRIM1_21_2, StructuredDataKey.TRIM1_21_4);
127127
dataContainer.remove(StructuredDataKey.CUSTOM_MODEL_DATA1_20_5);
128128
}
129129

@@ -133,7 +133,7 @@ public static void downgradeItemData(final Item item) {
133133
dataContainer.replaceKey(StructuredDataKey.BUNDLE_CONTENTS1_21_4, StructuredDataKey.BUNDLE_CONTENTS1_21_2);
134134
dataContainer.replaceKey(StructuredDataKey.CONTAINER1_21_4, StructuredDataKey.CONTAINER1_21_2);
135135
dataContainer.replaceKey(StructuredDataKey.USE_REMAINDER1_21_4, StructuredDataKey.USE_REMAINDER1_21_2);
136-
dataContainer.replaceKey(StructuredDataKey.TRIM1_21_4, StructuredDataKey.TRIM1_20_5);
136+
dataContainer.replaceKey(StructuredDataKey.TRIM1_21_4, StructuredDataKey.TRIM1_21_2);
137137
dataContainer.remove(StructuredDataKey.CUSTOM_MODEL_DATA1_21_4);
138138
}
139139
}

common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/Protocol1_21To1_21_2.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -216,7 +216,7 @@ protected void onMappingDataLoaded() {
216216
StructuredDataKey.INTANGIBLE_PROJECTILE, StructuredDataKey.STORED_ENCHANTMENTS, StructuredDataKey.DYED_COLOR,
217217
StructuredDataKey.MAP_COLOR, StructuredDataKey.MAP_ID, StructuredDataKey.MAP_DECORATIONS, StructuredDataKey.MAP_POST_PROCESSING,
218218
StructuredDataKey.POTION_CONTENTS1_21_2, StructuredDataKey.SUSPICIOUS_STEW_EFFECTS, StructuredDataKey.WRITABLE_BOOK_CONTENT,
219-
StructuredDataKey.WRITTEN_BOOK_CONTENT, StructuredDataKey.TRIM1_20_5, StructuredDataKey.DEBUG_STICK_STATE, StructuredDataKey.ENTITY_DATA,
219+
StructuredDataKey.WRITTEN_BOOK_CONTENT, StructuredDataKey.TRIM1_21_2, StructuredDataKey.DEBUG_STICK_STATE, StructuredDataKey.ENTITY_DATA,
220220
StructuredDataKey.BUCKET_ENTITY_DATA, StructuredDataKey.BLOCK_ENTITY_DATA, StructuredDataKey.INSTRUMENT1_21_2,
221221
StructuredDataKey.RECIPES, StructuredDataKey.LODESTONE_TRACKER, StructuredDataKey.FIREWORK_EXPLOSION, StructuredDataKey.FIREWORKS,
222222
StructuredDataKey.PROFILE, StructuredDataKey.NOTE_BLOCK_SOUND, StructuredDataKey.BANNER_PATTERNS, StructuredDataKey.BASE_COLOR,

common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/BlockItemPacketRewriter1_21_2.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -587,6 +587,13 @@ public static void updateItemData(final Item item) {
587587
itemComponentsTag.putString("custom_name", ComponentUtil.plainToJson(lock).toString());
588588
return predicateTag;
589589
});
590+
dataContainer.replace(StructuredDataKey.TRIM1_20_5, StructuredDataKey.TRIM1_21_2, trim -> {
591+
// TODO Rewrite from int to string id via sent registry
592+
if (trim.material().isDirect()) {
593+
trim.material().value().overrideArmorMaterials().clear();
594+
}
595+
return trim;
596+
});
590597
}
591598

592599
public static void downgradeItemData(final Item item) {
@@ -624,6 +631,13 @@ public static void downgradeItemData(final Item item) {
624631
}
625632
return new FoodProperties1_20_5(food.nutrition(), food.saturationModifier(), food.canAlwaysEat(), eatSeconds, useRemainderData, foodEffects.toArray(new FoodProperties1_20_5.FoodEffect[0]));
626633
});
634+
dataContainer.replace(StructuredDataKey.TRIM1_21_2, StructuredDataKey.TRIM1_20_5, trim -> {
635+
// TODO
636+
if (trim.material().isDirect()) {
637+
trim.material().value().overrideArmorMaterials().clear();
638+
}
639+
return trim;
640+
});
627641
dataContainer.replaceKey(StructuredDataKey.CONTAINER1_21_2, StructuredDataKey.CONTAINER1_21);
628642
dataContainer.replaceKey(StructuredDataKey.CHARGED_PROJECTILES1_21_2, StructuredDataKey.CHARGED_PROJECTILES1_21);
629643
dataContainer.replaceKey(StructuredDataKey.BUNDLE_CONTENTS1_21_2, StructuredDataKey.BUNDLE_CONTENTS1_21);

common/src/main/java/com/viaversion/viaversion/rewriter/StructuredItemRewriter.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,8 @@ protected void updateItemDataComponents(final UserConnection connection, final I
118118
if (mappingData.getItemMappings() != null) {
119119
final Int2IntFunction itemIdRewriter = clientbound ? mappingData::getNewItemId : mappingData::getOldItemId;
120120
container.replace(StructuredDataKey.TRIM1_20_5, value -> value.rewrite(itemIdRewriter));
121+
container.replace(StructuredDataKey.TRIM1_21_2, value -> value.rewrite(itemIdRewriter));
122+
container.replace(StructuredDataKey.TRIM1_21_4, value -> value.rewrite(itemIdRewriter));
121123
container.replace(StructuredDataKey.POT_DECORATIONS, value -> value.rewrite(itemIdRewriter));
122124
container.replace(StructuredDataKey.REPAIRABLE, value -> value.rewrite(itemIdRewriter));
123125
}

0 commit comments

Comments
 (0)