Today we are going to make a custom block and an item for the block, an ItemBlock. This tutorial will only cover the most basic block, just a solid cube with a texture. In some of the next tutorials we will go more in depth in making a block.
Just like with the item we are going to create some files to keep some structure in our mod.
- Create a package in the mod package and call it blocks. This package will contain all of the custom block classes.
- Inside the blocks package create a class called BlockBasic. This will contain all of our custom logic, for this block it won’t be much.
- Inside the init package create a class called ModBlocks. Here we will instantiate all of our blocks.
If you have been following the other tutorials your package structure should look a little like this:
Open the BlockBasic class and make it extend Block (Make sure you import “net.minecraft.block.Block”). It will now give an error because it needs a constructor. Add a constructor with as arguments a String, for the name, and a Material (Make sure you call super(material) in the constructor). Then just as in the ItemBasic class, call the methods setUnlocalizedName and setRegistryName.
Now open the ModBlocks class. This class is almost the same as the ModItems class, except it will register a Block and an ItemBlock. When you register a Block you just register the placed version of a Block, if you want an Item for the block you have to also register an ItemBlock. Some blocks don’t have an Item version (like the end portal frame), but most blocks have (like the dirt block).
In the ModBlocks class add a static Block variable, I will call it tutorialBlock. Now we need to add 5 public static void methods.
- The first method that will be called is init this will initialize our block variable, it doesn’t take any arguments.
- Then we add registerBlocks, this needs an argument of type RegistryEvent.Register<Block>, it also needs an @SubscribeEvent annotation. This method will be called when forge registers all of the blocks.
- The third method will be registerItemBlocks, this also needs an argument but now of type (RegistryEvent.Register<Item>. And it also needs an @SubscribeEvent. Here we will register our ItemBlocks.
- Just like in the ModItems class we need to register our ItemBlock models. So add a method registerRenders, this needs an argument of type ModelRegistryEvent and also an @SubscribeEvent.
- The last method will be registerRender this is exactly the same as in the ModItems class, it needs an Item as argument.
We also have to add @Mod.EventBusSubscriber(modid=Reference.MODID) above the class declaration.
The class should now look a little like this:
In the init function initialize the tutorialBlock variable. I will name it “tutorial_block” and for the material I will pass it Material.ROCK (You can see all of the materials in net.minecraft.block.material).
In the registerBlocks function get the Registry by calling getRegistry on the event and call registerAll on the registry.
The registerItemBlocks function is almost the same, except you don’t pass it the tutorialBlock but you instantiate a new ItemBlock and pass it the tutorialBlock. You also have to call setRegistryName on the ItemBlock.
In the registerRenders function call registerRender and pass it the ItemBlock, you can get the ItemBlock by calling the static method Item.getItemFromBlock and passing it the tutorialBlock.
And the registerRender function is the same as in the ModItems class.
Now we just have to call ModBlocks.init() in the main mod class’ preInit method.
We now have a working block, you can run the game and use the command “/give @p modid:itemname” to get the item or place the block by using “/setblock ~ ~ ~ modid:blockname”. But it looks a little strange, that is because we haven’t added the models and textures yet, so lets do that now.
In the assets.modid.models package create a new package named block. In this package add a new text file, give the text file the following content you can name it whatever you want but it has to be lowercase:
Replace the words in caps with your values.
In the assets.modid package create a new package called blockstates. In this package create a text file and give it the following content, again save it as blockname.json:
Replace the words in caps with your values.
Now in the assets.modid.textures package create a new package called blocks, here you can put your texture, it has to be a power of 2 (16×16,32×32, etc.) and a .png. I will use this texture:
This is enough for the block itself to get a texture, but the ItemBlock doesn’t have a texture yet. We will fix that now:
In the assets.modid.models.item folder create a new text file. Give it the following content and name it blockname.json:
Now we just have to fix the name. Go to your lang file (assets.modid.lang) and add the following line:
You should now have a working AND good looking block!
To change some of the behaviour of the block you can do the same as with the item, call a set method while instantiating the block. To change the time it takes to mine call setHardness and pass it a float, I will pass it 1.5f just like the vanilla stone block. The higher the value the longer it takes to mine.
It will probably also be useful to put it in a creativetab, this is done exactly the same as with Items, just call the method setCreativeTab and pass it the tab you want. I will put it in CreativeTabs.BUILDING_BLOCKS.
I will also make it give of light, this is done by calling the method setLightLevel. This method asks for a float this has to be a value from 0 to 1. If you want it to give of a light level of 15 (Highest value) enter 15f/15f or 1.0f. For a value of i (Where 0≤i≤15) enter i/15f.
This is the final line:
tutorialBlock = new BlockBasic("tutorial_block", Material.ROCK).setHardness(1.5f).setCreativeTab(CreativeTabs.BUILDING_BLOCKS).setLightLevel(1.0f);
I also want to make it only mineable by a iron pickaxe or higher. Because I used Material.ROCK it is already only mineable by pickaxes, but to have it only mineable by iron or hight call setHarvestLevel. This method asks for 2 variables, 1 String toolClass and one int Level:
- toolClass can be:
- level can be:
- 0 for wood or gold
- 1 for stone
- 2 for iron
- 3 for diamond
This message can’t be used in chaining because it doesn’t return the Block instance we were working on. So you need to call this method separately.
Here are all of the classes we created this tutorial:
This way you can change some basic things of the block, you can click here to see all of the code from this part. In some of the next tutorials we will make some more advanced blocks.
But for now that was it.
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!