Handling a large command list?

Discussion in 'Plugin Development' started by ArmyAnt, Sep 29, 2012.

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

    ArmyAnt

    Hey guys,

    I have a rather large list of commands for a player to use, and was wondering what would be the best way to handle them all. Currently I'm using something like this:

    Code:
       
    @Override
       public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String[] args){
            if (!(sender instanceof Player)) {
                //Need to be a player to use the commands  
               
            } else {
               
                playerCommand((player)sender, cmd, commandLable,  args);
            return true;
        }
       
        public final void playerCommand(Player player, Command cmd, String commandLable, String[] args){
            if (cmd.getName().compareToIgnoreCase("COMMAND 1") == 0 ) {
                //Do something
            }
            if (cmd.getName().compareToIgnoreCase("COMMAND 2") == 0 ) {
                //Do something
            }
            if (cmd.getName().compareToIgnoreCase("COMMAND 3") == 0 ) {
                //Do something
            }
           
        }
    But what happens if I end up with over 40 commands? There has to be something better then searching through 40 if statements.

    If any one can show be a better way to do this, I'd really appreciate it.
     
  2. Offline

    hawkfalcon

    You could use a switch case:3
     
  3. Offline

    Sagacious_Zed Bukkit Docs

    You give each command its own command executor. For more information see the wiki.
     
  4. Offline

    Jake230599

    Normally what I do is I make a new package called "commands". Then in that package I make an interface, that interface includes the method you want to use for commands so you don't have to continuously retype it. Then in the main class of your plugins put
    Code:
     getCommand("Command").setExecutor(new ClassName(this)); 
    Then you create the constructor in ClassName and implement CommandExecutor and the interface which should look like this
    Code:
     public class ClassName implements Interface, CommandExecutor {
    
    public ForcePlayerChat(ToadsVille toadsVille) {
        }
    }
    
    Then you can just make a new class for every command. Name the class the command name and its simple to keep up with.
     
  5. Offline

    ArmyAnt

    Hrm.... That's not to bad of an idea Jake230599, its a lot faster then what i was coming up with.

    Any one else got other suggestions?
    Any way to avoid having to place a "getCommand("Command").setExecutor..." for every command?

    Iv tried reading through Essentials code on github to see how they went about doing it, but iv had no luck understanding what they did. Does any one know another plugin that is up on github? Some example code to read through would be nice.

    EDIT by Moderator: merged posts, please use the edit button instead of double posting.
     
    Last edited by a moderator: May 29, 2016
Thread Status:
Not open for further replies.

Share This Page