#Saving Entity Inventory and giving it back
1 messages · Page 1 of 1 (latest)
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?
memory is preferred
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.
/structure save str ~ ~ ~ ~ ~ ~ true memory false
I don't want structures though
The can't save a radius of like 5x5
Where the player is centered
u can...
Tried with my old script and it didn't work like that...
cus it was disk
No, it didn't safe 5 blocks around the player
/structure save nameId ~-2 ~-1 ~-2 ~+2 ~+3 ~+2
Let me write a script to see if it works like this
Cuz old script refused to save like that
now that runCommand is back on preview i wanna do wrapper for commands
yea
why u wana save the blocks?
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`))
}
I'll try
And 1 question
Since tick is gone, how would I detect when the item is held to run code only once and repeatedly?
system.runInterval()
How does this run code if I select item?
I see it
Ur updating this a lot
try now
What function do I call I'm the tick event?
how did u start it back then?
I was dumb then
#1046133595342913546 message
What item Spawns the entity?
Darn I stupid
Wait wait
Ur script
I can't call the function
the entity should despawn when item isnt in mainhand, right?
ur code...
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`))
}```
No, I'm triggering doStuff
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)
}
})
Doesn't call
entity?.successCount
doStuff
cus it didnt spawn an entity?
I disabled that function for now
And still Spawns nothn
Ok
Function gets called
But no entity
ur code...
It stops at if(entity.?successCoumt)
?.
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`))
}*/```
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)
})
}
It spawned entity now
w8, do u even have to do anything in the entity?
Like what?
it's all save/ load structure now
Yea..
Btw
Depsawm function doesn't get called now
Also, what is meant by //item code stuff?
Okok
prob to get item to transfer to player?
manage is called, but after the const it doesn't work.
Uhh.... 😆
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
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)
}
addTag is undefined somehow
one-time error?
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)
}```
What we do now?
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)
hmmmmm
Yea, Entity isn't valid here
Even if it's part of the function, it's not defined as an entity
might be the query
{ type: 'betterbedrock:player_corpse', name: id,location:{x,y,z}, closest: 1 }
sheesh
Where this
getEntities
I put item there
oh w8
is it working now?
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
Forgot to define entity
alr
It can't save
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)
}```
Did
Remove memory
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
why not save the json of the inventory of each player into a tag when they are hurt