Fork of Poseidon providing Bukkit #1060 to older Beta versions (b1.0-b1.7.3)
1package org.bukkit.command;
2
3import org.bukkit.plugin.Plugin;
4
5/**
6 * Represents a {@link Command} belonging to a plugin
7 */
8public final class PluginCommand extends Command {
9 private final Plugin owningPlugin;
10 private CommandExecutor executor;
11
12 protected PluginCommand(String name, Plugin owner) {
13 super(name);
14 this.executor = owner;
15 this.owningPlugin = owner;
16 this.usageMessage = "";
17 }
18
19 /**
20 * Executes the command, returning its success
21 *
22 * @param sender Source object which is executing this command
23 * @param commandLabel The alias of the command used
24 * @param args All arguments passed to the command, split via ' '
25 * @return true if the command was successful, otherwise false
26 */
27 public boolean execute(CommandSender sender, String commandLabel, String[] args) {
28 boolean success = false;
29
30 if (!owningPlugin.isEnabled()) {
31 return false;
32 }
33
34 if (!testPermission(sender)) {
35 return true;
36 }
37
38 try {
39 success = executor.onCommand(sender, this, commandLabel, args);
40 } catch (Throwable ex) {
41 throw new CommandException("Unhandled exception executing command '" + commandLabel + "' in plugin " + owningPlugin.getDescription().getFullName(), ex);
42 }
43
44 if (!success && usageMessage.length() > 0) {
45 for (String line : usageMessage.replace("<command>", commandLabel).split("\n")) {
46 sender.sendMessage(line);
47 }
48 }
49
50 return success;
51 }
52
53 /**
54 * Sets the {@link CommandExecutor} to run when parsing this command
55 *
56 * @param executor New executor to run
57 */
58 public void setExecutor(CommandExecutor executor) {
59 this.executor = executor;
60 }
61
62 /**
63 * Gets the {@link CommandExecutor} associated with this command
64 *
65 * @return CommandExecutor object linked to this command
66 */
67 public CommandExecutor getExecutor() {
68 return executor;
69 }
70
71 /**
72 * Gets the owner of this PluginCommand
73 *
74 * @return Plugin that owns this command
75 */
76 public Plugin getPlugin() {
77 return owningPlugin;
78 }
79}