1. The forums will be archived and moved to a read only mode in about 2 weeks (mid march).

get ALL players in certain gamemode

Discussion in 'Development' started by Zuruki, Apr 18, 2017.

  1. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    how can I foreach/ other method to get all players in survival,adventure,creative,spectator etc!
     
  2. Miste

    Miste Baby Zombie

    Messages:
    109
    GitHub:
    Misteboss
    foreach all the online players then if($p->getGamemode() === 0 [for survival gamemode] ){
    //code to execute
    }
     
    Zuruki likes this.
  3. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    ah. I was trying to make a HUD , replacing {SURVIVAL_PLAYERS} {SPECTATOR_PLAYERS} ,, how can l str_replace this?
     
  4. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    count it with an increment counter
     
  5. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    no... I mean .. str_replace {SURVIVAL_PLAYERS} // with people in survival . I am not understand you my friend xd
     
  6. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    i hope you arent using a translator or so,
    either ways let me elaborate it again
    you foreach all players then check for gamemode int, if it's in specified gamemode you increment said counter after that you replaces said string with said incremental counter
     
  7. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Something as simple as this:

    PHP:
    function formatPlayerGamemodesToString($string, array $players) : string{
    $gm = [=> [], => [], => [], => []];

    Foreach(
    $players as $player){
    $gm[$player->gamemode][] = $player->getName();
    }

    $string str_ireplace (['{SURVIVAL_PLAYERS}''{CREATIVE_PLAYERS}''{ADVENTURE_PLAYERS}''{SPECTATOR_PLAYERS}'], [implode(', '$gm[0]), implode(', '$gm[1]), implode(', '$gm[2]), implode(', '$gm[3])], $string);

    return 
    $string;
    }
    Usage:

    PHP:
    $string formatPlayerGamemodesToString("Players in survival : {SURVIVAL_PLAYERS}" [$player1$player2]);

    echo 
    $string;//if players are in in survival output should be: Players in survival : player1, player2
     
  8. Marabou

    Marabou Baby Zombie

    Messages:
    137
    GitHub:
    wiligangster
    PHP:
       foreach($this->getServer()->getOnlinePlayers() as $check) {
           
    $count 0;
              if (
    $check->getGamemode() == 3) {
                  
    $count++;
              }
               
    $check->sendPopup("there are ".$count." spectator(s) !");
     
    //You can use str_replace("{SPECTATOR_COUNT}", $count, $string); like for config
           
    }
              
    Example :)
     
    Last edited: Apr 19, 2017
    Zuruki likes this.
  9. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
    ahh thankyou! but what if a player changes gamemode back to survival. the increment counter stay the same... but Is false
     
  10. Marabou

    Marabou Baby Zombie

    Messages:
    137
    GitHub:
    wiligangster
    No it will change if you use a task
     
  11. Zuruki

    Zuruki Baby Zombie

    Messages:
    118
    GitHub:
    zuruki
  12. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks

    PlayerGameModeChangeEvent
     
  13. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    actually i think that's better then recalculating it every tick for every player
    even tho it would give off a significant memory impact
     
    Zuruki likes this.
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.