Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Command With Varying Amount of Args

Comments in 'Plugin Development' started by Tethered_, Dec 26, 2014.

  1. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    If I have a command (lets call it /a info) and want to be able to call two separate functions when /a info and /a info <player> are called.

    So basically if I have a command that can be run with 1 or 2 args, how can I set it up?
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Use an if isset $args[$offset] block.

    @iksaku I know you're going to talk about your method, but I want to remind you that your method is a bit strict and other people cannot understand it easily, and it has a higher chance of using repeated code.

    The method I use in WorldEditArt and xEcon for optional switches is:
    PHP:
    // execution for command /cmd <arg0> <arg1> [a] [b <value, default "default">] [details ...]
    if(!isset($args[1])) return false// 0 and 1 are required, so send usage message and exit
    $arg0 array_shift($args);
    $arg1 array_shift($args);
    $hasA false;
    $bValue "default";
    while(isset(
    $args[0])){
      switch(
    $switch array_shift($args)){
        case 
    "a":
          
    $hasA true;
          break;
        case 
    " b":
          if(!isset(
    $args[0])) return false// switch "b" has a required value after it but player didn't give, so exit and send usage
          
    $bValue array_shift($args[0]);
          break;
      }
    }
    $details implode(" "$args); // implode all the remaining args as details
    It is much more readable and is easier to make changes.
    Last edited: Dec 26, 2014
  3. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    Didn't know this was a function, thanks!
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What function?

    Also reload page to see my edited post.
  5. Tethered_
    Offline

    Tethered_ Active Member Plugin Developer

    Joined:
    Jun 24, 2014
    Posts:
    178
    Plugins:
    2
    Minecraft User:
    Frostbyte58
    I just didn't know how to use isset() or what it was. Looks like a pretty solid method though.
  6. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
  7. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,707
    Plugins:
    22
    Minecraft User:
    Falkirknh
    It may be easier to make changes and better overall, but that doesn't appear to be very readable.
    PEMapModder likes this.

Share This Page

Advertisement