Basic Modding - First Block

Goal

I want to show you how to create a very basic block in your mod.

Difficulty

2/10 - Quite simple

Prerequisites

Forge Version

This Tutorial was created with Forge 10.13.0.1180 for Minecraft 1.7.10. If anything doesn't work with other versions, please contact me!


Creating a Block

To create a block we first should create a new class for the blocks. We create a package named com.yourname.yourmodname.block and create a class named ModBlocks inside it. In this tutorial I'll use com.bedrockminer.tutorial.block.ModBlocks.

 

Now we can create the init() method exactly like we did in the ModItems class. We also can create the block object. Your class should now look like this:

ModBlocks.java:
package com.bedrockminer.tutorial.block;

public final class ModBlocks {

    public static Block tutorialBlock;

    public static final void init() {

    }

}

For the Items we just used the constructor defined in Item.class. For Blocks, this is declared as protected, thus we have no access to it.

Therefore, we have to create another class which is a subclass of Block.class.

We create a new class and name it BasicBlock. Then we have to make Block a superclass of BasicBlock. To do this, we type "extends Block" behind the class name. We press Ctrl+Shift+O and choose net.minecraft.block.Block as the import source. The class should now look like this:

BasicBlock.java:
package com.bedrockminer.tutorial.block;

import net.minecraft.block.Block;

public class BasicBlock extends Block {

}

We see that there is an error in the class. We hover over the red marked class name and wait until a dropdown box opens. There we can see that the constructor of the class is not defined. We choose the quick fix "Generate Constructor 'BasicBlock(Material)' ". This will override the constructor of the Block class and add it to your BasicBlock class.

BasicBlock.java:
package com.bedrockminer.tutorial.block;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;

public class BasicBlock extends Block {

    protected BasicBlock(Material p_i45394_1_) {
        super(p_i45394_1_);
    }
}

If we want to we can rename the argument for the material by selecting it and pressing Alt+Shift+R. Then we type a new name and press Return. This will change the name of the argument and update all occurrences of it.

 

Now we can add some settings for the block.

There are much more possibilities for a block than for an item:

new Block(Material) constructor sets the material for the block which defines basic settings.  
.setBlockName required sets the name of the block.  
.setBlockTextureName highly recommended sets the name of the texture of the block.  default: missing texture
.setCreativeTab optional sets the creative tab to display the block in. default: none
.setHardness recommended sets how long it takes to break the block. default:depends on material; stone:1.5F; obsidian:50.0F
.setResistance recommended sets the block's resistance against explosions. default:depends on material; stone:10.0F; obsidian:2000.0F
.setStepSound recommended sets the step sound of a block. default:depends on material
.setLightOpacity optional sets how much light is subtracted when going through this block
This is only used if isOpaqueCube() returns false.
default: 16 (completely opaque); maximum: 0 (100% translucent)
.setLightLevel optional sets how much light is emitted from the block. default: 0.0F (nothing); maximum: 1.0F (full sunlight)
.setHarvestLevel highly recommended sets the tool and the tool level to break this block. If you don't use this, your block's destruction level is set by the material.

tool: "pickaxe", "axe", "shovel"

level: 0=wood; 1=stone; 2=iron; 3=diamond tool

.setBlockUnbreakable optional makes the block unbreakable in survival.

only Bedrock is unbreakable

.setTickRandomly optional if true the block receives random update ticks. Used for example for the decaying of leaves.

default: false

 

The block I'll create in this tutorial is a blue colored block of glowstone.

This is the texture for the block:

We place the texture file in the "src/resources" folder in this path: assets/modid/textures/blocks and again we press F5 with our Project selected in the Package Explorer to update. It should now look like this:

Picture - Show

Note for Eclipse users: If the Package Explorer shows every package with its full name, even if one package contains another, try to click the small arrow pointing downwards at the top of the Package Explorer. In the drop-down menu select "Package presentation" and then click on "Hierarchical", if its not yet selected. I prefer this setting, but you can change it back to "Flat" if you want to.

 

These are the settings for the block:

BasicBlock.java:
package com.bedrockminer.tutorial.block;

import com.bedrockminer.tutorial.Main;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;

public class BasicBlock extends Block {

    protected BasicBlock(String unlocalizedName, Material material) {
        super(material);
        this.setBlockName(unlocalizedName);
        this.setBlockTextureName(Main.MODID + ":" + unlocalizedName);
        this.setCreativeTab(CreativeTabs.tabBlock);
        this.setHardness(2.0F);
        this.setResistance(6.0F);
        this.setLightLevel(1.0F);
        this.setHarvestLevel("pickaxe", 3);
        this.setStepSound(soundTypeMetal);
    }
}

What you can see is that I externalized the unlocalizedName to be passed as an argument. This makes the code easier to understand, because you have all unlocalized names together with the block declaration in the ModBlocks class.

 

The ModBlocks class now looks like this:

ModBlocks.java:
package com.bedrockminer.tutorial.block;

import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;

public final class ModBlocks {

    public static Block tutorialBlock;

    public static final void init() {
        GameRegistry.registerBlock(tutorialBlock = new BasicBlock("tutorialBlock", Material.iron), "tutorialBlock");
    }
}

To shorten the code, I registered the block in the same line as I created it. I'd recommend to do it this way rather than writing it in two methods because otherwise the code gets a little bit confusing if you have some more blocks. This is also a good enhancement for the registration of items.

 

The last thing we have to do is to call the ModBlocks class from the CommonProxy.

CommonProxy.java:
package com.bedrockminer.tutorial;

import com.bedrockminer.tutorial.block.ModBlocks;
import com.bedrockminer.tutorial.item.ModItems;

import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;

public class CommonProxy {

    public void preInit(FMLPreInitializationEvent e) {
        ModItems.init();
        ModBlocks.init();
    }

    public void init(FMLInitializationEvent e) {

    }

    public void postInit(FMLPostInitializationEvent e) {

    }
}

If we now run minecraft, your block should be added to the game. We can find it in the "Blocks" tab in creative mode.

Picture - Show

You can download the code used in this tutorial as a .zip file from here.


Recommended tutorials to continue with


Comments and Questions:

If you want to report modding problems, please make sure to include the code in a pastebin link or something else! Don't just write "It doesn't work", otherwise your post will be deleted. For more complicated problems, please use the troubleshooter form.