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

creating a timer with milliseconds

Discussion in 'Development' started by rektpixel, Jan 18, 2018.

  1. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    Hello, I have this code:
    main:
    PHP:
        public function onMove(PlayerMoveEvent $event){
            
    $level $this->getServer()->getDefaultLevel();
            
    $player $event->getPlayer();
            
    $x $player->getX();
            
    $y $player->getY();
            
    $z $player->getZ();
            
    $IBlock $level->getBlock(new Vector3($x$y-2$z));
            
    $GBlock $level->getBlock(new Vector3($x$y-1$z));
            if(
    $IBlock->getId() == 42) {
                if(
    $GBlock->getId() == 41) {
                    
    $this->seconds time();
                    
    $this->getServer()->getScheduler()->scheduleRepeatingTask(new TimerTask($this$player), 20);
                }
            }
        }
    }
    TimerTask:
    PHP:
        public function onRun($currentTick){
            
    $this->secondsElapsed time();
            
    $sec $this->secondsElapsed;
            
    $timer $sec $this->getOwner()->seconds;
            
    $millis $timer 10;
            
    $nanos $millis 10;
            
    $this->player->sendPopup(TextFormat::YELLOW "§eParkour Timer:§f " $timer":" $millis "" $nanos "");
        }
    }
    I want to display the time in seconds, milliseconds and nanoseconds but this shows me a popup which displays this:
    edit: changed $secs in the popup to $timer but still not working
     

    Attached Files:

    Last edited: Jan 18, 2018
  2. kenygamer

    kenygamer Banned Banned

    Messages:
    106
    GitHub:
    kenygamer
    You should use the block constants found here.
     
    Last edited: Jan 19, 2018
  3. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    that works tho, the problem is showing the timer in microtime
     
  4. kenygamer

    kenygamer Banned Banned

    Messages:
    106
    GitHub:
    kenygamer
    Still, using the block constants will help developers understand what you are trying to do.
     
  5. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    well basically the blocks have nothing to do with what I'm trying to do
     
  6. kenygamer

    kenygamer Banned Banned

    Messages:
    106
    GitHub:
    kenygamer
    The block IDs are irrelevant to your question: yes, it's just a coding practice tip.
     
    Last edited: Feb 4, 2018
    rektpixel likes this.
  7. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    You're really-over complicating this. All you need to do is store a seconds property(i.e. public/protected/private $secondsElapsed) and increment it until they're done. Just store the task in an array under the player's name or UUID and retrieve/stop the task once they finish the course. You can use the function gmdate/date to format the numbers into a time string for you. In your case, you'd use gmdate/date("i:s", $secondsVariable), where "i" means the minutes and "s" is the seconds. Note that gmdate/date don't have an option for nanoseconds nor milliseconds that I know of.

    Here's how I would do the task(Don't mind the type-hinting, I did this all on Sublime off of the top of my head):

    PHP:
    <?php

    use pocketmine\scheduler\PluginTask;
    use 
    pocketmine\Player;

    class 
    TimerTask extends PluginTask {

       
    /** @var int */
       
    private $secondsElapsed 0;

       
    /** @var Player */
       
    private $player;

       public function 
    __construct(YourMainClassHere $pluginPlayer $player) {
           
    parent::__construct($plugin);
           
    $this->player $player;
       }

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

       
    /**
       * @var $currentTick
       */
       
    public function onRun($currentTick) {
           
    $this->secondsElapsed++;
           
    $this->getPlayer()->sendPopup("§eParkour Timer:§f " gmdate("i:s"$this->secondsElapsed));
       }

    }
     
    rektpixel likes this.
  8. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    this didn't work I got 00:1 (1 being the second, 00 didn't change) However I did come up with this but I'm unsure if it's the correct way of doing it but this does work just that the time in which the digits .00 (millisecond and nanosecond) update seems a bit choppy as sometimes they'll go fast and sometimes slow down but I think this is just the laggy server I'm testing this on since I usually experience lagback on this server.
    plus I always get this message in the console (not only after I added this timer but b4 as well):
    Code:
    [16:20:30] [Server thread/WARNING]: Can't keep up! Is the server overloaded?
    
    anyway here's what I've done:
    PHP:
    <?php
    namespace timer;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\utils\TextFormat;
    use 
    pocketmine\Player;

    class 
    TimerTask extends PluginTask{
        private 
    $player;
     
        public function 
    __construct($pluginPlayer $player){
            
    $this->plugin $plugin;
            
    $this->player $player;
            
    parent::__construct($plugin);
        }
        public function 
    onRun($currentTick){
            
    $this->secondsElapsed time();
            
    $sec $this->secondsElapsed;
            
    $sec microtime(true);
            
    $timer $sec $this->getOwner()->seconds;
            
    $time round($timer2);
            
    $this->player->sendPopup("§6Parkour Timer:§f " $time "");
        }
    }
     
    Last edited: Jan 19, 2018
  9. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Is that exactly what you wrote?
     
  10. Irish

    Irish Baby Zombie

    Messages:
    156
    GitHub:
    irishpacks
    Make sure to cancel the tasks after they're done, otherwise you'll keep all of those tasks running and that's no bueno.
     
    rektpixel likes this.
  11. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    Ahhhh yes it is..
     
  12. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    My guesses for why it wouldn't work:
    • You are never creating that class instance ("new TimerTask") anywhere (or else you will be getting an error whenever you create it.
    • You are running on a very old version of PMMP (the Task::onRun() function is now strict-typed).
    • Maybe that isn't your exact code? :p
     
    OnTheVerge likes this.
  13. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    are you asking why this isn't working?
    PHP:
        public function onRun($currentTick){
            
    $this->secondsElapsed time();
            
    $sec $this->secondsElapsed;
            
    $timer $sec $this->getOwner()->seconds;
            
    $millis $timer 10;
            
    $nanos $millis 10;
            
    $this->player->sendPopup(TextFormat::YELLOW "§eParkour Timer:§f " $timer":" $millis "" $nanos "");
        }
    }
     
  14. kenygamer

    kenygamer Banned Banned

    Messages:
    106
    GitHub:
    kenygamer
    Not really.
     
  15. kenygamer

    kenygamer Banned Banned

    Messages:
    106
    GitHub:
    kenygamer
    Anyway, the op already figured out how to fix it. Should be marked as solved.
     
    Last edited: Jan 19, 2018
  16. rektpixel

    rektpixel Baby Zombie

    Messages:
    186
    it works but it's still a bit choppy/laggy there is probably a better way to do it.
     
  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.