Tutorial 4. Block

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

Code is on github

WEBSITE IS DEPRICATED

VISIT THE NEW WEBSITE

 

Highly recommended to look on github for source

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.


package mcmodtutorial.blocks;

public class ModBlocks
{

public static void init()
 {
 }

}

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


package mcmodtutorial.blocks;
import net.minecraft.block.Block;

public class ModBlocks
{
public static Block testBlock;

public static void init()
{
testBlock = new TestBlock();
}

}

package mcmodtutorial.blocks;

public class TestBlock extends BlockMcModTutorial
{
}

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


package mcmodtutorial.blocks;

import mcmodtutorial.lib.Strings;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;

public class TestBlock extends BlockMcModTutorial
{
 public TestBlock()
 {
 this.setBlockName(Strings.TestBlockName);
 this.setHardness(1f);
 this.setResistance(3f);
 this.setCreativeTab(CreativeTabs.tabBlock);
 this.setStepSound(Block.soundTypeAnvil);
ModBlocks.register(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.


public static void register(Block block)
 {
GameRegistry.registerBlock(block, block.getUnwrappedUnlocalizedName(block.getUnlocalizedName()));
 }

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.


package mcmodtutorial.lib;

public class Strings
{
 public static final String TestBlockName = "testBlock";
}

Done, your block is in. Run the game.

Back to List
Next tutorial

Advertisements

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s