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.
Just use args[1] in the onCommand() part of your code to get the second argument (321) of your command.
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
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}
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?
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
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}
Something like this: PHP: public $data = [];// your onEnable() code herepublic function onCommand(CommandSender $sender, Command $command, string $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 }}
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
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.