Blocks

Overview

A Block is an immutable object containing:

  • Namespace & protocol id

  • Map<String, String> containing properties (e.g. waterlogged)

  • State id which is the numerical id defining the block visual used in chunk packets and a few others

  • Optional nbt

  • A BlockHandler

The immutability allows block references to be cached and reused.

Usage

Instance instance = ...;
// Each vanilla block has a constant visible from the `Block` interface
instance.setBlock(0, 40, 0, Block.STONE);

// Retrieve the tnt block and create a new block with the `unstable`
// property sets to "true".
// Property names are defined by Mojang and usable in various commands
Block tnt = Block.TNT.withProperty("unstable", "true");
instance.setBlock(0, 41, 0, tnt);

Registry

Each block has unique data which can be retrieved with Block#registry().

Tags

Block implements TagReadable meaning that they can contain all kinds of data. (see Tags)

Tags data can be serialized and will be saved on disk automatically.

Handlers

The BlockHandler interface allows blocks to have behavior by listening to some events like placement or interaction. And can be serialized to disk thanks to their namespace.

You can then decide to use one handler per block, or share it with several.

Last updated

Was this helpful?