Monday, March 2, 2015

Minecraft Tutorials: A Basic Item

This is a continuation of my Minecraft modding tutorial series.  In it I will discuss creating a custom item for Minecraft.  It assumes that you have at least created a basic mod file.  If you do not know how to do this, please read this first.  The mod file I'll be showing also assumes that you've followed the A Basic Block tutorial, but that tutorial is not necessary to understand the concepts in this tutorial.

The item we will be creating is the Copper Ingot, which will go nicely with the Copper Ore we created in A Basic Block.

Since I like having nice clean tidy packages I'll start by creating a new sub package of my main mod package for items.  I'll call my package me.codasylph.grindermod.items and in it I will create a new class called CopperIngot which will extend Item.  Right now my class looks like this:
package me.codasylph.grindermod.items;

import net.minecraft.item.Item;

public class CopperIngot extends Item
{

}

And the truth is, that's all that's needed, we could register that, and it would exist as a texture-less nameless item that did nothing.  But lets add a little info anyway.  First let's store a name, so inside my class declaration I'll add the line:
private final String unlocalizedName = "copperIngot";

and under that I'll add a constructor like this:
public CopperIngot()
{
      super()
      this.setUnlocalizedName(unlocalizedName);
      this.setTextureName("grindermod:"+unlocalizedName);
      this.setCreativeTab(CreativeTabs.tabMaterials);
}

What did that do?  Well, first we call the super constructor, this way anything that happens in the parent class's constructor happens when our new class is constructed, too.  No reason to do double work.

setUnlocalizedName() gives our item the name we stored in the String unlocalizedName.
setCreativeTab() indicates that in creative mode this item should be found in the Materials tab.  Usually you would create your own Creative Tab for all mod items and blocks, but for now we will just place this here with the vanilla ingots and other materials.
setTextureName() indicates where our item's texture can be found and what it's called. In this case its saying to look in the folder assets/grindermod/textures/items for a png named copperIngot. All textures for Minecraft are pngs.

It may have occurred to you that that folder and png do not yet exist.  So let's fix that, by creating a package called assets.grindermod.textures.items in the resources folder and placing this image file into it.


Our item class is done!  It should look like this:
package me.codasylph.grindermod.items;

import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;

public class CopperIngot extends Item
{
       private final String unlocalizedName = "copperIngot";
      
       public CopperIngot()
       {
              super();
              this.setUnlocalizedName(unlocalizedName);
              this.setTextureName("grindermod:"+unlocalizedName);
              this.setCreativeTab(CreativeTabs.tabMaterials);
       }
}

Now it needs to be registered.  Items can be registered by making a call to the registerItem method in the class GameRegistry directly in the preInit() method of our main mod file. However, since most mods will have multiple items, I like keeping my main mod file tidy and clean, and creating a new class for loading all my items.  
To do that I will create a new class in the me.codasylph.grindermod.items package called ModItems.
In it, I will instantiate a Block called copperIngot and initialize it to a new instance of the CopperIngot() class.  Then I will create a new public method to contain my call to registerItem().  It will all look like this:
package me.codasylph.grindermod.items;

import cpw.mods.fml.common.registry.GameRegistry;
import net.minecraft.item.Item;

public class ModItems
{
       public static Item copperIngot = new CopperIngot();
      
       public static void init()
       {
              GameRegistry.registerItem(copperIngot, copperIngot.getUnlocalizedName());
       }
}

Now in our main mod class, GrindeMod.class I will add a call to ModItems.init() to our preInit method.  My main mod file looks like this now:
package me.codasylph.grindermod;

import me.codasylph.grindermod.blocks.CopperOre;
import me.codasylph.grindermod.blocks.ModBlocks;
import me.codasylph.grindermod.items.ModItems;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.event.FMLPostInitializationEvent;
import cpw.mods.fml.common.event.FMLPreInitializationEvent;
import cpw.mods.fml.common.registry.GameRegistry;

@Mod(modid = "grindermod", name = "Grinder Mod", version = "1.0")

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

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

       }

       @Mod.EventHandler
       public void postInit(FMLPostInitializationEvent event)
       {
             
       }
}
Note: if you didn't do the block tutorial, you won't have ModBlocks.init(); that's ok.

Finally, we need to give our item a local name. If you've followed A Basic Block, then you already have a language file called en_US.lang.  If not, at this point you want to create a new package in your resources folder called assets.grindermod.lang and in that package create a new file called en_US.lang.
Now add the following line to en_US.lang:
item.copperIngot.name=Copper Ingot
Mind the spaces or lack thereof.

And that is that.  If you run the testing environment you should be able to go into the creative tab Materials and obtain your own shiny new Copper Ingot!

You may return to the table contents by clicking here.

No comments:

Post a Comment