Modding 1.5.1: Basic Block Part 2

In this tutorial I will show you how to make the Block file that you will need to add a basic Block to minecraft. This is the code in the mod file from the last tutorial.


package tutorial;

import net.minecraft.block.Block;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
importcpw.mods.fml.common.event.FMLInitializationEvent;
importcpw.mods.fml.common.network.NetworkMod;
importcpw.mods.fml.common.registry.GameRegistry;
importcpw.mods.fml.common.registry.LanguageRegistry;
@Mod(modid = Tutorial.modid, name = “Mod Name”, version = “1.0”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
publicclass Tutorial
{
       publicstaticfinal String modid = “YourName_ModName”;
      
       publicstatic Block tutorialBlock;
      
       @Init
       publicvoid load(FMLInitializationEvent event)
       {
             tutorialBlock = new BlockTutorialBlock(500, Material.rock).setUnlocalizedName(“tutorialBlock”);
            
             GameRegistry.registerBlock(tutorialBlock, modid + tutorialBlock.getUnlocalizedName2());
            
             LanguageRegistry.addName(tutorialBlock, “Tutorial Block”);
       }
}

In this tutorial all you don’t have to do anything in the mod file. Just click on the package of this mod or hover your mouse over the BlockTutorialBlock error and create a class with that name.
When you create the file it will probably look like this.


package tutorial;

publicclass BlockTutorialBlock {
}

The first thing that you will have to do is make BlockTutorialBlock extend Block. Do this by adding this to the file.


package tutorial;

import net.minecraft.block.Block;
publicclass BlockTutorialBlock extends Block
{
}

When you do this you will get an error under BlockTutorialBlock. To fix this you will have to add this method.


public BlockTutorialBlock(int id, Material par2Material)

{
       super(id, par2Material);
}

You need to make sure that the method is public FileName and that it contains 1 integer and 1 Material inside of the brackets. You can change the names of those 2 variables into anything you want, but the names they have right now are their functions.
The line inside of the method with super tells the Block file what the id of the block is. It also tells Block which kind of material it is made out of. In this case the material is rock. Making it minable by certain items, but not with others. Material can also add some other interesting properties like making it impossible to be moved by a piston, but that doesn’t count for cloth. To find out all the materials go to the Material file.

Another line that you might want to add is on which creative tab in the creative menu it should be shown. To do this you have to add this line inside of the method.


this.setCreativeTab(CreativeTabs.tabBlock);


CreativeTabs is the file that contains all the tabs from the creative inventory and tabBlock is the one you start out in when you open up the inventory on creative mode.

The whole file should now look like this.


package tutorial;

import net.minecraft.block.Block;
importnet.minecraft.block.material.Material;
importnet.minecraft.creativetab.CreativeTabs;
publicclass BlockTutorialBlock extends Block
{
       public BlockTutorialBlock(int id, Material par2Material)
       {
             super(id, par2Material);
             this.setCreativeTab(CreativeTabs.tabBlock);
       }
}

You can download the source code over here.
In the next tutorials I will show you how to add some special settings to a block like being able to walk through it etc.

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="">