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
#store an itemStack in an entity
1 messages · Page 1 of 1 (latest)
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
how a scoreboard can save an object?
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
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
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 :/
nvm, solved!
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.
In short, items that uses data on them and container