#How to save nbt data into JsonIO properly?

4 messages · Page 1 of 1 (latest)

glossy jolt
#

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!

neon wraithBOT
#

Once your ticket has been resolved, please close it with </ticket close:1054771505520717835> command!

glossy jolt
#

Problem is doing event.server.tell on armorToSave shows it actually has data, but only saves the key, and all values becomes null when saving to JSON

spice narwhalBOT
#

Here's a simple snippet showing how to use NBTIO to read/write uncompressed NBT files anywhere within the Minecraft folder:

// Reading the contents of the file from the given path
config = NBTIO.read('kubejs/config/myawesomeconfig.nbt')

// Writing to an existing/new file
NBTIO.write('kubejs/config/myawesomeconfig.nbt', {settings: 'creeper', weirdblock: 'minecraft:end_gateway'})