properly upgrade items with newer MC versions

This commit is contained in:
EoD 2025-06-17 20:01:14 +00:00
parent ca2ef85dda
commit 0e37cdc091
2 changed files with 25 additions and 2 deletions

View File

@ -73,7 +73,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);

View File

@ -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.core.registries.BuiltInRegistries;
import net.minecraft.nbt.*;
import net.minecraft.network.chat.Component;
@ -10,6 +12,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<Tag> dynamicTagInput = new Dynamic<>(NbtOps.INSTANCE, parsedTag);
Dynamic<Tag> 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;
}