import { EnchantmentType, ItemStack, system, world } from "@minecraft/server";
/*@param {Player} player /
export function saveInventory(player, invName = player.name, storage = player) {
let { container, inventorySize } = player.getComponent("inventory");
const items = [];
const listOfEquipmentSlots = ["Head", "Chest", "Legs", "Feet", "Offhand"];
let wornArmor = [];
for (let i = 0; i < listOfEquipmentSlots.length; i++) {
const equipment = player.getComponent("equippable").getEquipment(listOfEquipmentSlots[i]);
if (!equipment) {
wornArmor.push(null);
continue;
}
const data = {
typeId: equipment.typeId,
props: {
amount: equipment.amount,
keepOnDeath: equipment.keepOnDeath,
lockMode: equipment.lockMode
},
lore: equipment.getLore(),
components: {}
};
if (equipment.nameTag) data.props.nameTag = equipment.nameTag;
if (equipment.hasComponent("enchantable")) {
data.components.enchantable = equipment.getComponent("enchantable").getEnchantments().map(e => ({ type: e.type.id, level: e.level }));
}
if (equipment.hasComponent("durability")) {
data.components.durability = equipment.getComponent("durability").damage;
}
wornArmor.push(data);
}
storage.setDynamicProperty(`armor:${invName}`, JSON.stringify(wornArmor));
for (let i = 0; i < inventorySize; i++) {
const item = container.getItem:info:;
if (!item) {
items.push(null);
continue;
}
const data = {
typeId: item.typeId,
props: {
amount: item.amount,
keepOnDeath: item.keepOnDeath,
lockMode: item.lockMode
},
lore: item.getLore(),
components: {}
};
if (item.nameTag) data.props.nameTag = item.nameTag;
if (item.hasComponent("enchantable")) {
data.components.enchantable = item.getComponent("enchantable").getEnchantments().map(e => ({ type: e.type.id, level: e.level }));
}
if (item.hasComponent("durability")) {
data.components.durability = item.getComponent("durability").damage;
}
items.push(data);
}
storage.setDynamicProperty(`inventory:${invName}`, JSON.stringify(items));
return {items, wornArmor};
}
/*@param {Player} player /
export function loadInventory(player, invName = player.name, storage = player) {
let { container, inventorySize } = player.getComponent("inventory");
const items = JSON.parse(storage.getDynamicProperty(`inventory:${invName}`) ?? "[]");
const wornArmor = JSON.parse(storage.getDynamicProperty(`armor:${invName}`) ?? "[]");
const listOfEquipmentSlots = ["Head", "Chest", "Legs", "Feet", "Offhand"];
for (let i = 0; i < listOfEquipmentSlots.length; i++) {
const equipment = player.getComponent("equippable");
const data = wornArmor[i];
if (!data) {
equipment.setEquipment(listOfEquipmentSlots[i], null);
} else {
const item = new ItemStack(data.typeId);
for (const key in data.props) {
item[key] = data.props[key];
}
item.setLore(data.lore);
if (data.components.enchantable) {
const enchantable = item.getComponent("minecraft:enchantable");
if (enchantable) {
enchantable.addEnchantments(data.components.enchantable.map(e => ({...e, type: new EnchantmentType(e.type)})));
}
}
if (data.components.durability) {
const durability = item.getComponent("minecraft:durability");
if (durability) {
durability.damage = data.components.durability;
}
}
equipment.setEquipment(listOfEquipmentSlots[i], item);
}
}
for (let i = 0; i < inventorySize; i++) {
const data = items[i];
if (!data) {
container.setItem(i, null);
} else {
const item = new ItemStack(data.typeId);
for (const key in data.props) {
item[key] = data.props[key];
}
item.setLore(data.lore);
if (data.components.enchantable) {
const enchantable = item.getComponent("minecraft:enchantable");
if (enchantable) {
enchantable.addEnchantments(data.components.enchantable.map(e => ({...e, type: new EnchantmentType(e.type)})));
}
}
if (data.components.durability) {
const durability = item.getComponent("minecraft:durability");
if (durability) {
durability.damage = data.components.durability;
}
}
container.setItem(i, item);
}
}
}
Una vez lo intento cargar, todo bien, pero si me salgo del mundo procede a crashearse, esto podria ser un error ya que no hemos encontrado como evitarlo, esto esta relacionado a el desarrollamiento de scripts
Thank you for your report!
Thank you for taking the time to report a bug. Unfortunately we are only able to accept bug reports in English since it is the common language used on this tracker.
You are welcome to create a new ticket about your issue in English. You can use an online translator, if it helps.
However, please search before writing a new bug report, as it's very likely that someone else already reported your issue before.
If you find an existing ticket about your issue, you can add a vote and any new information to it instead of reporting it yourself.
Please note that this bug tracker is not for support or technical issues. If you have a payment issue or an issue with your account, please contact Microsoft Support. If you need help with a technical issue (e.g. Minecraft doesn't start at all), please contact Community Support.
Quick Links:
📓 Bug Tracker Guidelines – 💬 Community Support – 📧 Mojang Support (Technical Issues) – 📧 Microsoft Support (Account Issues)
📓 Project Summary – ✍️ Feedback and Suggestions – 📖 Game Wiki