Why I created this thread I've seen a few threads talking about timers and tasks but all the useful information is surrounded by other posts saying "learn PHP" or "learn the PocketMine-API", these threads have some useful information but don't really give you a clear answer on how to create a timer or use a task. Why do I need a timer? Creating timers in your plugins allows you to create Mini-games and have things happen with a delay. So how do I create a timer? To create a timer you will need to use a repeating task, a repeating task will execute the task after the specified amount of ticks have passed. Creating a repeating task that executes every 20 ticks will create a task that runs every second, here's an example of how to create a repeating task: PHP: public function createTask() { $task = new Task($this); //The Task handler $h = $this->getServer()->getScheduler()->scheduleRepeatingTask($task, 20); //Set the task's handler $task->setHandler($h);} This will create the task but you will have no easy way of tracking your tasks, to easily track your tasks you should create an array and store it as a variable to keep track of all your tasks, here's an example of how to do that: PHP: //The PHP file that will runpublic $tasks = [];public function createTask() { $task = new Task($this); //The Task handler $h = $this->getServer()->getScheduler()->scheduleRepeatingTask($task, 20); //Set the task's handler $task->setHandler($h); //Add the task to the array $this->tasks[$task->getTaskId()] = $task->getTaskId();} This will add the ID of the task to the $this->tasks array each time you create one. The easiest way to create the task or timer is to create another class (PHP file), here's an example of a basic task: PHP: <?phpnamespace name\space;use pocketmine\scheduler\PluginTask;use plugin\Main;class Task extends PluginTask { public $plugin; public $seconds; public function __construct(Main $plugin, Player $player, $time) { parent::__construct($plugin); $this->plugin = $plugin; $this->seconds = 0; } public function getPlugin() { return $this->plugin; } public function onRun($tick) { //Sends a message to the console with how many seconds the task has been running for $this->getPlugin()->getLogger()->info("Task has run for " . $this->seconds . "!"); //Checks if $this->seconds has the same value of 10 if($this->seconds === 10) { //Tells the console that the task is being stopped and at how many seconds $this->getPlugin()->getLogger()->info("A Task has run for " . $this->seconds . " and is now stopping..."); //Calls a function from your Main that removes the task and stops it from running $this->getPlugin()->removeTask($this->getTaskId()); } //Adds 1 to $this->seconds $this->seconds++; }} So you've got your task set up and running but your wondering how to cancel it and stop it from running, the easiest way to do this is to create a function in your Main file that cancels a task, it should look something like this: PHP: public function removeTask($id) { //Reomves the task from your array of tasks unset($this->tasks[$id]); //Cancels the task and stops it from running $this->getServer()->getScheduler()->cancelTask($id);} These are the basics of how to create a simple repeating task and how to use one as a timer, I hope this helped you or was of some use to you, if there is anything I missed or anything you would like me to add be sure to let me know! I have recreated this thread over at the new forums: forums.pmmp.gq
It is a bad practice to create classes with the same simple name as a class in the PocketMine source.
PHP: public function removeTask($id) { //Reomves the task from your array of tasks unset($this->tasks[$id]); //Cancels the task and stops it from running $this->getServer()->getScheduler()->cancelTask($id);} $id is the first and only parameter of removeTask().
men pleace can you give me full code like i do /ti and your timer will runing sorry for bad english !
uhmm any update? Code: [01:00:48] [Server thread/CRITICAL]: "Could not pass event 'pocketmine\event\player\PlayerInteractEvent' to 'Parkour v1.0.0': Argument 2 passed to Parkour\handler::__construct() must be an instance of pocketmine\Player, none given, called in C:\Users\Owner\Desktop\XdNetWork\XdNetWork\plugins\Parkour\src\Parkour\parkour.php on line 28 on Parkour\parkour Handler PHP: public function __construct(parkour $plugin, Player $player, $time) { parent::__construct($plugin); $this->plugin = $plugin; $this->seconds = 0; } Main class PHP: public function createTask() { $task = new handler($this);
You are asking for $player and $time in the handler class, but you did not pass these values when you construct the handler. Also, classes usually start with upper case. This will make it easier to understand for other people.
but... how to pass a value to $player? ohh thx...I just deleted it LOL but there is still a warning...
This thread has been recreated over at the new forums forum.pmmp.gq, please continue any discussions over there