From f4253641e1e9219774c7fc1ed828cd5dff6d32ce Mon Sep 17 00:00:00 2001 From: Nassim Jahnke Date: Sun, 29 Sep 2024 13:42:26 +0200 Subject: [PATCH] Update VV usage --- .../BackwardsStructuredItemRewriter.java | 20 +++------ .../StructuredEnchantmentRewriter.java | 42 ++++++++++--------- .../BlockItemPacketRewriter1_20_5.java | 13 +++--- .../rewriter/BlockItemPacketRewriter1_21.java | 8 ++-- 4 files changed, 38 insertions(+), 45 deletions(-) diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsStructuredItemRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsStructuredItemRewriter.java index eb30e308..1a4818c0 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsStructuredItemRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/BackwardsStructuredItemRewriter.java @@ -20,14 +20,12 @@ package com.viaversion.viabackwards.api.rewriters; import com.viaversion.nbt.tag.CompoundTag; import com.viaversion.nbt.tag.IntTag; import com.viaversion.nbt.tag.ListTag; -import com.viaversion.nbt.tag.NumberTag; import com.viaversion.nbt.tag.Tag; import com.viaversion.viabackwards.api.BackwardsProtocol; import com.viaversion.viabackwards.api.data.BackwardsMappingData; import com.viaversion.viabackwards.api.data.MappedItem; import com.viaversion.viaversion.api.connection.UserConnection; 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; @@ -86,12 +84,12 @@ public class BackwardsStructuredItemRewriter customData = item.dataContainer().getNonEmpty(StructuredDataKey.CUSTOM_DATA); - return customData != null ? customData.value() : null; - } - protected void saveListTag(CompoundTag tag, ListTag original, String name) { // Multiple places might try to backup data String backupName = nbtTagName(name); diff --git a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/StructuredEnchantmentRewriter.java b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/StructuredEnchantmentRewriter.java index 9ac5d311..9686bb09 100644 --- a/common/src/main/java/com/viaversion/viabackwards/api/rewriters/StructuredEnchantmentRewriter.java +++ b/common/src/main/java/com/viaversion/viabackwards/api/rewriters/StructuredEnchantmentRewriter.java @@ -25,7 +25,6 @@ import com.viaversion.nbt.tag.Tag; import com.viaversion.viabackwards.api.data.BackwardsMappingData; import com.viaversion.viabackwards.utils.ChatUtil; import com.viaversion.viaversion.api.data.Mappings; -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; @@ -66,23 +65,19 @@ public class StructuredEnchantmentRewriter { public void handleToServer(final Item item) { final StructuredDataContainer data = item.dataContainer(); - final StructuredData customData = data.getNonEmpty(StructuredDataKey.CUSTOM_DATA); - if (customData == null) { - return; + final CompoundTag customData = data.get(StructuredDataKey.CUSTOM_DATA); + if (customData != null) { + rewriteEnchantmentsToServer(data, customData, StructuredDataKey.ENCHANTMENTS); + rewriteEnchantmentsToServer(data, customData, StructuredDataKey.STORED_ENCHANTMENTS); } - - final CompoundTag tag = customData.value(); - rewriteEnchantmentsToServer(data, tag, StructuredDataKey.ENCHANTMENTS); - rewriteEnchantmentsToServer(data, tag, StructuredDataKey.STORED_ENCHANTMENTS); } public void rewriteEnchantmentsToClient(final StructuredDataContainer data, final StructuredDataKey key, final IdRewriteFunction rewriteFunction, final DescriptionSupplier descriptionSupplier, final boolean storedEnchant) { - final StructuredData enchantmentsData = data.getNonEmpty(key); - if (enchantmentsData == null || enchantmentsData.value().size() == 0) { + final Enchantments enchantments = data.get(key); + if (enchantments == null || enchantments.size() == 0) { return; } - final Enchantments enchantments = enchantmentsData.value(); final List loreToAdd = new ArrayList<>(); boolean removedEnchantments = false; boolean updatedLore = false; @@ -104,7 +99,7 @@ public class StructuredEnchantmentRewriter { if (!removedEnchantments) { // Backup original before doing modifications - final CompoundTag customData = data.computeIfAbsent(StructuredDataKey.CUSTOM_DATA, $ -> new CompoundTag()).value(); + final CompoundTag customData = customData(data); itemRewriter.saveListTag(customData, asTag(enchantments), key.identifier()); removedEnchantments = true; } @@ -127,12 +122,12 @@ public class StructuredEnchantmentRewriter { } if (removedEnchantments) { - final CompoundTag tag = data.computeIfAbsent(StructuredDataKey.CUSTOM_DATA, $ -> new CompoundTag()).value(); + final CompoundTag tag = customData(data); if (!storedEnchant && enchantments.size() == 0) { // Add glint override if there are no enchantments left - final StructuredData glintOverride = data.getNonEmpty(StructuredDataKey.ENCHANTMENT_GLINT_OVERRIDE); + final Boolean glintOverride = data.get(StructuredDataKey.ENCHANTMENT_GLINT_OVERRIDE); if (glintOverride != null) { - tag.putBoolean(itemRewriter.nbtTagName("glint"), glintOverride.value()); + tag.putBoolean(itemRewriter.nbtTagName("glint"), glintOverride); } else { tag.putBoolean(itemRewriter.nbtTagName("noglint"), true); } @@ -146,10 +141,10 @@ public class StructuredEnchantmentRewriter { if (updatedLore) { // Save original lore - final CompoundTag tag = data.computeIfAbsent(StructuredDataKey.CUSTOM_DATA, $ -> new CompoundTag()).value(); - final StructuredData loreData = data.getNonEmpty(StructuredDataKey.LORE); - if (loreData != null) { - final List loreList = Arrays.asList(loreData.value()); + final CompoundTag tag = customData(data); + final Tag[] lore = data.get(StructuredDataKey.LORE); + if (lore != null) { + final List loreList = Arrays.asList(lore); itemRewriter.saveGenericTagList(tag, loreList, "lore"); loreToAdd.addAll(loreList); } else { @@ -159,6 +154,15 @@ public class StructuredEnchantmentRewriter { } } + private CompoundTag customData(final StructuredDataContainer data) { + CompoundTag tag = data.get(StructuredDataKey.CUSTOM_DATA); + if (tag == null) { + tag = new CompoundTag(); + data.set(StructuredDataKey.CUSTOM_DATA, tag); + } + return tag; + } + private ListTag asTag(final Enchantments enchantments) { final ListTag listTag = new ListTag<>(CompoundTag.class); for (final Int2IntMap.Entry entry : enchantments.enchantments().int2IntEntrySet()) { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_5to1_20_3/rewriter/BlockItemPacketRewriter1_20_5.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_5to1_20_3/rewriter/BlockItemPacketRewriter1_20_5.java index 6d16ccdc..ad93a2d9 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_5to1_20_3/rewriter/BlockItemPacketRewriter1_20_5.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_20_5to1_20_3/rewriter/BlockItemPacketRewriter1_20_5.java @@ -30,7 +30,6 @@ import com.viaversion.viaversion.api.connection.UserConnection; import com.viaversion.viaversion.api.minecraft.Holder; import com.viaversion.viaversion.api.minecraft.Particle; import com.viaversion.viaversion.api.minecraft.SoundEvent; -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; @@ -363,25 +362,25 @@ public final class BlockItemPacketRewriter1_20_5 extends BackwardsStructuredItem // Text components since we skip the usual rewrite method updateComponent(connection, item, StructuredDataKey.ITEM_NAME, "item_name"); updateComponent(connection, item, StructuredDataKey.CUSTOM_NAME, "custom_name"); - final StructuredData loreData = data.getNonEmpty(StructuredDataKey.LORE); - if (loreData != null) { - for (final Tag tag : loreData.value()) { + final Tag[] lore = data.get(StructuredDataKey.LORE); + if (lore != null) { + for (final Tag tag : lore) { protocol.getComponentRewriter().processTag(connection, tag); } } // In 1.20.6, some items have default values which are not written into the components - if (item.identifier() == 1105 && !data.contains(StructuredDataKey.FIREWORKS)) { + if (item.identifier() == 1105 && !data.has(StructuredDataKey.FIREWORKS)) { data.set(StructuredDataKey.FIREWORKS, new Fireworks(1, new FireworkExplosion[0])); } - final StructuredData customData = data.getNonEmpty(StructuredDataKey.CUSTOM_DATA); + final CompoundTag customData = data.get(StructuredDataKey.CUSTOM_DATA); final Item oldItem = vvProtocol.getItemRewriter().toOldItem(connection, item, DATA_CONVERTER); if (customData != null) { // We later don't know which tags are custom data and which are not because the VV conversion // keeps converted data, so we backup the original custom data and restore it later - oldItem.tag().put(nbtTagName(), customData.value().copy()); + oldItem.tag().put(nbtTagName(), customData.copy()); } if (oldItem.tag() != null && oldItem.tag().isEmpty()) { diff --git a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/rewriter/BlockItemPacketRewriter1_21.java b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/rewriter/BlockItemPacketRewriter1_21.java index f86bceef..02091d06 100644 --- a/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/rewriter/BlockItemPacketRewriter1_21.java +++ b/common/src/main/java/com/viaversion/viabackwards/protocol/v1_21to1_20_5/rewriter/BlockItemPacketRewriter1_21.java @@ -28,7 +28,6 @@ import com.viaversion.viabackwards.protocol.v1_21to1_20_5.storage.EnchantmentsPa import com.viaversion.viabackwards.protocol.v1_21to1_20_5.storage.OpenScreenStorage; import com.viaversion.viabackwards.protocol.v1_21to1_20_5.storage.PlayerRotationStorage; import com.viaversion.viaversion.api.connection.UserConnection; -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; @@ -195,7 +194,7 @@ public final class BlockItemPacketRewriter1_21 extends BackwardsStructuredItemRe downgradeItemData(item); final StructuredDataContainer dataContainer = item.dataContainer(); - if (dataContainer.contains(StructuredDataKey.RARITY)) { + if (dataContainer.has(StructuredDataKey.RARITY)) { return item; } @@ -233,12 +232,11 @@ public final class BlockItemPacketRewriter1_21 extends BackwardsStructuredItemRe } private void rewriteEnchantmentToServer(final EnchantmentsPaintingsStorage storage, final Item item, final StructuredDataKey key) { - final StructuredData enchantmentsData = item.dataContainer().getNonEmpty(key); - if (enchantmentsData == null) { + final Enchantments enchantments = item.dataContainer().get(key); + if (enchantments == null) { return; } - final Enchantments enchantments = enchantmentsData.value(); final List updatedIds = new ArrayList<>(); for (final Int2IntMap.Entry entry : enchantments.enchantments().int2IntEntrySet()) { final int id = entry.getIntKey();