    001package net.minecraft.command;
    002
    003import java.util.Arrays;
    004import java.util.Collections;
    005import java.util.List;
    006import java.util.Map;
    007import net.minecraft.entity.player.EntityPlayer;
    008import net.minecraft.server.MinecraftServer;
    009import net.minecraft.util.EnumChatFormatting;
    010
    011public class CommandHelp extends CommandBase
    012{
    013    public String getCommandName()
    014    {
    015        return "help";
    016    }
    017
    018    /**
    019     * Return the required permission level for this command.
    020     */
    021    public int getRequiredPermissionLevel()
    022    {
    023        return 0;
    024    }
    025
    026    public String getCommandUsage(ICommandSender par1ICommandSender)
    027    {
    028        return par1ICommandSender.translateString("commands.help.usage", new Object[0]);
    029    }
    030
    031    public List getCommandAliases()
    032    {
    033        return Arrays.asList(new String[] {"?"});
    034    }
    035
    036    public void processCommand(ICommandSender par1ICommandSender, String[] par2ArrayOfStr)
    037    {
    038        List list = this.getSortedPossibleCommands(par1ICommandSender);
    039        byte b0 = 7;
    040        int i = (list.size() - 1) / b0;
    041        boolean flag = false;
    042        ICommand icommand;
    043        int j;
    044
    045        try
    046        {
    047            j = par2ArrayOfStr.length == 0 ? 0 : parseIntBounded(par1ICommandSender, par2ArrayOfStr[0], 1, i + 1) - 1;
    048        }
    049        catch (NumberInvalidException numberinvalidexception)
    050        {
    051            Map map = this.getCommands();
    052            icommand = (ICommand)map.get(par2ArrayOfStr[0]);
    053
    054            if (icommand != null)
    055            {
    056                throw new WrongUsageException(icommand.getCommandUsage(par1ICommandSender), new Object[0]);
    057            }
    058
    059            throw new CommandNotFoundException();
    060        }
    061
    062        int k = Math.min((j + 1) * b0, list.size());
    063        par1ICommandSender.sendChatToPlayer(EnumChatFormatting.DARK_GREEN + par1ICommandSender.translateString("commands.help.header", new Object[] {Integer.valueOf(j + 1), Integer.valueOf(i + 1)}));
    064
    065        for (int l = j * b0; l < k; ++l)
    066        {
    067            icommand = (ICommand)list.get(l);
    068            par1ICommandSender.sendChatToPlayer(icommand.getCommandUsage(par1ICommandSender));
    069        }
    070
    071        if (j == 0 && par1ICommandSender instanceof EntityPlayer)
    072        {
    073            par1ICommandSender.sendChatToPlayer(EnumChatFormatting.GREEN + par1ICommandSender.translateString("commands.help.footer", new Object[0]));
    074        }
    075    }
    076
    077    /**
    078     * Returns a sorted list of all possible commands for the given ICommandSender.
    079     */
    080    protected List getSortedPossibleCommands(ICommandSender par1ICommandSender)
    081    {
    082        List list = MinecraftServer.getServer().getCommandManager().getPossibleCommands(par1ICommandSender);
    083        Collections.sort(list);
    084        return list;
    085    }
    086
    087    protected Map getCommands()
    088    {
    089        return MinecraftServer.getServer().getCommandManager().getCommands();
    090    }
    091}