From a1e1616eac239fb2b7f19dd9f5eaa72a201230c1 Mon Sep 17 00:00:00 2001 From: EoD <293499+EoD@users.noreply.github.com> Date: Tue, 17 Jun 2025 20:01:14 +0000 Subject: [PATCH] properly upgrade items with newer MC versions --- .../fubuki/playersync/sync/ModsSupport.java | 2 +- .../fubuki/playersync/sync/VanillaSync.java | 25 ++++++++++++++++++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/main/java/vip/fubuki/playersync/sync/ModsSupport.java b/src/main/java/vip/fubuki/playersync/sync/ModsSupport.java index 4523efb..7af2fd4 100644 --- a/src/main/java/vip/fubuki/playersync/sync/ModsSupport.java +++ b/src/main/java/vip/fubuki/playersync/sync/ModsSupport.java @@ -74,7 +74,7 @@ public class ModsSupport { String serialized = entry.getValue(); try { String nbtString = VanillaSync.deserializeString(serialized); - CompoundTag tag = NbtUtils.snbtToStructure(nbtString); + CompoundTag tag = VanillaSync.snbtToFixedCompoundTag(nbtString); ItemStack stack = ItemStack.of(tag); if (handler.getCurios().containsKey(slotType)) { ICurioStacksHandler stacksHandler = handler.getCurios().get(slotType); diff --git a/src/main/java/vip/fubuki/playersync/sync/VanillaSync.java b/src/main/java/vip/fubuki/playersync/sync/VanillaSync.java index 63684d0..3a8b8b7 100644 --- a/src/main/java/vip/fubuki/playersync/sync/VanillaSync.java +++ b/src/main/java/vip/fubuki/playersync/sync/VanillaSync.java @@ -1,7 +1,9 @@ package vip.fubuki.playersync.sync; import com.mojang.brigadier.exceptions.CommandSyntaxException; +import com.mojang.serialization.Dynamic; import net.minecraft.ChatFormatting; +import net.minecraft.SharedConstants; import net.minecraft.nbt.*; import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Style; @@ -9,6 +11,8 @@ import net.minecraft.resources.ResourceLocation; import net.minecraft.server.MinecraftServer; import net.minecraft.server.PlayerAdvancements; import net.minecraft.server.level.ServerPlayer; +import net.minecraft.util.datafix.DataFixers; +import net.minecraft.util.datafix.fixes.References; import net.minecraft.world.InteractionHand; import net.minecraft.world.effect.MobEffect; import net.minecraft.world.effect.MobEffectInstance; @@ -253,7 +257,7 @@ public class VanillaSync { } String nbtString = deserializeString(serializedNbt); - CompoundTag compoundTag = NbtUtils.snbtToStructure(nbtString); + CompoundTag compoundTag = snbtToFixedCompoundTag(nbtString); if (compoundTag == null || compoundTag.isEmpty() || !compoundTag.contains("id", Tag.TAG_STRING)) { return ItemStack.EMPTY; // Invalid or empty tag @@ -346,6 +350,23 @@ public class VanillaSync { return placeholder; } + public static CompoundTag snbtToFixedCompoundTag(String nbtString) throws CommandSyntaxException { + CompoundTag parsedTag = TagParser.parseTag(nbtString); + + int currentDataVersion = SharedConstants.getCurrentVersion().getDataVersion().getVersion(); + int snbtDataVersion = NbtUtils.getDataVersion(parsedTag, 500); + + Dynamic dynamicTagInput = new Dynamic<>(NbtOps.INSTANCE, parsedTag); + + Dynamic updatedDynamicTag = DataFixers.getDataFixer().update( + References.ITEM_STACK, + dynamicTagInput, + snbtDataVersion, + currentDataVersion); + CompoundTag compoundTag = (CompoundTag) updatedDynamicTag.getValue(); + return compoundTag; + } + /** * Deserializes a string from the database back into an NBT string. * Handles both the new Base64 format (prefixed with "B64:") and the old custom format. @@ -453,6 +474,8 @@ public class VanillaSync { // Serialize the ItemStack to NBT CompoundTag compoundTag = new CompoundTag(); itemStack.save(compoundTag); + // Adding data version to allow newer version of Minecraft to properly update the itemstack from the db + NbtUtils.addCurrentDataVersion(compoundTag); return compoundTag; }