Added command registration to plugin config file. Registered commands are sent to the corresponding plugin.onCommand method when executed.

By: VictorD <victor.danell@gmail.com>
This commit is contained in:
Bukkit/Spigot 2011-01-16 16:30:34 +01:00
parent c72a7064d8
commit a17e7470e8
9 changed files with 184 additions and 0 deletions

View file

@ -3,6 +3,7 @@ package org.bukkit;
import org.bukkit.entity.Player;
import java.util.List;
import org.bukkit.plugin.PluginManager;
/**

View file

@ -3,6 +3,7 @@ package org.bukkit.fillr;
import org.bukkit.*;
import org.bukkit.plugin.*;
import org.bukkit.plugin.java.*;
import org.bukkit.entity.Player;
import org.bukkit.event.*;
import java.io.File;
@ -30,4 +31,8 @@ public class Fillr extends JavaPlugin {
listener = new FillrListener(getServer());
getServer().getPluginManager().registerEvent(Event.Type.PLAYER_COMMAND, listener, Event.Priority.Normal, this);
}
public void onCommand(Player player, String command, String[] args) {
// TODO Auto-generated method stub
}
}

View file

@ -0,0 +1,31 @@
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;
}
}

View file

@ -0,0 +1,28 @@
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);
}

View file

@ -0,0 +1,29 @@
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()) {
String description = entry.getValue().get("description").toString();
String usageText = entry.getValue().get("usage").toString();
cmds.add(new Command(entry.getKey(), description, usageText, plugin));
}
}
return cmds;
}
}

View file

@ -3,6 +3,7 @@ package org.bukkit.plugin;
import java.io.File;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.util.config.Configuration;
/**
@ -61,4 +62,9 @@ public interface Plugin {
* Called when this plugin is enabled
*/
public void onEnable();
/**
* Called when a command registered by this plugin is received.
*/
public void onCommand(Player player, String command, String[] args);
}

View file

@ -17,6 +17,7 @@ public final class PluginDescriptionFile {
private String name = null;
private String main = null;
private String version = null;
private Object commands = null;
@SuppressWarnings("unchecked")
public PluginDescriptionFile(final InputStream stream) throws InvalidDescriptionException {
@ -79,6 +80,10 @@ public final class PluginDescriptionFile {
public String getMain() {
return main;
}
public Object getCommands() {
return commands;
}
private void loadMap(Map<String, Object> map) throws InvalidDescriptionException {
try {
@ -104,6 +109,14 @@ public final class PluginDescriptionFile {
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "main is of wrong type");
}
try {
commands = map.get("commands");
} catch (NullPointerException ex) {
throw new InvalidDescriptionException(ex, "command is not defined");
} catch (ClassCastException ex) {
throw new InvalidDescriptionException(ex, "command is of wrong type");
}
}
private Map<String, Object> saveMap() {
@ -111,6 +124,7 @@ public final class PluginDescriptionFile {
map.put("name", name);
map.put("main", main);
map.put("version", version);
map.put("command", commands);
return map;
}
}

View file

@ -0,0 +1,62 @@
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;
}
}

View file

@ -3,6 +3,7 @@ package org.bukkit.plugin.java;
import java.io.File;
import org.bukkit.Server;
import org.bukkit.entity.Player;
import org.bukkit.plugin.Plugin;
import org.bukkit.plugin.PluginDescriptionFile;
import org.bukkit.plugin.PluginLoader;
@ -136,4 +137,11 @@ 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!
}
}