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

My timer code isnt working

Discussion in 'Development' started by kaliiks, Dec 23, 2016.

  1. Orlando092

    Orlando092 Creeper

    Messages:
    5
    GitHub:
    orlando092
    PHP:
    public function onRun($currentTick){

            
    $players $this->plugin->getServer()->getLevelByName("GDHub")->getPlayers();
    Foreach(
    $players as $array){
    Echo(
    count($players));
            if (
    count($players) >= 1) {
    Echo(
    'test 1');
                
    $this->tts--;
                
    $this->plugin->getServer ()->broadcastPopup($this->plugin->gameprefix C::GREEN "Starting in" $this->tts$array);
                if (
    $this->tts == 0) {
    Echo(
    'test 2');
                    
    $this->plugin->getServer()->broadcastMessage($this->gameprefix "Test");
                     }
                }
            }
        }
    }
    Try this
     
  2. kaliiks

    kaliiks Zombie

    Messages:
    250
    When i change
    PHP:
    if(count($pl) > 1) {
    to
    PHP:
    if(count($pl) > 0) {
    it work and start timer with 0 players in world
    but if it is
    PHP:
    if(count($pl) > 1) {
    and i join arena it doesnt start.. It start when join 2nd player or why it dont work???
     
  3. Orlando092

    Orlando092 Creeper

    Messages:
    5
    GitHub:
    orlando092
    You should use count($pl) >= 1
     
  4. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    Learn how logic operators work in PHP.
    PHP:
    // Check if there is at least one player in the world
    if(count($pl) >= 1) {
      
    // do stuff
    }
    You should already know PHP before you start writing PocketMine plugins, or you'll be stuck here with stupid issues like this until you finally decide to learn PHP.
     
    Besher and Orlando092 like this.
  5. kaliiks

    kaliiks Zombie

    Messages:
    250
    Why it doesnt work with count($pl) >= 5 ?? It is god or no??
    PHP:
    <?php

    namespace GetDown;

    use 
    pocketmine\Player;
    use 
    pocketmine\Server;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\utils\TextFormat as C;
    use 
    GetDown\Main;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\plugin\Plugin;

    class 
    Timer extends PluginTask
    {
        private 
    $plugin;
        public 
    $tts;

        public function 
    __construct(Main $plugin)
        {
            
    $this->plugin $plugin;
            
    $this->tts 30;
            
    parent::__construct($plugin);
        }

        
    /**
         * @param $currentTick
         */
        
    public function onRun($currentTick)
        {
            foreach (
    $this->plugin->getServer()->getLevelByName("GDHub")->getPlayers() as $pl) {
                if (
    count($pl)>=5) {
                    
    $this->tts--;
                    
    //$pl->setExpLevel($pl->getExpLevel() - 1);
                    
    $pl->sendPopup($this->tts);
                    if (
    $this->tts == 0) {
                        
    $this->plugin->getServer()->getScheduler()->cancelTask($this->getTaskId());
                        
    $pl->sendMessage($this->plugin->gameprefix C::GREEN "Game started!");
                        
    $this->plugin->changePhase(2);
                        
    $pl->teleport(new Vector3(111"gd1"));
                        
    $this->plugin->getServer()->getScheduler()->cancelTask($this->getTaskId());
                    }
                }
            }
        }
    }
     
  6. Sandertv

    Sandertv Zombie Pigman Poggit Reviewer

    Messages:
    786
    GitHub:
    Sandertv
    You're counting a non-array. You can't count a foreached array.
     
  7. GamakCZ

    GamakCZ Zombie Pigman

    Messages:
    598
    GitHub:
    GamakCZ
    count can be used only when the array

    PHP:
    <?php

    namespace GetDown;

    use 
    pocketmine\Player;
    use 
    pocketmine\Server;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\utils\TextFormat as C;
    use 
    GetDown\Main;
    use 
    pocketmine\math\Vector3;
    use 
    pocketmine\plugin\Plugin;

    class 
    Timer extends PluginTask
    {
        private 
    $plugin;
        public 
    $tts;

        public function 
    __construct(Main $plugin)
        {
            
    $this->plugin $plugin;
            
    $this->tts 30;
            
    parent::__construct($plugin);
        }

        
    /**
         * @param $currentTick
         */
        
    public function onRun($currentTick)
        {
            
    $p $this->plugin->getServer()->getLevelByName("GDHub")->getPlayers();
            if(
    count($p)>=5) {
                foreach(
    $p as $pl) {
                    
    $this->tts--;
                    
    //$pl->setExpLevel($pl->getExpLevel() - 1);
                    
    $pl->sendPopup($this->tts);
                    if (
    $this->tts == 0) {
                        
    $this->plugin->getServer()->getScheduler()->cancelTask($this->getTaskId());
                        
    $pl->sendMessage($this->plugin->gameprefix C::GREEN "Game started!");
                        
    $this->plugin->changePhase(2);
                        
    $pl->teleport(new Vector3(111"gd1"));
                        
    $this->plugin->getServer()->getScheduler()->cancelTask($this->getTaskId());
                    }
                }
            }
        }
    }
     
  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.