Table of Contents

This page has been moved, the new location is https://docs.fabricmc.net/develop/blocks/first-block.

Adding a Block

Adding blocks to your mod follows a similar process to adding an item. You can create an instance of Block or a custom class, and then register it under Registries.BLOCK (for 1.19.3 and above) or Registry.BLOCK (for 1.19.2 and below). You also need to provide a texture and blockstate/model file to give your block visuals. For more information on the block model format, view the Minecraft Wiki Model page.

Creating a Block

:!: If you are using 1.21.2 or later versions, please directly see Registering blocks in 1.21.2+.

Start by creating an instance of Block. It can be stored at any location, but we will start at the top of your ModInitializer. The Block constructor requires an AbstractBlock.Settings instance, which is a builder for configuring block properties. Fabric provides a FabricBlockSettings builder class with more available options.

ExampleMod.java
  1. public class ExampleMod implements ModInitializer {
  2.  
  3. /* Declare and initialize our custom block instance.
  4.   We set our block material to `METAL`.
  5.  
  6.   `strength` sets both the hardness and the resistance of a block to the same value.
  7.   Hardness determines how long the block takes to break, and resistance determines how strong the block is against blast damage (e.g. explosions).
  8.   Stone has a hardness of 1.5f and a resistance of 6.0f, while Obsidian has a hardness of 50.0f and a resistance of 1200.0f.
  9.  
  10.   You can find the stats of all vanilla blocks in the class `Blocks`, where you can also reference other blocks.
  11.   */
  12.  
  13. // For versions below 1.20:
  14. // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f));
  15. // For versions below 1.20.5:
  16. // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f));
  17. // For versions since 1.20.5 below 1.21.2:
  18. public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f));
  19.  
  20. @Override
  21. public void onInitialize() {
  22.  
  23. }
  24. }

Registering your Block

Blocks should be registered under the Registries.BLOCK registry. Similar to registering items, just call Registry.register and pass in the appropriate arguments. You can either register the block in onInitialize method or directly when creating the block instance in the static context, as the register method returns the block instance as well.

If you're using version 1.19.2 or below, please replace Registries.BLOCK with Registry.BLOCK

ExampleMod.java
  1. public class ExampleMod implements ModInitializer {
  2.  
  3. // For versions below 1.20:
  4. // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f));
  5. // For versions below 1.20.5:
  6. // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f));
  7. // For versions since 1.20.5 below 1.21.2:
  8. public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f));
  9.  
  10. @Override
  11. public void onInitialize() {
  12. // For versions below 1.21:
  13. // Registry.register(Registries.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);
  14. // For versions since 1.21:
  15. Registry.register(Registries.BLOCK, Identifier.of("tutorial", "example_block"), EXAMPLE_BLOCK);
  16. }
  17. }

Your custom block will not be accessible as an item yet, but it can be seen in-game by using the command /setblock <position> tutorial:example_block.

Registering an Item for your Block

In most cases, you want to be able to place your block using an item. To do this, you need to register a corresponding BlockItem in the item registry. You can do this by registering an instance of BlockItem under Registries.ITEM. The registry name of the item should usually be the same as the registry name of the block.

ExampleMod.java
  1. public class ExampleMod implements ModInitializer {
  2.  
  3. // For versions below 1.20:
  4. // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).strength(4.0f));
  5. // For versions below 1.20.5:
  6. // public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.create().strength(4.0f));
  7. // For versions since 1.20.5 below 1.21.2:
  8. public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.create().strength(4.0f));
  9.  
  10. @Override
  11. public void onInitialize() {
  12. Registry.register(Registries.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);
  13. // For versions below 1.20.5:
  14. // Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new FabricItemSettings()));
  15. // For versions below 1.21:
  16. // Registry.register(Registries.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings()));
  17. // For versions since 1.21:
  18. Registry.register(Registries.ITEM, Identifier.of("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings()));
  19. }
  20. }

Best practice of registering blocks

:!: This section does not apply to versions 1.21.2 and later.

Sometimes you have many blocks in the mod. If you register them in such ways, you have to write complex codes for each of them, and the code will be messy. Therefore, similar to registering items, we create a separate class for blocks, and a utility methods to register the block and item.

TutorialBlocks.java
public final class TutorialBlocks {
    public static final Block EXAMPLE_BLOCK = register("example_block", new Block(Block.Settings.create().strength(4.0f)));
 
    private static <T extends Block> T register(String path, T block) {
        Registry.register(Registries.BLOCK, Identifier.of("tutorial", path), block);
        Registry.register(Registries.ITEM, Identifier.of("tutorial", path), new BlockItem(block, new Item.Settings()));
        return block;
    }
 
    public static void initialize() {
    }
}

Remember to initialize the TutorialBlocks class in the ModInitializer:

ExampleMod.java
public class ExampleMod implements ModInitializer {
    @Override
    public void onInitialize() {
        TutorialBlocks.initialize();
    }
}

Registering blocks in 1.21.2+

In 1.21.2+, RegistryKey should be added into the AbstractBlock.Settings for the block, as well as Item.Settings for the item. It looks troublesome, but luckily, Minecraft's Blocks.register and Items.register helps you do that.

public class ExampleMod implements ModInitializer {
  public static final Block EXAMPLE_BLOCK = register("example_block", Block::new, Block.Settings.create().strength(4.0f));
 
  private static Block register(String path, Function<AbstractBlock.Settings, Block> factory, AbstractBlock.Settings settings) {
    final Identifier identifier = Identifier.of("tutorial", path);
    final RegistryKey<Block> registryKey = RegistryKey.of(RegistryKeys.BLOCK, identifier);
 
    final Block block = Blocks.register(registryKey, factory, settings);
    Items.register(block);
    return block;
  }
}

In the code above, Blocks.register helps you to write the registry key into the AbstractBlock.Settings at first, and then create the block instance and register. Items.register directly creates a simple BlockItem instance, use the id same to the block, and then register it. If you need more complex operations, such as creating subclasses of BlockItem, you may call other methods with different signatures which are also named Items.register.

Giving your Block Visuals

At this point, your new block will appear as a purple and black checkerboard pattern in-game. This is Minecraft's way of showing you that something went wrong while loading the block's assets (or visuals). A full list of issues will be printed to your log when you run your client. You will need these files to give your block visuals:

The files should be located here:

The blockstate file determines which model a block should use depending on its blockstate. Our block doesn't have any potential states, so we cover everything with “”.

src/main/resources/assets/tutorial/blockstates/example_block.json
{
  "variants": {
    "": { "model": "tutorial:block/example_block" }
  }
}

The block model file defines the shape and texture of your block. Our model will have block/cube_allas a parent, which applies the texture all to all sides of the block.

src/main/resources/assets/tutorial/models/block/example_block.json
{
  "parent": "block/cube_all",
  "textures": {
    "all": "tutorial:block/example_block"
  }
}

In most cases, you will want the block to look the same in item form. You can make an item model that has the block model file as a parent, which makes it appear exactly like the block:

src/main/resources/assets/tutorial/models/item/example_block.json
{
  "parent": "tutorial:block/example_block"
}

Load up Minecraft and your block should have visuals!

Configuring Block Drops

To make your block drop items when broken, you will need a loot table. The following file will cause your block to drop its respective item form when broken:

For versions since 1.21, the path is src/main/resources/data/tutorial/loot_table/blocks/example_block.json. Before version 1.21, the path was src/main/resources/data/tutorial/loot_tables/blocks/example_block.json.

src/main/resources/data/tutorial/loot_table/blocks/example_block.json
{
  "type": "minecraft:block",
  "pools": [
    {
      "rolls": 1,
      "entries": [
        {
          "type": "minecraft:item",
          "name": "tutorial:example_block"
        }
      ],
      "conditions": [
        {
          "condition": "minecraft:survives_explosion"
        }
      ]
    }
  ]
}

The condition minecraft:survives_explosion means that, if the block is destroyed in an explosion with decay (such as creeper explosion, not TNT explosion), it may not drop. If there is no this condition, the block always drops in explosions with decay.

In minecraft 1.17, there has been a change for breaking blocks. Now, to define harvest tools and harvest levels, we need to use tags. Read about tags at: Tags Tutorial. The tags that we need to add the block to are:

* Harvest tool: src/main/resources/data/minecraft/tags/block/mineable/<tooltype>.json, where <tooltype> can be any of: axe, pickaxe, shovel or hoe (replace “block” with “blocks” for versions below 1.21) * Harvest level: src/main/resources/data/minecraft/tags/block/needs_<tier>_tool.json, where <tier> can be any of: stone, iron or diamond (not including netherite) (replace “block” with “blocks” for versions below 1.21)

src/main/resources/data/minecraft/tags/block/mineable/pickaxe.json
{
  "replace": false,
  "values": [
    "tutorial:example_block"
  ]
}
src/main/resources/data/minecraft/tags/block/needs_stone_tool.json
{
  "replace": false,
  "values": [
    "tutorial:example_block"
  ]
}

For the harvest level tags (needs_stone_tool, needs_iron_tool and needs_diamond_tool) to take effect, add requiresTool() to the Block.Settings in the block declaration (example of versions above 1.20.5):

  1. public static final Block EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.create().strength(4.0f).requiresTool());

Creating a Custom Block Class

The above approach works well for simple blocks but falls short when you want a block with unique mechanics. We'll create a separate class that extends Block to do this. The class needs a constructor that takes in an AbstractBlock.Settings argument:

ExampleBlock.java
  1. public class ExampleBlock extends Block {
  2. public ExampleBlock(Settings settings) {
  3. super(settings);
  4. }
  5. }

You can override methods in the block class for custom functionality. Here's an implementation of the onUse method, which is called when you right-click the block. We check if the interaction is occurring on the server, and then send the player a message saying, “Hello, world!”

ExampleBlock.java
  1. public class ExampleBlock extends Block {
  2.  
  3. public ExampleBlock(Settings settings) {
  4. super(settings);
  5. }
  6.  
  7. // For versions below 1.20.5, the parameters should be "BlockState state, World world, BlockPos pos, PlayerEntity player, Hand hand, BlockHitResult hit"
  8. @Override
  9. public ActionResult onUse(BlockState state, World world, BlockPos pos, PlayerEntity player, BlockHitResult hit) {
  10. if (!world.isClient) {
  11. player.sendMessage(Text.literal("Hello, world!"), false);
  12. }
  13.  
  14. return ActionResult.SUCCESS;
  15. }
  16. }

To use your custom block class, replace new Block with new ExampleBlock:

TutorialBlocks.java
  1. public final class TutorialBlocks {
  2.  
  3. public static final Block EXAMPLE_BLOCK = register("example_block", new ExampleBlock(Block.Settings.create().strength(4.0f)));
  4.  
  5. // ...
  6. }

Custom Shape

When using block models that do not entirely fill the block (eg. Anvil, Slab, Stairs), while its voxel shape is full, the hidden faces of the adjacent blocks will be exposed:

To fix this, we have to define the VoxelShape of the new block:

ExampleBlock.java
public class ExampleBlock extends Block {
    [...]
    @Override
    public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, ShapeContext context) {
        return VoxelShapes.cuboid(0f, 0f, 0f, 1f, 1.0f, 0.5f);
    }
}

You can also define other types of shapes for the block. The type of shapes of blocks include:

Next Steps

Adding simple state to a block, like ints and booleans.

Giving blocks a block entity so they can have advanced state like inventories. Also needed for many things like GUI and custom block rendering.

To make your block flammable (that is, can be burned in fire), you may use FlammableBlockRegistry.