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:
@@ -3,8 +3,6 @@ package org.bukkit.command;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.bukkit.command.CommandSender;
|
||||
|
||||
public abstract class Command {
|
||||
private final String name;
|
||||
private List<String> aliases;
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
|
||||
package org.bukkit.command;
|
||||
|
||||
/**
|
||||
* Represents a class which contains a single method for executing commands
|
||||
*/
|
||||
public interface CommandExecutor {
|
||||
/**
|
||||
* Executes the given command, returning its success
|
||||
*
|
||||
* @param sender Source of the command
|
||||
* @param command Command which was executed
|
||||
* @param label Alias of the command which was used
|
||||
* @param args Passed command arguments
|
||||
* @return true if a valid command, otherwise false
|
||||
*/
|
||||
public boolean onCommand(CommandSender sender, Command command, String label, String[] args);
|
||||
}
|
||||
@@ -31,4 +31,12 @@ public interface CommandMap {
|
||||
* Clears all registered commands.
|
||||
*/
|
||||
public void clearCommands();
|
||||
|
||||
/**
|
||||
* Gets the command registered to the specified name
|
||||
*
|
||||
* @param name Name of the command to retrieve
|
||||
* @return Command with the specified name
|
||||
*/
|
||||
public Command getCommand(String name);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
@@ -23,7 +23,7 @@ public final class SimpleCommandMap implements CommandMap {
|
||||
private void setDefaultCommands(final Server server) {
|
||||
register("bukkit", new VersionCommand("version", server));
|
||||
register("bukkit", new ReloadCommand("reload", server));
|
||||
register("bukkit", new PluginsCommand("plugins",server));
|
||||
register("bukkit", new PluginsCommand("plugins", server));
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,6 +91,10 @@ public final class SimpleCommandMap implements CommandMap {
|
||||
}
|
||||
}
|
||||
|
||||
public Command getCommand(String name) {
|
||||
return knownCommands.get(name);
|
||||
}
|
||||
|
||||
private static class VersionCommand extends Command {
|
||||
private final Server server;
|
||||
|
||||
|
||||
Reference in New Issue
Block a user