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

Solved ServerTeam command (Send msg to players in array)

Comments in 'Plugin Development' started by Tim // robske Büba, Jan 29, 2016.

  1. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    I want to send an message to my whole server team, their player names are stored in an array (saved in server team.yml)
    I don't really know how to do this with foreach and the php manual didn't help me
    PHP:
                case "serverteam":
                if(isset(
    $args[0]))
                {
                    
    $Msg TF::GREEN "[ServerTeam]" TF::WHITE $args[0]
                    
    $sender->getPlayer()->sendMessage($Msg); //EXAMPLE
                    //NOW I WANT TO BROADCAST THIS MESSAGE TO ALL PLAYERS (PLAYERNAME) IN THIS ARRAY:
                    
    $players = array('1' => "Player"'2' => "Player2")
                }
                else
                {return 
    false;}  
                return 
    true
  2. PocketKiller
    Offline

    PocketKiller Notable Member

    Joined:
    Jul 20, 2015
    Posts:
    741
    PHP:
    foreach($players as $p){
          if(
    $this->getServer()->getPlayer($p) !== null){
                 
    $this->getServer()->getPlayer($p)->sendMessage("Hello, $Msg");
            }
    }
    Last edited: Jan 29, 2016
  3. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    If the player does not exist, getPlayer() will return null, and you can't invoke a method upon it.
    And you should use getPlayerExact() instead.
    And why search the player twice? Store it into a variable.
    First, why $sender->getPlayer()->sendMessage() instead of $sender->sendMessage()?
    CraftYourBukkit likes this.
  4. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    Thanks, but figured that out myself :) (With the on null stuff)
    I will use Exact later...
    My code is now:
    PHP:
            switch($command->getName())
            {
                case 
    "serverteam":
                if(isset(
    $args[0]))
                {
                    
    $FinalMsg $args[0];
                    
    $IndexArgS 0;
                    
    $ReachedEndArgs false;
                    while(
    $ReachedEndArgs == false)
                    {
                        
    $IndexArgS++;
                        if(isset(
    $args[$IndexArgS]))
                        {
                            
    $FinalMsg $FinalMsg." ".$args[$IndexArgS];
                        }
                        else
                        {
                            
    $ReachedEndArgs true;
                        }
                    }
                    
    $Msg TF::GREEN "[ServerTeam] " TF::WHITE $FinalMsg;
                    
    $players = array('1' => "robske_110"'2' => "robske_110"'3' => "Chrislp15"'4' => "iPadSuchtiHD"'5' => "The_Hero"'6' => "foxy1978");
                    
    $this->getServer()->getLogger()->info($Msg);
                    foreach(
    $players as $p){
                        if(
    $this->getServer()->getPlayer($p) instanceof Player)
                        {
                            if(
    $this->getServer()->getPlayer($p)->isOnline())
                            {
                                
    $this->getServer()->getPlayer($p)->sendMessage($Msg);
                            }
                        }
                    }
                }
                else
                {return 
    false;}
                return 
    true;
            }
    Last edited: Jan 29, 2016
  5. Legoboy0215
    Offline

    Legoboy0215 Notable Member

    Joined:
    Nov 1, 2014
    Posts:
    1,724
    Minecraft User:
    Legoboy0215
    If he is instance of Player, he will be online.
  6. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    Can't an player be offline but be instanceof Player?
  7. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    If he left the game, the server will de-reference the player object to avoid memory leak, so you won't get an instance of the Player object because the server no longer holds it.
    If he didn't leave the game but he is online, he's still in building terrain screen. You may or may not need to check that. Most of the time, this is unreasonable to happen and hence redundant to check, if the player cannot join the team before he spawns.
  8. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    PHP:
            switch($command->getName())
            {
                case 
    "serverteam":
                if(isset(
    $args[0]))
                {
                    
    $FinalMsg $args[0];
                    
    $IndexArgS 0;
                    
    $ReachedEndArgs false;
                    while(
    $ReachedEndArgs == false)
                    {
                        
    $IndexArgS++;
                        if(isset(
    $args[$IndexArgS]))
                        {
                            
    $FinalMsg $FinalMsg." ".$args[$IndexArgS];
                        }
                        else
                        {
                            
    $ReachedEndArgs true;
                        }
                    }
                    
    $Msg TF::GREEN "[ServerTeam] " TF::WHITE $FinalMsg;
                    
    $players = array('1' => "robske_110"'2' => "robske_110"'3' => "Chrislp15"'4' => "iPadSuchtiHD"'5' => "The_Hero"'6' => "foxy1978");
                    
    $this->getServer()->getLogger()->info($Msg);
                    foreach(
    $players as $p){
                        if(
    $this->getServer()->getPlayerExact($p) instanceof Player)
                        {
                            if(
    $this->getServer()->getPlayerExact($p)->isOnline())
                            {
                                
    $this->getServer()->getPlayerExact($p)->sendMessage($Msg);
                            }
                        }
                    }
                }
                else
                {return 
    false;}  
                return 
    true
            }
    Ok, this is my code now (IT WORKS). I have one question is this part of the code an good practise (It's to get ALL args and put them in the msg):
    PHP:
                    $FinalMsg $args[0];
                    
    $IndexArgS 0;
                    
    $ReachedEndArgs false;
                    while(
    $ReachedEndArgs == false)
                    {
                        
    $IndexArgS++;
                        if(isset(
    $args[$IndexArgS]))
                        {
                            
    $FinalMsg $FinalMsg." ".$args[$IndexArgS];
                        }
                        else
                        {
                            
    $ReachedEndArgs true;
                        }
                    }
  9. Tim // robske Büba
    Offline

    Tim // robske Büba Notable Member

    Joined:
    Feb 26, 2014
    Posts:
    606
    Minecraft User:
    robske_110
    ^^
    Assuming it's an good practise i will add the solved :)

Share This Page

Advertisement