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 an Item, register it and give it a texture/textures. 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 name of the block, in the form of an string, it should be of the format modid:name where modid is the id of the mod and name is the block's name. Finally, the third parameter takes in the block that you want to register. This has to be done in the onInitialize method.
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 can now used ingame by using /setblock ~ ~ ~ modid:name.
Registering a BlockItem
In most cases you want the block to accessable in a creative tab and as an item. To do that use Registry.register again but this time with Registry.ITEM instead of Registry.BLOCK. The name should in most cases be the same as the block, the third parameter should instead of an block instance be a BlockItem which can be created with new BlockItem(block) Like blocks, this should be done in onInitialize.
public class ExampleMod implements ModInitializer { // block creation […] @Override public void onInitialize() { Registry.register(Registry.ITEM, "my-mod:example_block", new BlockItem(EXAMPLE_BLOCK); } }
Giving your block a texture
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 hasen't got a texture. Texturing a block is more advanced then texturing a item. You will need three files. A blockstate file, a block model file and a item model file (if the block has a blockitem). A texture file is also required if you don't use vanilla ones. The files should be located here:
Blockstate: .../resources/assets/my-mod/blockstates/example_block.json Block model: .../resources/assets/my-mod/models/block/example_block.json Item model: .../resources/assets/my-mod/models/item/example_block.json Block texture: .../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 only has one blockstate, the file is a simple as this:
{ "variants": { "": { "model": "my-mod:block/example_block" } } }
The block model file changing the shape and texture of your block. In this case it will be a cube.
{ "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 copies the properties 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 costructor 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.TRANSPARENT; }
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.