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

[NEW API]Broadcast Message to special players

Comments in 'Plugin Development' started by Sensej, Aug 7, 2014.

  1. Sensej
    Offline

    Sensej New Member

    Joined:
    Aug 5, 2014
    Posts:
    64
    Hello great coders,
    I saw a code for the Old API which broadcast can send messages to special players:

    PHP:
    public $target = ["player1""player2"...];
    $targets $this->target
    foreach($targets as $p)
    {
    $this->api->chat->broadcast(false"Hello"$p->username);
     }
    But this format dont works for the new api,
    Can somebody help?


    -Sensej
  2. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    435
    Plugins:
    4
    Minecraft User:
    Lambo
    So you have a array of player usernames that you went to send a message to?
  3. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    435
    Plugins:
    4
    Minecraft User:
    Lambo
    If so,
    PHP:
    $players = array("Lambo","PEMapModder");
    foreach(
    $players as $player){
       
    $player $this->getServer()->getPlayer($player);
       if(
    $player !== null$player->sendMessage("Hello");
    }
    This should work
    PEMapModder likes this.
  4. Sensej
    Offline

    Sensej New Member

    Joined:
    Aug 5, 2014
    Posts:
    64
    Here you can see an example :)

    PHP:
    class blahblah extends PluginBase implements Listener
    {
        public 
    $targets = ["Some Players"];
        
        public function 
    onEnable()
        {
        
    //Some Codes...
        
    }
        
        public function 
    onCommand(CommandSender $senderCommand $cmd$label, array $args)
        {
            switch(
    $cmd->getName())
            {
                case 
    "blahblah": /
                    
    $targetsp implode(", "$this->targets);
                    
                    
    $targets $this->targets;
                    
    $this->getServer()->BroadcastMessage(false"The players are: $targetsp"$targets);
                    
    $sender->sendMessage("blahblah");
                    break;
                
    //Some codes...
            
    }
        }
        
  5. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    435
    Plugins:
    4
    Minecraft User:
    Lambo
    Oh,
    PHP:
    $players = array("Lambo","PEMapModder");
    $targets implode(", ",$players);
    foreach(
    $players as $player){
       
    $player $this->getServer()->getPlayer($player);
       if(
    $player !== null$player->sendMessage("The players are: ".$targets);
    }
    PEMapModder and Sensej like this.
  6. Sensej
    Offline

    Sensej New Member

    Joined:
    Aug 5, 2014
    Posts:
    64
    Ok Thank You :)
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    @Lambo: Liked your posts just because you used my name :D

    @Sensej To actually broadcast a global message:
    PHP:
    $this->getServer()->broadcastMessage($msgToBroadcast); // broadcasts to everyone

    $this->getServer()->broadcast($msgToBroadcast$permPlayerMustHaveToReceive); // only broadcast the message to players who have the permission of $permPlayerMustHaveToReceive
    iJoshuaHD, Lambo and Sensej like this.
  8. Sensej
    Offline

    Sensej New Member

    Joined:
    Aug 5, 2014
    Posts:
    64
    Ok thank you i will try this
  9. Wantedkillerss
    Offline

    Wantedkillerss Active Member Plugin Developer

    Joined:
    May 9, 2014
    Posts:
    142
    Plugins:
    1
    Minecraft User:
    Wantedkillers
    Ok so how do I broadcast a message to only a certain world
  10. Lambo
    Offline

    Lambo Notable Member Plugin Developer

    Joined:
    Sep 14, 2013
    Posts:
    435
    Plugins:
    4
    Minecraft User:
    Lambo
    I liked your post because you used my name ;)
    PEMapModder likes this.
  11. Dutok
    Offline

    Dutok Notable Member Plugin Developer

    Joined:
    Jun 20, 2014
    Posts:
    372
    Plugins:
    3
    Minecraft User:
    Dutok
    If the player is inside the world, send them a message.
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Stop asking specific questions and start learning PHP. Learn about if clauses.
    iJoshuaHD and Dutok like this.
  13. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,132
    Plugins:
    4
    Minecraft User:
    iksaku
    PHP:
    $level $this->getServer()->getLevelByName($levelname);
    $players $level->getPlayers();
    foreach(
    $players as $player){
         
    $p->sendMessage("Your message here!");
    }
    Last edited: Aug 8, 2014
    iJoshuaHD likes this.
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,325
    Plugins:
    11
    Minecraft User:
    PEMapModder
    getLevelByName(). You made a fatal mistake. :D
    iJoshuaHD likes this.
  15. iksaku
    Offline

    iksaku Notable Member Plugin Developer

    Joined:
    Sep 2, 2013
    Posts:
    1,132
    Plugins:
    4
    Minecraft User:
    iksaku
    True, thanks! :p

Share This Page

Advertisement