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

Add more worlds to counter players

Discussion in 'Plugin Help' started by WEATHERCRAFTYT1, Dec 1, 2020.

  1. WEATHERCRAFTYT1

    WEATHERCRAFTYT1 Baby Zombie

    Messages:
    121
    Hello I want this code here
    PHP:
      $CC count($this->getServer()->getLevelByName("World")->getPlayers());
    To add more worlds so it can count all once for my minigame plugin NPC.
     
  2. Pqoster

    Pqoster Spider Jockey

    Messages:
    25
    PHP:
        /**
         * @return int
         */
        
    public function cc(): int
       
    {
            return 
    count($this->getServer()->getLevelByName("Disney")->getPlayers()) + count($this->getServer()->getLevelByName("World")->getPlayers()) + count...;
        }

    // $this->cc();
     
  3. AsponPL

    AsponPL Spider Jockey

    Messages:
    47
    some examples:


    PHP:
        public function getPlayersOnWorld() : int {
            return 
    count($this->getServer()->getOnlinePlayers());
        }

    PHP:
        public function getPlayersOnSpecifyWorlds() : int{

            
    $worlds = ["world""minigame1""minigame2"];
            
    $playersCount 0;

            foreach(
    $worlds as $world)
                
    $playersCount += count($this->getServer()->getLevelByName($world)->getPlayers());
           
            return 
    $playersCount;
        }
     
  4. OguzhanUmutlu

    OguzhanUmutlu Witch

    Messages:
    63
    GitHub:
    OguzhanUmutlu
    More efficient function:
    PHP:
        public function getWorldsPlayerCount(array $worlds) : int{
            
    $playersCount 0;
            foreach(
    $worlds as $world)
                
    $playersCount += count($this->getServer()->getLevelByName($world)->getPlayers());
            return 
    $playersCount;
        }
    Usage:
    PHP:
    $this->getWorldsPlayerCount(["myworld","myotherworld"]);
    // Returns worlds player count
     
  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.