Hi all, I've got a simple question about PlayerChatEvent and its function setRecipients. I want to prevent a player from sending and receiving messages. How can I do this? How does setRecipients work and how should I use it in this case?
Sending is easy: cancel the event setRecipients: look at that post: https://forums.pocketmine.net/threads/correct-way-to-use-setrecipients.13352/
For sending, just cancel the PlayerChatEvent. PlayerChatEvent::setRecipients($array); while $array is an array out of Player Objects. For Example: PHP: $array = array();foreach($this->getServer()->getOnlinePlayers() as $pl){ if(strpos(strtolower($pl->getName()), "a"){ array_push($array, $pl); }}$event->setRecipients($array); in PlayerChatEvent will put every Player with an "a" in his name as a Recipient. Every Player that is not in the array $array, will not recieve the message.
1. Get all recipients. 2. For each $i as $recipient, if you want to block $recipient, unset $recipients[$i] 3. Set all recipients to your array, after iteration.
Nope. PHP supports concurrent array editing during iteration. The array will be like cloned when it is passed onto foreach. http://php.net/manual/en/control-structures.foreach.php#114759
Thanks for all replies Now I understand how it works. Maybe it was a simple English grammar related thing (I didn't know that recipients in English are same as receivers or "destinatari" in Italian so I didn't know what that function was used for )