[@minecraft/server ver: 2.1.0, client ver: 26.21]
I want to access components (only read) on an item entity. I can get the 'minecraft:item' component for it, but any attempt to get actual item components fail, always returning undefined. I also used found that the output of ItemStack.getComponents() seems to just return an empty array?
I then had the idea to take the ItemStack.typeId, which was accessible, to create a fresh ItemStack that I could hopefully access more properly. Unfortunately, I found only the same thing.
Here was my code to do that:
world.afterEvents.entitySpawn.subscribe((event) => {
const {entity} = event
if (entity.hasComponent('minecraft:item')) {
let item = entity.getComponent('minecraft:item').itemStack
let itemType = new ItemStack(item.typeId)
debugLog(itemType.getComponents(), 'itemComps')
let foodData = itemType.getComponent('minecraft:food')
debugLog(foodData)
}
})
Both using item.getComponent(...) and itemType.getComponent(...) yeild the same result.
debugLog(text, tag) is a simple function a made, consider it equivalent to console.log() or world.sendMessage()