    001package net.minecraft.command;
    002
    003import java.util.ArrayList;
    004import java.util.Collection;
    005import java.util.Iterator;
    006import java.util.List;
    007import net.minecraft.entity.player.EntityPlayerMP;
    008import net.minecraft.server.MinecraftServer;
    009
    010public abstract class CommandBase implements ICommand
    011{
    012    private static IAdminCommand theAdmin = null;
    013
    014    /**
    015     * Return the required permission level for this command.
    016     */
    017    public int getRequiredPermissionLevel()
    018    {
    019        return 4;
    020    }
    021
    022    public String getCommandUsage(ICommandSender par1ICommandSender)
    023    {
    024        return "/" + this.getCommandName();
    025    }
    026
    027    public List getCommandAliases()
    028    {
    029        return null;
    030    }
    031
    032    /**
    033     * Returns true if the given command sender is allowed to use this command.
    034     */
    035    public boolean canCommandSenderUseCommand(ICommandSender par1ICommandSender)
    036    {
    037        return par1ICommandSender.canCommandSenderUseCommand(this.getRequiredPermissionLevel(), this.getCommandName());
    038    }
    039
    040    /**
    041     * Adds the strings available in this command to the given list of tab completion options.
    042     */
    043    public List addTabCompletionOptions(ICommandSender par1ICommandSender, String[] par2ArrayOfStr)
    044    {
    045        return null;
    046    }
    047
    048    /**
    049     * Parses an int from the given string.
    050     */
    051    public static int parseInt(ICommandSender par0ICommandSender, String par1Str)
    052    {
    053        try
    054        {
    055            return Integer.parseInt(par1Str);
    056        }
    057        catch (NumberFormatException numberformatexception)
    058        {
    059            throw new NumberInvalidException("commands.generic.num.invalid", new Object[] {par1Str});
    060        }
    061    }
    062
    063    /**
    064     * Parses an int from the given sring with a specified minimum.
    065     */
    066    public static int parseIntWithMin(ICommandSender par0ICommandSender, String par1Str, int par2)
    067    {
    068        return parseIntBounded(par0ICommandSender, par1Str, par2, Integer.MAX_VALUE);
    069    }
    070
    071    /**
    072     * Parses an int from the given string within a specified bound.
    073     */
    074    public static int parseIntBounded(ICommandSender par0ICommandSender, String par1Str, int par2, int par3)
    075    {
    076        int k = parseInt(par0ICommandSender, par1Str);
    077
    078        if (k < par2)
    079        {
    080            throw new NumberInvalidException("commands.generic.num.tooSmall", new Object[] {Integer.valueOf(k), Integer.valueOf(par2)});
    081        }
    082        else if (k > par3)
    083        {
    084            throw new NumberInvalidException("commands.generic.num.tooBig", new Object[] {Integer.valueOf(k), Integer.valueOf(par3)});
    085        }
    086        else
    087        {
    088            return k;
    089        }
    090    }
    091
    092    public static double func_82363_b(ICommandSender par0ICommandSender, String par1Str)
    093    {
    094        try
    095        {
    096            return Double.parseDouble(par1Str);
    097        }
    098        catch (NumberFormatException numberformatexception)
    099        {
    100            throw new NumberInvalidException("commands.generic.double.invalid", new Object[] {par1Str});
    101        }
    102    }
    103
    104    /**
    105     * Returns the given ICommandSender as a EntityPlayer or throw an exception.
    106     */
    107    public static EntityPlayerMP getCommandSenderAsPlayer(ICommandSender par0ICommandSender)
    108    {
    109        if (par0ICommandSender instanceof EntityPlayerMP)
    110        {
    111            return (EntityPlayerMP)par0ICommandSender;
    112        }
    113        else
    114        {
    115            throw new PlayerNotFoundException("You must specify which player you wish to perform this action on.", new Object[0]);
    116        }
    117    }
    118
    119    public static EntityPlayerMP func_82359_c(ICommandSender par0ICommandSender, String par1Str)
    120    {
    121        EntityPlayerMP entityplayermp = PlayerSelector.matchOnePlayer(par0ICommandSender, par1Str);
    122
    123        if (entityplayermp != null)
    124        {
    125            return entityplayermp;
    126        }
    127        else
    128        {
    129            entityplayermp = MinecraftServer.getServer().getConfigurationManager().getPlayerForUsername(par1Str);
    130
    131            if (entityplayermp == null)
    132            {
    133                throw new PlayerNotFoundException();
    134            }
    135            else
    136            {
    137                return entityplayermp;
    138            }
    139        }
    140    }
    141
    142    public static String func_96332_d(ICommandSender par0ICommandSender, String par1Str)
    143    {
    144        EntityPlayerMP entityplayermp = PlayerSelector.matchOnePlayer(par0ICommandSender, par1Str);
    145
    146        if (entityplayermp != null)
    147        {
    148            return entityplayermp.getEntityName();
    149        }
    150        else if (PlayerSelector.hasArguments(par1Str))
    151        {
    152            throw new PlayerNotFoundException();
    153        }
    154        else
    155        {
    156            return par1Str;
    157        }
    158    }
    159
    160    public static String func_82360_a(ICommandSender par0ICommandSender, String[] par1ArrayOfStr, int par2)
    161    {
    162        return func_82361_a(par0ICommandSender, par1ArrayOfStr, par2, false);
    163    }
    164
    165    public static String func_82361_a(ICommandSender par0ICommandSender, String[] par1ArrayOfStr, int par2, boolean par3)
    166    {
    167        StringBuilder stringbuilder = new StringBuilder();
    168
    169        for (int j = par2; j < par1ArrayOfStr.length; ++j)
    170        {
    171            if (j > par2)
    172            {
    173                stringbuilder.append(" ");
    174            }
    175
    176            String s = par1ArrayOfStr[j];
    177
    178            if (par3)
    179            {
    180                String s1 = PlayerSelector.matchPlayersAsString(par0ICommandSender, s);
    181
    182                if (s1 != null)
    183                {
    184                    s = s1;
    185                }
    186                else if (PlayerSelector.hasArguments(s))
    187                {
    188                    throw new PlayerNotFoundException();
    189                }
    190            }
    191
    192            stringbuilder.append(s);
    193        }
    194
    195        return stringbuilder.toString();
    196    }
    197
    198    /**
    199     * Joins the given string array into a "x, y, and z" seperated string.
    200     */
    201    public static String joinNiceString(Object[] par0ArrayOfObj)
    202    {
    203        StringBuilder stringbuilder = new StringBuilder();
    204
    205        for (int i = 0; i < par0ArrayOfObj.length; ++i)
    206        {
    207            String s = par0ArrayOfObj[i].toString();
    208
    209            if (i > 0)
    210            {
    211                if (i == par0ArrayOfObj.length - 1)
    212                {
    213                    stringbuilder.append(" and ");
    214                }
    215                else
    216                {
    217                    stringbuilder.append(", ");
    218                }
    219            }
    220
    221            stringbuilder.append(s);
    222        }
    223
    224        return stringbuilder.toString();
    225    }
    226
    227    public static String func_96333_a(Collection par0Collection)
    228    {
    229        return joinNiceString(par0Collection.toArray(new String[0]));
    230    }
    231
    232    /**
    233     * Returns true if the given substring is exactly equal to the start of the given string (case insensitive).
    234     */
    235    public static boolean doesStringStartWith(String par0Str, String par1Str)
    236    {
    237        return par1Str.regionMatches(true, 0, par0Str, 0, par0Str.length());
    238    }
    239
    240    /**
    241     * Returns a List of strings (chosen from the given strings) which the last word in the given string array is a
    242     * beginning-match for. (Tab completion).
    243     */
    244    public static List getListOfStringsMatchingLastWord(String[] par0ArrayOfStr, String ... par1ArrayOfStr)
    245    {
    246        String s1 = par0ArrayOfStr[par0ArrayOfStr.length - 1];
    247        ArrayList arraylist = new ArrayList();
    248        String[] astring1 = par1ArrayOfStr;
    249        int i = par1ArrayOfStr.length;
    250
    251        for (int j = 0; j < i; ++j)
    252        {
    253            String s2 = astring1[j];
    254
    255            if (doesStringStartWith(s1, s2))
    256            {
    257                arraylist.add(s2);
    258            }
    259        }
    260
    261        return arraylist;
    262    }
    263
    264    /**
    265     * Returns a List of strings (chosen from the given string iterable) which the last word in the given string array
    266     * is a beginning-match for. (Tab completion).
    267     */
    268    public static List getListOfStringsFromIterableMatchingLastWord(String[] par0ArrayOfStr, Iterable par1Iterable)
    269    {
    270        String s = par0ArrayOfStr[par0ArrayOfStr.length - 1];
    271        ArrayList arraylist = new ArrayList();
    272        Iterator iterator = par1Iterable.iterator();
    273
    274        while (iterator.hasNext())
    275        {
    276            String s1 = (String)iterator.next();
    277
    278            if (doesStringStartWith(s, s1))
    279            {
    280                arraylist.add(s1);
    281            }
    282        }
    283
    284        return arraylist;
    285    }
    286
    287    /**
    288     * Return whether the specified command parameter index is a username parameter.
    289     */
    290    public boolean isUsernameIndex(String[] par1ArrayOfStr, int par2)
    291    {
    292        return false;
    293    }
    294
    295    public static void notifyAdmins(ICommandSender par0ICommandSender, String par1Str, Object ... par2ArrayOfObj)
    296    {
    297        notifyAdmins(par0ICommandSender, 0, par1Str, par2ArrayOfObj);
    298    }
    299
    300    public static void notifyAdmins(ICommandSender par0ICommandSender, int par1, String par2Str, Object ... par3ArrayOfObj)
    301    {
    302        if (theAdmin != null)
    303        {
    304            theAdmin.notifyAdmins(par0ICommandSender, par1, par2Str, par3ArrayOfObj);
    305        }
    306    }
    307
    308    /**
    309     * Sets the static IAdminCommander.
    310     */
    311    public static void setAdminCommander(IAdminCommand par0IAdminCommand)
    312    {
    313        theAdmin = par0IAdminCommand;
    314    }
    315
    316    /**
    317     * Compares the name of this command to the name of the given command.
    318     */
    319    public int compareTo(ICommand par1ICommand)
    320    {
    321        return this.getCommandName().compareTo(par1ICommand.getCommandName());
    322    }
    323
    324    public int compareTo(Object par1Obj)
    325    {
    326        return this.compareTo((ICommand)par1Obj);
    327    }
    328}