Today we are going to make a custom block, I am going to show you how to register your blocks, register block models and add ItemBlocks.
First, we need to create a class where we are going to register all of our blocks. So inside the init package create a class ModBlocks.
The block we will be creating will be the most basic block possible, it won’t have any special effects.
In the ModBlocks class, we will be instantiating and registering our objects.
Now we need to let forge know we want to register a block, this is done in a RegistryEvent just like for items.
Add a public static method called registerBlocks it has to have an argument of type RegistryEvent.Register<Block> (this is how forge knows to call this method when the event is fired) I’ll refer to this argument as event but a better more descriptive name could be blockRegistryEvent. Above the method add an annotation @SubscribeEvent and above the class, declaration add an annotation @Mod.EventBusSubscriber and set the modid argument to your modid and the bus value to Bus.MOD (In pre 1.13 versions you didn’t need to set the bus, but this changed in 1.13).
- 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 create an instance by calling Block.Properties::create, and you can call methods on this instance to set things like the hardness, light value, etc.
- Now you also need to set the registry name on for your item, you do this by calling setRegistryName on the block instance and passing in your modid for the first argument, and the item name as the second argument, the name has to be all lowercase and can only contain the following characters: a-z 0-9 / . _ –
- We need to access the block instance later again to create an ItemBlock. So 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 item’s registry name (Because it is final you need to set it to null, but forge will set it to your item’s instance). It’s better to use ObjectHolder because people could possibly replace your item, and now you’ll automatically use that replacement.
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.
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!
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!