I was wondering if someone can show me in detail how to detect a player using a command. For example: /crash (Player) I don't know how to make to make my plugin detect that "player". Can someone help me? Thanks. Also please make your answer detailed.
Step 1. Get the string that refers to the player. In this case, it should be $args[0]. Step 2. Get the Player object from the string. You can use $server->getPlayer($name) to get a player with the shortest name that starts with $name. You can also use $server->getPlayerExact($name) so that the player must be exact $name and not anything else. Note that these searches are case-insensitive. Also, make a $player instanceof Player check so that you can handle errors if there is no player of such name.
Code: public function onCommand(CommandSender $sender, Command $cmd, $label, array $args) { switch($cmd->getName()) { case "crash": if(!isset($args[0])) { $sender->sendMessage("[EXAMPLEPLUGIN] Usage: /crash <player>"); break; } $player = $this->getServer()->getPlayer($args[0]); if(!$player instanceof Player) { // ... break; } // ... break; // ... } return true; }