Wuppy’s Minecraft Forge Modding Tutorials for 1.6.2: Basic Block Part 1

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.


package tutorial;

import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
@Mod(modid = Tutorial.modid, name = “Mod Name”, version = “1.0”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial
{
       public static final String modid = “YourName_ModName”;
      
       @Eventhandler
       public void load(FMLInitializationEvent event)
       {
        
       }
}

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(tutorialBlockmodid + 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.


package tutorial;

import net.minecraft.block.Block;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;
@Mod(modid = Tutorial.modid, name = “Mod Name”, version = “1.0”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial
{
       public static final String modid = “YourName_ModName”;
      
       public static Block tutorialBlock;
      
       @EventHandler
       public void load(FMLInitializationEvent event)
       {
             tutorialBlock = new BlockTutorialBlock(500, Material.rock).setUnlocalizedName(“tutorialBlock”);
            
             GameRegistry.registerBlock(tutorialBlockmodid + tutorialBlock.getUnlocalizedName().substring(5));
            
             LanguageRegistry.addName(tutorialBlock“Tutorial Block”);
       }
}

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">