4 Blocks
TheMode edited this page 2020-05-04 21:35:36 +02:00

Blocks are a bit special on Minestom, there are normal vanilla blocks which are only visual and CustomBlock which contains multiple callbacks in order to expand their functionalities.

Custom block

In order to create your own CustomBlock you need to create a class extending CustomBlock, implement all of its abstract method and finally register it using the BlockManager.

BlockManager blockManager = MinecraftServer.getBlockManager();
blockManager.registerCustomBlock(YOUR_CUSTOMBLOCK_CONSTRUCTOR);

Examples of custom blocks can be found here

Some things to point out are the CustomBlock#getCustomBlockId which should return a UNIQUE id independent from the vanilla block id and should never be changed after having chunks saved since it could lead to corruption.

There is also a special CustomBlock#getBreakDelay which can be used to customize the breaking time of any custom block, can be disabled when < 0

Batches

When manipulating a lot of blocks, it is wiser to make use of a Batch to update all the chunks at once. There exist two kinds of Batch, the BlockBatch, and the ChunkBatch.

The BlockBatch is used to modify blocks at multiple different chunks.

In contrary, the ChunkBatch is used to modify a single chunk only (like for chunk generation)

// Create a BlockBatch
BlockBatch blockBatch = instance.createBlockBatch();

// Create a ChunkBatch
ChunkBatch chunkBatch = instance.createChunkBatch(THE_CHUNK_TO_MODIFY);