Tutorial 4. Block

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

Code is on github

FINALLY! We’re gonna add our OWN block!

Lets start right away :3

In your main class, in the preInit, call ModBlocks.init();

You’ll get an error, because ModBlocks.class doesnt excist yet.

So, create that class inside the blocks package.
Now add the init method.

Now, add a Block object, with a conveiniant blockname.
Also add a cast to a new Class, and create that class.

Now we can add the constructor. This is the fun part. Here you give value’s to your block.
A few examples are

  • setHardness(float) [How long it takes to mine your block]
  • setResistance(float) [How well your block can resist blasts]
  • setCreativeTab(CreativeTab.tab) [What creative tab your block is in]
  • setStepSound(Block.stepsound) [Stepsound]
  • setUnlocalizedName(String) [REQUIRED, the incode blockname (not ingame)]

I have this

Two things to notice, is that i have one more thing than i told about.


ModBlocks.register(this);
That is, because all blocks you add, need to be registered.
It will throw an error for now, but lets fix that.
The proper way to register is to call GameRegistry.registerBlock(block, name).
Therefor, lets add that in ModBlocks.

As I showed in TestBlock, we call register and pass it this (a block)
We take that block, and give it to registerBlock, together with its unlocalized name.

And the other thing I didnt tell about, is the this.setBlockName(Strings.TestBlockName);
This is, because i create a new class Strings in the lib package, where i stored my blockname.

Done, your block is in. Run the game.


Back to List
Next tutorial


2 Responses to Tutorial 4. Block

  1. Sven says:

    Nice tutorial, it’s really helping me get started!
    Only shouldn’t the “Block” parameter in
    public static void register(Block block)
    be “BlockMcModTutorial”? Otherwise you can’t get the getUnwrappedUnlocalizedName() method.

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