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

Activate and Cancel Task Help

Discussion in 'Help' started by ZackyVN, Dec 26, 2018.

  1. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    150
    So i am trying to do Wing Plugin but i dont have any knowledge about task. Can someone help me to activate and cancel the task?
     
  2. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Ah, the plugin that gives you the ability to /fly?
     
  3. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    150
    Wing Particles
     
  4. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    150
    I mean:
    When a player use /wing heartandwater on, the server will automatic activate WingHeartAndWater task at him ( Repeating Task)
    And when a player use /wing heartandwater off, the server will automatic cancel the WingHeartAndWater Task
     
  5. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    I was going to link you to a resource but seems like most are outdated here. Anyway...

    You start by creating a class that extends pocketmine's Task class.
    PHP:
    class WingTask extends Task{
        public function 
    onRun(int $tick) : void{
        }
    }
    Notice the empty onRun() method I declared in the class? Pocketmine will keep calling that method after every x interval. You call set the interval by scheduling the task.
    PHP:
    $plugin->getScheduler()->scheduleRepeatingTask(new WingTask(), 20);
    That schedules the task to repeat with a 20 tick delay in between. Anything you put inside the onRun() method will be called every 20 ticks.
    To cancel the task, you could map the task by the player's identity and work your way up from there.
    PHP:
    public $tasks = [];
    PHP:
    $task = new WingTask();
    $this->tasks[$player->getId()] = $task;
    $plugin->getScheduler()->scheduleRepeating...
    Then to cancel the task...
    PHP:
    //Possibly do an isset check before this
    $task $this->tasks[$player->getId()];
    unset(
    $this->tasks[$player->getId()]);

    $task->getHandler()->cancel();
     
    ZackyVN likes this.
  6. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    150
    wait what is $plugin and $this->tasks? and do i need to create a function for Unset task?
    I need the full code to understand because i from other country
     
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    Contstruct? It's your plugin instance. If you are calling the code from your main plugin file (the class that extends PluginBase), you can replace it by $this since you're calling it from inside your main plugin file.
     
    ZackyVN likes this.
  8. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    If you like to, you can pass anything in the task's constructor. Have you worked with classes in PHP before?
     
  9. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    150
    and $this->task?
     
  10. ZackyVN

    ZackyVN Baby Zombie

    Messages:
    150
    Umm hello?
     
  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.