I want to send an message to my whole server team, their player names are stored in an array (saved in server team.yml) I don't really know how to do this with foreach and the php manual didn't help me PHP: case "serverteam": if(isset($args[0])) { $Msg = TF::GREEN . "[ServerTeam]" . TF::WHITE . $args[0] $sender->getPlayer()->sendMessage($Msg); //EXAMPLE //NOW I WANT TO BROADCAST THIS MESSAGE TO ALL PLAYERS (PLAYERNAME) IN THIS ARRAY: $players = array('1' => "Player", '2' => "Player2") } else {return false;} return true;
PHP: foreach($players as $p){ if($this->getServer()->getPlayer($p) !== null){ $this->getServer()->getPlayer($p)->sendMessage("Hello, $Msg"); }}
If the player does not exist, getPlayer() will return null, and you can't invoke a method upon it. And you should use getPlayerExact() instead. And why search the player twice? Store it into a variable. First, why $sender->getPlayer()->sendMessage() instead of $sender->sendMessage()?
Thanks, but figured that out myself (With the on null stuff) I will use Exact later... My code is now: PHP: switch($command->getName()) { case "serverteam": if(isset($args[0])) { $FinalMsg = $args[0]; $IndexArgS = 0; $ReachedEndArgs = false; while($ReachedEndArgs == false) { $IndexArgS++; if(isset($args[$IndexArgS])) { $FinalMsg = $FinalMsg." ".$args[$IndexArgS]; } else { $ReachedEndArgs = true; } } $Msg = TF::GREEN . "[ServerTeam] " . TF::WHITE . $FinalMsg; $players = array('1' => "robske_110", '2' => "robske_110", '3' => "Chrislp15", '4' => "iPadSuchtiHD", '5' => "The_Hero", '6' => "foxy1978"); $this->getServer()->getLogger()->info($Msg); foreach($players as $p){ if($this->getServer()->getPlayer($p) instanceof Player) { if($this->getServer()->getPlayer($p)->isOnline()) { $this->getServer()->getPlayer($p)->sendMessage($Msg); } } } } else {return false;} return true; }
If he left the game, the server will de-reference the player object to avoid memory leak, so you won't get an instance of the Player object because the server no longer holds it. If he didn't leave the game but he is online, he's still in building terrain screen. You may or may not need to check that. Most of the time, this is unreasonable to happen and hence redundant to check, if the player cannot join the team before he spawns.
PHP: switch($command->getName()) { case "serverteam": if(isset($args[0])) { $FinalMsg = $args[0]; $IndexArgS = 0; $ReachedEndArgs = false; while($ReachedEndArgs == false) { $IndexArgS++; if(isset($args[$IndexArgS])) { $FinalMsg = $FinalMsg." ".$args[$IndexArgS]; } else { $ReachedEndArgs = true; } } $Msg = TF::GREEN . "[ServerTeam] " . TF::WHITE . $FinalMsg; $players = array('1' => "robske_110", '2' => "robske_110", '3' => "Chrislp15", '4' => "iPadSuchtiHD", '5' => "The_Hero", '6' => "foxy1978"); $this->getServer()->getLogger()->info($Msg); foreach($players as $p){ if($this->getServer()->getPlayerExact($p) instanceof Player) { if($this->getServer()->getPlayerExact($p)->isOnline()) { $this->getServer()->getPlayerExact($p)->sendMessage($Msg); } } } } else {return false;} return true; } Ok, this is my code now (IT WORKS). I have one question is this part of the code an good practise (It's to get ALL args and put them in the msg): PHP: $FinalMsg = $args[0]; $IndexArgS = 0; $ReachedEndArgs = false; while($ReachedEndArgs == false) { $IndexArgS++; if(isset($args[$IndexArgS])) { $FinalMsg = $FinalMsg." ".$args[$IndexArgS]; } else { $ReachedEndArgs = true; } }