Block entities
These types carry a block entity’s data where vanilla leaves it to each loader: to the clients that show it, and onto the item a broken block drops. Use them in place of each loader’s update packet and of a loot table that copies block entity data.
At a glance
Section titled “At a glance”| Type | Summary | Status | Since | Side | Thread |
|---|---|---|---|---|---|
BlockEntityItems |
Block-entity data on a block’s item. | Experimental | 2.15.0 | both | any |
BlockEntitySync |
Sends a SyncedBlockEntity’s client data after it changes. |
Experimental | 2.10.0 | server | server |
SyncedBlockEntity |
A mod’s BlockEntity whose clients need some of its state, such as a tank’s fill. |
Experimental | 2.10.0 | both | any |
Quick start
Section titled “Quick start”In TankBlockEntity, with amount its stored millibuckets and AMOUNT its NBT key, the
block entity implements SyncedBlockEntity and sends its data again when it fills:
@Overridepublic void writeClientData(CompoundTag tag) { tag.putLong(AMOUNT, amount);}
@Overridepublic void readClientData(CompoundTag tag) { amount = tag.getLong(AMOUNT);}
void fill(long mb) { amount += mb; setChanged(); BlockEntitySync.changed(this);}SyncedBlockEntity
Section titled “SyncedBlockEntity”BlockEntitySync: Experimental · Since 2.10.0 · Side: server · Thread: server
SyncedBlockEntity: Experimental · Since 2.10.0 · Side: both · Thread: any
A block entity that implements SyncedBlockEntity writes the data its clients show, and
BlockEntitySync#changed sends it again after a change. A tank whose look follows its fill
redraws when the data arrives:
@Overridepublic void onClientData() { level.sendBlockUpdated(worldPosition, getBlockState(), getBlockState(), Block.UPDATE_IMMEDIATE);}onClientData’s block update redraws the tank with its new fill; a block whose look does not follow the data leaves it out.
BlockEntityItems
Section titled “BlockEntityItems”Experimental · Since 2.15.0 · Side: both · Thread: any
BlockEntityItems writes chosen block entity data onto a block’s item and reads it back, so a
broken tank’s item keeps its fluid and placing the item restores it. The block writes the data
on its own drop, without the loot table, so a data pack cannot drop a tank without its fluid or
copy the fluid onto two items. In TankBlock, with TANK_ENTITY, its block entity type’s
handle, and writeKept, the tank’s method that writes the keys to keep:
@Override@SuppressWarnings("deprecation") // vanilla's own override point for a block's dropspublic List<ItemStack> getDrops(BlockState state, LootParams.Builder params) { ItemStack drop = new ItemStack(this); if (params.getOptionalParameter(LootContextParams.BLOCK_ENTITY) instanceof TankBlockEntity tank) { BlockEntityItems.setData(drop, TANK_ENTITY.get(), tank.writeKept()); } return List.of(drop);}Interaction with SyncedBlockEntity
Section titled “Interaction with SyncedBlockEntity”- A tank whose clients show its fill sends the restored data the way it sends any change, so a placed tank shows its fluid.
See also
Section titled “See also”- Block behavior: placing and breaking the blocks that hold these block entities.
- Block rendering:
mimic and data models, which draw from the data
SyncedBlockEntitysends. - Transfer: energy, items, and fluid a block entity exposes to other mods.