This is an old revision of the document!
Table of Contents
Adding a Block
Introduction
Like items, new blocks are added by most mods. You'll need to create a Block, register it and create a model for it. To add additional behavior to the block you will need a Block class.
Creating a Block
The first step is to create a Block. This can be done by using the Block.Settings's method of which takes in a material. The Block's Block.Settings define many of the Block's characteristics like hardness and blast resistance. This will create a basic block which won't always be enough, but that will be covered further down.
public class ExampleMod implements ModInitializer { // an instance of our new block public static final Block EXAMPLE_BLOCK = new Block(Block.Settings.of(Material.STONE)); [...] }
Registering a Block
As blocks exist in vanilla, they are registered with the vanilla registry that can be gotten by using Registry.register. The first parameter defines what should be registered, so for blocks its Registry.BLOCK. The second parameter is the registry name of the block in the form of an identifier. The identifier will consist of the id of the mod and the name of the block. Finally, the third parameter takes in the block that you want to register. We will use the initializer's onInitialize method to register the block.
public class ExampleMod implements ModInitializer { // block creation […] @Override public void onInitialize() { Registry.register(Registry.BLOCK, new Identifier("my-mod", "example_block"), EXAMPLE_BLOCK); } }
Your block will currently not be accessible as an item, but it can now be seen ingame by using /setblock ~ ~ ~ modid:name.
Registering a BlockItem
In most cases you want the block to accessible in a creative tab and as an item. To make the block available as an item, you need to register a corresponding BlockItem in the item registry. The registry name of the item should usually be the same as the registry name of the block. A BlockItem can be created with new BlockItem(block). Like registering the block, we will register the BlockItem in the initializer's onInitialize method.
public class ExampleMod implements ModInitializer { // block creation […] @Override public void onInitialize() { // block registration [...] Registry.register(Registry.ITEM, new Identifier("my-mod", "example_block"), new BlockItem(EXAMPLE_BLOCK, new Item.Settings().itemGroup(ItemGroup.MISC))); } }
Giving your block a model
As you probably have noticed, the block both in world and in item form is a purple and black checkerboard pattern. This is Minecraft's way to show that the block has no model. Modeling a block can be more advanced than modeling an item. You will need three files: A blockstate file, a block model file and a item model file (if the block has a BlockItem). Textures are also required if you don't use vanilla ones. The files should be located here:
Blockstate: src/main/resources/assets/my-mod/blockstates/example_block.json Block model: src/main/resources/assets/my-mod/models/block/example_block.json Item model: src/main/resources/assets/my-mod/models/item/example_block.json Block texture: src/main/resources/assets/my-mod/textures/block/example_block.png
The blockstate file determines which model that the block should use depending on it's blockstate. As our block has only one state, the file is a simple as this:
{ "variants": { "": { "model": "my-mod:block/example_block" } } }
The block model file defines the shape and texture of your block. We will use block/cube_all, which will allow us to easily set the same texture on all sides of the block.
{ "parent": "block/cube_all", "textures": { "all": "my-mod:block/example_block" } }
In most cases you want the block to look the same in hand as in the world and we can therefore create a item model file that simply inherits from the block model file.
{ "parent": "my-mod:block/example_block" }
Load up Minecraft and your block should finally have a texture!
Creating a Block class
When creating a simple decorational block the above approch works well, but sometimes you want to create something more advanced and will therefore need a custom Block class. We will call it ExampleBlock. The class needs a constructor that takes in a BlockSettings argument.
public class ExampleBlock extends Block { public ExampleBlock(Settings settings) { super(settings); } }
Here in the your custom block class you can customize your blocks in many ways, for example make you block function like a door or be transparent. That is what we will do now.
@Environment(EnvType.CLIENT) public BlockRenderLayer getRenderLayer() { return BlockRenderLayer.TRANSLUCENT; }
To add this block into the game you have to replace new Block with new ExampleBlock when you create the block.
public class ExampleMod implements ModInitializer { // an instance of our new item public static final ExampleBlock EXAMPLE_BLOCK = new ExampleBlock(Block.Settings.of(Material.STONE)); [...] }
Your custom block should now be transparent if you have followed the tutorial correctly and use a transparent texture.