tutorial:blockentity_sync_itemstack
This is an old revision of the document!
Table of Contents
Syncing BlockEntity data with ItemStack
Introduction
When you create a Block
with BlockEntity
, you might want to place the block with predefined Nbt data from an ItemStack
(of your BlockItem
), or save the BlockEntity
data in the ItemStack
after breaking the block.
Before proceeding, you will need a Block (with BlockItem) and a BlockEntity.
Block Drops with data
For a block to drop an ItemStack
with the Nbt from the BlockEntity
of the broken block, we only need to change the loot table:
- src/main/resources/data/tutorial/loot_tables/blocks/demo_block.json
{ "type": "minecraft:block", "pools": [ { "rolls": 1.0, "entries": [ { "type": "minecraft:item", "name": "tutorial:demo_block", "functions": [ { "function": "minecraft:copy_nbt", "source": "block_entity", "ops": [ { "source": "number", "target": "BlockEntityTag.number", "op": "replace" } ] } ] } ] } ] }
Where:
- source is the Tag key String we used in the
writeNbt
andreadNbt
methods in ourDemoBlockEntity
class – “number” - target is the Tag key hierarchy in the dropped
ItemStack
(source prefixed with the “BlockEntityTag” key, which is needed for placing the block back with the saved data) – “BlockEntityTag.number”
To save more fields, just add more replace operations (with source, target and op) to the “ops” array.
Reading saved data from ItemStack
tutorial/blockentity_sync_itemstack.1687086938.txt.gz · Last modified: 2023/06/18 11:15 by terra