    001    package net.minecraft.item;
    002
    003    import java.util.ArrayList;
    004    import java.util.Random;
    005
    006    import net.minecraft.block.Block;
    007    import net.minecraft.creativetab.CreativeTabs;
    008    import net.minecraft.enchantment.Enchantment;
    009    import net.minecraft.enchantment.EnchantmentHelper;
    010    import net.minecraft.entity.EntityLiving;
    011    import net.minecraft.entity.item.EntityItem;
    012    import net.minecraft.entity.player.EntityPlayer;
    013    import net.minecraft.stats.StatList;
    014    import net.minecraft.world.World;
    015
    016    import net.minecraftforge.common.IShearable;
    017
    018    public class ItemShears extends Item
    019    {
    020        public ItemShears(int par1)
    021        {
    022            super(par1);
    023            this.setMaxStackSize(1);
    024            this.setMaxDamage(238);
    025            this.setCreativeTab(CreativeTabs.tabTools);
    026        }
    027
    028        public boolean onBlockDestroyed(ItemStack par1ItemStack, World par2World, int par3, int par4, int par5, int par6, EntityLiving par7EntityLiving)
    029        {
    030            if (par3 != Block.leaves.blockID && par3 != Block.web.blockID && par3 != Block.tallGrass.blockID && par3 != Block.vine.blockID && par3 != Block.tripWire.blockID && !(Block.blocksList[par3] instanceof IShearable))
    031            {
    032                return super.onBlockDestroyed(par1ItemStack, par2World, par3, par4, par5, par6, par7EntityLiving);
    033            }
    034            else
    035            {
    036                return true;
    037            }
    038        }
    039
    040        /**
    041         * Returns if the item (tool) can harvest results from the block type.
    042         */
    043        public boolean canHarvestBlock(Block par1Block)
    044        {
    045            return par1Block.blockID == Block.web.blockID || par1Block.blockID == Block.redstoneWire.blockID || par1Block.blockID == Block.tripWire.blockID;
    046        }
    047
    048        /**
    049         * Returns the strength of the stack against a given block. 1.0F base, (Quality+1)*2 if correct blocktype, 1.5F if
    050         * sword
    051         */
    052        public float getStrVsBlock(ItemStack par1ItemStack, Block par2Block)
    053        {
    054            return par2Block.blockID != Block.web.blockID && par2Block.blockID != Block.leaves.blockID ? (par2Block.blockID == Block.cloth.blockID ? 5.0F : super.getStrVsBlock(par1ItemStack, par2Block)) : 15.0F;
    055        }
    056
    057        @Override
    058        public boolean itemInteractionForEntity(ItemStack itemstack, EntityLiving entity)
    059        {
    060            if (entity.worldObj.isRemote)
    061            {
    062                return false;
    063            }
    064            if (entity instanceof IShearable)
    065            {
    066                IShearable target = (IShearable)entity;
    067                if (target.isShearable(itemstack, entity.worldObj, (int)entity.posX, (int)entity.posY, (int)entity.posZ))
    068                {
    069                    ArrayList<ItemStack> drops = target.onSheared(itemstack, entity.worldObj, (int)entity.posX, (int)entity.posY, (int)entity.posZ,
    070                            EnchantmentHelper.getEnchantmentLevel(Enchantment.fortune.effectId, itemstack));
    071
    072                    Random rand = new Random();
    073                    for(ItemStack stack : drops)
    074                    {
    075                        EntityItem ent = entity.entityDropItem(stack, 1.0F);
    076                        ent.motionY += rand.nextFloat() * 0.05F;
    077                        ent.motionX += (rand.nextFloat() - rand.nextFloat()) * 0.1F;
    078                        ent.motionZ += (rand.nextFloat() - rand.nextFloat()) * 0.1F;
    079                    }
    080                    itemstack.damageItem(1, entity);
    081                }
    082                return true;
    083            }
    084            return false;
    085        }
    086
    087        @Override
    088        public boolean onBlockStartBreak(ItemStack itemstack, int x, int y, int z, EntityPlayer player)
    089        {
    090            if (player.worldObj.isRemote)
    091            {
    092                return false;
    093            }
    094            int id = player.worldObj.getBlockId(x, y, z);
    095            if (Block.blocksList[id] instanceof IShearable)
    096            {
    097                IShearable target = (IShearable)Block.blocksList[id];
    098                if (target.isShearable(itemstack, player.worldObj, x, y, z))
    099                {
    100                    ArrayList<ItemStack> drops = target.onSheared(itemstack, player.worldObj, x, y, z,
    101                            EnchantmentHelper.getEnchantmentLevel(Enchantment.fortune.effectId, itemstack));
    102                    Random rand = new Random();
    103
    104                    for(ItemStack stack : drops)
    105                    {
    106                        float f = 0.7F;
    107                        double d  = (double)(rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
    108                        double d1 = (double)(rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
    109                        double d2 = (double)(rand.nextFloat() * f) + (double)(1.0F - f) * 0.5D;
    110                        EntityItem entityitem = new EntityItem(player.worldObj, (double)x + d, (double)y + d1, (double)z + d2, stack);
    111                        entityitem.delayBeforeCanPickup = 10;
    112                        player.worldObj.spawnEntityInWorld(entityitem);
    113                    }
    114
    115                    itemstack.damageItem(1, player);
    116                    player.addStat(StatList.mineBlockStatArray[id], 1);
    117                }
    118            }
    119            return false;
    120        }
    121    }