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:
Bukkit/Spigot
2011-01-18 01:12:50 +01:00
parent b46210453c
commit ab6f5d4bc2
11 changed files with 233 additions and 161 deletions

View File

@@ -0,0 +1,61 @@
package org.bukkit.command;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.bukkit.entity.Player;
public final class SimpleCommandMap implements CommandMap {
private final Map<String, Command> knownCommands = new HashMap<String, Command>();
/**
* Registers multiple commands. Returns name of first command for which fallback had to be used if any.
* @param plugin
* @return
*/
public void registerAll(String fallbackPrefix, List<Command> commands) {
if (commands != null) {
for(Command c : commands) {
List<String> names = new ArrayList<String>();
names.add(c.getName());
names.addAll(c.getAliases());
for(String name : names) {
register(name, fallbackPrefix, c);
}
}
}
}
/**
* {@inheritDoc}
*/
public boolean register(String name, String fallbackPrefix, Command command) {
boolean nameInUse = (knownCommands.get(command.getName()) != null);
if (nameInUse)
name = fallbackPrefix + ":" + name;
knownCommands.put(name, command);
System.out.println("Adding cmd: " + name + " for plugin " + fallbackPrefix);
return !nameInUse;
}
/**
* {@inheritDoc}
*/
public boolean dispatch(Player sender, String commandLine) {
String[] args = commandLine.split(" ");
String sentCommandLabel = args[0].substring(1);
Command target = knownCommands.get(sentCommandLabel);
boolean isRegisteredCommand = (target != null);
if (isRegisteredCommand) {
target.execute(sender, sentCommandLabel, args);
}
return isRegisteredCommand;
}
}