Adding Blocks in 1.7.10

In this tutorial I will add a red diamond block, that does nothing, and has the same texture all over the six sides. Easy enough? Let’s start.

Like adding items, it is recommended to make a new class solely for your blocks. We’ll put it in the com.emx.tutorial.init package, like before. Immediately, I am going to add the block itself to the class.

public class ModBlocks
{
    public static Block red_diamond_block;

    public static void init()
    {
        red_diamond_block = new BlockRedDiamond();
    }
}

Using the above code, you will get an error. That’s because we do not have the class for the block. So, let’s make that.

public class BlockRedDiamond extends Block
{
    private static final String name = "red_diamond_block";

    public BlockRedDiamond()
    {
        super(Material.rock);
        GameRegistry.registerBlock(this, name);
        setBlockName(name);
        setBlockTextureName(TutorialMod.MODID + ":" + name);
        setCreativeTab(CreativeTabs.tabBlock);
    }
}

There are a few methods here that is needed:

  • setBlockName() :: Equivalent to setUnlocalizedName() for items, two blocks cannot have the same name.
  • setBlockTextureName() :: Self explanatory. “tutorial:red_diamond_block” points to the file “red_diamond_block.png” in “assets.tutorial.textures.blocks”.
  • setCreativeTab() :: Same as items, sets where you want this block to be available in the creative menu. Not needed if you don’t want it in the creative menu.
private static final String name = "red_diamond_block";

As you can see from the above piece of code, I use a field instead of saying the name one by one. This is easier to code and easier to change if you need it later on.

GameRegistry.registerBlock(this, name);

From this line you can see that we need to register the block too, but instead, it is neater to do it in the actual block class. But you can put it in the ModBlocks class if you prefer.

Now, you can go ahead and put the textures into “assets.tutorial.textures.blocks”, making sure it’s named “red_diamond_block.png”. If you want to use my texture, it’s this:

After that, you can add your block to the language file, so once again it can be translated to real human language. Open your en_US.lang file, and in a new line, type in:

tile.red_diamond_block.name=Red Diamond Block

Unlike items that use “item”, blocks use “tile”.

Finally, make a call to the ModBlocks class, if you haven’t so your blocks get initialized. Add the following to your main mod class:

Modblocks.init();

Your mod class should now look like this:

@Mod(modid = TutorialMod.MODID, name = TutorialMod.NAME, version = TutorialMod.VERSION)
public class TutorialMod
{
    public static final String NAME = "Tutorial Mod";
    public static final String MODID = "tutorial";
    public static final String VERSION = "1.7.10-R1";

    @Mod.Instance("tutorial")
    public static TutorialMod instance;

    @Mod.EventHandler
    public void preInit(FMLPreInitializationEvent event)
    {
        ModItems.init();
        ModBlocks.init();
    }

    @Mod.EventHandler
    public void init(FMLInitializationEvent event) {}

    @Mod.EventHandler
    public void postInit(FMLPostInitializationEvent event) {}
}

And you’re done! You made your first block, congratulations! The next tutorial will be on how to add crafting recipes.

2 thoughts on “Adding Blocks in 1.7.10

  1. ANONYMOUS February 20, 2016 / 12:21 AM

    Why can’t I leave comments on the 1.8.9 tutorial?

    Like

Leave a comment