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: <?phpnamespace 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 $sender, Command $command, $label, array $args){ switch(strtolower($cmd = $command->getName();)){ case "broadcast" $sender->broadcastMessage("Broadcast Test"); break; case "tester" $sender->sendMessage("test message"); } }}?>
PHP: public function onCommand(CommandSender $sender, Command $command, $label, array $args){ switch(strtolower($command->getName()){ case "broadcast" $sender->broadcastMessage("Broadcast Test"); break; case "tester" $sender->sendMessage("test message");break; }}
PHP: $message = "";foreach($args as $arg){ $message = $message . $arg;}$this->getServer()->broadcastMessage($message);
PHP: public function onCommand(CommandSender $sender, Command $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; }}
I got where it goes and understand the majority of this what i dont understand is why PHP: foreach($args as $arg) Is needed
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