package net.minecraft.item;

import java.util.Iterator;
import java.util.List;
import net.minecraft.block.Block;
import net.minecraft.creativetab.CreativeTabs;
import net.minecraft.entity.EntityLeashKnot;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.world.World;

public class ItemLead extends Item
{
	private static final String __OBFID = "CL_00000045";

	public ItemLead()
	{
		this.setCreativeTab(CreativeTabs.tabTools);
	}

	public boolean onItemUse(ItemStack par1ItemStack, EntityPlayer par2EntityPlayer, World par3World, int par4, int par5, int par6, int par7, float par8, float par9, float par10)
	{
		Block block = par3World.getBlock(par4, par5, par6);

		if (block.getRenderType() == 11)
		{
			if (par3World.isRemote)
			{
				return true;
			}
			else
			{
				func_150909_a(par2EntityPlayer, par3World, par4, par5, par6);
				return true;
			}
		}
		else
		{
			return false;
		}
	}

	public static boolean func_150909_a(EntityPlayer p_150909_0_, World p_150909_1_, int p_150909_2_, int p_150909_3_, int p_150909_4_)
	{
		EntityLeashKnot entityleashknot = EntityLeashKnot.getKnotForBlock(p_150909_1_, p_150909_2_, p_150909_3_, p_150909_4_);
		boolean flag = false;
		double d0 = 7.0D;
		List list = p_150909_1_.getEntitiesWithinAABB(EntityLiving.class, AxisAlignedBB.getAABBPool().getAABB((double)p_150909_2_ - d0, (double)p_150909_3_ - d0, (double)p_150909_4_ - d0, (double)p_150909_2_ + d0, (double)p_150909_3_ + d0, (double)p_150909_4_ + d0));

		if (list != null)
		{
			Iterator iterator = list.iterator();

			while (iterator.hasNext())
			{
				EntityLiving entityliving = (EntityLiving)iterator.next();

				if (entityliving.getLeashed() && entityliving.getLeashedToEntity() == p_150909_0_)
				{
					if (entityleashknot == null)
					{
						entityleashknot = EntityLeashKnot.func_110129_a(p_150909_1_, p_150909_2_, p_150909_3_, p_150909_4_);
					}

					entityliving.setLeashedToEntity(entityleashknot, true);
					flag = true;
				}
			}
		}

		return flag;
	}
}