Jump to content
Search In
  • More options...
Find results that contain...
Find results in...

Custom Tool returning false on canHarvest but does nothing.


Recommended Posts

CustomItemTool.java

 

package riderj.testmod.items;

 

import java.util.Set;

 

import com.google.common.collect.Multimap;

 

import net.minecraft.block.Block;

import net.minecraft.creativetab.CreativeTabs;

import net.minecraft.entity.EntityLivingBase;

import net.minecraft.entity.SharedMonsterAttributes;

import net.minecraft.entity.ai.attributes.AttributeModifier;

import net.minecraft.item.Item;

import net.minecraft.item.ItemStack;

import net.minecraft.util.BlockPos;

import net.minecraft.world.World;

import net.minecraftforge.fml.relauncher.Side;

import net.minecraftforge.fml.relauncher.SideOnly;

import riderj.testmod.enums.CustomToolMaterials;

 

public class CustomItemTool extends Item

{

    private Set effectiveBlocks;

    protected float efficiencyOnProperMaterial = 4.0F;

    /** Damage versus entities. */

    private float damageVsEntity;

    /** The material this tool is made from. */

    protected CustomToolMaterials.ToolMaterial toolMaterial;

    private static final String __OBFID = "CL_00000019";

 

    protected CustomItemTool(float attackDamage, CustomToolMaterials.ToolMaterial material, Set effectiveBlocks)

    {

        this.toolMaterial = material;

        this.effectiveBlocks = effectiveBlocks;

        this.maxStackSize = 1;

        this.setMaxDamage(material.getMaxUses());

        this.efficiencyOnProperMaterial = material.getEfficiencyOnProperMaterial();

        this.damageVsEntity = attackDamage + material.getDamageVsEntity();

        this.setCreativeTab(CreativeTabs.tabTools);

        /*if (this instanceof ItemPickaxe)

        {

            toolClass = "pickaxe";

        }

        else if (this instanceof ItemAxe)

        {

            toolClass = "axe";

        }

        else if (this instanceof ItemSpade)

        {

            toolClass = "shovel";

        }*/

    }

 

    public float getStrVsBlock(ItemStack stack, Block block)

    {

        return this.effectiveBlocks.contains(block) ? this.efficiencyOnProperMaterial : 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.

    * 

    * @param target The Entity being hit

    * @param attacker the attacking entity

    */

    public boolean hitEntity(ItemStack stack, EntityLivingBase target, EntityLivingBase attacker)

    {

        stack.damageItem(2, attacker);

        return true;

    }

 

    /**

    * Called when a Block is destroyed using this Item. Return true to trigger the "Use Item" statistic.

    */

    public boolean onBlockDestroyed(ItemStack stack, World worldIn, Block blockIn, BlockPos pos, EntityLivingBase playerIn)

    {

        if ((double)blockIn.getBlockHardness(worldIn, pos) != 0.0D)

        {

            stack.damageItem(1, playerIn);

        }

 

        return true;

    }

 

    /**

    * Returns True is the item is renderer in full 3D when hold.

    */

    @SideOnly(Side.CLIENT)

    public boolean isFull3D()

    {

        return true;

    }

 

    public CustomToolMaterials.ToolMaterial getToolMaterial()

    {

        return this.toolMaterial;

    }

 

    /**

    * 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.

    * 

    * @param toRepair The ItemStack to be repaired

    * @param repair The ItemStack that should repair this Item (leather for leather armor, etc.)

    */

    public boolean getIsRepairable(ItemStack toRepair, ItemStack repair)

    {

        ItemStack mat = this.toolMaterial.getRepairItemStack();

        if (mat != null && net.minecraftforge.oredict.OreDictionary.itemMatches(mat, repair, false)) return true;

        return super.getIsRepairable(toRepair, repair);

    }

 

    /**

    * Gets a map of item attribute modifiers, used by ItemSword to increase hit damage.

    */

    public Multimap getItemAttributeModifiers()

    {

        Multimap multimap = super.getItemAttributeModifiers();

        multimap.put(SharedMonsterAttributes.attackDamage.getAttributeUnlocalizedName(), new AttributeModifier(itemModifierUUID, "Tool modifier", (double)this.damageVsEntity, 0));

        return multimap;

    }

 

    /*===================================== FORGE START =================================*/

    private String toolClass;

    @Override

    public int getHarvestLevel(ItemStack stack, String toolClass)

    {

        int level = super.getHarvestLevel(stack, toolClass);

        if (level == -1 && toolClass != null && toolClass.equals(this.toolClass))

        {

            return this.toolMaterial.getHarvestLevel();

        }

        else

        {

            return level;

        }

    }

 

    @Override

    public Set<String> getToolClasses(ItemStack stack)

    {

        return toolClass != null ? com.google.common.collect.ImmutableSet.of(toolClass) : super.getToolClasses(stack);

    }

 

    @Override

    public float getDigSpeed(ItemStack stack, net.minecraft.block.state.IBlockState state)

    {

        for (String type : getToolClasses(stack))

        {

            if (state.getBlock().isToolEffective(type, state))

                return efficiencyOnProperMaterial;

        }

        return super.getDigSpeed(stack, state);

    }

    /*===================================== FORGE END =================================*/

}

 

ItemCustomPickaxe.java

 

package riderj.testmod.items;

 

import java.util.HashSet;

 

import net.minecraft.block.Block;

import net.minecraft.block.material.Material;

import net.minecraft.init.Blocks;

import net.minecraft.item.ItemStack;

import riderj.testmod.enums.CustomToolMaterials;

 

public class ItemCustomPickaxe extends CustomItemTool {

 

    protected ItemCustomPickaxe(float attackDamage, CustomToolMaterials.ToolMaterial material, HashSet<Block> effectiveBlocks)

    {

        super(attackDamage, material, effectiveBlocks);

    }

 

    /**

    * Check whether this Item can harvest the given Block

    */

    public boolean canHarvestBlock(Block blockIn)

    {

        return false;

    }

 

    public float getStrVsBlock(ItemStack stack, Block block)

    {

        return block.getMaterial() != Material.iron && block.getMaterial() != Material.anvil && block.getMaterial() != Material.rock ? super.getStrVsBlock(stack, block) : this.efficiencyOnProperMaterial;

    }

}

 

 

ItemForgedPickaxe.java

 

package riderj.testmod.items;

 

import java.util.HashSet;

 

import com.google.common.collect.Sets;

 

import net.minecraft.block.Block;

import net.minecraft.block.material.Material;

import net.minecraft.client.Minecraft;

import net.minecraft.entity.player.EntityPlayer;

import net.minecraft.init.Blocks;

import net.minecraft.item.ItemStack;

import net.minecraft.util.BlockPos;

import net.minecraft.util.ChatComponentText;

import net.minecraft.util.EnumFacing;

import net.minecraft.world.World;

import riderj.testmod.enums.CustomToolMaterials;

 

public class ItemForgedPickaxe extends ItemCustomPickaxe {

private final static HashSet<Block> effectiveBlocks = Sets.newHashSet(new Block[] {Blocks.obsidian});

 

 

public ItemForgedPickaxe(float attackDamage, CustomToolMaterials.ToolMaterial material) {

 

super(attackDamage, material, effectiveBlocks);

super.efficiencyOnProperMaterial = 520.0F;

// TODO Auto-generated constructor stub

}

 

@Override

public boolean canHarvestBlock(Block blockIn)

    {

Minecraft.getMinecraft().thePlayer.sendChatMessage(""+(blockIn == Blocks.obsidian));

        return blockIn == Blocks.obsidian;// ? this.toolMaterial.getHarvestLevel() == 3 : (blockIn != Blocks.diamond_block && blockIn != Blocks.diamond_ore ? (blockIn != Blocks.emerald_ore && blockIn != Blocks.emerald_block ? (blockIn != Blocks.gold_block && blockIn != Blocks.gold_ore ? (blockIn != Blocks.iron_block && blockIn != Blocks.iron_ore ? (blockIn != Blocks.lapis_block && blockIn != Blocks.lapis_ore ? (blockIn != Blocks.redstone_ore && blockIn != Blocks.lit_redstone_ore ? (blockIn.getMaterial() == Material.rock ? true : (blockIn.getMaterial() == Material.iron ? true : blockIn.getMaterial() == Material.anvil)) : this.toolMaterial.getHarvestLevel() >= 2) : this.toolMaterial.getHarvestLevel() >= 1) : this.toolMaterial.getHarvestLevel() >= 1) : this.toolMaterial.getHarvestLevel() >= 2) : this.toolMaterial.getHarvestLevel() >= 2) : this.toolMaterial.getHarvestLevel() >= 2);

    }

 

@Override

public float getStrVsBlock(ItemStack stack, Block block)

    {

        if (this.effectiveBlocks.contains(block)){

        return getEfficiency(block);

        }

        return super.getStrVsBlock(stack, block);

    }

   

    public boolean onItemUse(ItemStack stack, EntityPlayer playerIn, World worldIn, BlockPos pos, EnumFacing side, float hitX, float hitY, float hitZ)

    {

    playerIn.addChatMessage(new ChatComponentText((this.getDamage(playerIn.getCurrentEquippedItem()))+"/"+(this.getMaxDamage(playerIn.getCurrentEquippedItem()))));

        return false;

    }

   

    public float getEfficiency(Block block){

return efficiencyOnProperMaterial;

   

    }

 

 

}

 

 

 

    I have been messing around with creating custom tools, and am stuck with the pickaxe. I created a custom class that handles the tools directly from the Item class. I did this so I can have my own custom enums for the materials. The problem I am facing is that the canHarvestBlock doesn't prevent the destruction of blocks, even if the function returns false in all of my classes.

 

    From what I have read on how tools destroy blocks are handled in minecraft (greyminecraftcoder blog post) the canHarvestBlock should prevent the block from being destroyed. This is not the case here, but I had figured out that if I modify the getStrVsBlock method to only change the efficiency  I can prevent it from mining blocks, but I would like to get the canHarvestBlock method working. Any input on what could be wrong? There aren't errors.

 

ItemCustomPickaxe extends CustomItemTool, and ItemForgedPickaxes extends ItemCustomPickaxe.

Link to post
Share on other sites

Shouldn't it ignore the efficiency value passed through getStrVsBlock then? Also is canHarvestBlock used solely by the modder inside their class? I changed the return to include cobblestone, but I see no difference in the speed it mines it. Does getStrVsBlock trump the canHarvestBlock?

Link to post
Share on other sites

CanHarvest is whether the block drop itself. All blocks are breakable by any item, except bedrock.

If you return false here, the default "innefficient speed" value is assumed.

GetStrVsBlock is the speed of breaking. Return 0 would make the item not break.

Link to post
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.

Guest
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.

×
×
  • Create New...

Important Information

By using this site, you agree to our Privacy Policy.