package common.item.block;

import common.block.Block;
import common.block.natural.BlockSnow;
import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.item.Item;
import common.item.ItemStack;
import common.util.BlockPos;
import common.util.Facing;
import common.world.State;
import common.world.World;

public class ItemSmallBlock extends Item {
	private Block block;

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

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

	public boolean onItemUse(ItemStack stack, EntityNPC player, World world, BlockPos pos, Facing side, float hitX, float hitY, float hitZ) {
		State state = world.getState(pos);
		Block block = state.getBlock();

		if(block == Blocks.snow_layer && state.getValue(BlockSnow.LAYERS).intValue() < 1)
			side = Facing.UP;
		else if(!block.isReplaceable(world, pos))
			pos = pos.offset(side);

		if(!player.canPlayerEdit(pos, side, stack) || stack.size == 0)
			return false;
		if(world.canBlockBePlaced(this.block, pos, false, side, null, stack)) {
			State newState = this.block.onBlockPlaced(world, pos, side, hitX, hitY, hitZ, player);

			if(world.setState(pos, newState, 3)) {
				newState = world.getState(pos);

				if(newState.getBlock() == this.block) {
					ItemBlock.setTileEntityNBT(world, player, pos, stack);
					newState.getBlock().onBlockPlacedBy(world, pos, newState, player, stack);
				}

				world.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.size;
				return true;
			}
		}
		return false;
	}

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