In this tutorial I will show you how to add your first basic block to Minecraft. A part of this code is done in the mod file. This is the file I will start with.
The first line you have to add for a block is this.
public static Block tutorialBlock;
You will also have to import net.minecraft.src.Block. Do this by pressing Ctrl, Shift, O or by hovering your mouse over the error under block and clicking the import net.minecraft.src.Block fix option.
This line has to be placed inside of the class so past the bracket below Tutorial. It also has te be above the @Init.
This line will add a Block variable called tutorialBlock to the mod file. It is public and static so that means that any class can acces it by using Tutorial.tutorialBlock. Right now tutorialBlock is set to nothing so that is what you will have to add next.
To set tutorialBlock to something you have to add this line.
tutorialBlock = new BlockTutorialBlock(500, Material.rock).setUnlocalizedName(“tutorialBlock”);
This line has to be inside of the load method.
What this does is it sets tutorialBlock to BlockTutorialBlock which is a new file that we are going to create in the next part of this tutorial. The number inside the brackets is the id of the block.
The second parameter is the Material for the block. You can see other materials by going to the Material file. Materials set some important settings for your block like which tools are required etc.
.setUnlocalizedName() sets the name of the block. It is smart to keep the name exactly the same as the block variable name. It will not crash if you don’t, but it might give you some strange errors if you don’t.
Right know you will have an error under BlockTutorialBlock. This is because you are calling a file that isn’t there. We will be creating this file in the next part of this tutorial.
You might be thinking why I didn’t just do both of those things on one line. This is because you can have configurable block id’s when you write it this way. Configurable block id’s will also be in a later tutorial.
There are 2 more lines that you will have to add. The first is to let Forge know that tutorialBlock is a Block. To do that you have to add this inside of the load method.
GameRegistry.registerBlock(tutorialBlock, modid + tutorialBlock.getUnlocalizedName().substring(5));
The first parameter in this method is the Block you are registering. The second parameter in here is an unique String for your block. The easiest thing to do is use the modid variable in combination with the unlocalized name for the block. you need to use getUnlocalizedName().substring(5) because minecraft adds 5 characters in front of the block name which you don’t want.
The last line you have to add is this.
LanguageRegistry.addName(tutorialBlock, “Tutorial Block”);
This line adds the name behind the comma and inside of the quotation marks to the variable before the comma. This name will be displayed when you hover your mouse over the block in your inventory.
The whole file should now look like this.
This is everything you have to add to your mod file to make a good block. In the next tutorial I will show you how to create the block file.
The tutorial source code will be available at the last part of the basic block tutorials.