package common.item;

import common.block.Block;
import common.block.BlockSnow;
import common.entity.Entity;
import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.util.BlockPos;
import common.util.Facing;
import common.world.State;
import common.world.World;

public class ItemReed extends Item
{
    private Block block;

    public ItemReed(Block block)
    {
        this.block = block;
    }

    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)
    {
        State iblockstate = worldIn.getState(pos);
        Block block = iblockstate.getBlock();

        if (block == Blocks.snow_layer && ((Integer)iblockstate.getValue(BlockSnow.LAYERS)).intValue() < 1)
        {
            side = Facing.UP;
        }
        else if (!block.isReplaceable(worldIn, pos))
        {
            pos = pos.offset(side);
        }

        if (!playerIn.canPlayerEdit(pos, side, stack))
        {
            return false;
        }
        else if (stack.stackSize == 0)
        {
            return false;
        }
        else
        {
            if (worldIn.canBlockBePlaced(this.block, pos, false, side, (Entity)null, stack))
            {
                State iblockstate1 = this.block.onBlockPlaced(worldIn, pos, side, hitX, hitY, hitZ, 0, playerIn);

                if (worldIn.setState(pos, iblockstate1, 3))
                {
                    iblockstate1 = worldIn.getState(pos);

                    if (iblockstate1.getBlock() == this.block)
                    {
                        ItemBlock.setTileEntityNBT(worldIn, playerIn, pos, stack);
                        iblockstate1.getBlock().onBlockPlacedBy(worldIn, pos, iblockstate1, playerIn, stack);
                    }

                    worldIn.playSound(this.block.sound.getPlaceSound(), (double)((float)pos.getX() + 0.5F), (double)((float)pos.getY() + 0.5F), (double)((float)pos.getZ() + 0.5F), 1.0F);
                    --stack.stackSize;
                    return true;
                }
            }

            return false;
        }
    }
    
    public boolean isMagnetic() {
    	return this.block.isMagnetic();
    }
}
