I cant save them properly, some properties become null
I was planning on saving the NBt data into json file on death, and on respawn bring them back (a keep armor on death functionality) but the NBT data is not being saved properly. Thought of saving as string, but i dont know how to parse string to NBT
EntityEvents.death(event => {
if (!event.entity.isPlayer()) return
let armorData = {};
const filePath = 'kubejs/config/armordata.json'
if (!JsonIO.read(filePath)) {
JsonIO.write(filePath, {})
}
armorData = JsonIO.read(filePath)
const player = event.entity
const playerId = player.getUuid().toString()
const armor = player.inventory.armor
armorData[playerId] = {}
armorData[playerId].savedArmor = {}
JsonIO.write(filePath, armorData)
// Save each armor piece with its slot index
for (let i = 0; i < armor.size(); i++) {
let armorPiece = armor.get(i)
if (!armorPiece.isEmpty()) {
// Store the item as NBT data
let armorToSave = armorPiece.toNBT();
armorData[playerId].savedArmor[i] = armorToSave;
JsonIO.write(filePath, armorData)
event.server.tell(`Saved armor piece in slot ${i}: ${armorPiece.id}`)
armorPiece.count = 0;
}
}
// event.server.tell(`Armor saved for player ${playerId}`)
})
The data saved
{
"157896ba-bd34-4c67-a25d-4f641d7a764c": {
"savedArmor": {
"0": {
"id": null,
"count": null
}
}
}
}
Hoping someone can help me here. Thanks!