Tweaks to command system to allow setting executors via plugins (no more ambiguous onCommand in plugins)
By: Dinnerbone <dinnerbone@dinnerbone.com>
This commit is contained in:
@@ -1,37 +1,35 @@
|
||||
package org.bukkit.command;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
|
||||
public final class PluginCommand extends Command {
|
||||
private final Plugin owningPlugin;
|
||||
private CommandExecutor executor;
|
||||
|
||||
public PluginCommand(String name, Plugin owner) {
|
||||
protected PluginCommand(String name, Plugin owner) {
|
||||
super(name);
|
||||
this.executor = owner;
|
||||
this.owningPlugin = owner;
|
||||
this.usageMessage = "";
|
||||
}
|
||||
|
||||
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
|
||||
boolean cmdSuccess = false;
|
||||
|
||||
boolean success = false;
|
||||
|
||||
try {
|
||||
cmdSuccess = owningPlugin.onCommand(sender, this, commandLabel, args);
|
||||
success = executor.onCommand(sender, this, commandLabel, args);
|
||||
} catch (Throwable ex) {
|
||||
throw new CommandException("Unhandled exception executing command '" + commandLabel + "' in plugin " + owningPlugin.getDescription().getFullName(), ex);
|
||||
}
|
||||
|
||||
if (!cmdSuccess && !usageMessage.isEmpty()) {
|
||||
String tmpMsg = usageMessage.replace("<command>", commandLabel);
|
||||
String[] usageLines = tmpMsg.split("\\n");
|
||||
for(String line: usageLines) {
|
||||
while (line.length() > 0) {
|
||||
int stripChars = (line.length() > 53 ? 53:line.length());
|
||||
sender.sendMessage(ChatColor.RED + line.substring(0, stripChars));
|
||||
line = line.substring(stripChars);
|
||||
}
|
||||
}
|
||||
if (!success && !usageMessage.isEmpty()) {
|
||||
sender.sendMessage(usageMessage.replace("<command>", commandLabel));
|
||||
}
|
||||
return cmdSuccess;
|
||||
|
||||
return success;
|
||||
}
|
||||
|
||||
public void setExecutor(CommandExecutor executor) {
|
||||
this.executor = executor;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user