Jump to content

Help - How To Make Custom Tools Repairable


SilentDoom

Recommended Posts

Hello everyone, I've starting making a mod with a whole bunch of different tools and came to realise that I needed to make them repairable.

 

So, im trying to figure out to make it so that when i place an emerald, it will be repair my emerald tools. Can anyone tell me the code for that? Would I Put it in my main class file? or my tool file (EmeraldPickaxe.java).

 

This is what i used to created my tool material:

"public static EnumToolMaterial EMERALD_PICKAXE = EnumHelper.addToolMaterial("EMERALD_PICKAXE", 1, 0, 4.0F, 3, 5);"

 

and then declared it:

"pickaxeEmerald = new ItemPickaxeEmerald(pickaxeEmeraldID, EMERALD_PICKAXE).setUnlocalizedName("pickaxeEmerald");"

 

I'm fairly new to modding, and some things are difficulty. Thanks in advance!

 

 

Link to comment
Share on other sites

I don't know about doing it in an anvil, I've looked but I can't quite work out how anvils work

 

Regardless, you could do this in two ways

 

One is by using multiple tools. It automatically adds in the repair feature where if you put two tools into a crafting grid it adds the remaining durabilities together up to the a full one

So if you've got two emerald pickaxes at half durability and you put them in the crafting grid, you'll get a single full one out of it

 

Or you could work around the anvil and just create a crafting recipe for it

as in

 

		GameRegistry.addShapelessRecipe(new ItemStack(Yourmod.pickaxeEmerald), new Object[]{
		new ItemStack(Item.emerald), new ItemStack (Yourmod.pickaxeEmerald)
	});

 

you could always do it that way

it'll mean that it doesn't take levels from you (not sure if that's good or bad for you) but you could use it as a temporary measure until you can work out anvils

Link to comment
Share on other sites

Okay, I've got how you do it

 

You're going to need to make a few extra files though (if you haven't already got them)

 

First you need to make a custom EnumToolMaterial file, like so (I've added annotations in the files to show you what to change btw)

 

 


package deadworld;

import net.minecraft.block.Block;
import net.minecraft.item.Item;


public enum EnumDeadToolMaterial
{

//YOUR TOOL MATERIAL

    deadwood(1, 20, 6.0F, 1, 20),
    Darksteel(3, 335, 6.0F, 2, 20),
    Bonetool(1, 175, 4.0F, 1, 20);
    

    /**
     * The level of material this tool can harvest (3 = DIAMOND, 2 = IRON, 1 = STONE, 0 = IRON/GOLD)
     */
    private final int harvestLevel;

    /**
     * The number of uses this material allows. (wood = 59, stone = 131, iron = 250, diamond = 1561, gold = 32)
     */
    private final int maxUses;

    /**
     * The strength of this tool material against blocks which it is effective against.
     */
    private final float efficiencyOnProperMaterial;

    /** Damage versus entities. */
    private final int damageVsEntity;

    /** Defines the natural enchantability factor of the material. */
    private final int enchantability;

    public Item customCraftingMaterial = null;

    private EnumDeadToolMaterial(int par3, int par4, float par5, int par6, int par7)
    {
        this.harvestLevel = par3;
        this.maxUses = par4;
        this.efficiencyOnProperMaterial = par5;
        this.damageVsEntity = par6;
        this.enchantability = par7;
    }

    /**
     * The number of uses this material allows. (wood = 59, stone = 131, iron = 250, diamond = 1561, gold = 32)
     */
    public int getMaxUses()
    {
        return this.maxUses;
    }

    /**
     * The strength of this tool material against blocks which it is effective against.
     */
    public float getEfficiencyOnProperMaterial()
    {
        return this.efficiencyOnProperMaterial;
    }

    /**
     * Damage versus entities.
     */
    public int getDamageVsEntity()
    {
        return this.damageVsEntity;
    }

    /**
     * The level of material this tool can harvest (3 = DIAMOND, 2 = IRON, 1 = STONE, 0 = IRON/GOLD)
     */
    public int getHarvestLevel()
    {
        return this.harvestLevel;
    }

    /**
     * Return the natural enchantability factor of the material.
     */
    public int getEnchantability()
    {
        return this.enchantability;
    }

    /**
     * Return the crafting material for this tool material, used to determine the item that can be used to repair a tool
     * with an anvil
     */
    public int getToolCraftingMaterial()
    {
        switch (this)
        {
        
        	//"deadwood" being your tool material and "deadplanks" being what you want to use to repair it
        	
            case deadwood:    return Deadworld.deadplanks.blockID;
            case Bonetool:	  return Item.bone.itemID;
            case Darksteel:   return Deadworld.darksteelIngot.itemID;
            default:      return (customCraftingMaterial == null ? 0 : customCraftingMaterial.itemID);
        }
    }
}



 

 

You can use this to keep all your tool materials in as well, which is pretty cool, keeps the main mod file neater

 

After this, make a copy of ItemSword and ItemTool, like so

 

custom ItemTool:

 

package deadworld;

import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;
import net.minecraftforge.common.ForgeHooks;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class ItemDeadTool extends Item
{
    /** Array of blocks the tool has extra effect against. */
    private Block[] blocksEffectiveAgainst;
    public float efficiencyOnProperMaterial = 4.0F;

    /** Damage versus entities. */
    public int damageVsEntity;

    // Change "EnumDeadToolMaterial" to your custom EnumToolMaterial class
    protected EnumDeadToolMaterial toolMaterial;

    //Change ItemDeadTool to your custom ItemTool and change EnumDeadToolMaterial to your custom EnumToolMaterial
    protected ItemDeadTool(int par1, int par2, EnumDeadToolMaterial par3EnumToolMaterial, Block[] par4ArrayOfBlock)
    {
        super(par1);
        this.toolMaterial = par3EnumToolMaterial;
        this.blocksEffectiveAgainst = par4ArrayOfBlock;
        this.maxStackSize = 1;
        this.setMaxDamage(par3EnumToolMaterial.getMaxUses());
        this.efficiencyOnProperMaterial = par3EnumToolMaterial.getEfficiencyOnProperMaterial();
        this.damageVsEntity = par2 + par3EnumToolMaterial.getDamageVsEntity();
        this.setCreativeTab(CreativeTabs.tabTools);
    }

    /**
     * Returns the strength of the stack against a given block. 1.0F base, (Quality+1)*2 if correct blocktype, 1.5F if
     * sword
     */
    public float getStrVsBlock(ItemStack par1ItemStack, Block par2Block)
    {
        for (int i = 0; i < this.blocksEffectiveAgainst.length; ++i)
        {
            if (this.blocksEffectiveAgainst[i] == par2Block)
            {
                return this.efficiencyOnProperMaterial;
            }
        }

        return 1.0F;
    }

    /**
     * Current implementations of this method in child classes do not use the entry argument beside ev. They just raise
     * the damage on the stack.
     */
    public boolean hitEntity(ItemStack par1ItemStack, EntityLiving par2EntityLiving, EntityLiving par3EntityLiving)
    {
        par1ItemStack.damageItem(2, par3EntityLiving);
        return true;
    }

    public boolean onBlockDestroyed(ItemStack par1ItemStack, World par2World, int par3, int par4, int par5, int par6, EntityLiving par7EntityLiving)
    {
        if ((double)Block.blocksList[par3].getBlockHardness(par2World, par4, par5, par6) != 0.0D)
        {
            par1ItemStack.damageItem(1, par7EntityLiving);
        }

        return true;
    }

    /**
     * Returns the damage against a given entity.
     */
    public int getDamageVsEntity(Entity par1Entity)
    {
        return this.damageVsEntity;
    }

    @SideOnly(Side.CLIENT)

    /**
     * Returns True is the item is renderer in full 3D when hold.
     */
    public boolean isFull3D()
    {
        return true;
    }

    /**
     * Return the enchantability factor of the item, most of the time is based on material.
     */
    public int getItemEnchantability()
    {
        return this.toolMaterial.getEnchantability();
    }

    /**
     * Return the name for this tool's material.
     */
    public String getToolMaterialName()
    {
        return this.toolMaterial.toString();
    }

    /**
     * Return whether this item is repairable in an anvil.
     */
    public boolean getIsRepairable(ItemStack par1ItemStack, ItemStack par2ItemStack)
    {
        return this.toolMaterial.getToolCraftingMaterial() == par2ItemStack.itemID ? true : super.getIsRepairable(par1ItemStack, par2ItemStack);
    }

    /** FORGE: Overridden to allow custom tool effectiveness */
    @Override
    public float getStrVsBlock(ItemStack stack, Block block, int meta) 
    {
        if (ForgeHooks.isToolEffective(stack, block, meta))
        {
            return efficiencyOnProperMaterial;
        }
        return getStrVsBlock(stack, block);
    }
}

 

 

and custom ItemSword:

 

package deadworld;

import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;
import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.EnumAction;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;
import net.minecraft.world.World;

public class ItemDeadSword extends Item
{
    private int weaponDamage;
    // Change "EnumDeadToolMaterial" to your custom EnumToolMaterial class
    private final EnumDeadToolMaterial toolMaterial;

    // Change "EnumDeadToolMaterial" to your custom EnumToolMaterial class
    public ItemDeadSword(int par1, EnumDeadToolMaterial par2EnumToolMaterial)
    {
        super(par1);
        this.toolMaterial = par2EnumToolMaterial;
        this.maxStackSize = 1;
        this.setMaxDamage(par2EnumToolMaterial.getMaxUses());
        this.setCreativeTab(CreativeTabs.tabCombat);
        this.weaponDamage = 4 + par2EnumToolMaterial.getDamageVsEntity();
}

public int func_82803_g()
    {
        return this.toolMaterial.getDamageVsEntity();
    }

    /**
     * Returns the strength of the stack against a given block. 1.0F base, (Quality+1)*2 if correct blocktype, 1.5F if
     * sword
     */
    public float getStrVsBlock(ItemStack par1ItemStack, Block par2Block)
    {
        if (par2Block.blockID == Block.web.blockID)
        {
            return 15.0F;
        }
        else
        {
            Material material = par2Block.blockMaterial;
            return material != Material.plants && material != Material.vine && material != Material.coral && material != Material.leaves && material != Material.pumpkin ? 1.0F : 1.5F;
        }
    }

    /**
     * Current implementations of this method in child classes do not use the entry argument beside ev. They just raise
     * the damage on the stack.
     */
    public boolean hitEntity(ItemStack par1ItemStack, EntityLiving par2EntityLiving, EntityLiving par3EntityLiving)
    {
        par1ItemStack.damageItem(1, par3EntityLiving);
        return true;
    }

    public boolean onBlockDestroyed(ItemStack par1ItemStack, World par2World, int par3, int par4, int par5, int par6, EntityLiving par7EntityLiving)
    {
        if ((double)Block.blocksList[par3].getBlockHardness(par2World, par4, par5, par6) != 0.0D)
        {
            par1ItemStack.damageItem(2, par7EntityLiving);
        }

        return true;
    }

    /**
     * Returns the damage against a given entity.
     */
    public int getDamageVsEntity(Entity par1Entity)
    {
        return this.weaponDamage;
    }

    @SideOnly(Side.CLIENT)

    /**
     * Returns True is the item is renderer in full 3D when hold.
     */
    public boolean isFull3D()
    {
        return true;
    }

    /**
     * returns the action that specifies what animation to play when the items is being used
     */
    public EnumAction getItemUseAction(ItemStack par1ItemStack)
    {
        return EnumAction.block;
    }

    /**
     * How long it takes to use or consume an item
     */
    public int getMaxItemUseDuration(ItemStack par1ItemStack)
    {
        return 72000;
    }

    /**
     * Called whenever this item is equipped and the right mouse button is pressed. Args: itemStack, world, entityPlayer
     */
    public ItemStack onItemRightClick(ItemStack par1ItemStack, World par2World, EntityPlayer par3EntityPlayer)
    {
        par3EntityPlayer.setItemInUse(par1ItemStack, this.getMaxItemUseDuration(par1ItemStack));
        return par1ItemStack;
    }

    /**
     * Returns if the item (tool) can harvest results from the block type.
     */
    public boolean canHarvestBlock(Block par1Block)
    {
        return par1Block.blockID == Block.web.blockID;
    }

    /**
     * Return the enchantability factor of the item, most of the time is based on material.
     */
    public int getItemEnchantability()
    {
        return this.toolMaterial.getEnchantability();
    }

    /**
     * Return the name for this tool's material.
     */
    public String getToolMaterialName()
    {
        return this.toolMaterial.toString();
    }

    /**
     * Return whether this item is repairable in an anvil.
     */
    public boolean getIsRepairable(ItemStack par1ItemStack, ItemStack par2ItemStack)
    {
        return this.toolMaterial.getToolCraftingMaterial() == par2ItemStack.itemID ? true : super.getIsRepairable(par1ItemStack, par2ItemStack);
    }
}

 

 

Then, you need to go into your Emerald Sword/Pick/Axe/Shovel/Hoe and change "extends ItemTool" to "extends ItemDeadTool" and "extends ItemSword" to "extends ItemDeadSword"

Obviously, change "ItemDeadSword" and "ItemDeadTool" to whatever you named your custom ItemSword and custom ItemTool

 

HOWEVER

If you've set your tools (pick/axe/shovel/hoe but NOT sword) up like this:

 

package tutorial;

import net.minecraft.client.renderer.texture.IconRegister;
import net.minecraft.item.EnumToolMaterial;
import net.minecraft.item.ItemAxe;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class TutAxe extends ItemAxe {

public TutAxe(int par1, EnumToolMaterial par2EnumToolMaterial) {
	super(par1, par2EnumToolMaterial);
	// TODO Auto-generated constructor stub
}

@SideOnly(Side.CLIENT)
public void registerIcons(IconRegister par1IconRegister)
{
	this.itemIcon = par1IconRegister.registerIcon(Tutorial.modid + ":" + (this.getUnlocalizedName().substring(5)));
}

}

 

 

You need to change it to this:

 

 

package deadworld;

import net.minecraft.block.Block;
import net.minecraft.block.material.Material;
import net.minecraft.item.ItemStack;

public class TutAxe extends ItemDeadTool
{
    /** an array of the blocks this axe is effective against */
    public static final Block[] blocksEffectiveAgainst = new Block[] {Block.planks, Block.bookShelf, Block.wood, Block.chest, Block.stoneDoubleSlab, Block.stoneSingleSlab, Block.pumpkin, Block.pumpkinLantern};

    public TutAxe(int par1, EnumDeadToolMaterial par2EnumToolMaterial)
    {
        super(par1, 3, par2EnumToolMaterial, blocksEffectiveAgainst);
    }

    /**
     * Returns the strength of the stack against a given block. 1.0F base, (Quality+1)*2 if correct blocktype, 1.5F if
     * sword
     */
    public float getStrVsBlock(ItemStack par1ItemStack, Block par2Block)
    {
        return par2Block != null && (par2Block.blockMaterial == Material.wood || par2Block.blockMaterial == Material.plants || par2Block.blockMaterial == Material.vine) ? this.efficiencyOnProperMaterial : super.getStrVsBlock(par1ItemStack, par2Block);
    }
    @SideOnly(Side.CLIENT)
    public void registerIcons(IconRegister par1IconRegister)
{
	this.itemIcon = par1IconRegister.registerIcon(Deadworld.modid + ":" + (this.getUnlocalizedName().substring(5)));
}
}

 

 

I add that in because where I originally got this tutorial from, it used "extends ItemAxe" rather than "ItemTool" and didn't have the "blocksEffectiveAgainst" bit

It NEEDS to extend ItemDeadTool for it to work

Obviously, you need to change it around a bit for each different tool, but that's up to you

 

 

Then, wherever you registered your EnumToolMaterial before this, you need to find and either delete or turn into an annotation

For me, it looked like this:

 

 

//EnumToolMaterial Deadwood = EnumHelper.addToolMaterial("Deadwood", 1, 20, 6.0F, 1, 20);

 

 

I just added the "//" to turn it to an annotation, in case I ever decide to go back to how it was originally

 

Finally, you need to change the registry of the tools

 

It looks something like this:

 


	deadwoodsword = new DeadwoodSword(1285, deadwood).setUnlocalizedName("deadwoodsword").setCreativeTab(Deadworld.deadtab);
	LanguageRegistry.addName(deadwoodsword, "Dead Wood Sword");

 

 

You need to add in your custom EnumToolMaterial to where you define which tool material this tool/sword is

 

So it looks like this

 

 


	deadwoodsword = new DeadwoodSword(1285, EnumDeadToolMaterial.deadwood).setUnlocalizedName("deadwoodsword").setCreativeTab(Deadworld.deadtab);
	LanguageRegistry.addName(deadwoodsword, "Dead Wood Sword");

 

 

And that should be it

 

I've tested it with the sword and with the Tut Axe I just added in for it and it definitely works

 

 

There may be an easier way to do this, but I've not found one and I'm not the best java coder in the world, so this might seem a little messy

But it works :D

 

 

I'm not exactly sure how it works for armour, but I imagine it'll be something pretty similar

 

Anyway, I hope this has helped, and wasn't too confusing or anything haha

I've tried to make it as simple as I can

 

(Actually worked this out like 2 hours ago but it wouldn't let me on the forum haha)

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Unfortunately, your content contains terms that we do not allow. Please edit your content to remove the highlighted words below.
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Announcements



×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.