Hello. I am using PHP: foreach($this->getServer()->getOnlinePlayers() as $players){} To display my popup which is a task, what i want is to send a different popup to a specified player. Is there any way of doing that? Thanks in advanced.
PHP: <?phpnamespace yourpluginnamespace;use pocketmine\Player;use pocketmine\scheduler\PluginTask;use plugin\Loader;class Task extends PluginTask{ private $plugin, $player; public function __construct(Loader $plugin, Player $player){ $this->plugin = $plugin; $this->player = $player; } public function onRun(int $currentTick){ $this->player->sendPopup("Message"); $this->plugin->getServer()->broadcastPopup("Broadcast"); }}
You can get the player IDs and match them with your specified player ID if I remember correctly However,I am wondering what do you mean by "specified"
put in the player name in array Code: $array = array(“player”,”name”,”here”); then, send each player Code: foreach($array as $player){ $this->getServer()->getPlayer($player)->sendPopup(“message”); }
Thank you, you are the one who understands. I want to assure that i put an extra param in the task to check the array right?
well... put in the player name who want you to send popup. e.g. to send 'abc', 'def', 'ghi' and 'jkl', please write like Code: $a = array('abc','def','ghi','jkl'); then, send each player (before get player object) Code: foreach($a as $name){ $this->getServer()->getPlayer($name)->sendPopup("Message Here"); } to use task, write like Code: //defined player array in onEnable in $this->example //PluginTask public function __construct(Main $plugin) { parent::__construct($plugin); $this->plugin = $plugin; } public function onRun(int $ticks){ foreach($this->plugin->example as $name){ $this->getOwner()->getServer()->getPlayer($name)->sendPopup("message"); } }
I don't think you understand, but i now know the answer due to testing I have an array in the main file which i can get through the task, i check if PHP: foreach($this->plugin->getServer()->getOnlinePlayers() as $players){ //i check if $players is in the array of the main file if(in_array($players->getName(), $this->plugin->players)){ //unique players }else{ //normal players }}
PHP: if(in_array($this->getOwner()->getServer()->getOnlinePlayers(),$this->getOwner()->getServer()->getPlayer($this->plugin->players))){ //unique players}else{ //normal players}
sorry, PHP: foreach($this->getOwner()->getServer()->getOnlinePlayers() as $players){ if(in_array($players->getName(), $this->plugin->players)){ //unique players }else{ //normal players }}
For the sake of efficiency, you can have $this->plugin->players array structured like this: PHP: /** @var string[] */private $players = [];public function addPlayer(Player $player) : void{ $this->players[$player->getRawUniqueId()] = $player->getName();} and then broadcast the pop-up. PHP: /** @var Server $server */foreach($server->getOnlinePlayers() as $k => $player){ if(isset($this->players[$k])){ //unique player }else{ //normal player }} Or have an array of player names mapped to their pop-up PHP: $this->players["steve"] = "Enter pop-up here.";foreach($server->getOnlinePlayers() as $player){ if(isset($this->players[$key = $player->getLowerCaseName()])){ $player->sendPopup($this->players[$key]); }else{ //normal player }}