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

Command Arguments

Comments in 'Plugin Development' started by BlubberBoy333, May 23, 2015.

  1. BlubberBoy333
    Offline

    BlubberBoy333 Active Member

    Joined:
    Aug 31, 2014
    Posts:
    177
    Minecraft User:
    Blubberboy333
    How do I make a command argument? And how do I make it so I can do something like "/command <player>"
    RekkuzaRage likes this.
  2. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    251
    Minecraft User:
    RekkuzaRage
    Here is an example. This is my example command, /hi <player>
    It will send a message to a player when executed.
    PHP:
    case "Hi"// The command!
    if(!isset($args[0]) { // $args[0] represents/is the entered player, <player>
       // Checks if user typed the player, if not it will output the correct way to use the command.
      
    $sender->sendMessage("/hey <player>");
    }
      
    $player $this->getServer()->getPlayer($args[0]);
      if(
    $player instanceof Player) { // Checks if the player/$args[0] is a player. If true, then it will send the message to the player.
        
    $player->sendMessage("Hi!");
    }
    else { 
    // If not then...
      
    $sender->sendMessaage("Player not found");
    }
    LDX, BlubberBoy333 and iJoshuaHD like this.
  3. BlubberBoy333
    Offline

    BlubberBoy333 Active Member

    Joined:
    Aug 31, 2014
    Posts:
    177
    Minecraft User:
    Blubberboy333
    Ok that helped a lot. But the plugin i'm making is something that lets the player send the console a message. Like how you can do
    "/tell <player> <message>" How could I do something like that?
  4. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    You have to implode the arguments of message to send to target player by issuer
    RekkuzaRage likes this.
  5. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    This is the code I used to create my InventoryClear plugin:
    PHP:
    public function onCommand(CommandSender $senderCommand $cmd$label, array $args) {
            if(
    strtolower($cmd->getName()) === "clearinv") {
                if(
    $sender->hasPermission("inventoryclear.command.clearinv")) {
                    if(
    count($args) === 1) {
                        
    $name $args[0];
                        
    $target $this->getServer()->getPlayer($name);
                        if(
    $target instanceof Player) {
                            
    $target->getInventory()->setContents(array(Item::get(000)));
                            
    $target->sendMessage("Your inventory has been cleared by " $sender->getName());
                            
    $sender->sendMessage("Cleared " $target->getName() . "'s inventory.");
                        }
                        else {
                            
    $sender->sendMessage("Sorry, " $args[0] . " is not online!");
                        }
                  
                    }
                    else {
                        
    $sender->sendMessage("Usage: /clearinv <playername>");
                    }
                }
                else {
                    
    $sender->sendMessage("You don't have permissions to use this command.");
                }
            }
        }
    Hope it helps :)
  6. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    No need to use setContents(), have a look at this:
    PHP:
    $target->getInventory()->clearAll();
    RekkuzaRage likes this.
  7. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    I know but for some reason it causes crashes when used with PlayerQuitEvent
  8. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    Then.. Create an issue in github of PocketMine. clearAll() should work :D
  9. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    It's probably fixed in 0.11 but this is just my way around the crash.
  10. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Why are you guys so good at going off-topic?
    I like using this method to handle optional unordered (in any order) parameters like this.
    For example, if we have a command like this:
    Code:
    /cmd <mandatory argument a> <mandatory argument b> [.c <optional argument c>] [.d <optional argument d>]
    
    I would handle like this:
    PHP:
    public function onCommand(CommandSender $issuerCommand $cmd$lbl, array $params){
      if(
    $cmd->getName() === "cmd"){
        if(!isset(
    $params[1])){ // if parameter 1 (the first parameter is parameter 0, second is parameter 1) doesn't exist, it is wrong usage.
          
    return false// exit with "false" implying wrong usage
        
    }
        
    $a array_shift($params); // pull out the first parameter, and shift the parameters behind front, i.e. parameter 1 now becomes parameter 0, vice versa
        
    $b array_shift($params); // pull out the CURRENT first parameter, i.e. the second parameter
        // now we have got $a and $b as mandatory arguments a and b
        // now let's detect the optional arguments
        
    $c "c"// default value of optional argument c
        
    $d 1// default value of optional argument d
        
    while(isset($params[0])){ // while there are still values in $params, i.e. you still have to handle other arguments
          
    $name array_shift($params); // shfit the next argument as $name
          
    switch(strtolower($name)){ // convert $name to lowercase
            
    case ".c":
              if(!isset(
    $params[0])){
                
    $issuer->sendMessage(TextFormat::YELLOW "No value passed for \".c\", assuming default value (\"c\")");
                break; 
    // assume .c didn't get included
              
    }
              
    $c array_shift($args);
              break;
            case 
    ".d":
              if(!isset(
    $params[0])){
                
    $issuer->sendMessage(TextFormat::YELLOW "No value passed for \".d\", assuming default value (\"d\")");
                break; 
    // assume .d didn't get included
              
    }
              
    $d = (int) array_shift($args); // convert to integer
              
    break;
          }
        }
        
    // execute code with $a, $b, $c and $d
      
    }
    }
    See WorldEditArt source for more details.
    Andrey Nazarchuk likes this.
  11. BlubberBoy333
    Offline

    BlubberBoy333 Active Member

    Joined:
    Aug 31, 2014
    Posts:
    177
    Minecraft User:
    Blubberboy333
    When I implode the argument should it be something like this?
    PHP:
    implode($args[0]);
    Or this
    PHP:
    implode($args);
  12. BlubberBoy333
    Offline

    BlubberBoy333 Active Member

    Joined:
    Aug 31, 2014
    Posts:
    177
    Minecraft User:
    Blubberboy333
    I figured that out.
    When I send the message it removes the spaces. How can I fix that?
  13. BlubberBoy333
    Offline

    BlubberBoy333 Active Member

    Joined:
    Aug 31, 2014
    Posts:
    177
    Minecraft User:
    Blubberboy333
    Nevermind, I fixed that too. Thanks for the help guys it's working great now :D
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    http://php.net/implode
  15. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    PHP:
    implode(" "$args);
    Last edited: May 25, 2015
  16. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It is a better practice to put it in the correct order, although PHP will swap them for you anyway:
    PHP:
    implode($glue$pieces);
    Andrey Nazarchuk likes this.

Share This Page

Advertisement