Bukkit  1.4.7-R1.0
 All Classes Namespaces Files Functions Variables Enumerator Pages
PluginCommand.java
Go to the documentation of this file.
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 
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 
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 
63  public void setExecutor(CommandExecutor executor) {
64  this.executor = executor == null ? owningPlugin : executor;
65  }
66 
73  return executor;
74  }
75 
83  public void setTabCompleter(TabCompleter completer) {
84  this.completer = completer;
85  }
86 
93  return completer;
94  }
95 
101  public Plugin getPlugin() {
102  return owningPlugin;
103  }
104 
118  @Override
119  public java.util.List<String> tabComplete(CommandSender sender, String alias, String[] args) throws CommandException, IllegalArgumentException {
120  Validate.notNull(sender, "Sender cannot be null");
121  Validate.notNull(args, "Arguments cannot be null");
122  Validate.notNull(alias, "Alias cannot be null");
123 
124  List<String> completions = null;
125  try {
126  if (completer != null) {
127  completions = completer.onTabComplete(sender, this, alias, args);
128  }
129  if (completions == null && executor instanceof TabCompleter) {
130  completions = ((TabCompleter) executor).onTabComplete(sender, this, alias, args);
131  }
132  } catch (Throwable ex) {
133  StringBuilder message = new StringBuilder();
134  message.append("Unhandled exception during tab completion for command '/").append(alias).append(' ');
135  for (String arg : args) {
136  message.append(arg).append(' ');
137  }
138  message.deleteCharAt(message.length() - 1).append("' in plugin ").append(owningPlugin.getDescription().getFullName());
139  throw new CommandException(message.toString(), ex);
140  }
141 
142  if (completions == null) {
143  return super.tabComplete(sender, alias, args);
144  }
145  return completions;
146  }
147 
148  @Override
149  public String toString() {
150  StringBuilder stringBuilder = new StringBuilder(super.toString());
151  stringBuilder.deleteCharAt(stringBuilder.length() - 1);
152  stringBuilder.append(", ").append(owningPlugin.getDescription().getFullName()).append(')');
153  return stringBuilder.toString();
154  }
155 }