User Tools

Site Tools


tutorial:blocks

This is an old revision of the document!


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 Registry.BLOCK. 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

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.

  1. public class ExampleMod implements ModInitializer {
  2.  
  3. /* Declare and initialize our custom block instance.
  4.   We set out block material to METAL, which requires a pickaxe to efficiently break.
  5.   Hardness represents how long the break takes to break. Stone has a hardness of 1.5f, while Obsidian has a hardness of 50.0f.
  6.   */
  7. public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).hardness(4.0f));
  8.  
  9. @Override
  10. public void onInitialize() {
  11.  
  12. }
  13. }

Registering your Block

Blocks should be registered under the Block.REGISTRY registry. Call Registry.register and pass in the appropriate arguments.

  1. public class ExampleMod implements ModInitializer {
  2.  
  3. /* Declare and initialize our custom block instance.
  4.   We set out block material to METAL, which requires a pickaxe to efficiently break.
  5.   Hardness represents how long the break takes to break. Stone has a hardness of 1.5f, while Obsidian has a hardness of 50.0f.
  6.   */
  7. public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL).hardness(4.0f));
  8.  
  9. @Override
  10. public void onInitialize() {
  11. Registry.register(Registry.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);
  12. }
  13. }

Your custom block will not be accessible as an item yet, but it can be seen in-game by using the command /setblock 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 Registry.ITEM. The registry name of the item should usually be the same as the registry name of the block.

  1. public class ExampleMod implements ModInitializer {
  2.  
  3. // an instance of our new block
  4. public static final Block EXAMPLE_BLOCK = new Block(FabricBlockSettings.of(Material.METAL));
  5.  
  6. @Override
  7. public void onInitialize() {
  8. Registry.register(Registry.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);
  9. Registry.register(Registry.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings().group(ItemGroup.MISC)));
  10. }
  11. }

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:

  • A blockstate file
  • A block model file
  • A texture
  • An item model file (if the block has an item associated with it).

The files should be located here:

Blockstate: src/main/resources/assets/tutorial/blockstates/example_block.json
Block Model: src/main/resources/assets/tutorial/models/block/example_block.json
Item Model: src/main/resources/assets/tutorial/models/item/example_block.json
Block Texture: src/main/resources/assets/tutorial/textures/block/example_block.png

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 parent block/cube_all, which applies the texture set as 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 parents the block model file, 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:

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

Creating a Custom Block Class

The above approach works well for simple items 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:

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

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!”

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

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

  1. public class ExampleMod implements ModInitializer {
  2.  
  3. /* Declare and initialize our custom block instance using our new ExampleBlock class.
  4.   We set out block material to METAL, which requires a pickaxe to efficiently break.
  5.   Hardness represents how long the break takes to break. Stone has a hardness of 1.5f, while Obsidian has a hardness of 50.0f.
  6.   */
  7. public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.of(Material.STONE).hardness(4.0f));
  8.  
  9. @Override
  10. public void onInitialize() {
  11. Registry.register(Registry.BLOCK, new Identifier("tutorial", "example_block"), EXAMPLE_BLOCK);
  12. Registry.register(Registry.ITEM, new Identifier("tutorial", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings().group(ItemGroup.MISC)));
  13. }
  14. }

Custom VoxelShape

When using block models that do not entirely fill the block (eg. Anvil, Slab, Stairs), adjacent blocks hide their faces:

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

@Override
 public VoxelShape getOutlineShape(BlockState state, BlockView view, BlockPos pos, EntityContext ctx) {
     return VoxelShapes.cuboid(0f, 0f, 0f, 1f, 1.0f, 0.5f);
 }

Note that the collision shape of the block defaults to the outline shape if it is not specified.

Next Steps

tutorial/blocks.1592096313.txt.gz · Last modified: 2020/06/14 00:58 by draylar