Skip to content

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.

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

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:

Show a tank's fill on its clients
@Override
public void writeClientData(CompoundTag tag) {
tag.putLong(AMOUNT, amount);
}
@Override
public void readClientData(CompoundTag tag) {
amount = tag.getLong(AMOUNT);
}
void fill(long mb) {
amount += mb;
setChanged();
BlockEntitySync.changed(this);
}

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:

Redraw a tank when its fill arrives
@Override
public 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.

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:

Keep a tank's contents on its dropped item
@Override
@SuppressWarnings("deprecation") // vanilla's own override point for a block's drops
public 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);
}
  • A tank whose clients show its fill sends the restored data the way it sends any change, so a placed tank shows its fluid.
  • Block behavior: placing and breaking the blocks that hold these block entities.
  • Block rendering: mimic and data models, which draw from the data SyncedBlockEntity sends.
  • Transfer: energy, items, and fluid a block entity exposes to other mods.