    001    package net.minecraft.src;
    002
    003    import java.util.ArrayList;
    004    import java.util.HashMap;
    005    import java.util.HashSet;
    006    import java.util.Iterator;
    007    import java.util.List;
    008    import java.util.Map;
    009    import java.util.Set;
    010    import java.util.Map.Entry;
    011
    012    import net.minecraftforge.common.MinecraftForge;
    013    import net.minecraftforge.event.CommandEvent;
    014
    015    public class CommandHandler implements ICommandManager
    016    {
    017        /** Map of Strings to the ICommand objects they represent */
    018        private final Map commandMap = new HashMap();
    019
    020        /** The set of ICommand objects currently loaded. */
    021        private final Set commandSet = new HashSet();
    022
    023        public void executeCommand(ICommandSender par1ICommandSender, String par2Str)
    024        {
    025            if (par2Str.startsWith("/"))
    026            {
    027                par2Str = par2Str.substring(1);
    028            }
    029
    030            String[] var3 = par2Str.split(" ");
    031            String var4 = var3[0];
    032            var3 = dropFirstString(var3);
    033            ICommand var5 = (ICommand)this.commandMap.get(var4);
    034            int var6 = this.func_82370_a(var5, var3);
    035
    036            try
    037            {
    038                if (var5 == null)
    039                {
    040                    throw new CommandNotFoundException();
    041                }
    042
    043                if (var5.canCommandSenderUseCommand(par1ICommandSender))
    044                {
    045                    CommandEvent event = new CommandEvent(var5, par1ICommandSender, var3);
    046                    if (MinecraftForge.EVENT_BUS.post(event))
    047                    {
    048                        if (event.exception != null)
    049                        {
    050                            throw event.exception;
    051                        }
    052                        return;
    053                    }
    054
    055                    if (var6 > -1)
    056                    {
    057                        EntityPlayerMP[] var7 = PlayerSelector.func_82380_c(par1ICommandSender, var3[var6]);
    058                        String var8 = var3[var6];
    059                        EntityPlayerMP[] var9 = var7;
    060                        int var10 = var7.length;
    061
    062                        for (int var11 = 0; var11 < var10; ++var11)
    063                        {
    064                            EntityPlayerMP var12 = var9[var11];
    065                            var3[var6] = var12.getEntityName();
    066
    067                            try
    068                            {
    069                                var5.processCommand(par1ICommandSender, var3);
    070                            }
    071                            catch (PlayerNotFoundException var14)
    072                            {
    073                                par1ICommandSender.sendChatToPlayer("\u00a7c" + par1ICommandSender.translateString(var14.getMessage(), var14.getErrorOjbects()));
    074                            }
    075                        }
    076
    077                        var3[var6] = var8;
    078                    }
    079                    else
    080                    {
    081                        var5.processCommand(par1ICommandSender, var3);
    082                    }
    083                }
    084                else
    085                {
    086                    par1ICommandSender.sendChatToPlayer("\u00a7cYou do not have permission to use this command.");
    087                }
    088            }
    089            catch (WrongUsageException var15)
    090            {
    091                par1ICommandSender.sendChatToPlayer("\u00a7c" + par1ICommandSender.translateString("commands.generic.usage", new Object[] {par1ICommandSender.translateString(var15.getMessage(), var15.getErrorOjbects())}));
    092            }
    093            catch (CommandException var16)
    094            {
    095                par1ICommandSender.sendChatToPlayer("\u00a7c" + par1ICommandSender.translateString(var16.getMessage(), var16.getErrorOjbects()));
    096            }
    097            catch (Throwable var17)
    098            {
    099                par1ICommandSender.sendChatToPlayer("\u00a7c" + par1ICommandSender.translateString("commands.generic.exception", new Object[0]));
    100                var17.printStackTrace();
    101            }
    102        }
    103
    104        /**
    105         * adds the command and any aliases it has to the internal map of available commands
    106         */
    107        public ICommand registerCommand(ICommand par1ICommand)
    108        {
    109            List var2 = par1ICommand.getCommandAliases();
    110            this.commandMap.put(par1ICommand.getCommandName(), par1ICommand);
    111            this.commandSet.add(par1ICommand);
    112
    113            if (var2 != null)
    114            {
    115                Iterator var3 = var2.iterator();
    116
    117                while (var3.hasNext())
    118                {
    119                    String var4 = (String)var3.next();
    120                    ICommand var5 = (ICommand)this.commandMap.get(var4);
    121
    122                    if (var5 == null || !var5.getCommandName().equals(var4))
    123                    {
    124                        this.commandMap.put(var4, par1ICommand);
    125                    }
    126                }
    127            }
    128
    129            return par1ICommand;
    130        }
    131
    132        /**
    133         * creates a new array and sets elements 0..n-2 to be 0..n-1 of the input (n elements)
    134         */
    135        private static String[] dropFirstString(String[] par0ArrayOfStr)
    136        {
    137            String[] var1 = new String[par0ArrayOfStr.length - 1];
    138
    139            for (int var2 = 1; var2 < par0ArrayOfStr.length; ++var2)
    140            {
    141                var1[var2 - 1] = par0ArrayOfStr[var2];
    142            }
    143
    144            return var1;
    145        }
    146
    147        /**
    148         * Performs a "begins with" string match on each token in par2. Only returns commands that par1 can use.
    149         */
    150        public List getPossibleCommands(ICommandSender par1ICommandSender, String par2Str)
    151        {
    152            String[] var3 = par2Str.split(" ", -1);
    153            String var4 = var3[0];
    154
    155            if (var3.length == 1)
    156            {
    157                ArrayList var8 = new ArrayList();
    158                Iterator var6 = this.commandMap.entrySet().iterator();
    159
    160                while (var6.hasNext())
    161                {
    162                    Entry var7 = (Entry)var6.next();
    163
    164                    if (CommandBase.doesStringStartWith(var4, (String)var7.getKey()) && ((ICommand)var7.getValue()).canCommandSenderUseCommand(par1ICommandSender))
    165                    {
    166                        var8.add(var7.getKey());
    167                    }
    168                }
    169
    170                return var8;
    171            }
    172            else
    173            {
    174                if (var3.length > 1)
    175                {
    176                    ICommand var5 = (ICommand)this.commandMap.get(var4);
    177
    178                    if (var5 != null)
    179                    {
    180                        return var5.addTabCompletionOptions(par1ICommandSender, dropFirstString(var3));
    181                    }
    182                }
    183
    184                return null;
    185            }
    186        }
    187
    188        /**
    189         * returns all commands that the commandSender can use
    190         */
    191        public List getPossibleCommands(ICommandSender par1ICommandSender)
    192        {
    193            ArrayList var2 = new ArrayList();
    194            Iterator var3 = this.commandSet.iterator();
    195
    196            while (var3.hasNext())
    197            {
    198                ICommand var4 = (ICommand)var3.next();
    199
    200                if (var4.canCommandSenderUseCommand(par1ICommandSender))
    201                {
    202                    var2.add(var4);
    203                }
    204            }
    205
    206            return var2;
    207        }
    208
    209        /**
    210         * returns a map of string to commads. All commands are returned, not just ones which someone has permission to use.
    211         */
    212        public Map getCommands()
    213        {
    214            return this.commandMap;
    215        }
    216
    217        private int func_82370_a(ICommand par1ICommand, String[] par2ArrayOfStr)
    218        {
    219            if (par1ICommand == null)
    220            {
    221                return -1;
    222            }
    223            else
    224            {
    225                for (int var3 = 0; var3 < par2ArrayOfStr.length; ++var3)
    226                {
    227                    if (par1ICommand.func_82358_a(var3) && PlayerSelector.func_82377_a(par2ArrayOfStr[var3]))
    228                    {
    229                        return var3;
    230                    }
    231                }
    232
    233                return -1;
    234            }
    235        }
    236    }