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. kaliiks

    kaliiks Zombie

    Messages:
    250
    help please
    PHP:
    <?php

    namespace GetDown;

    use 
    pocketmine\scheduler\PluginTask;
    use 
    GetDown\Main;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\Config;
    //use pocketmine\

    class Timer extends PluginTask {

        private 
    $plugin;

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

        public function 
    onRun($currentTick) {
                
    $cfg = new Config($this->plugin->getDataFolder()."config.yml"Config::YAML);
                
    $po $this->plugin->getServer()->getLevelByName("GDHub")->getPlayers();
                if(
    count($po) > 1)
                {
                    
    $tts 30;
                    
    $tts--;
                    
    $cfg->set("TimeToStart"$tts);
                    
    $cfg->save();
                    
    $timetostart $cfg->get("TimeToStart");
                    
    $po->sendMessage("Start in $timetostart");
                    if(
    $timetostart==0)
                    {
                        
    $po->plugin->sendMessage("sTarTedD");
                    }
                }
            }
    }
    In main file
    PHP:
    public function onEnable()
    {
        
    $this->getLogger()->info("Plugin enabled");
        @
    mkdir($this->getDataFolder());
        
    $this->saveResource("config.yml"true);
        
    $this->getServer()->getPluginManager()->registerEvents($this$this);
        
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new SignRefresher($this), 50);
        
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new Timer($this), 20);
        
    $this->saveDefaultConfig();
        
    $cfg = new Config($this->getDataFolder() . "config.yml"Config::YAML);
    }
     
    Last edited: Dec 23, 2016
  2. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    Your logic is really bad practice. Why are you saving the value of a variable into the config, just so you can get it's value again? That's unwise and inefficient.

    This might help you understand better:
    PHP:
    <?php

    namespace GetDown;

    use 
    pocketmine\scheduler\PluginTask;
    use 
    GetDown\Main;
    use 
    pocketmine\Player;
    use 
    pocketmine\utils\Config;
    //use pocketmine\

    class Timer extends PluginTask {

        private 
    $plugin;
        private 
    $ticks;

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

        public function 
    onRun($currentTick) {
                
    $cfg = new Config($this->plugin->getDataFolder()."config.yml"Config::YAML);
                
    $po $this->plugin->getServer()->getLevelByName("GDHub")->getPlayers();
                if(
    count($po) > 1)
                {
                    --
    $this->ticks;
                    
    $po->sendMessage("Start in " .  $this->ticks);
                    if(
    $this->ticks == 0)
                    {
                        
    $this->plugin->getServer()->getScheduler()->cancelTask($this->getTaskId());
                        
    $po->plugin->sendMessage("sTarTedD");
                    }
                }
            }
    }
     
    Muqsit likes this.
  3. imYannic

    imYannic Baby Zombie

    Messages:
    113
    Don't create the Config class in the onRun() function. This takes useless RAM and time
     
  4. imYannic

    imYannic Baby Zombie

    Messages:
    113
    In the last line, you do $po->plugin->sendMessage()...

    But you initialized $po as a player array?
     
  5. xBeastMode

    xBeastMode Shog Chips

    Messages:
    0
    I just mentioned the bigger problems, that is a minor problem that is up to them to fix.
     
  6. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Try to declare as less a variable as possible in the onRun($tick) function.
    Because it is a repeating task, onRun() is basically a for-loop (just without the "for").
    You can declare the variables in the constructor.
    PHP:
    private $something;
    public function 
    __construct(MainClass $plugin){
        
    parent::__construct($plugin);
        
    $this->plugin $plugin;
        
    $this->something 100;
    }

    public function 
    onRun($tick){
        
    $this->plugin->getLogger()->info('$something is '.$this->something);
        
    //output: $something is 100
    }
    Back to you..
    PHP:
        public function __construct(Main $plugin) {
            
    $this->plugin $plugin;
            
    $this->tts 30//countdown
            
    $this->players $plugin->getServer()->getLevelByName("GDHub")->getPlayers();
            
    parent::__construct($plugin);
        }

        public function 
    onRun($currentTick) {
            if(
    count($this->players) > 1) {
                --
    $this->tts//pre incrementing
                
    foreach ($this->players as $po) {
                    
    $po->sendMessage("Start in $this->tts");
                }
            }
        }
     
    Last edited: Dec 23, 2016
  7. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    Bad idea declaring the player count in the construct method, what if all the players leave? Then you'll have a useless task running which will take up way more resources than assigning a value to a variable.
     
  8. Thunder33345

    Thunder33345 Moderator Staff Member

    Messages:
    2,137
    GitHub:
    Thunder33345
    if there's anywhere that will modify the value of "TimeToStart" on config you should instate try assign a reference instated by using &
    $a = 5; $b = &$a; $c = &$a;
    example code
    so if a or b or c is changed they all will be changed together
     
  9. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    True, my bad.
     
  10. kaliiks

    kaliiks Zombie

    Messages:
    250
    I tested muqsit s and xbeastmode s code and it dont start timer i dont know what i doing bad
    my actually code:
    PHP:
    <?php

    namespace GetDown;

    use 
    pocketmine\Player;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\utils\Config;
    use 
    GetDown\Main;

    class 
    Timer extends PluginTask
    {
        private 
    $plugin;
        public 
    $gameprefix "§l§0[ §r§6Get§eDown §l§0]§r ";

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

                public
                function 
    onRun($currentTick)
                {
                    
    $players $this->plugin->getServer()->getLevelByName("GDHub")->getPlayers();
                    if (
    count($players) > 1) {
                        {
                            --
    $this->tts;
                            
    $po->sendPopup($this->plugin->gameprefix C::GREEN "Starting in" $this->tts);
                            if (
    $this->tts == 0) {
                                
    $this->plugin->getServer()->broadcastMessage($this->gameprefix "Test");
                            }

                            }
                        }
                    }
    }
    Console do not give any errors
     
  11. kaliiks

    kaliiks Zombie

    Messages:
    250
    What do you mean by saying "Schedule it" ?
     
  12. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    You must put that on onEnable() function on the Main class
    PHP:
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new Timer($this), 20);
    Don't forget to add GetDown\Timer on use
     
  13. kaliiks

    kaliiks Zombie

    Messages:
    250
    yes i have this all
     
  14. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    Have you got on your plugins folder 2 plugins with the same namespaces?
     
  15. kaliiks

    kaliiks Zombie

    Messages:
    250
  16. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    Try this
    PHP:
    /**
    * @param $currentTick
    */
    public  function onRun($currentTick) {
        
    $players $this->plugin->getServer()->getLevelByName("GDHub")->getPlayers();
        if(
    count($players) > 1) {
            
    $this->tts--;
            
    $po->sendPopup($this->plugin->gameprefix C::GREEN "Starting in" $this->tts);
            if(
    $this->tts == 0) {
                
    $this->plugin->getServer()->broadcastMessage($this->gameprefix "Test");
            }
        }
    }
     
  17. kaliiks

    kaliiks Zombie

    Messages:
    250
    this is the same code as my
     
  18. Diduhless

    Diduhless Baby Zombie

    Messages:
    199
    GitHub:
    Diduhless
    Nope, there was a wrong thing
     
  19. kaliiks

    kaliiks Zombie

    Messages:
    250
    Mmm It do not work and no errors
    PHP:
    <?php

    namespace GetDown;

    use 
    pocketmine\Player;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\utils\Config;
    use 
    pocketmine\utils\TextFormat as C;
    use 
    GetDown\Main;

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

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

        
    /**
         * @param $currentTick
         */
        
    public function onRun($currentTick)
        {
            
    $players $this->plugin->getServer()->getLevelByName("GDHub")->getPlayers();
            if (
    count($players) > 1) {
                
    $this->tts--;
                
    $players->sendPopup($this->plugin->gameprefix C::GREEN "Starting in" $this->tts);
                if (
    $this->tts == 0) {
                    
    $this->plugin->getServer()->broadcastMessage($this->gameprefix "Test");
                }
            }
        }
    }
     
  20. robske_110 (Tim)

    robske_110 (Tim) Wither Skeleton Poggit Reviewer

    Messages:
    1,342
    GitHub:
    robske110
    $players->sendPopup is guaranteed to not work, you are calling a method on an array, wtf.
    This indicates that, in the case of no errors being displayed, that that line was never run. Please place some echo()s in apropos places to debug your code.
     
  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.