Solved How to make a countdown Task with minutes?

Discussion in 'Development' started by iBa4x, Mar 22, 2020.

  1. iBa4x

    iBa4x Silverfish

    Messages:
    24
    GitHub:
    iBa4x
    I know that when I want to make countdown I should use task!
    PHP:
    $this->getScheduler()->scheduleRepeatingTask(new NameTask($this), 20);
    and I know 20 mean 1 second so we will manage minutes to convert it to seconds.

    * every minute has 60 seconds !
    PHP:
    // $i mean Minutes !
    // $s mean Seconds !

    if($i >= && $s 0){
        
    $s--;
    }
    if(
    $s == && $i 0){
        
    $i--;
        
    $s 59;
    }
    so I tried making this coding for converting 10 minutes to seconds counting down and every 60 second a minute gone

    PHP:
    <?php

    class NameTask extends Task{

        public function 
    __construct(Main $plugin){
            
    $this->plugin $plugin;
            
    $this->miuntes 10;
            
    $this->seconds 0;
        }

        public function 
    onRun($tick){
            
    $i $this->miuntes;
            
    $s $this->seconds;
            if(
    $i >= && $s 0){
                
    $s--;
            }
            if(
    $s == && $i 0){
                
    $i--;
                
    $s 59;
            }
            
    $OnlinePlayer $this->plugin->getServer()->getOnlinePlayers();
            foreach(
    $OnlinePlayer as $player){
                if(
    $s <= 59 && $s >= 10){
                    
    $player->sendTip(TextFormat::GREEN $i." : ".$s);
                }else{
                    
    $player->sendTip(TextFormat::GREEN $i." : 0".$s);
                }
            }
        }

        public function 
    cancel() {
               
    $this->getHandler()->cancel();
        }
    }
    and I don't know what's wrong It moved just one second and stopped at " 9:59 "
    I hope you understand what I want to do and what the issue !
     
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,091
    GitHub:
    HimbeersaftLP
    By doing this:
    PHP:
            $i $this->miuntes;
            
    $s $this->seconds;
    you're making a copy of the variable, so instead of decrementing the class property, you just decrement a local variable (and then overwrite it with the unchanged class property on the next run).

    One fix would be to replace $i with $this->miuntes and $s with $this->seconds.
    Another way would be to use a reference instead:
    PHP:
            $i = &$this->miuntes;
            
    $s = &$this->seconds;
    although I don't think this is very good practise.

    btw you misspelled minutes
     
  3. iBa4x

    iBa4x Silverfish

    Messages:
    24
    GitHub:
    iBa4x
    I figured this out and I tried to think harder until I get it.
    Because I made this questions and I solve the issue so look why it's stopped at " 9:59 "
    Because the task repeat it self so I can't put a variable in it for second and miunte it will repeat the action the variable will take the number and the code will do it.
    After I fix this problem I got new one the countdown missing number 0 in second before move to anther minutes
    " 9:00 ", " 8:00 ", " 7:00 " ...
    so I coded the countdown again and finally I got it like this:
    PHP:
    if($this->miuntes >= && $this->seconds >= 0){
            
    $this->seconds--;
    }
    if(
    $this->seconds && $this->minutes 0){
            
    $this->miuntes--;
            
    $this->seconds 59;
    }
    and the hole countdown coding goes like this:

    PHP:
    <?php

    class NameTask extends Task{

        public function 
    __construct(Main $plugin){
            
    $this->plugin $plugin;
            
    $this->minutes10;
            
    $this->seconds 0;
        }

        public function 
    onRun($tick){
            if(
    $this->minutes>= && $this->seconds >= 0){
                
    $this->seconds--;
            }
            if(
    $this->seconds && $this->minutes 0){
                
    $this->minutes--;
                
    $this->seconds 59;
            }
            
    $OnlinePlayer $this->plugin->getServer()->getOnlinePlayers();
            foreach(
    $OnlinePlayer as $player){
                if(
    $s <= 59 && $s >= 10){
                    
    $player->sendTip(TextFormat::GREEN $this->minutes." : ".$this->seconds);
                }else{
                    
    $player->sendTip(TextFormat::GREEN $this->minutes." : 0".$this->seconds);
                }
            }
            if(
    $this->minutes== && $this->seconds == 0){
                
    $this->cancel();
            }
        }

        public function 
    cancel() {
               
    $this->getHandler()->cancel();
        }
    }
     
  4. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,961
    GitHub:
    sof3
    Using references unnecessarily is a bad idea. Consider writing assigning back $this->minutes = $i; after the last mutation of $i, vice versa $thi-s>seconds = $s;. Alternatively just use the full name `$this->seconds` every time and don't create local variables.
    See https://php.net/sprintf
    There is a flag that lets you add zero padding to a number.
     
    HimbeersaftLP likes this.
  5. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,961
    GitHub:
    sof3
    Of course, a much simpler way is to decrement only seconds, and assign
    PHP:
    $this->seconds--;
    $i = (int) floor($this->seconds 60);
    $s $this->seconds 60;
     
    HimbeersaftLP and iBa4x like this.
  6. iBa4x

    iBa4x Silverfish

    Messages:
    24
    GitHub:
    iBa4x
    Thx a lot you helped with let me know how to let variable as reference I think this will do help me a lot!
    I make a big progress, Thx a lot .
     
  7. iBa4x

    iBa4x Silverfish

    Messages:
    24
    GitHub:
    iBa4x
    I'll check this out : )
     
  8. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,961
    GitHub:
    sof3
    I'd even call it an antipattern in an untyped language without variable scopes. At least it would be more sane if they had behaviour like JavaScript's `let`.
     
    HimbeersaftLP likes this.

Share This Page

  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.