    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
    035            try
    036            {
    037                if (var5 == null)
    038                {
    039                    throw new CommandNotFoundException();
    040                }
    041
    042                if (var5.canCommandSenderUseCommand(par1ICommandSender))
    043                {
    044                    CommandEvent event = new CommandEvent(var5, par1ICommandSender, var3);
    045                    if (!MinecraftForge.EVENT_BUS.post(event))
    046                    {
    047                        var5.processCommand(par1ICommandSender, event.parameters);
    048                    }
    049                    else
    050                    {
    051                        if (event.exception != null)
    052                        {
    053                            throw event.exception;
    054                        }
    055                    }
    056                }
    057                else
    058                {
    059                    par1ICommandSender.sendChatToPlayer("\u00a7cYou do not have permission to use this command.");
    060                }
    061            }
    062            catch (WrongUsageException var7)
    063            {
    064                par1ICommandSender.sendChatToPlayer("\u00a7c" + par1ICommandSender.translateString("commands.generic.usage", new Object[] {par1ICommandSender.translateString(var7.getMessage(), var7.getErrorOjbects())}));
    065            }
    066            catch (CommandException var8)
    067            {
    068                par1ICommandSender.sendChatToPlayer("\u00a7c" + par1ICommandSender.translateString(var8.getMessage(), var8.getErrorOjbects()));
    069            }
    070            catch (Throwable var9)
    071            {
    072                par1ICommandSender.sendChatToPlayer("\u00a7c" + par1ICommandSender.translateString("commands.generic.exception", new Object[0]));
    073                var9.printStackTrace();
    074            }
    075        }
    076
    077        /**
    078         * adds the command and any aliases it has to the internal map of available commands
    079         */
    080        public ICommand registerCommand(ICommand par1ICommand)
    081        {
    082            List var2 = par1ICommand.getCommandAliases();
    083            this.commandMap.put(par1ICommand.getCommandName(), par1ICommand);
    084            this.commandSet.add(par1ICommand);
    085
    086            if (var2 != null)
    087            {
    088                Iterator var3 = var2.iterator();
    089
    090                while (var3.hasNext())
    091                {
    092                    String var4 = (String)var3.next();
    093                    ICommand var5 = (ICommand)this.commandMap.get(var4);
    094
    095                    if (var5 == null || !var5.getCommandName().equals(var4))
    096                    {
    097                        this.commandMap.put(var4, par1ICommand);
    098                    }
    099                }
    100            }
    101
    102            return par1ICommand;
    103        }
    104
    105        /**
    106         * creates a new array and sets elements 0..n-2 to be 0..n-1 of the input (n elements)
    107         */
    108        private static String[] dropFirstString(String[] par0ArrayOfStr)
    109        {
    110            String[] var1 = new String[par0ArrayOfStr.length - 1];
    111
    112            for (int var2 = 1; var2 < par0ArrayOfStr.length; ++var2)
    113            {
    114                var1[var2 - 1] = par0ArrayOfStr[var2];
    115            }
    116
    117            return var1;
    118        }
    119
    120        /**
    121         * Performs a "begins with" string match on each token in par2. Only returns commands that par1 can use.
    122         */
    123        public List getPossibleCommands(ICommandSender par1ICommandSender, String par2Str)
    124        {
    125            String[] var3 = par2Str.split(" ", -1);
    126            String var4 = var3[0];
    127
    128            if (var3.length == 1)
    129            {
    130                ArrayList var8 = new ArrayList();
    131                Iterator var6 = this.commandMap.entrySet().iterator();
    132
    133                while (var6.hasNext())
    134                {
    135                    Entry var7 = (Entry)var6.next();
    136
    137                    if (CommandBase.doesStringStartWith(var4, (String)var7.getKey()) && ((ICommand)var7.getValue()).canCommandSenderUseCommand(par1ICommandSender))
    138                    {
    139                        var8.add(var7.getKey());
    140                    }
    141                }
    142
    143                return var8;
    144            }
    145            else
    146            {
    147                if (var3.length > 1)
    148                {
    149                    ICommand var5 = (ICommand)this.commandMap.get(var4);
    150
    151                    if (var5 != null)
    152                    {
    153                        return var5.addTabCompletionOptions(par1ICommandSender, dropFirstString(var3));
    154                    }
    155                }
    156
    157                return null;
    158            }
    159        }
    160
    161        /**
    162         * returns all commands that the commandSender can use
    163         */
    164        public List getPossibleCommands(ICommandSender par1ICommandSender)
    165        {
    166            ArrayList var2 = new ArrayList();
    167            Iterator var3 = this.commandSet.iterator();
    168
    169            while (var3.hasNext())
    170            {
    171                ICommand var4 = (ICommand)var3.next();
    172
    173                if (var4.canCommandSenderUseCommand(par1ICommandSender))
    174                {
    175                    var2.add(var4);
    176                }
    177            }
    178
    179            return var2;
    180        }
    181
    182        /**
    183         * returns a map of string to commads. All commands are returned, not just ones which someone has permission to use.
    184         */
    185        public Map getCommands()
    186        {
    187            return this.commandMap;
    188        }
    189    }