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

how to do subcommands

Comments in 'Plugin Development' started by amiremad, Apr 15, 2016.

  1. amiremad
    Offline

    amiremad Active Member

    Joined:
    Sep 8, 2015
    Posts:
    192
    Minecraft User:
    amir107
    how to make this

    /command subcommand

    i know how to do a regular command /command

    but i want to know how to do subcommands
  2. basprohop
    Offline

    basprohop Active Member Plugin Developer

    Joined:
    Aug 8, 2015
    Posts:
    128
    Plugins:
    3
    Minecraft User:
    basprohop
  3. amiremad
    Offline

    amiremad Active Member

    Joined:
    Sep 8, 2015
    Posts:
    192
    Minecraft User:
    amir107
  4. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    When client sends a command it's one string
    Code:
    /time set day
    
    PocketMine explodes them by space and removes forward slash

    Command is now an array that looks like this

    Code:
    [0] => time, [1] => set, [2] => 'day' 
    
    In this example array keys from 1 to 2 is args, why [0] isn't? Because it is a command

    Sub-command is [1] which is 'set'

    PHP:
    if(isset($args[1])){ # Make sure that user sent sub-command at all
       
    case(strtolower($args[1])){ # Case-insensitive
          
    case 'set':
                 
    # Code to set time to $args[2]
              
    break;
          case default:
              break;
       }
    }
    applqpak likes this.
  5. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    Code:
    0 => "time", 1 => "set", 2 => "day"
    
    is wrong. "time" is the command name, $args starts at "set"
    applqpak, Primus and HotFireyDeath like this.
  6. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Wait what? Thats what I said o_O
    applqpak likes this.
  7. XFuryMCPE
    Offline

    XFuryMCPE Active Member Plugin Developer

    Joined:
    Jun 19, 2015
    Posts:
    137
    Plugins:
    2
    Minecraft User:
    XFuriousMC
    set is $args[0], $args is the array, not the command
    applqpak likes this.
  8. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    The command name is not in the array. Index 0 is "set", in this case.
    applqpak and Primus like this.
  9. XFuryMCPE
    Offline

    XFuryMCPE Active Member Plugin Developer

    Joined:
    Jun 19, 2015
    Posts:
    137
    Plugins:
    2
    Minecraft User:
    XFuriousMC
    Like I said xD
    applqpak likes this.
  10. BlubberBoy333
    Offline

    BlubberBoy333 Active Member

    Joined:
    Aug 31, 2014
    Posts:
    177
    Minecraft User:
    Blubberboy333
    PHP:
    public function onCommand(CommandSender $senderCommand $cmd$label, array $args){
        if(
    $command->getName() == "command"){
            if(isset(
    $args[0])){
                if(
    $args[0] == "yourSubCommand")
                    
    // In here, you put what you want the sub command to do. Example:
                    
    $sender->sendMessage("You used a sub command!");
                }
            }
        }
    }
  11. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Oh, my fault :D
    applqpak likes this.
  12. amiremad
    Offline

    amiremad Active Member

    Joined:
    Sep 8, 2015
    Posts:
    192
    Minecraft User:
    amir107
    is there anything i need to do in plugin.yml
  13. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
    No, it will stay the same. Just the command name has to be in there.
    HotFireyDeath and applqpak like this.

Share This Page

Advertisement