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

Solved Command args

Comments in 'Plugin Development' started by FlamingGenius, Sep 27, 2015.

  1. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    How do i accept arguments for example if the player type

    /broadcast Hello everyone

    then it would broadcast "hello everyone"
    In usage i have the command say
    /broadcast msg
    And heres my coding

    PHP:
    <?php

    namespace FlamingGenius\MessageSystem;

    use 
    pocketmine\command\Command;
    use 
    pocketmine\command\CommandSender;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\plugin\PluginBase;
    use 
    pocketmine\Server;


    class 
    main extends PluginBase{

     public function 
    onEnable(){
      
    $this->saveDefaultConfig();

     }

     public function 
    onCommand(CommandSender $senderCommand $command$label, array $args){
      switch(
    strtolower($cmd $command->getName();)){
       case 
    "broadcast"
        
    $sender->broadcastMessage("Broadcast Test");
       break;
       
       case 
    "tester"
        
    $sender->sendMessage("test message");
      }
     }



    }

    ?>
  2. TryoneLegendZzz
    Offline

    TryoneLegendZzz Active Member

    Joined:
    May 31, 2015
    Posts:
    201
    Minecraft User:
    SavionLegendZzz
    PHP:
    public function onCommand(CommandSender $senderCommand $command$label, array $args){
      switch(
    strtolower($command->getName()){
       case 
    "broadcast"
        
    $sender->broadcastMessage("Broadcast Test");
       break;
      
       case 
    "tester"
        
    $sender->sendMessage("test message");
    break;
      }
    }
  3. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    PHP:
    $message "";
    foreach(
    $args as $arg){
       
    $message $message $arg;
    }
    $this->getServer()->broadcastMessage($message);
  4. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    This confuses me 0.o i dont understand how this would work in my plugin
  5. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    PHP:
    public function onCommand(CommandSender $senderCommand $command$label, array $args){
      switch(
    strtolower($command->getName()){
       case 
    "broadcast"
        
    $message  "";
        foreach(
    $args as $arg){
         
    $message $message $arg;
        }
        
    $sender->broadcastMessage($message);
        break;

       case 
    "tester"
        
    $sender->sendMessage("test message");
        break;
      }
    }
  6. FlamingGenius
    Offline

    FlamingGenius Active Member

    Joined:
    Sep 23, 2015
    Posts:
    173
    Minecraft User:
    FlamingGenius
    I got where it goes and understand the majority of this what i dont understand is why
    PHP:
    foreach($args as $arg)
    Is needed
  7. MCPEPIG
    Offline

    MCPEPIG Notable Member

    Joined:
    Jun 22, 2014
    Posts:
    349
    Minecraft User:
    MCPEPIG
    $args is an array, so you can't just do
    PHP:
    $this->getServer()->broadcastMessage($args);
  8. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    Another way would be:
    PHP:
    if(count($args) == INT) {
      if(
    args[0] == "blah") {
      
    // Do something;
      
    return true;
    }

      if(!isset(
    $args[0])) { // To make it do something if the first array key is not correct (Can also work with the second, third, fourth...)
       // Do something;
      
    return true;
    }
    You can add how many args you want

Share This Page

Advertisement