#Persistent data not immediately available?

9 messages · Page 1 of 1 (latest)

keen quest
#

Hey y'all,

I'm currently attempting to write a set of scripts that will give the player an item on respawn to teleport to their last death.

However, right now the item teleports you to the death before your last death.

Context:

I first get the player death location, then store it as persistent data. When the player respawns, they get an item with the death location as nbt. Finally, when the player uses the item, kubejs reads the item nbt to teleport the player.

The problem: If I check the nbt data on the item, it's pointing to the death before the latest death, rather than the latest.

regal locustBOT
#

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

keen quest
#

Here is my startup script:

// Listen to item registry event
StartupEvents.registry('item', event => {
    event.create('recall_gem')
        .maxStackSize(1)
        .glow(true)
        .useAnimation('bow')
        .useDuration(itemstack => 64)
    .use((level, player, hand) => true)
    .finishUsing((itemstack, level, entity) => {
        if(itemstack.nbt != null){
            let coords = itemstack.nbt.coords;
            let dim = itemstack.nbt.dimension;
            itemstack.shrink(1)
            if(itemstack.nbt.coords != null){
                if (entity.player) {
                    entity.teleportTo(
                        dim,
                        coords.x, coords.y, coords.z,
                        0.0,0.0
                    );
                }
            }
        }
        return itemstack
    })
})
#

And here is my server script:

EntityEvents.death("player", event => {
    let {source, entity, server} = event;
    if (entity.player){
        const data = entity.persistentData;
        let deathLoc = entity.nbt.LastDeathLocation;
        data.put("lastDeath", deathLoc);
    }
})

PlayerEvents.respawned(event => {
    const data = event.player.persistentData;
    let deathLoc = data.get("lastDeath");

    let recallGem =
        Item.of(
            'kubejs:recall_gem',
            {
                "coords":{
                    "x": deathLoc.pos[0],
                    "y": deathLoc.pos[1],
                    "z": deathLoc.pos[2]
                },
                "dimension": deathLoc.dimension
            }
        ).weakNBT();

    event.player.give(recallGem);
})
rugged lance
#

This happens because the lastDeathLocation nbt hasn't been adjusted yet.

#

You don't need persostentData at all for this really. Just check the lastDeathLocation nbt in the player respawn event

keen quest
#

oh! Okay I will try that

#

That worked! Can't believe I didn't think of that, thank you

#

If anyone needs the solution in future, all I did was change my server script:

PlayerEvents.respawned(event => {
    const data = event.player.persistentData;
    let deathLoc = event.player.nbt.LastDeathLocation;

    event.player.tell("Welcome Back");
    event.player.tell("Last death: " + deathLoc.pos);
    event.player.tell("DeathDim: " + deathLoc.dimension);

    let recallGem =
        Item.of(
            'kubejs:recall_gem',
            {
                "coords":{
                    "x": deathLoc.pos[0],
                    "y": deathLoc.pos[1],
                    "z": deathLoc.pos[2]
                },
                "dimension": deathLoc.dimension
            }
        ).weakNBT();

    event.player.give(recallGem);
})