Bukkit-API  1.7.9-R0.2
The inofficial Bukkit-API
PluginCommand.java
1 package org.bukkit.command;
2 
3 import java.util.List;
4 
5 import org.apache.commons.lang.Validate;
6 import org.bukkit.plugin.Plugin;
7 
8 /**
9  * Represents a {@link Command} belonging to a plugin
10  */
11 public final class PluginCommand extends Command implements PluginIdentifiableCommand {
12  private final Plugin owningPlugin;
13  private CommandExecutor executor;
14  private TabCompleter completer;
15 
16  protected PluginCommand(String name, Plugin owner) {
17  super(name);
18  this.executor = owner;
19  this.owningPlugin = owner;
20  this.usageMessage = "";
21  }
22 
23  /**
24  * Executes the command, returning its success
25  *
26  * @param sender Source object which is executing this command
27  * @param commandLabel The alias of the command used
28  * @param args All arguments passed to the command, split via ' '
29  * @return true if the command was successful, otherwise false
30  */
31  @Override
32  public boolean execute(CommandSender sender, String commandLabel, String[] args) {
33  boolean success = false;
34 
35  if (!owningPlugin.isEnabled()) {
36  return false;
37  }
38 
39  if (!testPermission(sender)) {
40  return true;
41  }
42 
43  try {
44  success = executor.onCommand(sender, this, commandLabel, args);
45  } catch (Throwable ex) {
46  throw new CommandException("Unhandled exception executing command '" + commandLabel + "' in plugin " + owningPlugin.getDescription().getFullName(), ex);
47  }
48 
49  if (!success && usageMessage.length() > 0) {
50  for (String line : usageMessage.replace("<command>", commandLabel).split("\n")) {
51  sender.sendMessage(line);
52  }
53  }
54 
55  return success;
56  }
57 
58  /**
59  * Sets the {@link CommandExecutor} to run when parsing this command
60  *
61  * @param executor New executor to run
62  */
63  public void setExecutor(CommandExecutor executor) {
64  this.executor = executor == null ? owningPlugin : executor;
65  }
66 
67  /**
68  * Gets the {@link CommandExecutor} associated with this command
69  *
70  * @return CommandExecutor object linked to this command
71  */
73  return executor;
74  }
75 
76  /**
77  * Sets the {@link TabCompleter} to run when tab-completing this command.
78  * <p>
79  * If no TabCompleter is specified, and the command's executor implements
80  * TabCompleter, then the executor will be used for tab completion.
81  *
82  * @param completer New tab completer
83  */
84  public void setTabCompleter(TabCompleter completer) {
85  this.completer = completer;
86  }
87 
88  /**
89  * Gets the {@link TabCompleter} associated with this command.
90  *
91  * @return TabCompleter object linked to this command
92  */
94  return completer;
95  }
96 
97  /**
98  * Gets the owner of this PluginCommand
99  *
100  * @return Plugin that owns this command
101  */
102  public Plugin getPlugin() {
103  return owningPlugin;
104  }
105 
106  /**
107  * {@inheritDoc}
108  * <p>
109  * Delegates to the tab completer if present.
110  * <p>
111  * If it is not present or returns null, will delegate to the current
112  * command executor if it implements {@link TabCompleter}. If a non-null
113  * list has not been found, will default to standard player name
114  * completion in {@link
115  * Command#tabComplete(CommandSender, String, String[])}.
116  * <p>
117  * This method does not consider permissions.
118  *
119  * @throws CommandException if the completer or executor throw an
120  * exception during the process of tab-completing.
121  * @throws IllegalArgumentException if sender, alias, or args is null
122  */
123  @Override
124  public java.util.List<String> tabComplete(CommandSender sender, String alias, String[] args) throws CommandException, IllegalArgumentException {
125  Validate.notNull(sender, "Sender cannot be null");
126  Validate.notNull(args, "Arguments cannot be null");
127  Validate.notNull(alias, "Alias cannot be null");
128 
129  List<String> completions = null;
130  try {
131  if (completer != null) {
132  completions = completer.onTabComplete(sender, this, alias, args);
133  }
134  if (completions == null && executor instanceof TabCompleter) {
135  completions = ((TabCompleter) executor).onTabComplete(sender, this, alias, args);
136  }
137  } catch (Throwable ex) {
138  StringBuilder message = new StringBuilder();
139  message.append("Unhandled exception during tab completion for command '/").append(alias).append(' ');
140  for (String arg : args) {
141  message.append(arg).append(' ');
142  }
143  message.deleteCharAt(message.length() - 1).append("' in plugin ").append(owningPlugin.getDescription().getFullName());
144  throw new CommandException(message.toString(), ex);
145  }
146 
147  if (completions == null) {
148  return super.tabComplete(sender, alias, args);
149  }
150  return completions;
151  }
152 
153  @Override
154  public String toString() {
155  StringBuilder stringBuilder = new StringBuilder(super.toString());
156  stringBuilder.deleteCharAt(stringBuilder.length() - 1);
157  stringBuilder.append(", ").append(owningPlugin.getDescription().getFullName()).append(')');
158  return stringBuilder.toString();
159  }
160 }
boolean testPermission(CommandSender target)
void setTabCompleter(TabCompleter completer)
List< String > onTabComplete(CommandSender sender, Command command, String alias, String[] args)
boolean onCommand(CommandSender sender, Command command, String label, String[] args)
void sendMessage(String message)
PluginDescriptionFile getDescription()
void setExecutor(CommandExecutor executor)
java.util.List< String > tabComplete(CommandSender sender, String alias, String[] args)
boolean execute(CommandSender sender, String commandLabel, String[] args)