Updated commands to have optional aliases, and to fallback to /pluginName:cmdName on name conflict.
By: VictorD <victor.danell@gmail.com>
This commit is contained in:
@@ -1,31 +0,0 @@
|
||||
package org.bukkit.plugin;
|
||||
|
||||
public final class Command {
|
||||
private final String name;
|
||||
private final String tooltip;
|
||||
private final String usage;
|
||||
private final Plugin owner;
|
||||
|
||||
public Plugin getPlugin() {
|
||||
return owner;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public String getTooltip() {
|
||||
return tooltip;
|
||||
}
|
||||
|
||||
public String getHelpMessage() {
|
||||
return usage;
|
||||
}
|
||||
|
||||
public Command(String name, String tooltip, String helpMessage, Plugin owner) {
|
||||
this.name = name;
|
||||
this.tooltip = tooltip;
|
||||
this.usage = helpMessage;
|
||||
this.owner = owner;
|
||||
}
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
package org.bukkit.plugin;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public interface CommandManager {
|
||||
/**
|
||||
* Registers all the commands belonging to a certain plugin.
|
||||
* @param plugin
|
||||
* @return
|
||||
*/
|
||||
public boolean registerCommands(Plugin plugin);
|
||||
|
||||
/**
|
||||
* Adds a command to the registeredCommands map. Returns true on success; false if name is already taken.
|
||||
*
|
||||
* @param command Name of command, without '/'-prefix.
|
||||
* @return Returns true if command string was not already registered; false otherwise.
|
||||
*/
|
||||
public boolean registerCommand(String command, String tooltip, String helpMessage, Plugin plugin);
|
||||
|
||||
/** Looks up given string in registeredCommands map and calls the onCommand method on the
|
||||
* appropriate plugin if found.
|
||||
*
|
||||
* @param cmdLine command + arguments. Example: "/test abc 123"
|
||||
* @return targetFound returns false if no target is found.
|
||||
*/
|
||||
public boolean dispatchCommand(Player sender, String cmdLine);
|
||||
}
|
||||
@@ -1,37 +0,0 @@
|
||||
package org.bukkit.plugin;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Map.Entry;
|
||||
|
||||
public class CommandParserYaml {
|
||||
|
||||
public static List<Command> parse(Plugin plugin) {
|
||||
List<Command> cmds = new ArrayList<Command>();
|
||||
Object object = plugin.getDescription().getCommands();
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
Map<String, Map<String, Object>> map = (Map<String, Map<String, Object>>)object;
|
||||
|
||||
if (map != null) {
|
||||
for(Entry<String, Map<String, Object>> entry : map.entrySet()) {
|
||||
Object d = entry.getValue().get("description");
|
||||
Object u = entry.getValue().get("usage");
|
||||
String description = "";
|
||||
String usageText = "";
|
||||
|
||||
if (d != null)
|
||||
description = d.toString();
|
||||
|
||||
if (u != null)
|
||||
usageText = u.toString();
|
||||
|
||||
cmds.add(new Command(entry.getKey(), description, usageText, plugin));
|
||||
}
|
||||
}
|
||||
|
||||
return cmds;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package org.bukkit.plugin;
|
||||
|
||||
import java.io.File;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.util.config.Configuration;
|
||||
|
||||
@@ -65,6 +66,8 @@ public interface Plugin {
|
||||
|
||||
/**
|
||||
* Called when a command registered by this plugin is received.
|
||||
* @param commandLabel
|
||||
* @return TODO
|
||||
*/
|
||||
public void onCommand(Player player, String command, String[] args);
|
||||
public boolean onCommand(Player player, Command command, String commandLabel, String[] args);
|
||||
}
|
||||
|
||||
@@ -1,62 +0,0 @@
|
||||
package org.bukkit.plugin;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import org.bukkit.entity.Player;
|
||||
|
||||
public final class SimpleCommandManager implements CommandManager {
|
||||
private final Map<String, Command> registeredCommands = new HashMap<String, Command>();
|
||||
|
||||
/**
|
||||
* Registers all the commands specified in the description file of a plugin.
|
||||
* @param plugin
|
||||
* @return
|
||||
*/
|
||||
public boolean registerCommands(Plugin plugin) {
|
||||
List<Command> commands = CommandParserYaml.parse(plugin);
|
||||
boolean existsCommands = (commands != null);
|
||||
|
||||
if (existsCommands) {
|
||||
for(Command c : commands) {
|
||||
if (!registerCommand(c))
|
||||
return false; // Command name conflict :(
|
||||
}
|
||||
}
|
||||
return existsCommands;
|
||||
}
|
||||
|
||||
public boolean registerCommand(Command command) {
|
||||
return registerCommand(command.getName(), command.getTooltip(), command.getHelpMessage(), command.getPlugin());
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public boolean registerCommand(String command, String tooltip, String helpMessage, Plugin plugin) {
|
||||
boolean nameAvailable = (registeredCommands.get(command) == null);
|
||||
|
||||
if (nameAvailable) {
|
||||
Command newCmd = new Command(command, tooltip, helpMessage, plugin);
|
||||
registeredCommands.put(command, newCmd);
|
||||
}
|
||||
return nameAvailable;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public boolean dispatchCommand(Player sender, String cmdLine) {
|
||||
String[] args = cmdLine.split(" ");
|
||||
|
||||
// Remove '/'-prefix and check if command is registered.
|
||||
Command target = registeredCommands.get(args[0].substring(1));
|
||||
boolean targetFound = (target != null);
|
||||
|
||||
if (targetFound) {
|
||||
target.getPlugin().onCommand(sender, args[0], args);
|
||||
}
|
||||
return targetFound;
|
||||
}
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package org.bukkit.plugin.java;
|
||||
|
||||
import java.io.File;
|
||||
import org.bukkit.Server;
|
||||
import org.bukkit.command.Command;
|
||||
import org.bukkit.entity.Player;
|
||||
import org.bukkit.plugin.Plugin;
|
||||
import org.bukkit.plugin.PluginDescriptionFile;
|
||||
@@ -141,7 +142,7 @@ public abstract class JavaPlugin implements Plugin {
|
||||
/**
|
||||
* Called when a command registered by this plugin is received.
|
||||
*/
|
||||
public void onCommand(Player player, String command, String[] args) {
|
||||
// default implementation: do nothing!
|
||||
public boolean onCommand(Player player, Command cmd, String commandLabel, String[] args) {
|
||||
return false; // default implementation: do nothing!
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user