properly upgrade items with newer MC versions

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

View File

@ -74,7 +74,7 @@ public class ModsSupport {
String serialized = entry.getValue(); String serialized = entry.getValue();
try { try {
String nbtString = VanillaSync.deserializeString(serialized); String nbtString = VanillaSync.deserializeString(serialized);
CompoundTag tag = NbtUtils.snbtToStructure(nbtString); CompoundTag tag = VanillaSync.snbtToFixedCompoundTag(nbtString);
ItemStack stack = ItemStack.of(tag); ItemStack stack = ItemStack.of(tag);
if (handler.getCurios().containsKey(slotType)) { if (handler.getCurios().containsKey(slotType)) {
ICurioStacksHandler stacksHandler = handler.getCurios().get(slotType); ICurioStacksHandler stacksHandler = handler.getCurios().get(slotType);

View File

@ -1,7 +1,9 @@
package vip.fubuki.playersync.sync; package vip.fubuki.playersync.sync;
import com.mojang.brigadier.exceptions.CommandSyntaxException; import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.serialization.Dynamic;
import net.minecraft.ChatFormatting; import net.minecraft.ChatFormatting;
import net.minecraft.SharedConstants;
import net.minecraft.nbt.*; import net.minecraft.nbt.*;
import net.minecraft.network.chat.Component; import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.Style; import net.minecraft.network.chat.Style;
@ -9,6 +11,8 @@ import net.minecraft.resources.ResourceLocation;
import net.minecraft.server.MinecraftServer; import net.minecraft.server.MinecraftServer;
import net.minecraft.server.PlayerAdvancements; import net.minecraft.server.PlayerAdvancements;
import net.minecraft.server.level.ServerPlayer; 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.InteractionHand;
import net.minecraft.world.effect.MobEffect; import net.minecraft.world.effect.MobEffect;
import net.minecraft.world.effect.MobEffectInstance; import net.minecraft.world.effect.MobEffectInstance;
@ -253,7 +257,7 @@ public class VanillaSync {
} }
String nbtString = deserializeString(serializedNbt); String nbtString = deserializeString(serializedNbt);
CompoundTag compoundTag = NbtUtils.snbtToStructure(nbtString); CompoundTag compoundTag = snbtToFixedCompoundTag(nbtString);
if (compoundTag == null || compoundTag.isEmpty() || !compoundTag.contains("id", Tag.TAG_STRING)) { if (compoundTag == null || compoundTag.isEmpty() || !compoundTag.contains("id", Tag.TAG_STRING)) {
return ItemStack.EMPTY; // Invalid or empty tag return ItemStack.EMPTY; // Invalid or empty tag
@ -346,6 +350,23 @@ public class VanillaSync {
return placeholder; 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. * 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. * 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 // Serialize the ItemStack to NBT
CompoundTag compoundTag = new CompoundTag(); CompoundTag compoundTag = new CompoundTag();
itemStack.save(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; return compoundTag;
} }