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