Wuppy’s Minecraft Forge Modding Tutorials for 1.6.2: Metadata Items

In this tutorial I’m going to show you how to create metadata Items. For this you will have to make some small changes to the mod file and you will have to change a lot in the Item file. These are the files I will start with.
ItemTutorial


package tutorial;
public class ItemTutorial extends ItemGeneralTutorial
{
       public ItemTutorial(int id)
       {
              super(id);
       }
}

Mod file


package tutorial;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;
@Mod(modid = Tutorial.modid, name = “Mod Name”, version = “1.0”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial
{
       public static final String modid = “YourName_ModName”;
      
       public static Block tutorialBlock;
      
       public static Item tutorialItem;
      
       EventManager eventmanager = new EventManager();
      
       @Init
       public void load(FMLInitializationEvent event)
       {
              tutorialBlock = new BlockTutorialBlock(500, Material.rock).setUnlocalizedName(“tutorialBlock”);
             
              GameRegistry.registerBlock(tutorialBlock, ItemTutorialBlock.classmodid + (tutorialBlock.getUnlocalizedName().substring(5)));
             
              LanguageRegistry.addName(new ItemStack(tutorialBlock, 1, 0), “Tutorial Block”);
              LanguageRegistry.addName(new ItemStack(tutorialBlock, 1, 1), “Nether Tutorial Block”);
             
              tutorialItem = new ItemTutorial(5000).setUnlocalizedName(“tutorialItem”);
             
              LanguageRegistry.addName(tutorialItem“Tutorial Item”);
             
              GameRegistry.registerWorldGenerator(eventmanager);
             
              TutorialCrafting.addRecipes();
       }
}

Before you read this tutorial I suggest reading through the Block tutorial. There will be some references to that tutorial in here.
For metadata blocks you needed an ItemBlock. For Item metadata you don’t need one since there is already an Item file. There are some small differences, but in short for Item metadata you need the code from the Block and ItemBlock file all in the Item file.
In the current Item file we extend the ItemGeneralTutorial which can be used for basic Items. Since this is not a basic item you should change ItemGeneralTutorial into Item and copy the code from the General file in the ItemTutorial file. We are going to make quite some changes to the code. This is what the file should look like now.


package tutorial;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
public class ItemTutorial extends Item
{
       public ItemTutorial(int id)
       {
              super(id);
              this.setCreativeTab(CreativeTabs.tabMaterials);
       }
      
       public void registerIcons(IconRegister par1IconRegister)
    {
        this.itemIcon = par1IconRegister.registerIcon(Tutorial.modid + “:” + (this.getUnlocalizedName().substring(5)));
    }
}

Like the Block file you will need an Icon array to load all the images. We can just copy that part of the code from the Block file. The new code should look like this.


@SideOnly(Side.CLIENT)
private Icon[] icons;
      
@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister par1IconRegister)
{
       icons = new Icon[2];
             
       for(int i = 0; i < icons.length; i++)
       {
              icons[i] = par1IconRegister.registerIcon(Tutorial.modid + “:” + (this.getUnlocalizedName().substring(5)) + i);
       }
}

If you don’t know what this code means you should read the Block Metadata tutorial. The code is explained in there.
Again like the Block file you need to make sure that the Items get different textures depending on the metadata. Since Items don’t have sides the code for this is quite a bit easier. It should look like this for every Item if you follow the tutorial exactly.


public Icon getIconFromDamage(int par1)
{
return icons[par1];
}

This method has one parameter which is the damage of the item. The thing it wants returned is the Icon. Because we have an Icon array that has the same numbers for metadata as for the Icon we can simply return icons[par1] for every single metadata and that is the same in every metadata Item that is made like this.
There is one more thing that can be copied from the Block file. That is the getSubBlocks method. The only difference is, of course, that you have to change the name into getSubItems. The method should look like this.


@SideOnly(Side.CLIENT)
public void getSubItems(int par1, CreativeTabs par2CreativeTabs, List par3List)
{
    for (int x = 0; x < 2; x++)
    {
        par3List.add(new ItemStack(this, 1, x));
    }
}

This code is exactly the same as in the Block tutorial, if you don’t understand it, read that tutorial.
Now there are just 2 things left to do. The first is to make sure that the Item names are different for each metadata and then to make sure that the names in game are also different. The first thing is done by creating a String array that contains a special name for every metadata.
When you have made that array you will have to add the getUnlocalizedName method to the ItemTutorial file and add this code in there.


public static final String[] names = new String[] {“first”“second”};
      
public String getUnlocalizedName(ItemStack par1ItemStack)
{
    int i = MathHelper.clamp_int(par1ItemStack.getItemDamage(), 0, 15);
    return super.getUnlocalizedName() + “.” + names[i];
}

The code in this method is also the same for every single metadata Item.
There is one more important line of code you will have to add to the constructor. That code is the following.


this.setHasSubtypes(true);


The Item file should now look like this.


package tutorial;
import java.util.List;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.util.Icon;
import net.minecraft.util.MathHelper;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
public class ItemTutorial extends Item
{
       public ItemTutorial(int id)
       {
              super(id);
              this.setHasSubtypes(true);
              this.setCreativeTab(CreativeTabs.tabMaterials);
       }
      
       @SideOnly(Side.CLIENT)
       private Icon[] icons;
      
       @SideOnly(Side.CLIENT)
       public void registerIcons(IconRegister par1IconRegister)
       {
              icons = new Icon[2];
             
              for(int i = 0; i < icons.length; i++)
              {
                     icons[i] = par1IconRegister.registerIcon(Tutorial.modid + “:” + (this.getUnlocalizedName().substring(5)) + i);
              }
       }
      
       public static final String[] names = new String[] {“first”“second”};
      
       public String getUnlocalizedName(ItemStack par1ItemStack)
    {
        int i = MathHelper.clamp_int(par1ItemStack.getItemDamage(), 0, 15);
        return super.getUnlocalizedName() + “.” + names[i];
    }
      
       public Icon getIconFromDamage(int par1)
    {
       return icons[par1];
    }
    @SideOnly(Side.CLIENT)
    public void getSubItems(int par1, CreativeTabs par2CreativeTabs, List par3List)
    {
       for (int x = 0; x < 2; x++)
        {
            par3List.add(new ItemStack(this, 1, x));
        }
    }
}

The last step for metadata Items is to use LanguageRegistry to add different in-game news for the Item. The code for that should look like this.


LanguageRegistry.addName(new ItemStack(tutorialItem, 1, 0), “Tutorial Item”);
LanguageRegistry.addName(new ItemStack(tutorialItem, 1, 1), “Second Tutorial Item”);

The whole Mod file should now look like this.


package tutorial;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.Mod.Init;
import cpw.mods.fml.common.event.FMLInitializationEvent;
import cpw.mods.fml.common.network.NetworkMod;
import cpw.mods.fml.common.registry.GameRegistry;
import cpw.mods.fml.common.registry.LanguageRegistry;
@Mod(modid = Tutorial.modid, name = “Mod Name”, version = “1.0”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial
{
       public static final String modid = “YourName_ModName”;
      
       public static Block tutorialBlock;
      
       public static Item tutorialItem;
      
       EventManager eventmanager = new EventManager();
      
       @Init
       public void load(FMLInitializationEvent event)
       {
              tutorialBlock = new BlockTutorialBlock(500, Material.rock).setUnlocalizedName(“tutorialBlock”);
             
              GameRegistry.registerBlock(tutorialBlock, ItemTutorialBlock.classmodid + (tutorialBlock.getUnlocalizedName().substring(5)));
             
              LanguageRegistry.addName(new ItemStack(tutorialBlock, 1, 0), “Tutorial Block”);
              LanguageRegistry.addName(new ItemStack(tutorialBlock, 1, 1), “Nether Tutorial Block”);
             
              tutorialItem = new ItemTutorial(5000).setUnlocalizedName(“tutorialItem”);
             
              LanguageRegistry.addName(new ItemStack(tutorialItem, 1, 0), “Tutorial Item”);
              LanguageRegistry.addName(new ItemStack(tutorialItem, 1, 1), “Second Tutorial Item”);
             
              GameRegistry.registerWorldGenerator(eventmanager);
             
              TutorialCrafting.addRecipes();
       }
}

You can download the source code and the assets folder over here.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">