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

How do detect the number of players in a certain world and store it in a variable.

Comments in 'Plugin Development' started by Calrizer, May 27, 2015.

  1. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    Hi people of PocketMine,

    Just a quick question: How do detect the number of players in a certain world and store it in a variable?

    Any response will be helpful. Thx
  2. CraftYourBukkit
    Offline

    CraftYourBukkit Notable Member Plugin Developer

    Joined:
    Jan 20, 2015
    Posts:
    1,022
    Plugins:
    2
    Minecraft User:
    CraftYourBukkit
  3. BlubberBoy333
    Offline

    BlubberBoy333 Active Member

    Joined:
    Aug 31, 2014
    Posts:
    177
    Minecraft User:
    Blubberboy333
    In the beginning of the code, after the namespace, you should have
    PHP:
    use pocketmine\Level;
    And when you need the players in the world, have this
    PHP:
    $this->getLevel()->getPlayers();
    I think this will work, please correct me if i'm wrong
  4. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    You don't need to import pocketmine\Level.
    Level->getPlayers() returns an array of players. You have to count() it (as stated in the question) to get the size of the array.
    EvolSoft likes this.
  5. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    So if I wanted to get it working I would not use this:

    PHP:
    $event->getPlayer()->sendMessage(.$player->getName()." joined ".$this->getLevel($mapname)->getPlayers()."/30 In game.");
    How could I get it to work correctly?
  6. BlubberBoy333
    Offline

    BlubberBoy333 Active Member

    Joined:
    Aug 31, 2014
    Posts:
    177
    Minecraft User:
    Blubberboy333
    That period you have before $player shouldn't be there
    And it should be
    PHP:
    $event->getPlayer()->getDisplayName()->sendMessageMessage);
  7. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    Yeah I know, but that not my problem.

    I have now tried this:

    PHP:
    public $ingame;

    $event->getPlayer()->sendMessage("[AW] ".$player->getName()." joined ".$ingame."/30 in game.");
    But I keep getting this error mentioned here: https://forums.pocketmine.net/threads/super-annoying-message.8636/#post-89972
  8. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    Oh wait this is correct...
    PHP:
    public $ingame;

    $ingame count($this->getLevel($mapname)->getPlayers())

    $event->getPlayer()->sendMessage("[AW] ".$player->getName()." joined ".$ingame."/30 in game.");
    But still same error
  9. BlubberBoy333
    Offline

    BlubberBoy333 Active Member

    Joined:
    Aug 31, 2014
    Posts:
    177
    Minecraft User:
    Blubberboy333
    Oh well if you want to have the $event variable, you should have something like this
    PHP:
    public function onEvent(EventName $event){

    }
  10. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    Here's my whole code for the section I working on...

    PHP:
    public $ingame;

    public function 
    playerBlockTouch(PlayerInteractEvent $event){
           
            if(
    $event->getBlock()->getID() == 323 || $event->getBlock()->getID() == 63 || $event->getBlock()->getID() == 68){
                
    $sign $event->getPlayer()->getLevel()->getTile($event->getBlock());
                if(!(
    $sign instanceof Sign)){
                    return;
                }
               
                
    $sign $sign->getText();
               
                
    $player $event->getPlayer();
               
                if(
    $sign[0]=='[AWSet]'){
                   
                    if(empty(
    $sign[1]) !== true){
                       
                        
    $mapname $sign[1];
                       
                        
    $ingame count($this->getLevel($mapname)->getPlayers())
                       
                        
    $event->getPlayer()->sendMessage("[AW] Joining game: ".$mapname."...");
                       
                        if(
    Server::getInstance()->loadLevel($mapname) != false){
                           
                            
    $event->getPlayer()->sendMessage("[AW] Teleporting...");
                           
                            
    $event->getPlayer()->teleport(Server::getInstance()->getLevelByName($mapname)->getSafeSpawn());
                           
                            
    $event->getPlayer()->sendMessage("[AW] ".$player->getName()." joined ".$ingame."/30 in game.");
                           
                        }else{
                          
                            
    $event->getPlayer()->sendMessage("[AW] There was an error connnecting to: ".$mapname.".");
                           
                            
    $event->getPlayer()->sendMessage("[AW] Please try again.");
                        }
                    }
                }
            }
        }
    Any pointers??
  11. BlubberBoy333
    Offline

    BlubberBoy333 Active Member

    Joined:
    Aug 31, 2014
    Posts:
    177
    Minecraft User:
    Blubberboy333
    PHP:
    public $ingame;

    public function 
    playerBlockTouch(PlayerInteractEvent $event){
          
            if(
    $event->getBlock()->getID() == 323 || $event->getBlock()->getID() == 63 || $event->getBlock()->getID() == 68){ // to get the id, it should be $event->getBlock()->getId. The D doesn't need to be capitalised
                
    $sign $event->getPlayer()->getLevel()->getTile($event->getBlock());
                if(!(
    $sign instanceof Sign)){
                    return;
                }
              
                
    $sign $sign->getText();
              
                
    $player $event->getPlayer(); // $player = $event->getPlayer->getDisplayName();
              
                
    if($sign[0]=='[AWSet]'){
                  
                    if(empty(
    $sign[1]) !== true){
                      
                        
    $mapname $sign[1];
                      
                        
    $ingame count($this->getLevel($mapname)->getPlayers()) // You forgot a ';' here
                      
                        
    $event->getPlayer()->sendMessage("[AW] Joining game: ".$mapname."...");
                      
                        if(
    Server::getInstance()->loadLevel($mapname) != false){
                          
                            
    $event->getPlayer()->sendMessage("[AW] Teleporting...");
                          
                            
    $event->getPlayer()->teleport(Server::getInstance()->getLevelByName($mapname)->getSafeSpawn());
                          
                            
    $event->getPlayer()->sendMessage("[AW] ".$player->getName()." joined ".$ingame."/30 in game.");
                          
                        }else{
                         
                            
    $event->getPlayer()->sendMessage("[AW] There was an error connnecting to: ".$mapname.".");
                          
                            
    $event->getPlayer()->sendMessage("[AW] Please try again.");
                        }
                    }
                }
            }
        }
    Calrizer likes this.
  12. Calrizer
    Offline

    Calrizer Active Member

    Joined:
    Jan 17, 2015
    Posts:
    102
    Minecraft User:
    callumdrain
    I have made those changes, it now runs but when I tap the sign in the game it says this:
    "Undefined property: pocketmine\event\player\PlayerInteractEvent::$getPlayer"

    Any ideas??
  13. BlubberBoy333
    Offline

    BlubberBoy333 Active Member

    Joined:
    Aug 31, 2014
    Posts:
    177
    Minecraft User:
    Blubberboy333
    Are you using
    pocketmine\event\Listener
  14. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    What the? Do you know what you are saying? Why do you need to use Listener, while this has absolutely nothing to do with his error? And why do you need to getDisplayName()? It seems like you are just reciting some replies in other irrelevant help threads.
    First, please do not use Server::getInstance() if it is possible to get a direct server instance.
    Second, block IDs won't be 323, so that check is useless. Why is everyone doing that?
    Third, as @BlubberBoy333 said (that is the only thing you did meaningful here), the semicolon is missing.
    Fourth, consider loading the level in onEnable() directly. It makes your life better.
    Fifth, `empty($sign[1]) !== true` is wrong. A valid sign always returns an array with 4 strings, so it would never be empty, so you don't need to do this check. Also, empty() always returns a boolean, so it is pointless to do !== true instead of a !
    Somewhere you used ->getPlayer instead of ->getPlayer() on a PlayerInteractEvent.

Share This Page

Advertisement