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

Call a function from MainClass in a Task

Comments in 'Plugin Development' started by Guillaume351, Mar 29, 2015.

  1. Guillaume351
    Offline

    Guillaume351 Active Member Plugin Developer

    Joined:
    Oct 11, 2013
    Posts:
    150
    Plugins:
    1
    Minecraft User:
    Guillaume351
    How to Call a function from MainClass in a Task ? I need to use a function with a parameter that use values from the Mainclass, but it has to be a scheduled delayed task.

    Thank you.
  2. PEMapModder
    Offline

    PEMapModder Notable Member Plugin Developer

    Joined:
    Oct 9, 2013
    Posts:
    7,306
    Plugins:
    11
    Minecraft User:
    PEMapModder
    If it is a delayed task, pass the parameters into the constructor of the task.
    $this->getOwner() return an instance of the main class. Call method upon it, e.g. $this->getOwner()->getDataFolder().
    Guillaume351 likes this.
  3. Guillaume351
    Offline

    Guillaume351 Active Member Plugin Developer

    Joined:
    Oct 11, 2013
    Posts:
    150
    Plugins:
    1
    Minecraft User:
    Guillaume351
    I'm sorry I don't really understand. If, for example, I want to run the function checkIfPlayerWon($gameNumber) one second after a player get kill. What should I do ?
  4. Angelo Vidrio
    Offline

    Angelo Vidrio Active Member Plugin Developer

    Joined:
    Jan 16, 2014
    Posts:
    123
    Plugins:
    5
    Minecraft User:
    LilCrispy45
    This will be in your Main file.
    PHP:
    public function onDeath(PlayerDeathEvent $event)  {
         
    //some code that sets $gameNumber
         
    $name $event->getPlayer()->getName();
         
    $this->getServer()->getScheduler()->scheduleDelayedTask(new TaskName($this$name$gameNumber), 20);
    }
    public function 
    checkIfPlayerWon($name$gameNumber) { //You will need the $name parameter to check if a certain player won
         //Some code here...
    }
    This will be your TaskName.php file.
    PHP:
    <?php
    namespace Example;
    use 
    pocketmine\Server;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\Player;
    class 
    TaskName extends PluginTask{
       
       
        public function 
    __construct(Main $plugin$name$gameNumber){
            
    parent::__construct($plugin);
            
    $this->plugin $plugin;
            
    $this->name $name;
            
    $this->gameNumber  =  $gameNumber;
        }
       
        public function 
    onRun($currentTick) {
            
    $this->plugin->checkIfPlayerWon($this->name$this->gameNumber);
        }
    }
    PEMapModder and Guillaume351 like this.
  5. Guillaume351
    Offline

    Guillaume351 Active Member Plugin Developer

    Joined:
    Oct 11, 2013
    Posts:
    150
    Plugins:
    1
    Minecraft User:
    Guillaume351
    THANK YOU !!!

    But I don't use the $name parameter, because in fact, the gameNumber is used to get the map (1game = 1map), I only check how many players are remaining and if count($level->getlayers) == 1, the only remaining player wins..

    Anyway, thanks once again !
    Angelo Vidrio likes this.

Share This Page

Advertisement