Command + CommandMap

Discussion in 'Plugin Development' started by Shade, Feb 4, 2011.

Thread Status:
Not open for further replies.
  1. Offline

    Shade

    I tend to like sub commands. I'm just wondering if this is the proper structure for making commands with subcommands or if there's a certain way to alias then with yaml (oh an don't bite me for a static reference. >.> And I don't care about yaml help descriptions. They look like crap.

    /towny (also /towny ?, /towny help)
    /towny tree

    Code:
    	@Override
    	public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args) {
    		if (cmd.getName().equalsIgnoreCase("towny"))
    			return new TownyCommandMap(getServer()).execute(sender, commandLabel, args);
    		return false;	
    	}
    Code:
    public class TownyCommandMap extends TownyCommand {
    	SimpleCommandMap subCommands;
    	public TownyCommandMap(Server server) {
    		super("towny");
    		subCommands = new SimpleCommandMap(server);
    		TownyHelpCommand helpCommand = new TownyHelpCommand();
    		subCommands.register("", "", helpCommand);
    		subCommands.register("?", "", helpCommand);
    		subCommands.register("help", "", helpCommand);
    		subCommands.register("tree", "", new TownyTreeCommand());
    	}
    
    	@Override
    	public boolean execute(CommandSender sender, String currentAlias, String[] args) {
    		return subCommands.dispatch(sender, StringMgmt.join(args, " "));
    	}
    }
    Code:
    public class TownyHelpCommand extends TownyCommand {
    	public static final List<String> output = new ArrayList<String>();
    	static {
    		output.add(ChatTools.formatTitle("Towny Help"));
    		output.add(ChatTools.formatCommand("Debug", "towny", "tree", "Display universe tree"));
    	}
    	public TownyHelpCommand() {
    		super("help");
    	}
    
    	@Override
    	public boolean execute(CommandSender sender, String currentAlias, String[] args) {
    		if (sender instanceof Player) {
    			Player player = (Player)sender;
    			for (String line : output)
    				player.sendMessage(line);
    		} else
    			// Console
    			for (String line : output)
    				sender.sendMessage(Colors.strip(line));
    		return true;
    	}
    }
    Code:
    public class TownyTreeCommand extends TownyCommand {
    
    	public TownyTreeCommand() {
    		super("tree");
    	}
    
    	@Override
    	public boolean execute(CommandSender sender, String currentAlias, String[] args) {
    		universe.sendUniverseTree(sender);
    		return true;
    	}
    }
     
  2. Offline

    Mixcoatl

    I'm not certain there is a "proper" way. The convention in Bukkit seems to be one command per functionality. (I know, I don't like it either.) What you've done here is how I planned to handle "nested" commands, myself.
     
Thread Status:
Not open for further replies.

Share This Page