Reworked command registration so that aliases are secondary to the primary command label requested
Updated docs to reflect what the code really does This adds a few new methods to Command including:- * A full constructor that takes descriptio, usageMassage and aliases for convenience * getLabel() which returns the active label for a command, which is derived from the Command name or subsiquent call to setLabel(..) * A number of registration functions for use in the CommandMap implementer Also of note is Command.getAliases() no returns the "active" aliases By: stevenh <steven.hartland@multiplay.co.uk>
This commit is contained in:
@@ -7,6 +7,7 @@ import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
import java.util.Iterator;
|
||||
|
||||
import org.bukkit.ChatColor;
|
||||
import org.bukkit.Server;
|
||||
@@ -32,9 +33,7 @@ public final class SimpleCommandMap implements CommandMap {
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers multiple commands. Returns name of first command for which fallback had to be used if any.
|
||||
* @param plugin
|
||||
* @return
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public void registerAll(String fallbackPrefix, List<Command> commands) {
|
||||
if (commands != null) {
|
||||
@@ -44,41 +43,74 @@ public final class SimpleCommandMap implements CommandMap {
|
||||
}
|
||||
}
|
||||
|
||||
private void register(String fallbackPrefix, Command command) {
|
||||
register(command.getName(), fallbackPrefix, command);
|
||||
aliases.addAll(command.getAliases());
|
||||
aliases.remove(command.getName());
|
||||
|
||||
for (String name : command.getAliases()) {
|
||||
register(name, fallbackPrefix, command);
|
||||
}
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public boolean register(String fallbackPrefix, Command command) {
|
||||
return register(command.getName(), fallbackPrefix, command);
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public boolean register(String name, String fallbackPrefix, Command command) {
|
||||
boolean nameInUse = nameInUse(name);
|
||||
public boolean register(String label, String fallbackPrefix, Command command) {
|
||||
boolean registeredPassedLabel = register(label, fallbackPrefix, command, false);
|
||||
|
||||
if (nameInUse) {
|
||||
name = fallbackPrefix + ":" + name;
|
||||
Iterator iterator = command.getAliases().iterator();
|
||||
while (iterator.hasNext()) {
|
||||
if (!register((String) iterator.next(), fallbackPrefix, command, true)) {
|
||||
iterator.remove();
|
||||
}
|
||||
}
|
||||
|
||||
knownCommands.put(name.toLowerCase(), command);
|
||||
return !nameInUse;
|
||||
// Register to us so further updates of the commands label and aliases are postponed until its reregistered
|
||||
command.register(this);
|
||||
|
||||
return registeredPassedLabel;
|
||||
}
|
||||
|
||||
private boolean nameInUse(String name) {
|
||||
if (getCommand(name) != null) {
|
||||
return !aliases.contains(name);
|
||||
/**
|
||||
* Registers a command with the given name is possible, otherwise uses fallbackPrefix to create a unique name if its not an alias
|
||||
* @param name the name of the command, without the '/'-prefix.
|
||||
* @param fallbackPrefix a prefix which is prepended to the command with a ':' one or more times to make the command unique
|
||||
* @param command the command to register
|
||||
* @return true if command was registered with the passed in label, false otherwise.
|
||||
* If isAlias was true a return of false indicates no command was registerd
|
||||
* If isAlias was false a return of false indicates the fallbackPrefix was used one or more times to create a unique name for the command
|
||||
*/
|
||||
private synchronized boolean register(String label, String fallbackPrefix, Command command, boolean isAlias) {
|
||||
String lowerLabel = label.trim().toLowerCase();
|
||||
|
||||
if (isAlias && knownCommands.containsKey(lowerLabel)) {
|
||||
// Request is for an alias and it conflicts with a existing command or previous alias ignore it
|
||||
// Note: This will mean it gets removed from the commands list of active aliases
|
||||
return false;
|
||||
}
|
||||
return false;
|
||||
|
||||
boolean registerdPassedLabel = true;
|
||||
|
||||
// If the command exists but is an alias we overwrite it, otherwise we rename it based on the fallbackPrefix
|
||||
while (knownCommands.containsKey(lowerLabel) && !aliases.contains(lowerLabel)) {
|
||||
lowerLabel = fallbackPrefix + ":" + lowerLabel;
|
||||
registerdPassedLabel = false;
|
||||
}
|
||||
|
||||
if (isAlias) {
|
||||
aliases.add(lowerLabel);
|
||||
} else {
|
||||
// Ensure lowerLabel isn't listed as a alias anymore and update the commands registered name
|
||||
aliases.remove(lowerLabel);
|
||||
command.setLabel(lowerLabel);
|
||||
}
|
||||
knownCommands.put(lowerLabel, command);
|
||||
|
||||
return registerdPassedLabel;
|
||||
}
|
||||
|
||||
/**
|
||||
* {@inheritDoc}
|
||||
*/
|
||||
public boolean dispatch(CommandSender sender, String commandLine) {
|
||||
public boolean dispatch(CommandSender sender, String commandLine) throws CommandException {
|
||||
String[] args = commandLine.split(" ");
|
||||
|
||||
if (args.length == 0) {
|
||||
@@ -86,29 +118,27 @@ public final class SimpleCommandMap implements CommandMap {
|
||||
}
|
||||
|
||||
String sentCommandLabel = args[0].toLowerCase();
|
||||
|
||||
args = Arrays_copyOfRange(args, 1, args.length);
|
||||
|
||||
Command target = getCommand(sentCommandLabel);
|
||||
boolean isRegisteredCommand = (target != null);
|
||||
|
||||
if (isRegisteredCommand) {
|
||||
try {
|
||||
target.execute(sender, sentCommandLabel, args);
|
||||
} catch (CommandException ex) {
|
||||
throw ex;
|
||||
} catch (Throwable ex) {
|
||||
throw new CommandException("Unhandled exception executing '" + commandLine + "' in " + target, ex);
|
||||
}
|
||||
if (target == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
try {
|
||||
return target.execute(sender, sentCommandLabel, Arrays_copyOfRange(args, 1, args.length));
|
||||
} catch (CommandException ex) {
|
||||
throw ex;
|
||||
} catch (Throwable ex) {
|
||||
throw new CommandException("Unhandled exception executing '" + commandLine + "' in " + target, ex);
|
||||
}
|
||||
return isRegisteredCommand;
|
||||
}
|
||||
|
||||
public void clearCommands() {
|
||||
synchronized (this) {
|
||||
knownCommands.clear();
|
||||
setDefaultCommands(server);
|
||||
public synchronized void clearCommands() {
|
||||
for (Map.Entry<String,Command> entry : knownCommands.entrySet()) {
|
||||
entry.getValue().unregister(this);
|
||||
}
|
||||
knownCommands.clear();
|
||||
aliases.clear();
|
||||
setDefaultCommands(server);
|
||||
}
|
||||
|
||||
public Command getCommand(String name) {
|
||||
|
||||
Reference in New Issue
Block a user