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

PluginTask

Discussion in 'Development' started by Donquih0te, Aug 14, 2017.

  1. Donquih0te

    Donquih0te Spider

    Messages:
    7
    GitHub:
    donkihotd
    Hi all. I wonder how it is possible to obtain the number of the traversed time PluginTask, that is, from the beginning to the end and bring this time?
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Since you haven't provided a unit time, I'll be ignoring unit time too.

    Method 1 of ∞:
    PHP:
    class extends PluginTask {

        private 
    $firstTick;

        public function 
    onRun(int $tick){
            if(
    $this->firstTick === null){
                
    $this->firstTick $tick;
            }
            
    $ticksElapsed $tick $this->firstTick;
        }
    }
    Method 2 of ∞:
    PHP:
    //declare global variable $initialTime
    //in the constructor, set $initialTime's value to time()
    //to find the time elapsed, subtract time() with $initialTime
    Your question was to get the end time of the task, I am assuming you are using a no-break repeating task. In that case you can set the task to a variable before scheduling the task, and create a public variable, or a function to fetch the elapsed time.
    PHP:
    /** @var Plugin $plugin */
    $task = new MyPluginTask($plugin);
    $plugin->getServer()->getScheduler()->scheduleRepeatingTask($task20);

    //Getting time before cancelling the task:
    $time $task->getTimeElapsed();
    $plugin->getServer()->getScheduler()->cancelTask($task->getTaskId());
     
    Donquih0te likes this.
  3. SOFe

    SOFe Administrator Staff Member PMMP Team Poggit Admin

    Messages:
    1,968
    GitHub:
    sof3
    Use class properties, not global variables... Never declare any named values in the global scope. If you have to (e.g. thread store keys, stream wrapper names), use the plugin namespace as the prefix.
     
    Muqsit likes this.
  4. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    I didn't mean to say a global variable, my bad
     
  5. Donquih0te

    Donquih0te Spider

    Messages:
    7
    GitHub:
    donkihotd
    thanks
     
  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.