Today I’m going to show you how to make a custom block using the new DeferredRegister
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 almost the same way as items. I’ll put everything block related in a ModBlocks class but you can do it wherever you want.
Create a public static final DeferredRegister
public static final DeferredRegister<Block> BLOCKS = new DeferredRegister<>(ForgeRegistries.BLOCKS, TutorialMod.MOD_ID);
Now to register a block to this DeferredRegister all you need to do is call
BLOCKS.register(registryname, () -> blockinstance);. The registryname has to be all lowercase and can only contain the following characters: a-z 0-9 / . _ –.
Now create a public static final RegistryObject
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.
public static final RegistryObject<Block> TUTORIAL_BLOCK = BLOCKS.register("tutorial_block", () -> new Block(Block.Properties.create(Material.ROCK)));
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. For more information go to my previous tutorial about items. To get the reference to your block you should call ModBlocks.TUTORIAL_BLOCK.get().
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! Click here to browse the project for this tutorial!
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!