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:
Bukkit/Spigot 2011-02-28 00:30:59 +00:00
parent 258fa8e21c
commit 0a0e475f83
7 changed files with 48 additions and 39 deletions

View File

@ -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;

View File

@ -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);
}

View File

@ -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);
}

View File

@ -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 success;
}
}
return cmdSuccess;
public void setExecutor(CommandExecutor executor) {
this.executor = executor;
}
}

View File

@ -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;

View File

@ -3,14 +3,13 @@ package org.bukkit.plugin;
import java.io.File;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.CommandExecutor;
import org.bukkit.util.config.Configuration;
/**
* Represents a Plugin
*/
public interface Plugin {
public interface Plugin extends CommandExecutor {
/**
* Returns the folder that the plugin data's files are located in. The
* folder may not yet exist.
@ -63,11 +62,4 @@ public interface Plugin {
* Called when this plugin is enabled
*/
public void onEnable();
/**
* Called when a command registered by this plugin is received.
* @param commandLabel
* @return TODO
*/
public boolean onCommand(CommandSender sender, Command command, String commandLabel, String[] args);
}

View File

@ -2,10 +2,8 @@
package org.bukkit.plugin.java;
import java.io.File;
import java.util.ArrayList;
import org.bukkit.Server;
import org.bukkit.command.Command;
import org.bukkit.command.CommandSender;
import org.bukkit.command.CommandExecutor;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
@ -121,13 +119,6 @@ public abstract class JavaPlugin implements Plugin {
}
}
/**
* Called when a command registered by this plugin is received.
*/
public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
return false; // default implementation: do nothing!
}
/**
* Initializes this plugin with the given variables.
*