So, I have made a command that when the first argument is server or ranks it shows you a message. I want to make it to show a message when there are no arguments but I get errors on the console. Here is my code: PHP: public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool{ $cfg = new Config($this->getDataFolder() . "config.yml", Config::YAML); $server = $cfg->get("Server"); $ranks = $cfg->get("Ranks"); $name = $sender->getName(); switch ($command->getName()){ case "info": if(!empty($args[0])){ if($args[0] == "ranks"){ $sender->sendMessage($this->prefix . $ranks); return true; } if($args[0] == "server"){ $sender->sendMessage($this->prefix . $server); return true; } else { $sender->sendMessage($this->prefix. "Β§aUsage: /info ranks|server"); //my try to show a message when a player runs the command /info return true; } } }}
Well, its almost solved. Here is my new working code: PHP: 526 public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool{527 528 $cfg = new Config($this->getDataFolder() . "config.yml", Config::YAML);529 $server = $cfg->get("Server");530 $ranks = $cfg->get("Ranks");531 $name = $sender->getName();532 switch ($command->getName()){533 case "info":534 $sender->sendMessage($this->prefix. "Β§aUsage: /info ranks|server");535 if(!empty($args[0])){536 if($args[0] == "ranks"){537 $sender->sendMessage($this->prefix . $ranks);538 return true;539 }540 if($args[0] == "server"){541 $sender->sendMessage($this->prefix . $server);542 return true;543 }544 }545 }546 } But I still get a console error: Code: 2018-02-17 [09:58:29] [Server thread/INFO]: An unknown error occurred while attempting to perform this command 2018-02-17 [09:58:29] [Server thread/CRITICAL]: Unhandled exception executing command 'info' in info: Return value of MalakasPlayzMC\Core::onCommand() must be of the type boolean, none returned 2018-02-17 [09:58:29] [Server thread/CRITICAL]: TypeError: "Return value of MalakasPlayzMC\Core::onCommand() must be of the type boolean, none returned" (EXCEPTION) in "Core_v1.phar/src/MalakasPlayzMC/Core" at line 546 2018-02-17 [09:58:29] [Server thread/DEBUG]: #0 src/pocketmine/command/PluginCommand(58): MalakasPlayzMC\Core->onCommand(pocketmine\command\ConsoleCommandSender object, pocketmine\command\PluginCommand object, string info, array Array()) 2018-02-17 [09:58:29] [Server thread/DEBUG]: #1 src/pocketmine/command/SimpleCommandMap(258): pocketmine\command\PluginCommand->execute(pocketmine\command\ConsoleCommandSender object, string info, array Array()) 2018-02-17 [09:58:29] [Server thread/DEBUG]: #2 src/pocketmine/Server(1989): pocketmine\command\SimpleCommandMap->dispatch(pocketmine\command\ConsoleCommandSender object, string info) 2018-02-17 [09:58:29] [Server thread/DEBUG]: #3 src/pocketmine/Server(1974): pocketmine\Server->dispatchCommand(pocketmine\command\ConsoleCommandSender object, string info) 2018-02-17 [09:58:29] [Server thread/DEBUG]: #4 src/pocketmine/Server(2531): pocketmine\Server->checkConsole() 2018-02-17 [09:58:29] [Server thread/DEBUG]: #5 src/pocketmine/Server(2285): pocketmine\Server->tick() 2018-02-17 [09:58:29] [Server thread/DEBUG]: #6 src/pocketmine/Server(2158): pocketmine\Server->tickProcessor() 2018-02-17 [09:58:29] [Server thread/DEBUG]: #7 src/pocketmine/Server(1744): pocketmine\Server->start() 2018-02-17 [09:58:29] [Server thread/DEBUG]: #8 src/pocketmine/PocketMine(385): pocketmine\Server->__construct(BaseClassLoader object, pocketmine\utils\MainLogger object, string /home/gs/gs/data/servers/7069490/, string /home/gs/gs/data/servers/7069490/plugins/) 2018-02-17 [09:58:29] [Server thread/DEBUG]: #9 /home/gs/gs/data/servers/7069490/PocketMine-MP.phar(1): require(string phar:///home/gs/gs/data/servers/7069490/PocketMine-MP.phar/src/pocketmine/PocketMine.php)
PHP: public function onCommand(CommandSender $sender, Command $command, string $label, array $args) : bool{ $cfg = new Config($this->getDataFolder() . "config.yml", Config::YAML); $server = $cfg->get("Server"); $ranks = $cfg->get("Ranks"); $name = $sender->getName(); switch ($command->getName()){ case "info": if(!empty($args[0])){ if($args[0] == "ranks"){ $sender->sendMessage($this->prefix . $ranks); return true; } if($args[0] == "server"){ $sender->sendMessage($this->prefix . $server); return true; } else { $sender->sendMessage($this->prefix. "Β§aUsage: /info ranks|server"); //my try to show a message when a player runs the command /info return true; } } return true; }
Just try for 2 Arguments PHP: if(empty($args[0]) || empty($args[1])){#Send the usage}else{#code for the arguments Should work well