When I was in charge of implementing Version 2 of Citadel I was in charge of creating groups which allowed players in the group access reinforcements.
I also introduced the Command Pattern (which was later adopted by many plugin developers to easily manage the commands from the user’s console).
First we have to create the CommandHandler:
public class CommandHandler {
private Map<String, Command> commands = new LinkedHashMap<String, Command>();
private Map<String, Command> identifiers = new HashMap<String, Command>();
public void addCommand(Command command){
this.commands.put(command.getName().toLowerCase(), command);
for(String ident : command.getIdentifiers()){
this.identifiers.put(ident.toLowerCase(), command);
}
}
public boolean dispatch(CommandSender sender, String label, String[] args){
for(int argsIncluded = args.length; argsIncluded >= 0; argsIncluded--){
StringBuilder identifier = new StringBuilder(label);
for(int i = 0; i < argsIncluded; i++){
identifier.append(" ").append(args[i]);
}
Command cmd = getCmdFromIdent(identifier.toString(), sender);
if(cmd == null){
continue;
}
String[] realArgs = (String[])Arrays.copyOfRange(args, argsIncluded, args.length);
if(!cmd.isInProgress(sender)){
if((realArgs.length < cmd.getMinArguments()) || (realArgs.length > cmd.getMaxArguments())){
displayCommandHelp(cmd, sender);
return true;
}
if((realArgs.length > 0) && (realArgs[0].equals("?"))){
displayCommandHelp(cmd, sender);
return true;
}
}
cmd.execute(sender, realArgs);
return true;
}
return true;
}
private void displayCommandHelp(Command cmd, CommandSender sender){
sender.sendMessage(new StringBuilder().append("§cCommand:§e " ).append(cmd.getName()).toString());
sender.sendMessage(new StringBuilder().append("§cDescription:§e " ).append(cmd.getDescription()).toString());
sender.sendMessage(new StringBuilder().append("§cUsage:§e ").append(cmd.getUsage()).toString());
}
private Command getCmdFromIdent(String ident, CommandSender executor) {
ident = ident.toLowerCase();
if(this.identifiers.containsKey(ident)){
return (Command)this.identifiers.get(ident);
}
for(Command cmd : this.commands.values()){
if(cmd.isIdentifier(executor, ident)){
return cmd;
}
}
return null;
}
}
Then a command:
public class PasswordCommand extends PlayerCommand {
public PasswordCommand() {
super("Set Group Password");
setDescription("Sets the password for a group. Set password to \"null\" to make your group not joinable");
setUsage("/ctpassword §8<group-name> <password>");
setArgumentRange(2,2);
setIdentifiers(new String[] {"ctpassword", "ctpw"});
}
public boolean execute(CommandSender sender, String[] args) {
String groupName = args[0];
GroupManager groupManager = Citadel.getGroupManager();
Faction group = groupManager.getGroup(groupName);
if(group == null){
sendMessage(sender, ChatColor.RED, "Group doesn't exist");
return true;
}
String playerName = sender.getName();
if(!group.isFounder(playerName)){
sendMessage(sender, ChatColor.RED, "Invalid permission to modify this group");
return true;
}
String password = args[1];
if(password.isEmpty() || password.equals("")){
sendMessage(sender, ChatColor.RED, "Please enter a password");
return true;
}
group.setPassword(password);
groupManager.addGroup(group);
sendMessage(sender, ChatColor.GREEN, "Changed password for %s to \"%s\"", groupName, password);
return true;
}
}
Notice the following:
super("Set Group Password");
setDescription("Sets the password for a group. Set password to \"null\" to make your group not joinable");
setUsage("/ctpassword §8<group-name> <password>");
setArgumentRange(2,2);
setIdentifiers(new String[] {"ctpassword", "ctpw"});
If there are parameters missing it will return a message from the information provided in the code above.
Notice in CommandHandler:
displayCommandHelp(cmd, sender);
Next we instantiate the commands in the singleton or main file called Citadel:
public void registerCommands(){
commandHandler.addCommand(new AddModCommand());
commandHandler.addCommand(new AllowCommand());
commandHandler.addCommand(new BypassCommand());
commandHandler.addCommand(new CreateCommand());
commandHandler.addCommand(new DeleteCommand());
commandHandler.addCommand(new DisallowCommand());
commandHandler.addCommand(new FortifyCommand());
commandHandler.addCommand(new GroupCommand());
commandHandler.addCommand(new GroupsCommand());
commandHandler.addCommand(new InfoCommand());
commandHandler.addCommand(new JoinCommand());
commandHandler.addCommand(new LeaveCommand());
commandHandler.addCommand(new MaterialsCommand());
commandHandler.addCommand(new MembersCommand());
commandHandler.addCommand(new ModeratorsCommand());
commandHandler.addCommand(new NonReinforceableCommand());
commandHandler.addCommand(new OffCommand());
commandHandler.addCommand(new PasswordCommand());
commandHandler.addCommand(new PrivateCommand());
commandHandler.addCommand(new PublicCommand());
commandHandler.addCommand(new ReinforceCommand());
commandHandler.addCommand(new RemoveModCommand());
commandHandler.addCommand(new SecurableCommand());
commandHandler.addCommand(new StatsCommand());
commandHandler.addCommand(new TransferCommand());
commandHandler.addCommand(new VersionCommand());
}