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