package systems.brn.servershop; import net.minecraft.item.ItemStack; import net.minecraft.nbt.NbtCompound; import net.minecraft.nbt.NbtElement; import net.minecraft.registry.RegistryWrapper; import java.util.Optional; public record ItemPrice(int buyPrice, int sellPrice, ItemStack stack) { public NbtCompound toNbt(RegistryWrapper.WrapperLookup wrapperLookup) { NbtCompound nbt = new NbtCompound(); nbt.putInt("BuyPrice", this.buyPrice); nbt.putInt("SellPrice", this.sellPrice); // Serialize the ItemStack to NBT and add it to the compound NbtElement stackNbt = stack.encode(wrapperLookup); nbt.put("ItemStack", stackNbt); // Adds the ItemStack's NBT data to the main NBT compound return nbt; } public static ItemPrice fromNbt(NbtCompound nbt, RegistryWrapper.WrapperLookup wrapperLookup) { int buyPrice = nbt.getInt("BuyPrice"); int sellPrice = nbt.getInt("SellPrice"); // Deserialize the ItemStack from the NBT NbtElement stackElement = nbt.get("ItemStack"); Optional stack = ItemStack.fromNbt(wrapperLookup, stackElement); return stack.map(itemStack -> new ItemPrice(buyPrice, sellPrice, itemStack)).orElse(null); } }