package common.item.block;

import common.block.Block;
import common.block.tech.BlockRedstoneWire;
import common.entity.Entity;
import common.entity.npc.EntityNPC;
import common.item.CheatTab;
import common.item.Item;
import common.item.ItemStack;
import common.util.BlockPos;
import common.util.Facing;
import common.world.World;

public class ItemRedstone extends Item
{
	private final BlockRedstoneWire block;
	
    public ItemRedstone(BlockRedstoneWire block)
    {
    	this.block = block;
        this.setTab(CheatTab.TECHNOLOGY);
    }

    public Block getBlock()
    {
        return this.block;
    }

    /**
     * Called when a Block is right-clicked with this Item
     */
    public boolean onItemUse(ItemStack stack, EntityNPC playerIn, World worldIn, BlockPos pos, Facing side, float hitX, float hitY, float hitZ)
    {
        boolean flag = worldIn.getState(pos).getBlock().isReplaceable(worldIn, pos);
        BlockPos blockpos = flag ? pos : pos.offset(side);

        if (!playerIn.canPlayerEdit(blockpos, side, stack))
        {
            return false;
        }
        else
        {
            Block block = worldIn.getState(blockpos).getBlock();

            if (!worldIn.canBlockBePlaced(block, blockpos, false, side, (Entity)null, stack))
            {
                return false;
            }
            else if (this.block.canPlaceBlockAt(worldIn, blockpos))
            {
                --stack.size;
                worldIn.setState(blockpos, this.block.getState());
                return true;
            }
            else
            {
                return false;
            }
        }
    }
    
    public boolean isMagnetic() {
    	return true;
    }
}
