Hello everybody! i want to make a small disable chat plugin - i know that somebody had do this but i have an own idear but dont know if its possible or how to use the $event->setRecipients() code right. my idear: onPlayerJoin -> the player will add into an array on command -> player remove out of the array or will be add again onPlayerChatEvent -> only the players in the array are the recipients so my question is a) if its possible? and b) how to use the "setRecipients" right because i get all the time an error
PHP: // namespace and class declaration and use statements skippedpublic $isMute = [];public function onJoin(PlayerJoinEvent $event){ $this->isMute[$event->getPlayer()->getId()] = false;}public function onCommand(...){ if(!($sender instanceof Player)){ return true; // tell him only run in-game } $id = $event->getPlayer()->getId(); $this->isMute[$id] = !$this->isMute[$id]; // send message about it and return true}public function onChat(PlayerChatEvent $event){ $event->setRecipients(array_filter($event->getRecipients(), function(Player $recipient){ return !$this->isMute[$recipient->getId()]; }));}
ok iam not full of experiance but... a) onCommand you use $id = $event->getPlayer()->getId(); -> changed into $sender->getId(); but problem is by BOTH the same! b) if(strtolower($command->getName()) == "shchat") { $id = $sender->getId(); or $id = $event-getPlayer()->getId(); $this->isMute[$id] = !$this->isMute[$id]; } what u mean exactly with // send message about it and return true c) after test i get 14:42:20 [NOTICE] InvalidArgumentException: "Argument 1 passed to main\chatdisabler::main\{closure}() must be an instance of pocketmine\Player, instance of pocketmine\command\ConsoleCommandSender given" (E_RECOVERABLE_ERROR) in "/chatdisabler.phar/src/main/chatdisabler" at line 129 ($event->setRecipients(array_filter($event->getRecipients(), function(Player $recipient){ return !$this->isMute[$recipient->getId()]; })) dont know right whats wrong
a) oops you're right, they aren't the same of course. b) I'm just too lazy to do that part of code. c) Use this instead: PHP: $event->setRecipients(array_filter($event->getRecipients(), function($recipient){ return $recipient instanceof Player and !$this->isMute[$recipient->getId()];}));
ok i run it now on my server - and i got some error but i dont know exactly why - the function are not given 5:41:08 [CRITICAL] Could not pass event pocketmine\event\player\PlayerChatEvent to SurvivalHiveChatDisabler v1.2.0: Undefined offset: 272 on main\chatdisabler 15:41:08 [NOTICE] ArrayOutOfBoundsException: "Undefined offset: 272" (E_NOTICE) in "/chatdisabler.phar/src/main/chatdisabler" at line 134 with 2 players on a test server it was fine ... dont know right now -.-
ok it think its a problem by to many player - i test it with set down the playerlimit on 10 players - all fine - if i set the limit to 15 it beginns... ps line 134 -> return $recipient instanceof Player and !$this->isMute[$recipient->getId()]; i try to rewrite the array - i think there i s a problem...
ok, dont find a way out of this problem at the moment... know i now why other guys hat cancelled the complete chat event and send the messeges then to the players they want chat... think my idear was better then this but not really possible to use by pocketmine...
i look in his source code for some days but dont understand exactly what he is doing xD i know that he had make it but i want to code it by myself - dont want use it from an other developer - thats not my intressting because i cant learn from it. but thx!