1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

Command Args

Discussion in 'Development' started by Karanpatel567, Oct 23, 2017.

  1. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    Hey does anyone know how to get the amount of something in code. Example I want to pay someone money /pay karan 321, how would i get the 321 in code? Sorry if you don't understand.
     
  2. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Just use args[1] in the onCommand() part of your code to get the second argument (321) of your command.
     
  3. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    How? I want to keep both especially the player.
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Some common args parsing:

    $args[0] = first argument
    $args[1] = second argument
    $args[2] = third argument
    isset($args[0]) = does first argument exist?
    ((int) $args[0]) = the first argument, converted to a number (it is a string if you don't convert it)
    implode(" ", array_slice($args, 5, 7)) = Join the 6th to 8th arguments with spaces
     
    jasonwynn10 likes this.
  5. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Do it like this:
    PHP:
    $playername $args[0];   // Name of the player entered, in your example 'karan'
    $money = (int)$args[1];   // Amount of money to give to 'karan', in your example '321'

    // Check if player is online:
    if($this->getServer()->getPlayer($playername)) {   // returns NULL if player is offline
        // some code
    }

    // Check if both arguments are set:
    if(isset($args[0]) && isset($args[1])) {
        
    // some code
    } else {
        
    // error message
    }
     
    Last edited: Oct 23, 2017
    Karanpatel567 likes this.
  6. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    Ok but how would i do this. After you put the amount like "123" it gives you paper when you right click it it gives you money so args[1], how would i get args[1] on PlayerinteractEvent if you know what I mean?
     
  7. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Save it into a global array together with the player's name
     
  8. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    Any code?
     
  9. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    I am not sure what you mean by "global". In PHP, "global" only refers to global variables. Never use global variables in plugins; they are easily changed by other plugins accidentally.
    I believe you are referring to a class property
     
    HimbeersaftLP and jasonwynn10 like this.
  10. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    Do you know how?
     
  11. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Create a public property:
    PHP:
    public $data = [];
    Give the sender paper and add the player's name plus the amount the money to $data (in your onCommand() code):
    PHP:
    $sender->getInventory()->addItem(Item::PAPER);
    $this->data[$sender->getName()] = (int)$args[1]; // adds the command-sender's name as key and $args[1] as value
    Use the PlayerItemHeldEvent to check if the player is right-clicking/holding the paper item and get $args[1] (amount of money to give to the player):
    PHP:
    $player $event->getPlayer();
    if(
    array_key_exists($player->getName(), $this->data)) { // if player ran the command and got paper
        // give the player money
    }
     
    Last edited: Oct 24, 2017
    Karanpatel567 likes this.
  12. Karanpatel567

    Karanpatel567 Baby Zombie

    Messages:
    115
    GitHub:
    Karanpatel567
    Um how would this work
     
  13. WinterBuild7074

    WinterBuild7074 Zombie Pigman

    Messages:
    693
    GitHub:
    winterbuild7074
    Something like this:
    PHP:
    public $data = [];
    // your onEnable() code here

    public function onCommand(CommandSender $senderCommand $commandstring $label, array $args): bool {
        switch(
    $command->getName()) {
            case 
    "givemoney":
                if(isset(
    $args[0]) && isset($args[1])) {
                    
    $playername $args[0];
                    
    $money = (int)$args[1];
                    if(
    $this->getServer()->getPlayer($playername)) {   // returns NULL if player is offline
                        
    $sender->getInventory()->addItem(Item::PAPER); // gives the player paper
                        
    $this->data[$sender->getName()] = (int)$args[1]; // adds the player to the $data array, key = playername, value = amount of money to give
                    
    } else {
                        
    // error, user is offline
                    
    }
                } else {
                    
    // send usage
                
    }
                break;
        }
    }

    public function 
    onItemHeld(PlayerItemHeldEvent $event) {
        
    $player $event->getPlayer();
        if(
    array_key_exists($player->getName(), $this->data)) { // if player ran the command and got paper
            // give the player money
        
    }
    }
     
  14. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    It would be better to use a private class property for in-class use

    If you don't know how to program in PHP, learn before you ask questions on these forums. This is a place for PocketMine-MP API questions, not generic PHP
     
    Last edited: Oct 24, 2017
  15. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    Yes, I wasn't literally meaning a global variable, but referring form of variable that can be accessed in all the needed sections of the plugin.
     
  16. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    It is a class property not a variable!
     
    jasonwynn10 and EdwardHamHam like this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.