Modding 1.5.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;
publicclass ItemTutorial extends ItemGeneralTutorial
{
       public ItemTutorial(int id)
       {
              super(id);
       }
}

Mod file


package tutorial;
importnet.minecraft.block.Block;
importnet.minecraft.block.material.Material;
importnet.minecraft.item.Item;
importnet.minecraft.item.ItemStack;
importcpw.mods.fml.common.Mod;
importcpw.mods.fml.common.Mod.Init;
importcpw.mods.fml.common.event.FMLInitializationEvent;
importcpw.mods.fml.common.network.NetworkMod;
importcpw.mods.fml.common.registry.GameRegistry;
importcpw.mods.fml.common.registry.LanguageRegistry;
@Mod(modid = Tutorial.modid, name = “Mod Name”, version = “1.0”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
publicclass Tutorial
{
       publicstaticfinal String modid = “YourName_ModName”;
      
       publicstatic Block tutorialBlock;
      
       publicstatic Item tutorialItem;
      
       EventManager eventmanager = new EventManager();
      
       @Init
       publicvoidload(FMLInitializationEvent event)
       {
              tutorialBlock = newBlockTutorialBlock(500, Material.rock).setUnlocalizedName(“tutorialBlock”);
             
              GameRegistry.registerBlock(tutorialBlock, ItemTutorialBlock.class, modid + (tutorialBlock.getUnlocalizedName().substring(5)));
             
              LanguageRegistry.addName(new ItemStack(tutorialBlock, 1, 0), “Tutorial Block”);
              LanguageRegistry.addName(new ItemStack(tutorialBlock, 1, 1), “Nether Tutorial Block”);
             
              tutorialItem = newItemTutorial(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;
importnet.minecraft.client.renderer.texture.IconRegister;
importnet.minecraft.creativetab.CreativeTabs;
importnet.minecraft.item.Item;
publicclass ItemTutorial extends Item
{
       public ItemTutorial(int id)
       {
              super(id);
              this.setCreativeTab(CreativeTabs.tabMaterials);
       }
      
       publicvoidregisterIcons(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)
publicvoidregisterIcons(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)
{
returnicons[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)
publicvoid 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.


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

The code in this method is also the same for every single metadata Item.

The Item file should now look like this.


package tutorial;
import java.util.List;
importnet.minecraft.client.renderer.texture.IconRegister;
importnet.minecraft.creativetab.CreativeTabs;
importnet.minecraft.item.Item;
importnet.minecraft.item.ItemStack;
importnet.minecraft.util.Icon;
importnet.minecraft.util.MathHelper;
importcpw.mods.fml.relauncher.Side;
importcpw.mods.fml.relauncher.SideOnly;
publicclass ItemTutorial extends Item
{
       public ItemTutorial(int id)
       {
              super(id);
              this.setCreativeTab(CreativeTabs.tabMaterials);
       }
      
       @SideOnly(Side.CLIENT)
       private Icon[] icons;
      
       @SideOnly(Side.CLIENT)
       publicvoidregisterIcons(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);
              }
       }
      
       publicstaticfinal String[] names = new String[] {“first”, “second”};
      
       public String getUnlocalizedName(ItemStack par1ItemStack)
    {
        int i = MathHelper.clamp_int(par1ItemStack.getItemDamage(), 0, 15);
        returnsuper.getUnlocalizedName() + “.” + names[i];
    }
      
       public Icon getIconFromDamage(int par1)
    {
       returnicons[par1];
    }
    @SideOnly(Side.CLIENT)
    publicvoid 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;
importnet.minecraft.block.Block;
importnet.minecraft.block.material.Material;
importnet.minecraft.item.Item;
importnet.minecraft.item.ItemStack;
importcpw.mods.fml.common.Mod;
importcpw.mods.fml.common.Mod.Init;
importcpw.mods.fml.common.event.FMLInitializationEvent;
importcpw.mods.fml.common.network.NetworkMod;
importcpw.mods.fml.common.registry.GameRegistry;
importcpw.mods.fml.common.registry.LanguageRegistry;
@Mod(modid = Tutorial.modid, name = “Mod Name”, version = “1.0”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
publicclass Tutorial
{
       publicstaticfinal String modid = “YourName_ModName”;
      
       publicstatic Block tutorialBlock;
      
       publicstatic Item tutorialItem;
      
       EventManager eventmanager = new EventManager();
      
       @Init
       publicvoidload(FMLInitializationEvent event)
       {
              tutorialBlock = newBlockTutorialBlock(500, Material.rock).setUnlocalizedName(“tutorialBlock”);
             
              GameRegistry.registerBlock(tutorialBlock, ItemTutorialBlock.class, modid + (tutorialBlock.getUnlocalizedName().substring(5)));
             
              LanguageRegistry.addName(new ItemStack(tutorialBlock, 1, 0), “Tutorial Block”);
              LanguageRegistry.addName(new ItemStack(tutorialBlock, 1, 1), “Nether Tutorial Block”);
             
              tutorialItem = newItemTutorial(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 look at the source code 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="">