ModderDojo Java Modding 8: A Simple Item Mod

2014-02-15_08.36.11

Note: my main sources of information for this post were the following, though I had to update them quite a bit:

You need at least three things to create a new Item:

  1. A new item class that extends the standard Item class
  2. A mod class where you add it to the game registry
  3. A language file where you specify its name.

modfiles

Later we will add a texture. Until then, its appearance will be a pink and black chequer pattern.

In the src/main/minecraft folder, create a new package. I called it modderdojo.firstitem.

In this package, create a new class. I called it MyFirstItem. Replace the automatically generated code with the code below:

package modderdojo.firstitem;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
public class MyFirstItem extends Item
{
   public MyFirstItem()
   {
      // This is the constructor for our new item. Set basic properties in it.
      setMaxStackSize(64);
      setCreativeTab(CreativeTabs.tabTransport);
      setUnlocalizedName("MichaelFirstItem");
   }
}

The code above does the following:

  • It defines a new Java class  for your new type of item: extends Item means that it is based on the standard Item type, and you will then modify it. Note that you could extend another kind of item, such as Diamond.
  • In its constructor (a special method with the same name as the class, which is called whenever you create an item, it sets basic properties: the max stack size, which tab it will be on, and an unlocalized name (a name that will be used internally in Minecraft).
  • Note: normally the unlocalised name does not have any spaces.

Now create a second class that initialises the mod that will include this item:

package modderdojo.firstitem;

import net.minecraft.init.Blocks;
import net.minecraft.init.Items;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.EventHandler;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.GameRegistry;

@Mod(modid = "firstitem", name = "First Item Mod from ModderDojo by Michael", version = "1.0")
public class MyFirstMod
{
   @EventHandler
   public void preInit(FMLPreInitializationEvent event)
   {
      MyFirstItem first = new MyFirstItem();
      GameRegistry.registerItem(first, "MichaelFirstItem"); // This name doesn't have to be same as unlocalised name, but no harm in them being the same
   }
}

The code above creates a basic mod, like we did earlier. The main difference is in the preInit method, where we create an object of our MyFirstItem class. We call the object first. We use this object to add the item to the game registry, again giving it a name that will be used internally.

You can run your code now, and the mod should appear, displaying its unlocalised name and with the default texture. Next we will see how to change them.

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