ModderDojo Java Modding 12: Creating our First Block

2014-03-29_09.42.51

Getting Started

IMPORTANT: For this block mod code to work, you need at least release 1024 of Forge 1.7.2. It will not work with releases 999 that we downloaded before. (Explanation: Forge works by decompiling the Minecraft code, which gives fields and methods automatic names like func_149739_a(). The Forge people have to translate these into meaningful names, like getUnlocalizedName(). They had not done this for the Material fields in earlier releases.)

You can download the source here: http://files.minecraftforge.net/maven/net/minecraftforge/forge/1.7.2-10.12.0.1024/forge-1.7.2-10.12.0.1024-src.zip

You will need to unzip it and run the two gradlew commands – see Post 2.

Thanks to Eli and Ailish who between them were the first to get this working.

Files Needed

Very similarly to creating an Item mod (see Post 8) you need at least 4 things to create a new Block mod:

  1. A new block class that extends the standard Block class
  2. A mod class where you add it to the game registry (you can just add some lines to one you have already)
  3. A language file where you specify its name
  4. A texture file to control its appearance

I am keeping this code in the same package I already used for our item mod, modderdojo.firstitem.

NeededForBlockMod

The Block Class

Create a Java file in this package called MyFirstBlock with this code:

// Michael Madden March 2014.
// Ref: http://www.minecraftforge.net/wiki/Basic_Items
// Thanks to Ailish and Eli in CoderDojo Athenry who figured this out originally. 

package modderdojo.firstitem;

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

public class MyFirstBlock extends Block
{
 public MyFirstBlock()
 {
 // This is the constructor for our new item. Set basic properties in it.
 super(Material.iron); // Copy the properties of iron

 setCreativeTab(CreativeTabs.tabBlock);
 setBlockName("MichaelFirstBlock"); // This is the internal name, not display name
 }
}

 

 

Adding It to the Registry

Add this code to the preInit method in your main mod class (in my case called MyFirstMod):

 // Register my block and set its texture
 MyFirstBlock b = new MyFirstBlock();
 b.setBlockTextureName("modderdojo:mmblock"); // requires .png file with this name
 GameRegistry.registerBlock(b, "MichaelFirstBlock");

Appearance

For this step, you need to create a texture that will be used on all sides of the block.

Using a graphics package such as Paint.NET, create an image of size 32 x 32 (other sizes that are powers of 2 are also OK). Save it in PNG format.

In the src/main/resources folder, create a new package called assets.modderdojo.textures.blocks (just like the one you did for your first item, but with blocks instead of items in its name). Put the PNG file in it. In my case, the file is called mmblock.png.

In your main mod class preInit method, add/update this line:

b.setBlockTextureName("modderdojo:mmblock");

The name you set is based on where you put it “assets.modderdojo.textures.blocks” followed by a colon “:” followed by the name of the PNG file put without “.png”: mmblock.png.

Name

If you run your code now (and it’s a good idea to keep running after every step, to test what you have done), you will see that your block has the name tile.MichaelFirstBlock.name. To give it a better name, add a line to the language file, en_US.lang:

tile.MichaelFirstBlock.name=Michael's First Block Display Name

 

One thought on “ModderDojo Java Modding 12: Creating our First Block

  1. Pingback: ModderDojo Java Modding 13: All Our Mod Code So Far | CoderDojo Athenry

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s