Today I’m going to show you how to make a custom block, I am going to show you how to register your blocks, register block models, loot tables and add ItemBlocks.
For a block to work, have a texture, drop something and have a name the following things need to be done:
- The block needs to registered
- The block needs a blockstate json (this to map different variants, like rotation, to a model)
- The block needs at least 1 model json (can be more if you have multiple variants)
- The block needs at least 1 texture
- The block needs to have a loot table json (to make the block drop something)
- The block needs to have an entry in the lang file
Blocks are registered in the RegistryEvent.Register<Block> event (This event is fired on the MOD bus!), so I’ll add an event handler called registerBlocks. It doesn’t matter where you do this but most often this is done in a ModBlocks class, to seperate it from items. To match the structure of the tutorial example that class should be in the init package.
- In the registerBlocks method, you can register the block by calling event.registerAll and passing in a new instance of the Block class.
To create an instance of the Block class you need to pass in a Block.Properties instance. You can call methods on this instance to set things like the harvestLevel, hardness, etc. To create a Block.Properties instance you need to call Block.Properties#create and pass in a Material.
- You also need to set the registryname for your block, you do this by calling setRegistryName on the block instance and passing in your modid for the first argument, and the block name as the second argument, the name has to be all lowercase and can only contain the following characters: a-z 0-9 / . _ –
- You may want to access the block instance later again (especially if you want a block item to place it). Add the @ObjectHolder annotation above the class and pass in your modid. Now create a public static final Block variable, the name has to exactly match your block’s registry name (Because it is final you need to set it to null, but forge will set it to your block’s instance).
Now, let’s add the ItemBlock, you do this by instead of instantiating an Item you instantiate the ItemBlock, in here you need to pass in a reference to your block and the Item.Properties you want. (Don’t forget to set the registry name). For more information go to my previous tutorial about items. To get the reference to your block you should use the @ObjectHolder annotation as explained before.
In the assets/modid/ folder create a new folder called blockstates, in the model and texture folder you also need to create a block folder.
In the textures/block folder, you can add a texture (make sure its size is a power of 2, 16×16, 32×32, etc.). It doesn’t matter what you call it but it has to be lowercase and you have to remember the name. I will call it tutorial_block.png and I will use this texture:
In the models/block folder create a new JSON file, and add the following JSON:
Of course, you need to replace the words in caps with whatever your values are. The texture name should be without the extension. Save this model, and remember the name (make sure the name is lowercase)
In the blockstates folder create a new JSON file. In the JSON file add the following JSON code:
Remember to save it as NAME.json. You have to replace the words in capslock with the corresponding values, NAME should be the same as the block’s registry name.
Your item also needs a specific model so it looks like a block, you do this by creating a file in the models/item and adding the following JSON:
Make sure the file name matches the registry name!
To make your block drop something you have to add a loot table, to do this create a JSON file named REGISTRY_NAME.json in the resources/data/MODID/loot_tables/blocks folder.
Make sure each pool has a unique name or it won’t work! For more information about loot tables check the wiki.
You may have noticed that the name isn’t displaying correctly. In the lang file (assets/modid/lang/en_us.json) add a new entry, like this:
"block.modid.registryname": "Block Name"
Now you can start the game and you should have a block with texture and name, and an item to place it!
Well, that was it for this part!
As always if you have any question don’t hesitate to ask in the comments! Also if you saw any errors in the tutorial please let me know so I can fix it!