package net.minecraft.command;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.event.ClickEvent;
import net.minecraft.server.MinecraftServer;
import net.minecraft.util.ChatComponentTranslation;
import net.minecraft.util.EnumChatFormatting;
import net.minecraft.util.MathHelper;

public class CommandHelp extends CommandBase
{
	private static final String __OBFID = "CL_00000529";

	public String getCommandName()
	{
		return "help";
	}

	public int getRequiredPermissionLevel()
	{
		return 0;
	}

	public String getCommandUsage(ICommandSender par1ICommandSender)
	{
		return "commands.help.usage";
	}

	public List getCommandAliases()
	{
		return Arrays.asList(new String[] {"?"});
	}

	public void processCommand(ICommandSender par1ICommandSender, String[] par2ArrayOfStr)
	{
		List list = this.getSortedPossibleCommands(par1ICommandSender);
		byte b0 = 7;
		int i = (list.size() - 1) / b0;
		boolean flag = false;
		int k;

		try
		{
			k = par2ArrayOfStr.length == 0 ? 0 : parseIntBounded(par1ICommandSender, par2ArrayOfStr[0], 1, i + 1) - 1;
		}
		catch (NumberInvalidException numberinvalidexception)
		{
			Map map = this.getCommands();
			ICommand icommand = (ICommand)map.get(par2ArrayOfStr[0]);

			if (icommand != null)
			{
				throw new WrongUsageException(icommand.getCommandUsage(par1ICommandSender), new Object[0]);
			}

			if (MathHelper.parseIntWithDefault(par2ArrayOfStr[0], -1) != -1)
			{
				throw numberinvalidexception;
			}

			throw new CommandNotFoundException();
		}

		int j = Math.min((k + 1) * b0, list.size());
		ChatComponentTranslation chatcomponenttranslation1 = new ChatComponentTranslation("commands.help.header", new Object[] {Integer.valueOf(k + 1), Integer.valueOf(i + 1)});
		chatcomponenttranslation1.getChatStyle().setColor(EnumChatFormatting.DARK_GREEN);
		par1ICommandSender.addChatMessage(chatcomponenttranslation1);

		for (int l = k * b0; l < j; ++l)
		{
			ICommand icommand1 = (ICommand)list.get(l);
			ChatComponentTranslation chatcomponenttranslation = new ChatComponentTranslation(icommand1.getCommandUsage(par1ICommandSender), new Object[0]);
			chatcomponenttranslation.getChatStyle().setChatClickEvent(new ClickEvent(ClickEvent.Action.SUGGEST_COMMAND, "/" + icommand1.getCommandName() + " "));
			par1ICommandSender.addChatMessage(chatcomponenttranslation);
		}

		if (k == 0 && par1ICommandSender instanceof EntityPlayer)
		{
			ChatComponentTranslation chatcomponenttranslation2 = new ChatComponentTranslation("commands.help.footer", new Object[0]);
			chatcomponenttranslation2.getChatStyle().setColor(EnumChatFormatting.GREEN);
			par1ICommandSender.addChatMessage(chatcomponenttranslation2);
		}
	}

	protected List getSortedPossibleCommands(ICommandSender par1ICommandSender)
	{
		List list = MinecraftServer.getServer().getCommandManager().getPossibleCommands(par1ICommandSender);
		Collections.sort(list);
		return list;
	}

	protected Map getCommands()
	{
		return MinecraftServer.getServer().getCommandManager().getCommands();
	}
}