| 1 | package net.minecraft.command;
|
| 2 |
|
| 3 | import com.google.common.collect.Lists;
|
| 4 | import com.google.common.collect.Maps;
|
| 5 | import com.google.common.collect.Sets;
|
| 6 | import java.util.List;
|
| 7 | import java.util.Map;
|
| 8 | import java.util.Set;
|
| 9 | import java.util.Map.Entry;
|
| 10 | import net.minecraft.entity.Entity;
|
| 11 | import net.minecraft.util.BlockPos;
|
| 12 | import net.minecraft.util.ChatComponentTranslation;
|
| 13 | import net.minecraft.util.EnumChatFormatting;
|
| 14 | import org.apache.logging.log4j.LogManager;
|
| 15 | import org.apache.logging.log4j.Logger;
|
| 16 |
|
| 17 | public class CommandHandler implements ICommandManager
|
| 18 | {
|
| 19 | private static final Logger logger = LogManager.getLogger();
|
| 20 | private final Map<String, ICommand> commandMap = Maps.<String, ICommand>newHashMap();
|
| 21 | private final Set<ICommand> commandSet = Sets.<ICommand>newHashSet();
|
| 22 |
|
| 23 | /**
|
| 24 | * Attempt to execute a command. This method should return the number of times that the command was executed. If the
|
| 25 | * command does not exist or if the player does not have permission, 0 will be returned. A number greater than 1 can
|
| 26 | * be returned if a player selector is used.
|
| 27 | *
|
| 28 | * @param sender The person who executed the command. This could be an EntityPlayer, RCon Source, Command Block,
|
| 29 | * etc.
|
| 30 | * @param rawCommand The raw arguments that were passed. This includes the command name.
|
| 31 | */
|
| 32 | public int executeCommand(ICommandSender sender, String rawCommand)
|
| 33 | {
|
| 34 | rawCommand = rawCommand.trim();
|
| 35 |
|
| 36 | if (rawCommand.startsWith("/"))
|
| 37 | {
|
| 38 | rawCommand = rawCommand.substring(1);
|
| 39 | }
|
| 40 |
|
| 41 | String[] astring = rawCommand.split(" ");
|
| 42 | String s = astring[0];
|
| 43 | astring = dropFirstString(astring);
|
| 44 | ICommand icommand = (ICommand)this.commandMap.get(s);
|
| 45 | int i = this.getUsernameIndex(icommand, astring);
|
| 46 | int j = 0;
|
| 47 |
|
| 48 | if (icommand == null)
|
| 49 | {
|
| 50 | ChatComponentTranslation chatcomponenttranslation = new ChatComponentTranslation("commands.generic.notFound", new Object[0]);
|
| 51 | chatcomponenttranslation.getChatStyle().setColor(EnumChatFormatting.RED);
|
| 52 | sender.addChatMessage(chatcomponenttranslation);
|
| 53 | }
|
| 54 | else if (icommand.canCommandSenderUseCommand(sender))
|
| 55 | {
|
| 56 | if (i > -1)
|
| 57 | {
|
| 58 | List<Entity> list = PlayerSelector.<Entity>matchEntities(sender, astring[i], Entity.class);
|
| 59 | String s1 = astring[i];
|
| 60 | sender.setCommandStat(CommandResultStats.Type.AFFECTED_ENTITIES, list.size());
|
| 61 |
|
| 62 | for (Entity entity : list)
|
| 63 | {
|
| 64 | astring[i] = entity.getUniqueID().toString();
|
| 65 |
|
| 66 | if (this.tryExecute(sender, astring, icommand, rawCommand))
|
| 67 | {
|
| 68 | ++j;
|
| 69 | }
|
| 70 | }
|
| 71 |
|
| 72 | astring[i] = s1;
|
| 73 | }
|
| 74 | else
|
| 75 | {
|
| 76 | sender.setCommandStat(CommandResultStats.Type.AFFECTED_ENTITIES, 1);
|
| 77 |
|
| 78 | if (this.tryExecute(sender, astring, icommand, rawCommand))
|
| 79 | {
|
| 80 | ++j;
|
| 81 | }
|
| 82 | }
|
| 83 | }
|
| 84 | else
|
| 85 | {
|
| 86 | ChatComponentTranslation chatcomponenttranslation1 = new ChatComponentTranslation("commands.generic.permission", new Object[0]);
|
| 87 | chatcomponenttranslation1.getChatStyle().setColor(EnumChatFormatting.RED);
|
| 88 | sender.addChatMessage(chatcomponenttranslation1);
|
| 89 | }
|
| 90 |
|
| 91 | sender.setCommandStat(CommandResultStats.Type.SUCCESS_COUNT, j);
|
| 92 | return j;
|
| 93 | }
|
| 94 |
|
| 95 | protected boolean tryExecute(ICommandSender sender, String[] args, ICommand command, String input)
|
| 96 | {
|
| 97 | try
|
| 98 | {
|
| 99 | command.processCommand(sender, args);
|
| 100 | return true;
|
| 101 | }
|
| 102 | catch (WrongUsageException wrongusageexception)
|
| 103 | {
|
| 104 | ChatComponentTranslation chatcomponenttranslation2 = new ChatComponentTranslation("commands.generic.usage", new Object[] {new ChatComponentTranslation(wrongusageexception.getMessage(), wrongusageexception.getErrorObjects())});
|
| 105 | chatcomponenttranslation2.getChatStyle().setColor(EnumChatFormatting.RED);
|
| 106 | sender.addChatMessage(chatcomponenttranslation2);
|
| 107 | }
|
| 108 | catch (CommandException commandexception)
|
| 109 | {
|
| 110 | ChatComponentTranslation chatcomponenttranslation1 = new ChatComponentTranslation(commandexception.getMessage(), commandexception.getErrorObjects());
|
| 111 | chatcomponenttranslation1.getChatStyle().setColor(EnumChatFormatting.RED);
|
| 112 | sender.addChatMessage(chatcomponenttranslation1);
|
| 113 | }
|
| 114 | catch (Throwable var9)
|
| 115 | {
|
| 116 | ChatComponentTranslation chatcomponenttranslation = new ChatComponentTranslation("commands.generic.exception", new Object[0]);
|
| 117 | chatcomponenttranslation.getChatStyle().setColor(EnumChatFormatting.RED);
|
| 118 | sender.addChatMessage(chatcomponenttranslation);
|
| 119 | logger.warn("Couldn\'t process command: \'" + input + "\'");
|
| 120 | }
|
| 121 |
|
| 122 | return false;
|
| 123 | }
|
| 124 |
|
| 125 | /**
|
| 126 | * adds the command and any aliases it has to the internal map of available commands
|
| 127 | */
|
| 128 | public ICommand registerCommand(ICommand command)
|
| 129 | {
|
| 130 | this.commandMap.put(command.getCommandName(), command);
|
| 131 | this.commandSet.add(command);
|
| 132 |
|
| 133 | for (String s : command.getCommandAliases())
|
| 134 | {
|
| 135 | ICommand icommand = (ICommand)this.commandMap.get(s);
|
| 136 |
|
| 137 | if (icommand == null || !icommand.getCommandName().equals(s))
|
| 138 | {
|
| 139 | this.commandMap.put(s, command);
|
| 140 | }
|
| 141 | }
|
| 142 |
|
| 143 | return command;
|
| 144 | }
|
| 145 |
|
| 146 | /**
|
| 147 | * creates a new array and sets elements 0..n-2 to be 0..n-1 of the input (n elements)
|
| 148 | */
|
| 149 | private static String[] dropFirstString(String[] input)
|
| 150 | {
|
| 151 | String[] astring = new String[input.length - 1];
|
| 152 | System.arraycopy(input, 1, astring, 0, input.length - 1);
|
| 153 | return astring;
|
| 154 | }
|
| 155 |
|
| 156 | public List<String> getTabCompletionOptions(ICommandSender sender, String input, BlockPos pos)
|
| 157 | {
|
| 158 | String[] astring = input.split(" ", -1);
|
| 159 | String s = astring[0];
|
| 160 |
|
| 161 | if (astring.length == 1)
|
| 162 | {
|
| 163 | List<String> list = Lists.<String>newArrayList();
|
| 164 |
|
| 165 | for (Entry<String, ICommand> entry : this.commandMap.entrySet())
|
| 166 | {
|
| 167 | if (CommandBase.doesStringStartWith(s, (String)entry.getKey()) && ((ICommand)entry.getValue()).canCommandSenderUseCommand(sender))
|
| 168 | {
|
| 169 | list.add(entry.getKey());
|
| 170 | }
|
| 171 | }
|
| 172 |
|
| 173 | return list;
|
| 174 | }
|
| 175 | else
|
| 176 | {
|
| 177 | if (astring.length > 1)
|
| 178 | {
|
| 179 | ICommand icommand = (ICommand)this.commandMap.get(s);
|
| 180 |
|
| 181 | if (icommand != null && icommand.canCommandSenderUseCommand(sender))
|
| 182 | {
|
| 183 | return icommand.addTabCompletionOptions(sender, dropFirstString(astring), pos);
|
| 184 | }
|
| 185 | }
|
| 186 |
|
| 187 | return null;
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | public List<ICommand> getPossibleCommands(ICommandSender sender)
|
| 192 | {
|
| 193 | List<ICommand> list = Lists.<ICommand>newArrayList();
|
| 194 |
|
| 195 | for (ICommand icommand : this.commandSet)
|
| 196 | {
|
| 197 | if (icommand.canCommandSenderUseCommand(sender))
|
| 198 | {
|
| 199 | list.add(icommand);
|
| 200 | }
|
| 201 | }
|
| 202 |
|
| 203 | return list;
|
| 204 | }
|
| 205 |
|
| 206 | public Map<String, ICommand> getCommands()
|
| 207 | {
|
| 208 | return this.commandMap;
|
| 209 | }
|
| 210 |
|
| 211 | /**
|
| 212 | * Return a command's first parameter index containing a valid username.
|
| 213 | */
|
| 214 | private int getUsernameIndex(ICommand command, String[] args)
|
| 215 | {
|
| 216 | if (command == null)
|
| 217 | {
|
| 218 | return -1;
|
| 219 | }
|
| 220 | else
|
| 221 | {
|
| 222 | for (int i = 0; i < args.length; ++i)
|
| 223 | {
|
| 224 | if (command.isUsernameIndex(args, i) && PlayerSelector.matchesMultiplePlayers(args[i]))
|
| 225 | {
|
| 226 | return i;
|
| 227 | }
|
| 228 | }
|
| 229 |
|
| 230 | return -1;
|
| 231 | }
|
| 232 | }
|
| 233 | }
|
| 234 |
|