Added the ability to register commands dynamically.

By: sk89q <the.sk89q@gmail.com>
This commit is contained in:
Bukkit/Spigot
2011-10-13 01:05:20 -07:00
parent f5eee9b341
commit fa0a9be46c
4 changed files with 181 additions and 3 deletions

View File

@@ -0,0 +1,133 @@
package org.bukkit.command;
import java.util.ArrayList;
import java.util.List;
/**
* Represents a command that can be registered dynamically.
*
* @author sk89q
*/
public class CommandDefinition {
private String name;
private String description;
private String usage;
private List<String> aliases = new ArrayList<String>();
private String permission;
/**
* Construct the command with a given name.
*
* @param name The command name (without a preceding /)
*/
public CommandDefinition(String name) {
setName(name);
}
/**
* Construct the command with a given name and description.
*
* @param name The command name
* @param description The command description (without a preceding /)
*/
public CommandDefinition(String name, String description) {
setName(name);
setDescription(description);
}
/**
* Get the name.
*
* @return Command name
*/
public String getName() {
return name;
}
/**
* Sets the command name.
*
* @param name Command name
*/
public void setName(String name) {
if (name == null) {
throw new IllegalArgumentException("Cannot have a null command name");
}
this.name = name;
}
/**
* Get the description.
*
* @return Command description, possibly null
*/
public String getDescription() {
return description;
}
/**
* Sets the command description.
*
* @param name Command description, possibly null
*/
public void setDescription(String description) {
this.description = description;
}
/**
* Get the usage string.
*
* @return Command description, possibly null
*/
public String getUsage() {
return usage;
}
/**
* Sets the command usage string.
*
* @param name Command usage string, possibly null
*/
public void setUsage(String usage) {
this.usage = usage;
}
/**
* Get the list of aliases.
*
* @return List of aliases, possibly null
*/
public List<String> getAliases() {
return aliases;
}
/**
* Set the list of aliases.
*
* @param aliases List of aliases
*/
public void setAliases(List<String> aliases) {
this.aliases = aliases;
}
/**
* Get the permission.
*
* @return Command permission, possibly null
*/
public String getPermission() {
return permission;
}
/**
* Sets the permission.
*
* @param name Command permission, possibly null
*/
public void setPermission(String permission) {
this.permission = permission;
}
}

View File

@@ -7,7 +7,7 @@ import java.util.Map.Entry;
import org.bukkit.plugin.Plugin;
public class PluginCommandYamlParser {
public class PluginCommandUtil {
@SuppressWarnings("unchecked")
public static List<Command> parse(Plugin plugin) {
@@ -59,4 +59,31 @@ public class PluginCommandYamlParser {
}
return pluginCmds;
}
public static Command parse(CommandDefinition command, Plugin plugin) {
Command newCmd = new PluginCommand(command.getName(), plugin);
String description = command.getDescription();
String usage = command.getUsage();
List<String> aliases = command.getAliases();
String permission = command.getPermission();
if (description != null) {
newCmd.setDescription(description);
}
if (usage != null) {
newCmd.setUsage(usage);
}
if (aliases != null) {
newCmd.setAliases(aliases);
}
if (permission != null) {
newCmd.setPermission(permission);
}
return newCmd;
}
}