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?
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
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();
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
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.
If you like to, you can pass anything in the task's constructor. Have you worked with classes in PHP before?