Tutorial 3. Backbone Modblock

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

Code is on github

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)

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.

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

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.

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

  • Blocks: [modid]:[blockname]
  • Items: [modid]:[itemname]

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

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:

This will return ONLY the blockname.


so now we can finish the getUnlocalizedName.

 Cool, so we can say we finished names. Now lets do textures.

The method to regsiter textures, is registerBlockIcon, so lets override that

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

Done.

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


Back to List
Next tutorial


2 Responses to Tutorial 3. Backbone Modblock

  1. 1arthurf says:

    I do not know why but my block is coming up in the game as:
    MODID. NAME I GAVE.name
    Do you have any suggestions to why this is happening.

    • martijnwoudstra says:

      Because your language file isnt setup then/doesnt contain the good name.
      In the en_US.lang file, type a line.
      That line looks as
      NAME_YOU_SEE_INGAME = NAME_YOU_WANT

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=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">