#Saving Entity Inventory and giving it back

1 messages · Page 1 of 1 (latest)

fleet canyon
#

Title explains it.

#

This is for a backpack script

#

Needs an entity

#

And needs to save his inventory before despawn

#

But let's start by saving the player inventory for testing.

#

So what I think is, I loop the player inventory, and I get each item, but where do I safe it to?

dusky shard
#

memory is preferred

fleet canyon
#

What memory?

#

I don't think itemstacks keep their values on World reload, and they can't store multiple items at once I believe, otherwise I did this and give the saved itemstack to the player.

covert fossil
#

/structure save str ~ ~ ~ ~ ~ ~ true memory false

fleet canyon
#

I don't want structures though

#

The can't save a radius of like 5x5

#

Where the player is centered

covert fossil
#

u can...

fleet canyon
#

Tried with my old script and it didn't work like that...

covert fossil
#

cus it was disk

fleet canyon
#

No, it didn't safe 5 blocks around the player

covert fossil
#

~-5 ~-5 ~-5 ~+5 ~+5 ~+5

#

like that?

fleet canyon
#

No

#

Let me show example in game

#

@covert fossil

covert fossil
#

/structure save nameId ~-2 ~-1 ~-2 ~+2 ~+3 ~+2

fleet canyon
#

Let me write a script to see if it works like this

#

Cuz old script refused to save like that

dusky shard
#

now that runCommand is back on preview i wanna do wrapper for commands

fleet canyon
#

Wait what

#

They brought it back in preview?

dusky shard
#

yea

covert fossil
fleet canyon
#

Just testing if it saves the raduis

covert fossil
# fleet canyon Just testing if it saves the raduis

here's what i made that u can try

async function doStuff(plaer) {
    const { dimension: dim, id, location: { x, y, z } } = player
    const loc = [x, y, z].join(' ')
    let entity = await dim.runCommandAsync(`structure load ${id} ${loc} 0_degrees none true false`)
    if (entity.successCount) {
        system.run(async function find_mybp() {
            entity = [...dim.getEntities({ type: 'pog:backpack_uncommon', name: id, closest: 1 })][0]
            if (!entity) return system.run(find_mybp)
            return manage(player, entity)
        })
    } else {
        entity = dim.spawnEntity(`pog:backpack_uncommon`, { x, y, z })
        entity.nameTag = id
        return manage(player, entity)
    }

}

async function manage(player, entity) {
    const { container: inv, inventorySize: size } = entity.getComponent('inventory')
    for (let i = 0; i < size; i++) {
        const item = inv.getItem(i)
        if (!item?.amount) continue
        //item code stuff
    }
    await dim.runCommandAsync(`structure save ${id} ${loc} ${loc}`)
    system.run(() => entity.triggerEvent(`despawn_backpack_uncommon`))
}
fleet canyon
#

And 1 question

#

Since tick is gone, how would I detect when the item is held to run code only once and repeatedly?

covert fossil
#

system.runInterval()

fleet canyon
#

I use that

#

But then it's run code each tick

fleet canyon
#

I see it

covert fossil
#

try now

fleet canyon
#

What function do I call I'm the tick event?

covert fossil
#

how did u start it back then?

fleet canyon
#

Called the function that loads structure

#

I see okok

covert fossil
#

uhm i wouldnt do that

#

why is it loading for all players btw

fleet canyon
#

I was dumb then

covert fossil
#

#1046133595342913546 message

fleet canyon
#

What item Spawns the entity?

#

Darn I stupid

#

Wait wait

#

Ur script

#

I can't call the function

covert fossil
#

the entity should despawn when item isnt in mainhand, right?

fleet canyon
#

I can't even summon the entity

#

Function won't xall

covert fossil
#

ur code...

fleet canyon
#

Sending

#
import {system, world} from "@minecraft/server"

world.events.beforeItemUse.subscribe(data => {
    let { source: player } = data
  
      let invi = player.getComponent("inventory").container;
      let items = invi.getItem(player.selectedSlot);
      //this spawns the entity with a tag with the player name when the player does not have tag 'backpack1'
     if (items?.typeId == "minecraft:stick") {
      console.warn("test")
      doStuff(player)
    }
  })

    async function doStuff(player) {
        const { dimension: dim, id, location: { x, y, z } } = player
        const loc = [x, y, z].join(' ')
        let entity = await dim.runCommandAsync(`structure load ${id} ${loc} 0_degrees none true false`)
        if (entity.successCount) {
            system.run(async function find_mybp() {
                entity = [...dim.getEntities({ type: 'betterbedrock:player_corpse', name: id, closest: 1 })][0]
                if (!entity) return system.run(find_mybp)
                return manage(player, entity)
            })
        } else {
            entity = dim.spawnEntity(`betterbedrock:player_corpse`, { x, y, z })
            entity.nameTag = id
            return manage(player, entity)
        }
    
    }
    
    async function manage(player, entity) {
        const { container: inv, inventorySize: size } = entity.getComponent('inventory')
        for (let i = 0; i < size; i++) {
            const item = inv.getItem(i)
            if (!item?.amount) continue
            //item code stuff
        }
        await dim.runCommandAsync(`structure save ${id} ${loc} ${loc}`)
        system.run(() => entity.triggerEvent(`despawn_backpack_uncommon`))
    }```
covert fossil
#

dont trigger the despawn, yet

#

or maybe remove that

fleet canyon
#

No, I'm triggering doStuff

covert fossil
#
world.events.beforeItemUse.subscribe(data => {
    let { source: player, item } = data
    //this spawns the entity with a tag with the player name when the player does not have tag 'backpack1'
    if (item.typeId == "minecraft:stick") {
        console.warn("test")
        doStuff(player)
    }
})
fleet canyon
#

Doesn't call

covert fossil
#

entity?.successCount

fleet canyon
#

What?

#

Send to console?

covert fossil
#

no

#

uhh, what exactly isnt being called?

fleet canyon
#

doStuff

covert fossil
#

cus it didnt spawn an entity?

fleet canyon
#

Nope

#

Wait

#

Didn't spawn anything

covert fossil
#

ye

#

cus theres triggerEvent to despawn...

fleet canyon
#

I disabled that function for now

#

And still Spawns nothn

#

Ok

#

Function gets called

#

But no entity

covert fossil
#

ur code...

fleet canyon
#

It stops at if(entity.?successCoumt)

covert fossil
#

?.

fleet canyon
#

Typo k

#
import {system, world} from "@minecraft/server"

world.events.beforeItemUse.subscribe(data => {
    let { source: player, item } = data
    //this spawns the entity with a tag with the player name when the player does not have tag 'backpack1'
    if (item.typeId == "minecraft:stick") {
        console.warn("test")
        doStuff(player)
    }
})

    async function doStuff(player) {
        
        const { dimension: dim, id, location: { x, y, z } } = player
        const loc = [x, y, z].join(' ')
        let entity = await dim.runCommandAsync(`structure load ${id} ${loc} 0_degrees none true false`)
        if (entity?.successCount) {
            console.warn("testED")
            system.run(async function find_mybp() {
                entity = [...dim.getEntities({ type: 'betterbedrock:player_corpse', name: id, closest: 1 })][0]
                if (!entity) return system.run(find_mybp)
                //return manage(player, entity)
            })
        } else {
            entity = dim.spawnEntity(`betterbedrock:player_corpse`, { x, y, z })
            entity.nameTag = id
            //return manage(player, entity)
        }
    
    }
    
   /* async function manage(player, entity) {
        const { container: inv, inventorySize: size } = entity.getComponent('inventory')
        for (let i = 0; i < size; i++) {
            const item = inv.getItem(i)
            if (!item?.amount) continue
            //item code stuff
        }
        await dim.runCommandAsync(`structure save ${id} ${loc} ${loc}`)
        system.run(() => entity.triggerEvent(`despawn_backpack_uncommon`))
    }*/```
covert fossil
#
async function doStuff(player) {
    const { dimension: dim, id, location: { x, y, z } } = player
    const loc = [x, y, z].join(' ')
    dim.runCommandAsync(`structure load ${id} ${loc} 0_degrees none true false`).then(res => {
        console.warn(res.successCount);
        return system.run(async function find_mybp() {
            const entity = [...dim.getEntities({ type: 'betterbedrock:player_corpse', name: id, closest: 1 })][0]
            if (!entity) return system.run(find_mybp)
            manage(player, entity)
        })
    }, error => {
        console.warn(error);
        const entity = dim.spawnEntity(`betterbedrock:player_corpse`, { x, y, z })
        entity.nameTag = id
        return manage(player, entity)

    })
}
fleet canyon
#

It spawned entity now

covert fossil
#

w8, do u even have to do anything in the entity?

fleet canyon
#

Like what?

covert fossil
#

it's all save/ load structure now

fleet canyon
#

Yea..

#

Btw

#

Depsawm function doesn't get called now

#

Also, what is meant by //item code stuff?

covert fossil
#

nothing

#

uhh

fleet canyon
#

Okok

covert fossil
#

prob to get item to transfer to player?

fleet canyon
#

manage is called, but after the const it doesn't work.

fleet canyon
#

Idk what I'll do

#

Cuz, I can't detect if I'll hold item cuz new tick can't be canceled when it ran code

#

And I can't force open entity inv

covert fossil
# fleet canyon Cuz, I can't detect if I'll hold item cuz new tick can't be canceled when it ran...

try ```js
async function manage(player, entity) {
const { dimension: dim, id, location: { x, y, z }, selectedSlot } = player
const loc = [x, y, z].join(' ')
const inv = player.getComponent('inventory').container

const runId = system.runInterval(() => {
    const item = inv.getItem(selectedSlot) ?? {}
    const {typeId} = item
    if (entity.hasTag('despawn') || !player || typeId !== 'backpack:itemidk'){
        entity.addTag('despawn')
        entity.triggerEvent('despawn:event')
        return system.clearRun(runId)
    } 
    dim.runCommandAsync(`structure save ${id} ${loc} ${loc}`)
},5)

}

fleet canyon
#

addTag is undefined somehow

covert fossil
#

one-time error?

fleet canyon
#

I don't get any

#

Just the tag one

covert fossil
#

pls send ur code

#

XD

fleet canyon
#
import {system, world} from "@minecraft/server"

world.events.beforeItemUse.subscribe(data => {
    let { source: player, item, entity } = data
    //this spawns the entity with a tag with the player name when the player does not have tag 'backpack1'
    if (item.typeId == "minecraft:stick") {
        console.warn("test")
        doStuff(player)
    }
    if (item.typeId == "minecraft:arrow") {
        console.warn("test")
        manage(player, entity)
    }
})

async function doStuff(player) {
    const { dimension: dim, id, location: { x, y, z } } = player
    const loc = [x, y, z].join(' ')
    dim.runCommandAsync(`structure load ${id} ${loc} 0_degrees none true false`).then(res => {
        console.warn(res.successCount);
        return system.run(async function find_mybp() {
            const entity = [...dim.getEntities({ type: 'betterbedrock:player_corpse', name: id, closest: 1 })][0]
            if (!entity) return system.run(find_mybp)
            ///manage(player, entity)
        })
    }, error => {
        console.warn(error);
        const entity = dim.spawnEntity(`betterbedrock:player_corpse`, { x, y, z })
        entity.nameTag = id
        ///return manage(player, entity)

    })
}
    
async function manage(player, entity) {
    const { dimension: dim, id, location: { x, y, z }, selectedSlot } = player
    const loc = [x, y, z].join(' ')
    const inv = player.getComponent('inventory').container
    
    const runId = system.runInterval(() => {
        const item = inv.getItem(selectedSlot) ?? {}
        const {typeId} = item
        if ( !player || typeId !== 'minecraft:stick'){
            console.warn("llololololo")
            entity.addTag('despawn')
            entity.triggerEvent('delete_corpse')
            return system.clearRun(runId)
        } 
        dim.runCommandAsync(`structure save ${id} ${loc} ${loc}`)
    },5)
}```
covert fossil
#

entity.hasTag('despawn')

#

put it back

fleet canyon
#

Okk

#

can't read property of "hasTag" of undefined

fleet canyon
covert fossil
#
const item = inv.getItem(selectedSlot) ?? {}
const { typeId } = item
let despawn;
if (!player || typeId !== 'minecraft:stick') despawn = 1
if (!despawn) return dim.runCommandAsync(`structure save ${id} ${loc} ${loc}`)
entity.triggerEvent('delete_corpse')
system.clearRun(runId)
fleet canyon
#

can't read "triggerEvent" of undefined lol

#

Entity isn't defined properly

covert fossil
#

hmmmmm

fleet canyon
#

Yea, Entity isn't valid here

#

Even if it's part of the function, it's not defined as an entity

covert fossil
#

might be the query

#
{ type: 'betterbedrock:player_corpse', name: id,location:{x,y,z}, closest: 1 }
#

sheesh

fleet canyon
#

Where this

covert fossil
#

getEntities

fleet canyon
#

I put where

#

I see i see

#

Well

#

It depsawns

#

Didn't save entity

covert fossil
#

woops

#

put it there

#

the despawn variable

fleet canyon
#

I put item there

covert fossil
#

oh w8

fleet canyon
#

J

#

K

covert fossil
#

is it working now?

fleet canyon
#

What

#

Ur script can't save and load the structure

#

@covert fossil

covert fossil
#

now this

#
async function manage(player, entity) {
  const { dimension: dim, id, location: { x, y, z }, selectedSlot: slot_A, name } = player
  const loc = [x, y, z].join(' ')
  const getPlayer = () => [...dim.getPlayers({ name, location: { x, y, z } })][0]
  let despawn;
  const runId = system.runInterval(() => {
    if (despawn) return system.clearRun(runId)
    const p = getPlayer()
    const { selectedSlot: slot_B } = p
    const inv = p.getComponent('inventory').container
    const item = inv.getItem(slot_B) ?? {}
    const { typeId } = item
    if (!player || slot_A !== slot_B || typeId !== 'minecraft:stick') despawn = 1
    if (!despawn) return dim.runCommandAsync(`structure save "${id}" ${loc} ${loc}`)
    entity.triggerEvent('delete_corpse')
  }, 5)
}
#

tested

#

i have no clue why i have to getPlayer again in interval

fleet canyon
#

Forgot to define entity

covert fossil
#

also

#

structure load "${id}"

fleet canyon
#

I use a name for now

#

But no matter what

covert fossil
#

alr

fleet canyon
#

It can't save

covert fossil
#

send cude

#

lol

#

anyway, i gtg soon

fleet canyon
#

Same

#

Gonna sleep

#


async function doStuff(player) {
    const { dimension: dim, id, location: { x, y, z } } = player
    const loc = [x, y, z].join(' ')
    dim.runCommandAsync(`structure load "NAMED" ~ ~ ~ 0_degrees none true false`).then(res => {
        console.warn(res.successCount);
        return system.run(async function find_mybp() {
            const entity = [...dim.getEntities({ type: 'betterbedrock:player_corpse', name: id, closest: 1 })][0]
            if (!entity) return system.run(find_mybp)
            ///manage(player, entity)
        })
    }, error => {
        console.warn(error);
        const entity = dim.spawnEntity(`betterbedrock:player_corpse`, { x, y, z })
        entity.nameTag = id
        ///return manage(player, entity)

    })
}
    
async function manage(player, entity) {
    const { dimension: dim, id, location: { x, y, z }, selectedSlot: slot_A, name } = player
    const loc = [x, y, z].join(' ')
    const getPlayer = () => [...dim.getPlayers({ name, location: { x, y, z } })][0]
    let despawn;
    const runId = system.runInterval(() => {
        const entity = [...dim.getEntities({ type: 'betterbedrock:player_corpse', name: id, closest: 1 })][0]
      if (despawn) return system.clearRun(runId)
      const p = getPlayer()
      const { selectedSlot: slot_B } = p
      const inv = p.getComponent('inventory').container
      const item = inv.getItem(slot_B) ?? {}
      const { typeId } = item
      constole.warn("Loaded")
      if (!player || slot_A !== slot_B || typeId !== 'minecraft:stick') despawn = 1
      
      if (!despawn) return dim.runCommandAsync(`structure save "NAMED" ~-5 ~-1 ~-5 ~+5 ~+5 ~+5 memory`)
      entity.triggerEvent('delete_corpse')
    }, 5)
  }```
fleet canyon
covert fossil
#

Remove memory

fleet canyon
#

Ok

#

Still won't save

#

Command don't run

#

Ok

#

Tomorrow I'm gonna write a script in my way ok

#

I am gonna go nappy

twilit widget
#

why not save the json of the inventory of each player into a tag when they are hurt