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

Random select player and broadcast name

Comments in 'Plugin Development' started by wolfdale, Feb 27, 2015.

  1. wolfdale
    Offline

    wolfdale Active Member Plugin Developer

    Joined:
    Dec 3, 2014
    Posts:
    276
    Plugins:
    1
    Minecraft User:
    ace
    How to randomly select a player and broadcast his name?
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    PHP:
    function array_mt_rand(array $a){
      return 
    $a[array_keys($a)[mt_rand(0count($a) - 1)]];
    }

    ##################################

    /** @var \pocketmine\Server $server */
    $player array_mt_rand($server->getOnlinePlayers());
    $server->broadcastMessage("Random player: " $player->getName());
  3. Falk
    Offline

    Falk Staff Member Global Moderator

    Joined:
    Sep 2, 2013
    Posts:
    1,706
    Plugins:
    22
    Minecraft User:
    Falkirknh
    Slightly shorter (but maybe a little slower):

    PHP:
    /** @var \pocketmine\Server $server */
    $players $server->getOnlinePlayers();
    $server->broadcastMessage("Random player: " $players[array_rand($players)]->getName());
    Last edited: Feb 27, 2015
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    PHP:
    function array_mt_rand(array $a){
      return 
    array_values($a)[mt_rand(0count($a) - 1)];
    }
    Might be faster, might not be.
  5. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    Hi, sorry for bump this post, but I have a question:
    How I can save the random player from a specific level to a public variable? I try this:
    PHP:
    public function setPlayer($game)
        {
            
    $server $this->getServer();
            
    $players $server->getLevelByName($game)->getPlayers();
            
    $this->randomPlayer[$game] = $players[array_rand($players)]->getName();
            foreach(
    $server->getLevelByName($game)->getPlayers() as $p){
            
    $p->sendMessage(TextFormat::BOLD TextFormat::WHITE $this->randomPlayer[$game] . " was selected as beast!");
    /* It shows all players name! D':*/
        
    }
    And it didn't work...
    And I try to get the player name with:
    PHP:
    public function getRandomPlayer($game){
    return 
    $this->randomPlayer[$game];
    }
    Last edited: Dec 31, 2015
  6. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    PHP:
    $players $server->getOnlinePlayers();
    $server->broadcastMessage($players[mt_rand(0count($players) - 1)]->getName());
  7. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    Use PHP methods that lets you to get random things
    E.g. array_rand() rand() mt_rand()
  8. hoyinm14mc
    Offline

    hoyinm14mc Notable Member Plugin Developer

    Joined:
    Jun 4, 2014
    Posts:
    501
    Plugins:
    9
    Minecraft User:
    hoyinm14mc
    Oh that's PHP 7
  9. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    Actually, if you want to do so, you have to be very careful on memory leak as well as checking player online before doing things upon it.
    AndrewBit likes this.
  10. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4
    I don't know what I can do! :'/ I try A LOT of things for save a random player name... I try to save he to a class, to an array... I don't know what I can do...
  11. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    PHP:
    $players $level->getPlayers();
    $randomPlayer $players[array_rand($players)];
    $this->yourVariable[$randomPlayer->getName()] = $randomPlayer->getName();
    hoyinm14mc and AndrewBit like this.
  12. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    If you already save it in the key, don't waste memory saving the name on the value again. Instead, put a Boolean true for the value.

Share This Page

Advertisement