Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

how to use $event->setRecipients() right?

Comments in 'Plugin Development' started by Xenturio, Mar 12, 2015.

  1. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    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
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    PHP:
    // namespace and class declaration and use statements skipped
    public $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()];
      }));
    }
    Xenturio likes this.
  3. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    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
  4. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
  5. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    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()];
    }));
    Xenturio likes this.
  6. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
  7. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    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 -.-
  8. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    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...
    PEMapModder likes this.
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    No, it is a strange problem where the player session was not initialized.
    Xenturio likes this.
  10. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    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...
  11. iJoshuaHD
    Offline

    iJoshuaHD Notable Member Plugin Developer

    Joined:
    Nov 7, 2013
    Posts:
    1,201
    Plugins:
    4
    Minecraft User:
    iJoshuaHD
    IChat by @LDX already had this feature
    64FF00 likes this.
  12. Xenturio
    Offline

    Xenturio Active Member

    Joined:
    Sep 19, 2014
    Posts:
    333
    Minecraft User:
    Xenturio
    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!

Share This Page

Advertisement