Tutorial 3. Backbone Modblock

Before you read, make sure you have seen the previous tutorial

Code is on github




Highly recommended to look on github for source

In this tutorial, we are gonna make a backbone for our blocks.
This means, that we will make a block, which holds all values, that are the same for every block in our mod. This is very useful, because adding blocks later, will be ten times easier. In the next tutorial, we will create a real block.

So, create a new package, called blocks. Create a new class inside that package. I recommend to call it: Block+[ModID]
Now, to get all the methods that normal blocks have, we extend Block (net.minecraft.block.Block)

<br /><br />package mcmodtutorial.blocks;<br /><br />import net.minecraft.block.Block;<br /><br />public class BlockMcModTutorial extends Block<br />{<br />}<br />

You will get an error right away. Why? Because Block needs to get a parameter from our block. That parameter is a material.
So, we create a constructor, and pass it thru, whatever material we get from the blocks that will extend this block.

<br /><br />package mcmodtutorial.blocks;<br /><br />import net.minecraft.block.Block;<br /><br />public class BlockMcModTutorial extends Block<br />{<br /><br />public BlockMcModTutorial(Material material)<br />{<br />super(material);<br />}<br /><br />}<br />

Now, lets add a default material, so we dont have to super it everything, that we want a material.

<br /><br />package mcmodtutorial.blocks;<br /><br />import net.minecraft.block.Block;<br /><br />public class BlockMcModTutorial extends Block<br />{<br /><br />public BlockMcModTutorial()<br />{<br />super(Material.rock);<br />}<br /><br />public BlockMcModTutorial(Material material)<br />{<br />super(material);<br />}<br /><br />}<br />

So, now we have a class, which does absolutly nothing, except for passing through material. Lets add somethign to do.
There are 2 things i like to do for our global blocks.
– Names
– Textures

So, forge gets the block name, by calling getUnlocalizedName. We can change what forge gets from our blocks, by overriding that method.

<br /><br />@Override<br /> public String getUnlocalizedName()<br /> {<br /> }

Now, what do we want the name to be? Forge want to have the following format:

  • Blocks: tile.[modid]:[blockname]
  • Items: item.[modid]:[itemname]

Since we only work with blocks, lets make that.
Lets first work on the tile.[modid]: part.
we want to return “tile.” + “[modid]:”
Its the easiest to create a reference to the resource prefix (modid:) in our references.

    public static final String RESOURCESPREFIX     = MODID.toLowerCase() + ":";

Now we need to work on the itemname. By default, forge returns the unlocalized name with a prefix. We need to get rid of that.
So in our block class, we create this:

<br />private String getUnwrappedUnlocalizedName(String unlocalizedName)<br /><br /> return unlocalizedName.substring(unlocalizedName.indexOf(".") + 1);<br /> }

This will return ONLY the blockname.

so now we can finish the getUnlocalizedName.

<br /><br />@Override<br /> public String getUnlocalizedName()<br /> {<br /> return String.format("tile.%s%s", References.RESOURCESPREFIX, getUnwrappedUnlocalizedName(super.getUnlocalizedName()));<br /> }

 Cool, so we can say we finished names. Now lets do textures.
The method to regsiter textures, is registerBlockIcon, so lets override that

<br /><br />@Override<br /> @SideOnly(Side.CLIENT)<br /> public void registerBlockIcons(IIconRegister iconRegister)<br /> {<br /><br /> }

Now, in order for this to work, we need to give a value to this.blockIcon.
That value can be accessed through iconRegister.registerIcon([modid]:[unwrappedname]);
So, lets just call that in here

<br /><br />@Override<br />@SideOnly(Side.CLIENT)<br />public void registerBlockIcons(IIconRegister iconRegister)<br />{<br />this.blockIcon = iconRegister.registerIcon(References.RESOURCESPREFIX + getUnwrappedUnlocalizedName(super.getUnlocalizedName()));<br />}


So, now every block which extends this class, automaticly has the right name and texture!

Back to List
Next tutorial


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s