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