diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/BlockItemPacketRewriter1_21_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/BlockItemPacketRewriter1_21_2.java index 90acc67c7..60bedecde 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/BlockItemPacketRewriter1_21_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/BlockItemPacketRewriter1_21_2.java @@ -17,13 +17,13 @@ */ package com.viaversion.viaversion.protocols.v1_21to1_21_2.rewriter; +import com.viaversion.nbt.tag.CompoundTag; 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; import com.viaversion.viaversion.api.minecraft.HolderSet; import com.viaversion.viaversion.api.minecraft.Particle; -import com.viaversion.viaversion.api.minecraft.data.StructuredData; import com.viaversion.viaversion.api.minecraft.data.StructuredDataContainer; import com.viaversion.viaversion.api.minecraft.data.StructuredDataKey; import com.viaversion.viaversion.api.minecraft.item.Item; @@ -49,6 +49,7 @@ import com.viaversion.viaversion.rewriter.BlockRewriter; import com.viaversion.viaversion.rewriter.SoundRewriter; import com.viaversion.viaversion.rewriter.StructuredItemRewriter; import com.viaversion.viaversion.util.Key; +import com.viaversion.viaversion.util.TagUtil; import com.viaversion.viaversion.util.Unit; public final class BlockItemPacketRewriter1_21_2 extends StructuredItemRewriter { @@ -286,10 +287,25 @@ public final class BlockItemPacketRewriter1_21_2 extends StructuredItemRewriter< dataContainer.replaceKey(StructuredDataKey.BUNDLE_CONTENTS1_21, StructuredDataKey.BUNDLE_CONTENTS1_21_2); dataContainer.replaceKey(StructuredDataKey.POTION_CONTENTS1_20_5, StructuredDataKey.POTION_CONTENTS1_21_2); dataContainer.replace(StructuredDataKey.FIRE_RESISTANT, StructuredDataKey.DAMAGE_RESISTANT, fireResistant -> new DamageResistant("minecraft:is_fire")); + dataContainer.replace(StructuredDataKey.LOCK, lock -> { + final CompoundTag predicateTag = new CompoundTag(); + final CompoundTag itemComponentsTag = new CompoundTag(); + predicateTag.put("components", itemComponentsTag); + itemComponentsTag.put("custom_name", lock); + return predicateTag; + }); } public static void downgradeItemData(final Item item) { final StructuredDataContainer dataContainer = item.dataContainer(); + dataContainer.replace(StructuredDataKey.LOCK, StructuredDataKey.LOCK, lock -> { + final CompoundTag predicateTag = (CompoundTag) lock; + final CompoundTag itemComponentsTag = predicateTag.getCompoundTag("components"); + if (itemComponentsTag != null) { + return TagUtil.getNamespacedStringTag(itemComponentsTag, "custom_name"); + } + return null; + }); dataContainer.replace(StructuredDataKey.INSTRUMENT1_21_2, StructuredDataKey.INSTRUMENT1_20_5, instrument -> { if (instrument.hasId()) { return Holder.of(instrument.id()); @@ -298,11 +314,10 @@ public final class BlockItemPacketRewriter1_21_2 extends StructuredItemRewriter< return Holder.of(new Instrument1_20_5(value.soundEvent(), (int) (value.useDuration() * 20), value.range())); }); dataContainer.replace(StructuredDataKey.FOOD1_21_2, StructuredDataKey.FOOD1_21, food -> { - final StructuredData consumableData = dataContainer.getNonEmpty(StructuredDataKey.CONSUMABLE1_21_2); - final StructuredData useRemainderData = dataContainer.getNonEmpty(StructuredDataKey.USE_REMAINDER); - final Item usingConvertsTo = useRemainderData != null ? useRemainderData.value() : null; - final float eatSeconds = consumableData != null ? consumableData.value().consumeSeconds() : 1.6F; - return new FoodProperties1_20_5(food.nutrition(), food.saturationModifier(), food.canAlwaysEat(), eatSeconds, usingConvertsTo, new FoodProperties1_20_5.FoodEffect[0]); + final Consumable1_21_2 consumableData = dataContainer.get(StructuredDataKey.CONSUMABLE1_21_2); + final Item useRemainderData = dataContainer.get(StructuredDataKey.USE_REMAINDER); + final float eatSeconds = consumableData != null ? consumableData.consumeSeconds() : 1.6F; + return new FoodProperties1_20_5(food.nutrition(), food.saturationModifier(), food.canAlwaysEat(), eatSeconds, useRemainderData, new FoodProperties1_20_5.FoodEffect[0]); }); dataContainer.replaceKey(StructuredDataKey.CONTAINER1_21_2, StructuredDataKey.CONTAINER1_21); dataContainer.replaceKey(StructuredDataKey.CHARGED_PROJECTILES1_21_2, StructuredDataKey.CHARGED_PROJECTILES1_21); diff --git a/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/ComponentRewriter1_21_2.java b/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/ComponentRewriter1_21_2.java index e1c04429f..f1306c432 100644 --- a/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/ComponentRewriter1_21_2.java +++ b/common/src/main/java/com/viaversion/viaversion/protocols/v1_21to1_21_2/rewriter/ComponentRewriter1_21_2.java @@ -61,6 +61,7 @@ public final class ComponentRewriter1_21_2 extends ComponentRewriter itemIdRewriter = clientbound ? id -> mappedIdentifier(mappingData.getFullItemMappings(), id) : id -> unmappedIdentifier(mappingData.getFullItemMappings(), id); - container.updateIfPresent(StructuredDataKey.USE_COOLDOWN, value -> value.rewrite(itemIdRewriter)); + container.replace(StructuredDataKey.USE_COOLDOWN, value -> value.rewrite(itemIdRewriter)); } if (mappingData.getBlockMappings() != null) { final Int2IntFunction blockIdRewriter = clientbound ? mappingData::getNewBlockId : mappingData::getOldBlockId; diff --git a/common/src/main/java/com/viaversion/viaversion/util/TagUtil.java b/common/src/main/java/com/viaversion/viaversion/util/TagUtil.java index fa51e322e..ac34fe676 100644 --- a/common/src/main/java/com/viaversion/viaversion/util/TagUtil.java +++ b/common/src/main/java/com/viaversion/viaversion/util/TagUtil.java @@ -19,6 +19,7 @@ package com.viaversion.viaversion.util; 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 java.util.Map; import org.checkerframework.checker.nullness.qual.Nullable; @@ -69,6 +70,11 @@ public final class TagUtil { return listTag != null ? listTag : tag.getListTag(Key.stripMinecraftNamespace(key), CompoundTag.class); } + public static @Nullable StringTag getNamespacedStringTag(final CompoundTag tag, final String key) { + final StringTag stringTag = tag.getStringTag(Key.namespaced(key)); + return stringTag != null ? stringTag : tag.getStringTag(Key.stripMinecraftNamespace(key)); + } + public static Tag handleDeep(final Tag tag, final TagUpdater consumer) { return handleDeep(null, tag, consumer); }