Modding: Custom Pickaxe

In this tutorial I will teach you how to create your own pickaxe in minecraft. A lot of the code is the same as with the sword tutorial and parts of it will be used here. If you haven’t read it yet I suggest you go do that first. This is the mod file I will be starting with. I organized it a bit since the last tutorial to make it a bit easier to read and use.


package Tutorial.common;

import net.minecraft.src.Block;
import net.minecraft.src.EnumToolMaterial;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraftforge.common.DungeonHooks;
import net.minecraftforge.common.EnumHelper;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.SidedProxy;
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 = “YourName_ModName”, name = “ModName”, version = “Version number”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial 
{
//blocks
public static Block oreblock;

public static int oreblockId = 230;

//items
public static Item youritem;
public static Item yourfood;
public static Item yoursword;

//proxy
@SidedProxy(clientSide = “Tutorial.client.ClientProxyTutorial”, serverSide = “Tutorial.common.CommonProxyTutorial”)
    public static CommonProxyTutorial proxy;

//enums
static EnumToolMaterial yourtoolmaterial = EnumHelper.addToolMaterial(“yourmaterial”, 2, 500, 7F, 2, 14);

@Init
public void load(FMLInitializationEvent event) 
{
//proxy
proxy.registerRenderThings();

//blocks
oreblock = new BlockOres(oreblockId, 0).setStepSound(Block.soundStoneFootstep).setHardness(3F).setResistance(1.0F).setBlockName(“oreblock”);

//metadata register
Item.itemsList[oreblockId] = new ItemBlockOres(oreblockId-256, oreblock).setItemName(“oreblock”);

//block names
LanguageRegistry.instance().addStringLocalization(“tile.oreblock.ore.name”, “Your Ore”);
LanguageRegistry.instance().addStringLocalization(“tile.oreblock.netherore.name”, “Your Nether Ore”);

//items
youritem = new ItemTutorial(550).setIconIndex(1).setItemName(“youritem”);
yourfood = new ItemTutorialFood(551, 8, true).setIconIndex(3).setItemName(“yourfood”);
yoursword = new ItemYourSword(552, yourtoolmaterial).setIconIndex(4).setItemName(“yoursword”);

//item names
LanguageRegistry.addName(youritem, “Your Item”);
LanguageRegistry.addName(yourfood, “Your Food”);
LanguageRegistry.addName(yoursword, “Your Sword”);

//dungeon
DungeonHooks.addDungeonLoot(new ItemStack(youritem), 10, 2, 5);
DungeonHooks.setDungeonLootTries(50);

//game registry
GameRegistry.registerFuelHandler(new TutorialFuel());
GameRegistry.registerWorldGenerator(new WorldgeneratorTutorial());

//recipes
GameRegistry.addRecipe(new ItemStack(yoursword), new Object[]
{
“X”, “X”, “Z”, ‘X’, youritem, ‘Z’, Item.stick
});
}
}


To add a new pickaxe you will have to add this code. I have also added a recipe, but you don’t need it for the item to work.


public static Item yourpick;
yourpick = new ItemYourPick(553, yourtoolmaterial).setIconIndex(5).setItemName(“yourpick”);
LanguageRegistry.addName(yourpick, “Your Pickaxe”);


GameRegistry.addRecipe(new ItemStack(yourpick), new Object[]
{
“XXX”, ” Z “, ” Z “, ‘X’, youritem, ‘Z’, Item.stick
});


This code should be familiar to you now. The only things that have changed are the names, id and iconindex number.
The whole file should now look like this.


package Tutorial.common;

import net.minecraft.src.Block;
import net.minecraft.src.EnumToolMaterial;
import net.minecraft.src.Item;
import net.minecraft.src.ItemStack;
import net.minecraftforge.common.DungeonHooks;
import net.minecraftforge.common.EnumHelper;
import cpw.mods.fml.common.Mod;
import cpw.mods.fml.common.SidedProxy;
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 = “YourName_ModName”, name = “ModName”, version = “Version number”)
@NetworkMod(clientSideRequired = true, serverSideRequired = false)
public class Tutorial 
{
//blocks
public static Block oreblock;

public static int oreblockId = 230;

//items
public static Item youritem;
public static Item yourfood;
public static Item yoursword;
public static Item yourpick;

//proxy
@SidedProxy(clientSide = “Tutorial.client.ClientProxyTutorial”, serverSide = “Tutorial.common.CommonProxyTutorial”)
    public static CommonProxyTutorial proxy;

//enums
static EnumToolMaterial yourtoolmaterial = EnumHelper.addToolMaterial(“yourmaterial”, 2, 500, 7F, 2, 14);

@Init
public void load(FMLInitializationEvent event) 
{
//proxy
proxy.registerRenderThings();

//blocks
oreblock = new BlockOres(oreblockId, 0).setStepSound(Block.soundStoneFootstep).setHardness(3F).setResistance(1.0F).setBlockName(“oreblock”);

//metadata register
Item.itemsList[oreblockId] = new ItemBlockOres(oreblockId-256, oreblock).setItemName(“oreblock”);

//block names
LanguageRegistry.instance().addStringLocalization(“tile.oreblock.ore.name”, “Your Ore”);
LanguageRegistry.instance().addStringLocalization(“tile.oreblock.netherore.name”, “Your Nether Ore”);

//items
youritem = new ItemTutorial(550).setIconIndex(1).setItemName(“youritem”);
yourfood = new ItemTutorialFood(551, 8, true).setIconIndex(3).setItemName(“yourfood”);
yoursword = new ItemYourSword(552, yourtoolmaterial).setIconIndex(4).setItemName(“yoursword”);
yourpick = new ItemYourPick(553, yourtoolmaterial).setIconIndex(5).setItemName(“yourpick”);

//item names
LanguageRegistry.addName(youritem, “Your Item”);
LanguageRegistry.addName(yourfood, “Your Food”);
LanguageRegistry.addName(yoursword, “Your Sword”);
LanguageRegistry.addName(yourpick, “Your Pickaxe”);

//dungeon
DungeonHooks.addDungeonLoot(new ItemStack(youritem), 10, 2, 5);
DungeonHooks.setDungeonLootTries(50);

//game registry
GameRegistry.registerFuelHandler(new TutorialFuel());
GameRegistry.registerWorldGenerator(new WorldgeneratorTutorial());

//recipes
GameRegistry.addRecipe(new ItemStack(yoursword), new Object[]
{
“X”, “X”, “Z”, ‘X’, youritem, ‘Z’, Item.stick
});
GameRegistry.addRecipe(new ItemStack(yourpick), new Object[]
{
“XXX”, ” Z “, ” Z “, ‘X’, youritem, ‘Z’, Item.stick
});
}
}


Now there will be one error. This is that the ItemYourPick isn’t there yet. This is what it should look like when you just make a basic pickaxe.


package Tutorial.common;

import net.minecraft.src.EnumToolMaterial;
import net.minecraft.src.ItemPickaxe;

public class ItemYourPick extends ItemPickaxe
{

public ItemYourPick(int par1, EnumToolMaterial par2EnumToolMaterial)
{
super(par1, par2EnumToolMaterial);
}

public String getTextureFile()
{
return “/TutTextures.png”;
}
}


This is also completely the same as with the sword.
If you don’t want any special properties on this pickaxe continue to the end of the page now.

If you want to change some properties of this pickaxe you can do that by changing things that are already set in ItemPickaxe and ItemTool. You can do this by copying over the methods set in there and changing what is inside them. In this example I will show you how to change the speed of the pickaxe for just one block. The pickaxe will destroy obisidian at a very high speed. There are 2 methods you will have to change for this.


public boolean canHarvestBlock(Block par1Block)
    {
        return par1Block == Block.obsidian ? this.toolMaterial.getHarvestLevel() == 2 : (par1Block != Block.blockDiamond && par1Block != Block.oreDiamond ? (par1Block == Block.oreEmerald ? this.toolMaterial.getHarvestLevel() >= 2 : (par1Block != Block.blockGold && par1Block != Block.oreGold ? (par1Block != Block.blockSteel && par1Block != Block.oreIron ? (par1Block != Block.blockLapis && par1Block != Block.oreLapis ? (par1Block != Block.oreRedstone && par1Block != Block.oreRedstoneGlowing ? (par1Block.blockMaterial == Material.rock ? true : par1Block.blockMaterial == Material.iron) : this.toolMaterial.getHarvestLevel() >= 2) : this.toolMaterial.getHarvestLevel() >= 1) : this.toolMaterial.getHarvestLevel() >= 1) : this.toolMaterial.getHarvestLevel() >= 2)) : this.toolMaterial.getHarvestLevel() >= 2);
    }

public float getStrVsBlock(ItemStack par1ItemStack, Block par2Block)
    {
if(par2Block.blockID == Block.obsidian.blockID)
{
return 80F;
}
else
return par2Block != null && (par2Block.blockMaterial == Material.iron || par2Block.blockMaterial == Material.rock) ? this.efficiencyOnProperMaterial : super.getStrVsBlock(par1ItemStack, par2Block);
    }


The canHarvestBlock method looks a bit messy on the blogger page, but if you compare it with the one in ItemPickaxe you can’t find the difference easy. The small change is that the harvest level of obsidian is 2 instead of 3. This is because the pickaxe only has a harvest level of 2. You might think that it is useless to have done this, because obsidian is the only block with harvest level 3, but mods might add blocks that have that harvest level so it only changes this one block.
The second method that is changed is the get StrVsBlock. If you take a look at it in the ItemPickaxe class you don’t see the if/else statement in there. Just the one line in the else statement. I added this statement to make minecraft check if the block you are hitting is obsidian and if it is the pickaxe will have a speed of 80F. This is about 7 times the speed of gold. Any other blocks will be mined like normal.
The whole file should now look like this.


package Tutorial.common;

import net.minecraft.src.Block;
import net.minecraft.src.EnumToolMaterial;
import net.minecraft.src.ItemPickaxe;
import net.minecraft.src.ItemStack;
import net.minecraft.src.Material;

public class ItemYourPick extends ItemPickaxe
{

public ItemYourPick(int par1, EnumToolMaterial par2EnumToolMaterial)
{
super(par1, par2EnumToolMaterial);
}

public String getTextureFile()
{
return “/TutTextures.png”;
}

public boolean canHarvestBlock(Block par1Block)
    {
        return par1Block == Block.obsidian ? this.toolMaterial.getHarvestLevel() == 2 : (par1Block != Block.blockDiamond && par1Block != Block.oreDiamond ? (par1Block == Block.oreEmerald ? this.toolMaterial.getHarvestLevel() >= 2 : (par1Block != Block.blockGold && par1Block != Block.oreGold ? (par1Block != Block.blockSteel && par1Block != Block.oreIron ? (par1Block != Block.blockLapis && par1Block != Block.oreLapis ? (par1Block != Block.oreRedstone && par1Block != Block.oreRedstoneGlowing ? (par1Block.blockMaterial == Material.rock ? true : par1Block.blockMaterial == Material.iron) : this.toolMaterial.getHarvestLevel() >= 2) : this.toolMaterial.getHarvestLevel() >= 1) : this.toolMaterial.getHarvestLevel() >= 1) : this.toolMaterial.getHarvestLevel() >= 2)) : this.toolMaterial.getHarvestLevel() >= 2);
    }

public float getStrVsBlock(ItemStack par1ItemStack, Block par2Block)
    {
if(par2Block.blockID == Block.obsidian.blockID)
{
return 80F;
}
else
return par2Block != null && (par2Block.blockMaterial == Material.iron || par2Block.blockMaterial == Material.rock) ? this.efficiencyOnProperMaterial : super.getStrVsBlock(par1ItemStack, par2Block);
    }
}


 

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="">