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

How can I make task work once with command?

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

  1. iBa4x

    iBa4x Spider Jockey

    Messages:
    32
    GitHub:
    iBa4x
    I know if I want to let task work I do
    $this->getScheduler()->scheduleRepeatingTask(new NameTask($this), 20);

    What If I put it in command and I want if the first one write the command the second one can't
    let the task work while it's work!

    Hope you help me :).
     
  2. iBa4x

    iBa4x Spider Jockey

    Messages:
    32
    GitHub:
    iBa4x
    What I did now for fixing this issue I made config [Key => false]
    if it false task will work and would be Key => true
    so no one can do task job again but I don't want it in config what I do

    make sure that I don't know a lot in php I'm learning new stuff by ask and my ideas!!
    and I'm happy with that!
     
  3. tungstenvm

    tungstenvm Witch

    Messages:
    54
    if you only want the task running once, you can use DelayedTask instead of RepeatingTask

    Simply changing
    PHP:
    $this->getScheduler()->scheduleRepeatingTask(new NameTask($this), 20);
    Into
    PHP:
    #after 20 tick (= 1sec) the task will be excuted then cancelled
    $this->getScheduler()->scheduleDelayedTask(new NameTask($this), 20);
    And the Task will cancelled itself after one run
     
  4. iBa4x

    iBa4x Spider Jockey

    Messages:
    32
    GitHub:
    iBa4x
    PHP:
    #after 20 tick (= 1sec) the task will be excuted then cancelled
    $this->getScheduler()->scheduleDelayedTask(new NameTask($this), 20);
    This is for not work because it will work once so if I have a timer it would move one number!

    PHP:
    class NameTask extends Task{

            public function 
    __construct(Main $plugin){
                
    $this->plugin $plugin;
                
    $this->time =  10;
            }

            public function 
    onRun($tick){
                
    $this->time--;
                if(
    $this->time >= 0){
                    
    $this->plugin->getLogger()->info($this->time);
                }
                if(
    $this->time <  0){
                    
    $this->cancel();
                }
            }
            public function 
    cancel() {
               
    $this->getHandler()->cancel();
            }
    }
    I want from task If it's running! can't be running again while it's running !
    I hope you understand me :)
     
  5. tungstenvm

    tungstenvm Witch

    Messages:
    54
    ok i get you, you didnt want to create another task when already have one, i thought you didnt want to make the task run twice
     
    iBa4x likes this.
  6. iBa4x

    iBa4x Spider Jockey

    Messages:
    32
    GitHub:
    iBa4x
    NO PROBLEM, I learnt something new and I see that good for me.
    Thx about scheduleDelayedTask it would be helpful for I nice idae :)
     
  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.