Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
org.bukkit.command.SimpleCommandMap Class Reference
Inheritance diagram for org.bukkit.command.SimpleCommandMap:
org.bukkit.command.CommandMap

Public Member Functions

 SimpleCommandMap (final Server server)
 
void setFallbackCommands ()
 
void registerAll (String fallbackPrefix, List< Command > commands)
 
boolean register (String fallbackPrefix, Command command)
 
boolean register (String label, String fallbackPrefix, Command command)
 
boolean dispatch (CommandSender sender, String commandLine) throws CommandException
 
synchronized void clearCommands ()
 
Command getCommand (String name)
 
List< String > tabComplete (CommandSender sender, String cmdLine)
 
Collection< CommandgetCommands ()
 
void registerServerAliases ()
 

Protected Attributes

final Map< String, CommandknownCommands = new HashMap<String, Command>()
 

Detailed Description

Definition at line 20 of file SimpleCommandMap.java.

Member Function Documentation

synchronized void org.bukkit.command.SimpleCommandMap.clearCommands ( )

Clears all registered commands.

Implements org.bukkit.command.CommandMap.

Definition at line 191 of file SimpleCommandMap.java.

191  {
192  for (Map.Entry<String, Command> entry : knownCommands.entrySet()) {
193  entry.getValue().unregister(this);
194  }
195  knownCommands.clear();
196  setDefaultCommands();
197  }
boolean org.bukkit.command.SimpleCommandMap.dispatch ( CommandSender  sender,
String  commandLine 
) throws CommandException

Looks for the requested command and executes it if found.

Parameters
senderThe command's sender
cmdLinecommand + arguments. Example: "/test abc 123"
Returns
returns false if no target is found, true otherwise.
Exceptions
CommandExceptionThrown when the executor for the given command fails with an unhandled exception

Implements org.bukkit.command.CommandMap.

Definition at line 164 of file SimpleCommandMap.java.

References org.bukkit.command.Command.execute(), and org.bukkit.command.SimpleCommandMap.getCommand().

164  {
165  String[] args = PATTERN_ON_SPACE.split(commandLine);
166 
167  if (args.length == 0) {
168  return false;
169  }
170 
171  String sentCommandLabel = args[0].toLowerCase();
172  Command target = getCommand(sentCommandLabel);
173 
174  if (target == null) {
175  return false;
176  }
177 
178  try {
179  // Note: we don't return the result of target.execute as thats success / failure, we return handled (true) or not handled (false)
180  target.execute(sender, sentCommandLabel, Arrays_copyOfRange(args, 1, args.length));
181  } catch (CommandException ex) {
182  throw ex;
183  } catch (Throwable ex) {
184  throw new CommandException("Unhandled exception executing '" + commandLine + "' in " + target, ex);
185  }
186 
187  // return true as command was handled
188  return true;
189  }
abstract boolean execute(CommandSender sender, String commandLabel, String[] args)
Command org.bukkit.command.SimpleCommandMap.getCommand ( String  name)

Gets the command registered to the specified name

Parameters
nameName of the command to retrieve
Returns
Command with the specified name or null if a command with that label doesn't exist

Implements org.bukkit.command.CommandMap.

Definition at line 199 of file SimpleCommandMap.java.

Referenced by org.bukkit.command.SimpleCommandMap.dispatch(), and org.bukkit.command.SimpleCommandMap.tabComplete().

199  {
200  Command target = knownCommands.get(name.toLowerCase());
201  return target;
202  }
boolean org.bukkit.command.SimpleCommandMap.register ( String  fallbackPrefix,
Command  command 
)

Registers a command. Returns true on success; false if name is already taken and fallback had to be used. Caller can use:-

  • command.getName() to determine the label registered for this command
  • command.getAliases() to determine the aliases which where registered
Parameters
fallbackPrefixa prefix which is prepended to the command with a ':' one or more times to make the command unique
commandthe command to register, from which label is determined from the command name
Returns
true if command was registered with the passed in label, false otherwise, which indicates the fallbackPrefix was used one or more times

Implements org.bukkit.command.CommandMap.

Definition at line 95 of file SimpleCommandMap.java.

95  {
96  return register(command.getName(), fallbackPrefix, command);
97  }
boolean org.bukkit.command.SimpleCommandMap.register ( String  label,
String  fallbackPrefix,
Command  command 
)

Registers a command. Returns true on success; false if name is already taken and fallback had to be used. Caller can use:-

  • command.getName() to determine the label registered for this command
  • command.getAliases() to determine the aliases which where registered
Parameters
labelthe label of the command, without the '/'-prefix.
fallbackPrefixa prefix which is prepended to the command with a ':' one or more times to make the command unique
commandthe command to register
Returns
true if command was registered with the passed in label, false otherwise, which indicates the fallbackPrefix was used one or more times

Implements org.bukkit.command.CommandMap.

Definition at line 102 of file SimpleCommandMap.java.

102  {
103  label = label.toLowerCase().trim();
104  fallbackPrefix = fallbackPrefix.toLowerCase().trim();
105  boolean registered = register(label, command, false, fallbackPrefix);
106 
107  Iterator<String> iterator = command.getAliases().iterator();
108  while (iterator.hasNext()) {
109  if (!register(iterator.next(), command, true, fallbackPrefix)) {
110  iterator.remove();
111  }
112  }
113 
114  // If we failed to register under the real name, we need to set the command label to the direct address
115  if (!registered) {
116  command.setLabel(fallbackPrefix + ":" + label);
117  }
118 
119  // Register to us so further updates of the commands label and aliases are postponed until its reregistered
120  command.register(this);
121 
122  return registered;
123  }
void org.bukkit.command.SimpleCommandMap.registerAll ( String  fallbackPrefix,
List< Command commands 
)

Registers all the commands belonging to a certain plugin. Caller can use:-

  • command.getName() to determine the label registered for this command
  • command.getAliases() to determine the aliases which where registered
Parameters
fallbackPrefixa prefix which is prepended to each command with a ':' one or more times to make the command unique
commandsa list of commands to register

Implements org.bukkit.command.CommandMap.

Definition at line 84 of file SimpleCommandMap.java.

84  {
85  if (commands != null) {
86  for (Command c : commands) {
87  register(fallbackPrefix, c);
88  }
89  }
90  }
List<String> org.bukkit.command.SimpleCommandMap.tabComplete ( CommandSender  sender,
String  cmdLine 
)

Looks for the requested command and executes an appropriate tab-completer if found. This method will also tab-complete partial commands.

Parameters
senderThe command's sender.
cmdLineThe entire command string to tab-complete, excluding initial slash.
Returns
a list of possible tab-completions. This list may be immutable. Will be null if no matching command of which sender has permission.
Exceptions
CommandExceptionThrown when the tab-completer for the given command fails with an unhandled exception
IllegalArgumentExceptionif either sender or cmdLine are null

Implements org.bukkit.command.CommandMap.

Definition at line 204 of file SimpleCommandMap.java.

References org.bukkit.command.SimpleCommandMap.getCommand(), org.bukkit.util.StringUtil.startsWithIgnoreCase(), org.bukkit.command.Command.tabComplete(), and org.bukkit.command.Command.testPermissionSilent().

204  {
205  Validate.notNull(sender, "Sender cannot be null");
206  Validate.notNull(cmdLine, "Command line cannot null");
207 
208  int spaceIndex = cmdLine.indexOf(' ');
209 
210  if (spaceIndex == -1) {
211  ArrayList<String> completions = new ArrayList<String>();
212  Map<String, Command> knownCommands = this.knownCommands;
213 
214  final String prefix = (sender instanceof Player ? "/" : "");
215 
216  for (Map.Entry<String, Command> commandEntry : knownCommands.entrySet()) {
217  Command command = commandEntry.getValue();
218 
219  if (!command.testPermissionSilent(sender)) {
220  continue;
221  }
222 
223  String name = commandEntry.getKey(); // Use the alias, not command name
224 
225  if (StringUtil.startsWithIgnoreCase(name, cmdLine)) {
226  completions.add(prefix + name);
227  }
228  }
229 
230  Collections.sort(completions, String.CASE_INSENSITIVE_ORDER);
231  return completions;
232  }
233 
234  String commandName = cmdLine.substring(0, spaceIndex);
235  Command target = getCommand(commandName);
236 
237  if (target == null) {
238  return null;
239  }
240 
241  if (!target.testPermissionSilent(sender)) {
242  return null;
243  }
244 
245  String argLine = cmdLine.substring(spaceIndex + 1, cmdLine.length());
246  String[] args = PATTERN_ON_SPACE.split(argLine, -1);
247 
248  try {
249  return target.tabComplete(sender, commandName, args);
250  } catch (CommandException ex) {
251  throw ex;
252  } catch (Throwable ex) {
253  throw new CommandException("Unhandled exception executing tab-completer for '" + cmdLine + "' in " + target, ex);
254  }
255  }
List< String > tabComplete(CommandSender sender, String[] args)
boolean testPermissionSilent(CommandSender target)
static boolean startsWithIgnoreCase(final String string, final String prefix)
Definition: StringUtil.java:50

The documentation for this class was generated from the following file: