Custom Tools & Weapons

Now that we have a Red Diamond item, and we can mine it from a Red Diamond Ore block, we can think about creating tools and weapons.  Here we will create a Red Diamond Sword, but the same steps can be used to create a full set of diamond tools, see the related GitHub project. 

The first thing we will need is a new Java class for the Red Diamond Sword.  However, this will extend the ItemSword Minecraft class.  This will give us all of the functionality of the regular Minecraft sword, and allow us to only change the tool material being used.  We will need to do all the same things that we did with our first custom item, which at this point should be very familiar to you. 

package reddiamond.item;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import reddiamond.RedDiamondInfo;
import reddiamond.RedDiamondMod;
import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.item.EnumToolMaterial;
import net.minecraft.item.Item;
import net.minecraft.item.ItemSword;

public class RedDiamondSword extends ItemSword {
    
    public static int id = 5001;
    public static String internalName = "redDiamondSword";
    public static String externalName = "Red Diamond Sword";

    public RedDiamondSword(int id, EnumToolMaterial material) {
        super(id, material);
        this.setCreativeTab(CreativeTabs.tabCombat);
        setUnlocalizedName(internalName);
    }
    
    @Override
    @SideOnly(Side.CLIENT)
    public void registerIcons(IconRegister iconRegister){
        itemIcon = iconRegister.registerIcon(
            RedDiamondInfo.MOD_ID + ":RedDiamondSword");
    }
}

 

Here is what the Red Diamond Sword class looks like.  We use the next item id in our range, which is 5001.  We use an internal name similar to the class name, except with the first character in lowercase.  The external name is something descriptive.  Notice, that we are putting the sword in the combat tab, which makes sense since it is a sword and will be used in combat.  Finally we override the registerIcons() method to change the texture to use a custom one that we need to create.  We would do this in the same way, by starting with the default diamond_sword.png, and then changing the colors to red. 

 

public static Item redDiamondSword = new 
RedDiamondSword(RedDiamondSword.id, materialRedDiamond);

GameRegistry.registerItem(redDiamondSword, 
    RedDiamondSword.internalName);
LanguageRegistry.addName(redDiamondSword, 
    RedDiamondSword.externalName);
GameRegistry.addRecipe(new ItemStack(redDiamondSword, 1), 
    RedDiamondSword.recipe);

 

Finally, we need to define an instance variable and register the sword with the game in the RedDiamondMod class.  This should be very familiar by now.  It is exactly the same as we did earlier.  There is one difference, the addRecipe(), which will be explained in a separate section.