Advertisement
  1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Tutorial Creating a timer with Tasks

Comments in 'Resources' started by CrazedMiner, Sep 8, 2015.

  1. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    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($task20);
        
    //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 run
    public $tasks = [];

    public function 
    createTask() {
        
    $task = new Task($this);
        
    //The Task handler
        
    $h $this->getServer()->getScheduler()->scheduleRepeatingTask($task20);
        
    //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:
    <?php

    namespace name\space;

    use 
    pocketmine\scheduler\PluginTask;

    use 
    plugin\Main;

    class 
    Task extends PluginTask {

        public 
    $plugin;
        public 
    $seconds;

          public function 
    __construct(Main $pluginPlayer $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
    Last edited: Nov 21, 2016
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,294
    Plugins:
    11
    Minecraft User:
    PEMapModder
    It is a bad practice to create classes with the same simple name as a class in the PocketMine source.
    Falk likes this.
  3. AndrewBit
    Offline

    AndrewBit Notable Member

    Joined:
    Jun 18, 2015
    Posts:
    435
    Minecraft User:
    AndrewBit4


    This is exactly what I was looking for..
    Thanks <3
    yEduG0D and CrazedMiner like this.
  4. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    You didnt define $id
  5. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    Where? In which class?
    CrazedMiner likes this.
  6. Hotshot_9930
    Offline

    Hotshot_9930 Notable Member Plugin Developer

    Joined:
    May 26, 2014
    Posts:
    665
    Plugins:
    2
    Minecraft User:
    HotshotHD
    in the removeTask() function.
    What is $id? I dont see it defined anywhere.
  7. Gamecrafter
    Offline

    Gamecrafter

    Joined:
    Nov 20, 2014
    Posts:
    978
    Plugins:
    9
    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(). :p
  8. XWITHERHEROX
    Offline

    XWITHERHEROX Active Member

    Joined:
    Jun 4, 2015
    Posts:
    57
    Minecraft User:
    ReDaRWiTheR
    men pleace can you give me full code
    like i do /ti
    and your timer will runing
    sorry for bad english ! :)
  9. RekkuzaRage
    Offline

    RekkuzaRage Active Member

    Joined:
    Apr 15, 2014
    Posts:
    251
    Minecraft User:
    RekkuzaRage
    What's the point of handlers? I can use and cancel task perfectly without setting the handler.
    Hotshot_9930 and PEMapModder like this.
  10. XdmingXD
    Offline

    XdmingXD New Member

    Joined:
    Jun 24, 2016
    Posts:
    12
    Minecraft User:
    XdmingXd
    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 $pluginPlayer $player$time) {
      
    parent::__construct($plugin);
      
    $this->plugin $plugin;
      
    $this->seconds 0;
      }
    Main class

    PHP:
    public function createTask() {
      
    $task = new handler($this);
    Last edited: Jul 28, 2016
  11. SOFe
    Offline

    SOFe Banned

    Joined:
    May 28, 2016
    Posts:
    386
    Minecraft User:
    Herobrine
    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.
  12. XdmingXD
    Offline

    XdmingXD New Member

    Joined:
    Jun 24, 2016
    Posts:
    12
    Minecraft User:
    XdmingXd
    but... how to pass a value to $player?
    ohh thx...I just deleted it LOL but there is still a warning...
  13. CrazedMiner
    Offline

    CrazedMiner Notable Member Plugin Developer

    Joined:
    Jan 31, 2015
    Posts:
    348
    Plugins:
    2
    Minecraft User:
    CrazedMinerYT
    This thread has been recreated over at the new forums forum.pmmp.gq, please continue any discussions over there :)

Share This Page

Advertisement