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` interfaceinstance.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 commandsBlock 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().
Block block =Block.GRASS;// Some fields have their own dedicated methodboolean solid =block.registry().isSolid();// ... you can however retrieve them from stringdouble hardness =block.registry().getDouble("hardness");hardness =block.registry().hardness();
Tags
Block implements TagReadable meaning that they can contain all kinds of data. (see Tags)
Tag<String> tag =Tag.String("my-key");Block tnt =Block.TNT;// Create a new block with the tag sets to "my-value"tnt =tnt.withTag(tag,"my-value");// Retrieve the value from the newly created blockString value =tnt.getTag(tag);// Block can also expose a convenient view of their nbtNBTCompound nbt =tnt.nbt();
Tags data can be serialized and will be saved on disk automatically.
Tags id, x, y, z and keepPackedare used by the anvil loader and may cause unexpected behavior when added to blocks.
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.
publicclassDemoHandlerimplementsBlockHandler { @OverridepublicvoidonPlace(@NotNullPlacement placement) {if (placement instanceof PlayerPlacement) {// A player placed the block }Block block =placement.getBlock();System.out.println("The block "+block.name() +" has been placed"); } @Overridepublic @NotNullNamespaceIDgetNamespaceId() {// Namespace required for serialization purposereturnNamespaceID.from("minestom:demo"); }}
You can then decide to use one handler per block, or share it with several.
Block tnt =Block.TNT;// Create a new block with the specified handler.// Be aware that block objects can be reused, handlers should// therefore never assume to be assigned to a single block.tnt =tnt.withHandler(newDemoHandler());// Share the same handler reference with multiple blocksBlockHandler handler =newDemoHandler();Block stone =Block.STONE.withHandler(handler);Block grass =Block.GRASS.withHandler(handler);