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

making a countdown timer in hud..

Discussion in 'Development' started by Levi, Dec 5, 2017.

  1. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    how can I make a hud that counts down>>?

    I tried this on repeatingtask
    PHP:
    $this->seconds 60;
    $this->player->sendPopup("TImer: ".$this->seconds);
    $this->seconds--;
    $this->player is defined as the player
     
  2. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    You're setting time back to 60 all times when it execute onRun() ..
     
  3. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    U sure?
     
  4. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    In code you provided it looks like that
     
  5. friscowz

    friscowz Baby Zombie Ban Evader Banned

    Messages:
    128
    GitHub:
    friscowzmcpe
    here is an example:
    PHP:
    class Timer extends PluginTask{

        private 
    $seconds 60;
        private 
    $players = [];

        
    /**
         * Timer constructor.
         * @param Plugin $owner
         * @param array $players
         */
        
    public function __construct(Plugin $owner, array $players)
        {
            
    parent::__construct($owner);
            
    $this->players $players;
        }

        
    /**
         * @param int $currentTick
         */
        
    public function onRun(int $currentTick)
        {
            --
    $this->seconds;
            foreach (
    $this->players as $player){
                if (
    $player instanceof Player){
                    
    $player->sendPopup("Timer: " $this->seconds);
                }
            }
        }
    }
     
    Remarkabless likes this.
  6. friscowz

    friscowz Baby Zombie Ban Evader Banned

    Messages:
    128
    GitHub:
    friscowzmcpe
    tell me what you want to make i will help you :)
     
    Levi likes this.
  7. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    Thnx . This will show like 2:30 in Timer:? 2 minutes in 3 seconds
     
  8. friscowz

    friscowz Baby Zombie Ban Evader Banned

    Messages:
    128
    GitHub:
    friscowzmcpe
    PHP:
    /**
    * @param int $int
    * @return string
    */
    public static function intToString(int $int) : string
    {
        
    $mins floor($int 60);
        
    $seconds floor($int 60);
        return ((
    $m 10 "0" "") . $m ":" . ($s 10 "0" "") . $s);
    }
    // self::intToString(60); will return "1:00"
     
    Levi likes this.
  9. Levi

    Levi Skeleton

    Messages:
    955
    GitHub:
    captainleviftw
    but $this->seconds--; means -1 every second.. from my understanding
     
  10. friscowz

    friscowz Baby Zombie Ban Evader Banned

    Messages:
    128
    GitHub:
    friscowzmcpe
    yes use --$this->seconds; it's the same thing but a little bit faster :)
     
  11. friscowz

    friscowz Baby Zombie Ban Evader Banned

    Messages:
    128
    GitHub:
    friscowzmcpe
    Here is what you want to do:
    on the Main class add this function:
    PHP:
    public function teleportPlayer(Player $player){
        new 
    TeleportTask($this$player);
    }
    and when you want to teleport a player execute it :), ! remember to add the Task class !
    PHP:
    class TeleportTask extends PluginTask{
        
        private 
    $time 60$player;

        
    /**
         * TeleportTask constructor.
         * @param Plugin $owner
         * @param Player $player
         */
        
    public function __construct(Plugin $ownerPlayer $player)
        {
            
    parent::__construct($owner);
            
    $this->setPlayer($player);
            
    $this->setHandler($owner->getServer()->getScheduler()->scheduleRepeatingTask($this20));
        }

        
    /**
         * @param int $currentTick
         */
        
    public function onRun(int $currentTick)
        {
            
    $this->setTime($this->getTime() - 1);
            if(
    $this->getTime() == 0){
                
    $this->getPlayer()->teleport($this->getPlayer()->getLevel()->getSpawnLocation(), 00);
                
    $this->getHandler()->cancel();
            } else {
                
    $this->getPlayer()->sendPopup("You will be teleported to spawn in " $this->getTime() . " second(s)...");
            }
            
        }

        
    /**
         * @return int
         */
        
    public function getTime() : int
        
    {
            return 
    $this->time;
        }

        
    /**
         * @param int $time
         */
        
    public function setTime(int $time)
        {
            
    $this->time $time;
        }

        
    /**
         * @return Player
         */
        
    public function getPlayer() : Player
        
    {
            return 
    $this->player;
        }

        
    /**
         * @param Player $player
         */
        
    public function setPlayer(Player $player)
        {
            
    $this->player $player;
        }
    }
     
  12. akmalrizki

    akmalrizki Spider

    Messages:
    11
    GitHub:
    Beaconcraft
    Give me plugin:'( world spawn Teleporting countdown popup
     
  13. Swourire

    Swourire Spider Jockey

    Messages:
    49
    GitHub:
    swourire
    If you want a plugin go to plugin request section pretty sure.
     
  14. BayAlper10

    BayAlper10 Silverfish

    Messages:
    17
    GitHub:
    BayAlper10
  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.