#store an itemStack in an entity

1 messages · Page 1 of 1 (latest)

paper sage
#

I want to store the itemStack of a dropped item in an entity. I have managed to store it in a variable to later use it, but the itemStack disappears when restarting the world. My question is if there is a way to keep the itemStack created in an entity even when exiting and returning to the world, for example data like dynamicProperties but cannot store itemStack

jagged mist
#

you can use a Scoreboard database

#
- scoreboard database // save datas as a stringified object
- dynamicProperty // save it as a string with JSON.stringify(<yourObject>) and reverse it with JSON.parse(<yourString>) 
- tag // same as dynamicProperty
paper sage
sacred gyro
paper sage
# sacred gyro json stringify?

Ah, I didn't know that using that allowed you to store the data of an object, I hadn't set it when I used dynamicProperties, that's why I got errors

#

Thank you

sacred gyro
#

yea entity is not needed most times. i only really use for item databases when i will be handling large amounts of item data

#

but you can always just get the data from the item and then store it in dynamic properties or a score database

paper sage
# sacred gyro json stringify?

can you help me a bit? 😅 this isnt working

const itemStack = entity?.getComponent('item')?.itemStack;
itemGround.setDynamicProperty(`mb_lb_itemStack`, JSON.stringify(itemStack));
const itemProperty = sourceEntity?.getDynamicProperty(`mb_lb_itemStack`);
const itemStack2 = JSON.parse(itemProperty);
const inv = player?.getComponent("inventory").container;
inv?.addItem(itemStack2);```
it throws an error saying that itemStack2 isnt an itemStack
#

I tried using new ItemStack too but only Id and Amount is allowed to use :/

paper sage
#

nvm, solved!

void leaf
#

Keep in mind this won't work for all items. Anything that can't be created through the script API yet won't work. Like books, potions, Shulker Boxes, maps, etc.

burnt heart