Refactored event calling so its front loading avoiding the lookup for each event call.

This now uses an annoymous class implementing IExecutor that facilitates direct event method handler calling

Changed commands from being executed exclusively by a player to by a CommandSender to facilitate external command callers such as rcon

Fixed CustomEventListener

Merged in additional events

Added getFullName to PluginDescriptionFile which returns the combination of Name and Version

There's also a few bits of reformatting as it seems someones been editing with either tabs or dos eol :(

By: stevenh <steven.hartland@multiplay.co.uk>
This commit is contained in:
Bukkit/Spigot
2011-01-29 16:23:56 +00:00
parent 9755073204
commit df05c36540
18 changed files with 534 additions and 361 deletions

View File

@@ -3,7 +3,7 @@ package org.bukkit.command;
import java.util.ArrayList;
import java.util.List;
import org.bukkit.entity.Player;
import org.bukkit.command.CommandSender;
public abstract class Command {
private final String name;
@@ -17,12 +17,12 @@ public abstract class Command {
this.usageMessage = "/" + name;
}
public abstract boolean execute(Player player, String currentAlias, String[] args);
public abstract boolean execute(CommandSender sender, String currentAlias, String[] args);
public String getName() {
return name;
}
public List<String> getAliases() {
return aliases;
}
@@ -34,17 +34,17 @@ public abstract class Command {
public String getUsage() {
return usageMessage;
}
public Command setAliases(List<String> aliases) {
this.aliases = aliases;
return this;
}
public Command setTooltip(String tooltip) {
this.tooltip = tooltip;
return this;
}
public Command setUsage(String usage) {
this.usageMessage = usage;
return this;

View File

@@ -5,7 +5,7 @@ import org.bukkit.entity.Player;
public interface CommandMap {
/**
* Registers all the commands belonging to a certain plugin.
* Registers all the commands belonging to a certain plugin.
* @param plugin
* @return
*/
@@ -13,17 +13,17 @@ public interface CommandMap {
/**
* Registers a command. Returns true on success; false if name is already taken and fallback had to be used.
*
*
* @param a label for this command, without the '/'-prefix.
* @return Returns true if command was registered; false if label was already in use.
*/
public boolean register(String label, String fallbackPrefix, Command command);
/** Looks for the requested command and executes it if found.
*
*
* @param cmdLine command + arguments. Example: "/test abc 123"
* @return targetFound returns false if no target is found.
*/
public boolean dispatch(Player sender, String cmdLine);
public boolean dispatch(CommandSender sender, String cmdLine);
}

View File

@@ -13,15 +13,15 @@ public final class PluginCommand extends Command {
this.usageMessage = "";
}
public boolean execute(Player player, String commandLabel, String[] args) {
boolean cmdSuccess = owningPlugin.onCommand(player, this, commandLabel, args);
public boolean execute(CommandSender sender, String commandLabel, String[] args) {
boolean cmdSuccess = owningPlugin.onCommand(sender, this, commandLabel, args);
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());
player.sendMessage(ChatColor.RED + line.substring(0, stripChars));
sender.sendMessage(ChatColor.RED + line.substring(0, stripChars));
line = line.substring(stripChars);
}
}

View File

@@ -20,12 +20,12 @@ public final class SimpleCommandMap implements CommandMap {
register("reload", "bukkit", new Command("reload") {
@Override
public boolean execute(Player player, String currentAlias, String[] args) {
if (player.isOp()) {
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
if (sender.isOp()) {
server.reload();
player.sendMessage(ChatColor.GREEN + "Reload complete.");
sender.sendMessage(ChatColor.GREEN + "Reload complete.");
} else {
player.sendMessage(ChatColor.RED + "You do not have sufficient access"
sender.sendMessage(ChatColor.RED + "You do not have sufficient access"
+ " to reload this server.");
}
@@ -56,28 +56,28 @@ public final class SimpleCommandMap implements CommandMap {
register(name, fallbackPrefix, command);
}
}
/**
/**
* {@inheritDoc}
*/
public boolean register(String name, String fallbackPrefix, Command command) {
boolean nameInUse = (knownCommands.get(name) != null);
if (nameInUse)
name = fallbackPrefix + ":" + name;
knownCommands.put(name, command);
return !nameInUse;
}
/**
/**
* {@inheritDoc}
*/
public boolean dispatch(Player sender, String commandLine) {
public boolean dispatch(CommandSender sender, String commandLine) {
String[] args = commandLine.split(" ");
String sentCommandLabel = args[0].substring(1);
args = Arrays.copyOfRange(args, 1, args.length);
Command target = knownCommands.get(sentCommandLabel);
boolean isRegisteredCommand = (target != null);
if (isRegisteredCommand) {
@@ -98,12 +98,12 @@ public final class SimpleCommandMap implements CommandMap {
}
@Override
public boolean execute(Player player, String currentAlias, String[] args) {
public boolean execute(CommandSender sender, String currentAlias, String[] args) {
if (args.length == 0) {
player.sendMessage("This server is running " + ChatColor.GREEN
sender.sendMessage("This server is running " + ChatColor.GREEN
+ server.getName() + ChatColor.WHITE + " version " + ChatColor.GREEN + server.getVersion());
player.sendMessage("This server is also sporting some funky dev build of Bukkit!");
player.sendMessage("Plugins: " + getPluginList());
sender.sendMessage("This server is also sporting some funky dev build of Bukkit!");
sender.sendMessage("Plugins: " + getPluginList());
} else {
StringBuilder name = new StringBuilder();
@@ -118,26 +118,26 @@ public final class SimpleCommandMap implements CommandMap {
if (plugin != null) {
PluginDescriptionFile desc = plugin.getDescription();
player.sendMessage(ChatColor.GREEN + desc.getName() + ChatColor.WHITE + " version " + ChatColor.GREEN + desc.getVersion());
sender.sendMessage(ChatColor.GREEN + desc.getName() + ChatColor.WHITE + " version " + ChatColor.GREEN + desc.getVersion());
if (desc.getDescription() != null) {
player.sendMessage(desc.getDescription());
sender.sendMessage(desc.getDescription());
}
if (desc.getWebsite() != null) {
player.sendMessage("Website: " + ChatColor.GREEN + desc.getWebsite());
sender.sendMessage("Website: " + ChatColor.GREEN + desc.getWebsite());
}
if (!desc.getAuthors().isEmpty()) {
if (desc.getAuthors().size() == 1) {
player.sendMessage("Author: " + getAuthors(desc));
sender.sendMessage("Author: " + getAuthors(desc));
} else {
player.sendMessage("Authors: " + getAuthors(desc));
sender.sendMessage("Authors: " + getAuthors(desc));
}
}
} else {
player.sendMessage("This server is not running any plugin by that name.");
player.sendMessage("Plugins: " + getPluginList());
sender.sendMessage("This server is not running any plugin by that name.");
sender.sendMessage("Plugins: " + getPluginList());
}
}