package com.forgeessentials.teleport;

import java.util.List;

import com.forgeessentials.api.UserIdent;
import com.forgeessentials.commons.selections.WarpPoint;
import com.forgeessentials.core.commands.ForgeEssentialsCommandBase;
import com.forgeessentials.core.misc.TeleportHelper;
import com.forgeessentials.core.misc.TranslatedCommandException;

import net.minecraft.command.CommandException;
import net.minecraft.command.ICommandSender;
import net.minecraft.entity.player.EntityPlayerMP;
import net.minecraft.init.Blocks;
import net.minecraft.util.BlockPos;
import net.minecraftforge.fml.common.FMLCommonHandler;
import net.minecraftforge.permission.PermissionLevel;
import net.minecraftforge.permission.PermissionManager;

public class CommandTop extends ForgeEssentialsCommandBase {

	@Override
	public List<String> addTabCompletionOptions(ICommandSender sender, String[] args, BlockPos pos) {
		if (args.length == 1) {
			return getListOfStringsMatchingLastWord(args,
					FMLCommonHandler.instance().getMinecraftServerInstance().getAllUsernames());
		} else {
			return null;
		}
	}

	@Override
	public boolean canConsoleUseCommand() {
		return true;
	}

	@Override
	public String getCommandName() {
		return "fetop";
	}

	@Override
	public String getCommandUsage(ICommandSender sender) {
		return "/top <player> Teleport you or another player to the top of the world.";
	}

	@Override
	public String[] getDefaultAliases() {
		return new String[] { "top" };
	}

	@Override
	public PermissionLevel getPermissionLevel() {
		return PermissionLevel.TRUE;
	}

	@Override
	public String getPermissionNode() {
		return TeleportModule.PERM_TOP;
	}

	@Override
	public void processCommandConsole(ICommandSender sender, String[] args) throws CommandException {
		if (args.length == 1) {
			EntityPlayerMP player = UserIdent.getPlayerByMatchOrUsername(sender, args[0]);
			if (player != null) {
				top(player);
			} else {
				throw new TranslatedCommandException("Player %s does not exist, or is not online.", args[0]);
			}
		} else {
			throw new TranslatedCommandException("Improper syntax. Please try this instead: <player>");
		}
	}

	@Override
	public void processCommandPlayer(EntityPlayerMP sender, String[] args) throws CommandException {
		if (args.length == 0) {
			top(sender);
		} else if ((args.length == 1) && PermissionManager.checkPermission(sender, TeleportModule.PERM_TOP_OTHERS)) {
			EntityPlayerMP player = UserIdent.getPlayerByMatchOrUsername(sender, args[0]);
			if (player != null) {
				top(player);
			} else {
				throw new TranslatedCommandException("Player %s does not exist, or is not online.", args[0]);
			}
		} else {
			throw new TranslatedCommandException("Improper syntax. Please try this instead: <player>");
		}
	}

	public void top(EntityPlayerMP player) throws CommandException {
		WarpPoint point = new WarpPoint(player);
		point.setY(player.worldObj.getActualHeight());
		while (player.worldObj.getBlockState(point.getBlockPos()).getBlock() == Blocks.air) {
			point.setY(point.getY() - 1);
		}
		point.setY(point.getY() + 1);
		TeleportHelper.teleport(player, point);
	}

}

@@ -2915,16 +2915,36 @@
 %09while (
+point.getY() %3E 0 && 
 player.w

fix bug when theres all air blocks