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

how to get the number of players with a specific game-mode

Discussion in 'Development' started by Atomization, Apr 19, 2018.

  1. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    Hey, Iv'e tried to use:
    PHP:
    $alivePlayers count($levelArena->getPlayers()->getGamemode(0));
    $alivePlayers is defining the number of players in the arena that are still alive (in game-mode 0) and NOT in game-mode 3 (Spectator)... but it gives me this error:
    PHP:
    [10:15:40] [Server thread/CRITICAL]: Could not execute task Atomization\SkyWars\GameSenderCall to a member function getGamemode() on array
    [
    10:15:40] [Server thread/CRITICAL]: Error"Call to a member function getGamemode() on array" (EXCEPTIONin "SKYWARS/src/Atomization/SkyWars/Main" at line 932
    Can someone help me Understand the error so i dont need to ask about it again.

    Here is another thread that I looked at but didn't find a solution.
    https://forums.pmmp.io/threads/player-count.4053/

    I also have written: This will simply teleport the players back to spawn on game end.
    PHP:
    if($alivePlayers == 1){
               foreach(
    $playersInArena as $pl){

                        
    $pl->getInventory()->clearAll();
                        
    $pl->getArmorInventory()->clearAll();
                        
    $pl->removeAllEffects();
                        
    $pl->teleport($this->plugin->getServer()->getDefaultLevel()->getSafeSpawn());
                        
    $pl->setHealth(20);
                        
    $pl->setFood(20);
                        
    $pl->setNameTag($pl->getName());

                        if(!empty(
    $this->plugin->api)){
                           
    $this->plugin->api->addMoney($pl,$money);
                        }
              }
    }
     
  2. SleepSpace9

    SleepSpace9 Slime

    Messages:
    78
    GitHub:
    sleepspace9
    $levelArena->getPlayers() will probably return an array of player objects so you need to loop through each player, retrieve the gamemode and count if gamemode is 0.
     
    RyanShaw likes this.
  3. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    so something like?
    PHP:
    $alivePlayers $levelArena->getPlayers()->count(getGamemode(0));
     
  4. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    nope, didnt work... get same error:

    PHP:
    [10:15:40] [Server thread/CRITICAL]: Could not execute task Atomization\SkyWars\GameSenderCall to a member function getGamemode() on array
    [
    10:15:40] [Server thread/CRITICAL]: Error"Call to a member function getGamemode() on array" (EXCEPTIONin "SKYWARS/src/Atomization/SkyWars/Main" at line 932
    and to clarify some things up... $levelArena is defined in the code as:
    PHP:
    $levelArena $this->plugin->getServer()->getLevelByName($arena);
     
  5. TwistedAsylumMC

    TwistedAsylumMC Slime

    Messages:
    96
    GitHub:
    twistedasylummc
    use a foreach loop as getPlayers() returns an array
     
  6. Tee7even

    Tee7even Slime

    Messages:
    81
    GitHub:
    tee7even
    Nope, something like learn basic programming:
    PHP:
    $count 0;
    foreach (
    $levelArena->getPlayers() as $player) {
        if (
    $player->getGamemode() === 0) {
            
    $count++;
        }
    }
    ^ That is assuming that "$levelArena->getPlayers()" returns an array of "Player" objects.
     
  7. HBIDamian

    HBIDamian HBIDamian Staff Member

    Messages:
    365
    GitHub:
    HBIDamian
    Ouch.... @Tee7even dial it down a little...
     
    RyanShaw likes this.
  8. Tee7even

    Tee7even Slime

    Messages:
    81
    GitHub:
    tee7even
    Lol, considering all those combinations in OP's posts, I think I gave a useful advice.
     
  9. xXNiceAssassinlo YT

    xXNiceAssassinlo YT Zombie Pigman

    Messages:
    499
    GitHub:
    xXNiceYT
    Look at https://github.com/pmmp/PocketMine-MP/blob/master/src/pocketmine/command/defaults/ListCommand.php
     
  10. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    Thank you all you your help! i was able to find a solution to my problem with @Tee7even advice.. also, i had and did try other variants of my code before you posted here. so i was still working on a fix so thanks again!
     
  11. eDroid

    eDroid Witch

    Messages:
    59
    GitHub:
    edroiid
    Here's a single liner:

    PHP:
    $playersAlive count(array_filter($levelArena->getPlayers(), function($p){return $p->getGamemode() === 0;}));
     
    Last edited: Apr 20, 2018
  12. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    you forgot a ()
     
  13. eDroid

    eDroid Witch

    Messages:
    59
    GitHub:
    edroiid
    fixed :D
     
  14. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    Thanks for the one liner xd
    But what I currently have is easier to understand. Lol
     
  15. Tee7even

    Tee7even Slime

    Messages:
    81
    GitHub:
    tee7even
    Ha..! :shoghi:
    PHP:
    $count 0; foreach ($levelArena->getPlayers() as $player) { if ($player->getGamemode() === 0) { $count++; } }
    What I mean is that it's not exactly a single liner. :)
    PHP:
    $playersAlive count(array_filter($levelArena->getPlayers(), function($p) {
        return 
    $p->getGamemode() === 0;
    }));
     
  16. Atomization

    Atomization Baby Zombie

    Messages:
    120
    GitHub:
    iAtomPlaza
    Without OOP, Its a single liner.. so it can get confusing if code is not written on OOP
     
  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.