Easier Command Arguments?

Discussion in 'Plugin Development' started by Pink__Slime, Jun 7, 2013.

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

    Pink__Slime

    So I'm working on this plugin where I'm having it so it doesn't matter what order you put it in but I was wondering, is there an easier way of doing this:
    Code:java
    1. if(args[0].equalsIgnoreCase("warn")){
    2. if(args[1].startsWith("u:") && args[2].startsWith("r:") && args[3].startsWith("l:")){
    3.  
    4. }else if(args[1].startsWith("r:") && args[2].startsWith("l:") && args[3].startsWith("u:")){
    5.  
    6. }else if(args[1].startsWith("l:") && args[2].startsWith("u:") && args[3].startsWith("r:")){
    7.  
    8. }else if(args[1].startsWith("u") && args[2].startsWith("l:") && args[3].startsWith("r:")){
    9.  
    10. }else if(args[1].startsWith("l:") && args[2].startsWith("r:") && args[3].startsWith("u:")){
    11.  
    12. }else if(args[1].startsWith("r:") && args[2].startsWith("u:") && args[3].startsWith("l:")){
    13.  
    14. }
    15. }
     
  2. Offline

    Garris0n

    I'd check if each one is valid, but not check every possible order...I don't understand the point of that. If they're all valid just save each one as its proper value and continue with whatever the plugin is doing.
     
  3. Offline

    afistofirony

    For loops, switch-case blocks, and enums mix well :p

    Code:
    enum Parameter {
        USER("u", "user"),
      // ...(aliases)
    
        public final String[] aliases;
        Parameter (String... aliases) {
            this.aliases = aliases
        }
    
        public static Parameter fromString (String filter) {
            for (Parameter p : Parameter.values()) {
                for (String alias : p.aliases) {
                    if (alias.equalsIgnoreCase(filter)) return p;
                }
            } return null;
        }
    }
    And then in your command:

    Code:
    
    for (String arg : args) {
        if (arg.contains(":") {
            switch (Parameter.fromString(arg.split(":")[0])) {
                case USER:
                    // do stuff
    
                case ...:
                   // do other stuff
    
                default:
                   // if it matches none, do this
            }
        }
    }
     
Thread Status:
Not open for further replies.

Share This Page