Added alias support
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
|
||||
command home {
|
||||
|
||||
alias h;
|
||||
alias bleh;
|
||||
set [string:name] {
|
||||
[int:x] [int:y] [int:z] {
|
||||
run home_set_coords name x y z;
|
||||
@@ -32,4 +33,4 @@ command home {
|
||||
help Reks you;
|
||||
run noskope;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -230,6 +230,8 @@ public class CommandManager {
|
||||
boolean insideType = false;
|
||||
/* if we are currently gathering chars from the command name */
|
||||
boolean gettingName = false;
|
||||
/* if we are currently gathering chars for aliases */
|
||||
boolean gettingAlias = false;
|
||||
/* the previous char, used for backslash escaping */
|
||||
char previous = '\0';
|
||||
/* the current 'array' of sub-commands we are parsing */
|
||||
@@ -246,7 +248,7 @@ public class CommandManager {
|
||||
int line = 0;
|
||||
// buffer for '...' and '"' properties of string types
|
||||
StringBuilder sideBuffer = new StringBuilder();
|
||||
|
||||
|
||||
/* iterate over all characters */
|
||||
for (int i = 0; i < chars.length; i++) {
|
||||
/* get current char */
|
||||
@@ -288,7 +290,7 @@ public class CommandManager {
|
||||
/* help this is an example; */
|
||||
/* ^ */
|
||||
}else if (current == ';') {
|
||||
/* semicolon is bashslash escaped, treat it as a normal character */
|
||||
/* semicolon is backslash escaped, treat it as a normal character */
|
||||
if (previous == '\\') {
|
||||
buffer.append(';');
|
||||
}else{
|
||||
@@ -299,7 +301,10 @@ public class CommandManager {
|
||||
return false;
|
||||
}
|
||||
/* we are defining the 'help' property, set it to what we just gathered */
|
||||
if (currentProp == Property.HELP) {
|
||||
if (gettingAlias) {
|
||||
stack.get().aliases.add(buffer.toString());
|
||||
gettingAlias = false;
|
||||
}else if (currentProp == Property.HELP) {
|
||||
stack.get().help = buffer.toString();
|
||||
/* same as above, except its the function to run */
|
||||
}else if (currentProp == Property.EXECUTE) {
|
||||
@@ -412,6 +417,8 @@ public class CommandManager {
|
||||
if (buffer.toString().equals("command") && !gettingName && cmdName == null) {
|
||||
gettingName = true;
|
||||
/* we got other properties, their values will follow */
|
||||
}else if (buffer.toString().equals("alias") && !gettingAlias) {
|
||||
gettingAlias = true;
|
||||
}else if (buffer.toString().equals("help")) {
|
||||
currentProp = Property.HELP;
|
||||
}else if (buffer.toString().equals("run")) {
|
||||
@@ -596,7 +603,7 @@ public class CommandManager {
|
||||
components.permission = null;
|
||||
components.type = null;
|
||||
Executable cmd = new Executable(cmdName, constructHelpPages(cmdName, components));
|
||||
cmd.register(methods, plugin, methodContainer);
|
||||
cmd.register(methods, plugin, methodContainer, components.getAliases());
|
||||
}
|
||||
|
||||
private static ArrayList<HelpPageCommand[]> constructHelpPages(String cmdName, ChainComponent root) {
|
||||
|
||||
@@ -11,6 +11,7 @@ public class ChainComponent implements ICommandComponent {
|
||||
public String help;
|
||||
public String execute;
|
||||
public Type type;
|
||||
public ArrayList<String> aliases = new ArrayList<String>();
|
||||
|
||||
public ChainComponent() {
|
||||
components = new ArrayList<ICommandComponent>();
|
||||
@@ -47,4 +48,8 @@ public class ChainComponent implements ICommandComponent {
|
||||
public ArrayList<ICommandComponent> getComponents() {
|
||||
return components;
|
||||
}
|
||||
|
||||
public ArrayList<String> getAliases() {
|
||||
return aliases;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,7 +44,7 @@ public class Executable extends org.bukkit.command.Command {
|
||||
this.commands = new ArrayList<ExecutableDefinition>();
|
||||
}
|
||||
|
||||
public void register(ArrayList<Method> methods, JavaPlugin plugin, Object methodContainer) {
|
||||
public void register(ArrayList<Method> methods, JavaPlugin plugin, Object methodContainer, ArrayList<String> aliases) {
|
||||
for (HelpPageCommand[] page : help) {
|
||||
for (HelpPageCommand cmd : page) {
|
||||
if (cmd != null) {
|
||||
@@ -95,6 +95,13 @@ public class Executable extends org.bukkit.command.Command {
|
||||
Map<String, Command> knownCommands = (Map<String, Command>) knownCommandsField.get(map);
|
||||
knownCommands.remove(name);
|
||||
map.register(name, this);
|
||||
for (String alias : aliases) {
|
||||
Executable cmd = new Executable(alias, this.help);
|
||||
cmd.commands = this.commands;
|
||||
cmd.plugin = this.plugin;
|
||||
knownCommands.remove(alias);
|
||||
map.register(alias, cmd);
|
||||
}
|
||||
} catch (Exception e) {
|
||||
plugin.getLogger().log(Level.SEVERE, "Failed to register command '" + name + "'!");
|
||||
e.printStackTrace();
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
name: CommandManagerTest
|
||||
main: CmdMgrTest
|
||||
main: com.nemez.cmdMgrExample.CmdMgrExample
|
||||
version: 13.37
|
||||
author: Nemes
|
||||
author: Nemes
|
||||
|
||||
Reference in New Issue
Block a user