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

Making countdown timer

Discussion in 'Development' started by Levi, Nov 28, 2017.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    I want to make a countdown kind of thing like 10.. 9.. 8.. 3.. 2.. 1 but it's not repeating again when I tried it
    my attempt:
    PHP:
    class DropParty extends PluginTask
    {

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

        public function 
    close()
        {
            
    $this->close true;
        }

        public function 
    onRun($tick)
        {

                switch (
    $this->seconds) {

                    case 
    15:
                        
    $this->plugin->getLogger()->info("Triggered");
                        break;
                    case 
    3:
                        
    $this->plugin->getServer()->broadcastMessage("drop party in 3");
                        break;
                    case 
    2:
                        
    $this->plugin->getServer()->broadcastMessage("drop party in 2");
                        break;
                    case 
    1:
                        
    $this->plugin->getServer()->broadcastMessage("drop party in 1");
                        break;
                    case 
    0:
                        
    $this->plugin->getServer()->broadcastMessage("WOOOOOOOOOOOOOOOOOOSH!");
                        break;
                }
                
    $this->seconds--;
            }
    }
    this is in my main class onEnable
    PHP:
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new DropParty($this), 20);
    I get this working after I restart the server but it doesn't repeat.. only repeat if I restart the server again
     
    Last edited: Nov 28, 2017
  2. XCodeMCPE

    XCodeMCPE Slime

    Messages:
    96
    GitHub:
    xcodemcpe
    }
    foreach($this->getOwner()->getServer()->getOnlinePlayers() as $player) {
    if($time === 20) { // every 20 ticks is one second
    $player->sendMessage("drop party in 3");
    $click = new ClickSound($player);
    $player->getLevel()->addSound($click);
    }
    if($time === 40) {
    $player->sendMessage("drop party in 2");
    $click = new ClickSound($player);
    $player->getLevel()->addSound($click);
    }
    if($time === 60) {
    $player->sendMessage("drop party in 1");
    $click = new ClickSound($player);
    $player->getLevel()->addSound($click);
    }
    if($time === 80) {
    $player->sendMessage("WOOOOOOOOOOOOOOOOOOSH!");
    $player->getLevel()->addSound(new AnvilUseSound($pl));
    }
     
  3. MioTaku

    MioTaku Witch

    Messages:
    69
    GitHub:
    uselesswaifu
    Hmm, to help you... I will ask one simply question... What happens once $this->seconds is less than 0? you never set the time back up to 15 that should fix your issue :p
     
    XCodeMCPE and Levi like this.
  4. XCodeMCPE

    XCodeMCPE Slime

    Messages:
    96
    GitHub:
    xcodemcpe
    scheduleRepeatingTask
     
  5. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    yea, basically im asking how i make it loop back to the top.
     
  6. MioTaku

    MioTaku Witch

    Messages:
    69
    GitHub:
    uselesswaifu
    PHP:
    class DropParty extends PluginTask
    {

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

        public function 
    close()
        {
            
    $this->close true;
        }

        public function 
    onRun($tick)
        {

                switch (
    $this->seconds) {

                    case 
    15:
                        
    $this->plugin->getLogger()->info("Triggered");
                        break;
                    case 
    3:
                        
    $this->plugin->getServer()->broadcastMessage("drop party in 3");
                        break;
                    case 
    2:
                        
    $this->plugin->getServer()->broadcastMessage("drop party in 2");
                        break;
                    case 
    1:
                        
    $this->plugin->getServer()->broadcastMessage("drop party in 1");
                        break;
                    case 
    0:
                        
    $this->plugin->getServer()->broadcastMessage("WOOOOOOOOOOOOOOOOOOSH!");
                        
    $this->seconds 15;
                        break;
                }
                
    $this->seconds--;
            }
    }
     
    NickTehUnicorn and Levi like this.
  7. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
  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.