Skip to content

Block builder

BlockBuilder is a fluent builder class designed to simplify the creation and registration of custom blocks in Minecraft. It allows modders to specify block properties (hardness, resistance, luminance, etc.), optionally associate a TileEntity, tags, sounds, and items, and then build a Block object with all those properties applied.

It also contains a nested Registry class to handle ID allocation for custom blocks.


Key Features

  1. Fluent Property Setters

  2. setHardness(float) → sets block hardness.

  3. setResistance(float) → sets blast resistance.
  4. setLuminance(int) → sets light emitted by the block.
  5. setSlipperiness(float) → changes player movement speed on the block.
  6. setImmovable(), setUnbreakable(), setInfiniburn() → sets special behaviors.
  7. setFlammability(int, int) → sets fire spread and burn chance.

  8. Tile Entity & Custom Block Item

  9. setTileEntity(Supplier<TileEntity>) → attaches a tile entity to the block.

  10. setBlockItem(BlockLambda<ItemBlock<?>>) → allows specifying a custom item to represent the block in inventory.

  11. Tags & Textures

  12. setTags(Tag<Block<?>>...) and addTags(Tag<Block<?>>...) → assign Minecraft tags for behavior like PLANTABLE, INFINITE_BURN.

  13. textures array → holds 6 texture paths (one for each block face).

  14. Build Method

  15. build(String, int, BlockLogicSupplier<T>) → creates a Block<T> instance with all properties applied.

  16. Handles vanilla initialization, registration, and caching.

Cloning Approach

  • Every setter clones the builder to maintain immutability, e.g.:
public BlockBuilder setHardness(float hardness) {
    BlockBuilder blockBuilder = this.clone();
    blockBuilder.hardness = hardness;
    return blockBuilder;
}
  • clone() copies the textures array to prevent shared references.

Registry Nested Class

  • Helps allocate numeric IDs for new blocks.

  • Methods:

  • findOpenIds(int count) → finds a sequence of free IDs for new blocks.

  • findLength(int id, int terminate) → finds the length of a run of free IDs.
  • reserveRuns(String modId, Toml runs, int neededIds, Consumer<IdSupplier> function) → registers block ID runs from a TOML config.

  • Uses RunReserves and RunLengthConfig to track reserved block IDs to prevent collisions.


Example Usage

BlockBuilder builder = new BlockBuilder("mymod")
    .setHardness(2.0f)
    .setResistance(10.0f)
    .setLuminance(15)
    .setFlammability(5, 20)
    .setTileEntity(MyTileEntity::new);

Block<?> myBlock = builder.build("my_block", 300, MyBlockLogic::new);

This would create a block with custom hardness, resistance, luminance, flammability, and a tile entity.


Interesting Details

  • Uses BlockLambda as a functional interface for custom item creation.
  • Supports “infiniburn” blocks (like Netherrack in vanilla Minecraft).
  • Provides a mechanism for ticking blocks on load (tickOnLoad) and controlling visual updates.
  • Integrates with mod’s ID management through Registry for safe numeric ID assignment.