Forge Lesson 01 – Creating an item

The first lesson will be to create a simple mod, which adds an item that can spawn pigs.

First start with creating a package for our mod (in eclipse right click the src/main/java folder -> new -> package). Call the package noppes.tutorials.  Inside this package create new class called Tutorials (right click the package -> new -> Class).

To the top of the Class add the annotation @Mod with the parameters modid=”tutorial”, name=”Tutorial”, version=”1″. Inside of the class create a new function called init, with the annotation @EventHandler which takes the paramater FMLPreInitializationEvent ev.

Your class should now look like:

package noppes.tutorials;

import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;

@Mod(modid="tutorial",name="Tutorial",version="1")
public class Tutorial {

	@EventHandler
	public void init(FMLPreInitializationEvent ev) {

	}
}

Next create a new class called ItemWand which extends Item:

package noppes.tutorials;

import net.minecraft.item.Item;

public class ItemWand extends Item{

}

Inside your init function in the Tutorial class create a new ItemWand object with the integer 4000. Also call setUnlocalizedName with an unique item name and setCreativeTab with the value CreativeTabs.tabTools.

new ItemWand().setUnlocalizedName("tutorial_wand").setCreativeTab(CreativeTabs.tabTools);

Next run minecraft, open a creative world and in the tools tab you will see your item. It will however not have a proper name and no proper texture. Lets fix that next.

First create two new packages: assets.tutorial.lang and assets.tutorial.textures.items in the src/java/resources folder. Next download the wand texture or create your own and put it in the items package. After that create a new file in the lang package called en_US.lang.

When you ran minecraft and checked the name of the item, it should have shown something like item.tutorialwand.name. The tutorialwand is the name we gave it in the setUnlocalizedName function earlier. Now open your en_US.lang with a text editor and add the line:

item.tutorial_wand.name=Tutorial Wand

To point the items texture to the correct location call setTextureName with a string containing the modid:texturename

new ItemWand().setUnlocalizedName("tutorial_wand").
    setCreativeTab(CreativeTabs.tabTools).setTextureName("tutorial:wand");

When you run minecraft again and check the tools tab, it should show the proper name and texture.

All thats left is to make the item spawn pigs. To do that add the next function to your ItemWand:

@Override
public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int x, int y, int z, int face, float par8, float par9, float par10) {
	if (face != 1)
		return false;
	if (!par3World.isRemote) {
		EntityPig pig = new EntityPig(par3World);
		pig.setPosition(x + 0.5, y + 1, z + 0.5);
		par3World.spawnEntityInWorld(pig);
	}
	return true;
}

The onItemUse is called when a player clicks on the ground. Depending on what part of a block (top, bottom, sides) is clicked the face integer has a different value. When the top of a block is pressed the face value is 1, since we only want to spawn it when the top is pressed we return false if the top isnt pressed. The world.isRemote is to check if its called client or server side. You should only spawn new entities server side otherwise ghost entities will appear.

Side note: language files always go into the assets.<modid>. lang and item textures in the assets.<modid>.textures.items. Language files always need the en_US.lang, which is used by default if it cant find the selected minecraft language.

Download the Code