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