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

Send message to player in task

Discussion in 'Development' started by DanielYTK, Mar 4, 2017.

  1. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    The tittle say all.
    PHP:
    <?php
    namespace DanielYTK;

    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\Plugin;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    DanielYTK\MCMMO\Main;

    class 
    Task extends PluginTask{
       
        public 
    $segundos 0;
        public 
    $plugin;
       
        public function 
    __construct(Main $pluginPlayer $player){
            
    parent::__construct($plugin);
              
    $this->plugin $plugin;
             
    $this->player $player;
        }
        public function 
    getServer(){
            return 
    $this->plugin;
        }
        public function 
    onRun($ticks){
            
    $player->sendMessage("§eA task está em§b ".$this->seconds);
            if(
    $this->segundos === 10){
                
    $player->sendMessage("§cA task chegou em 10s");
                
    $this->getServer()->removeTask($this->getTaskId());
            }
            
    $this->segundos++;
        }
    }
    and this not work
     
  2. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    error:
    Code:
     TypeError: "Argument 2 passed to DanielYTK\MCMMO\Task::__construct() must be an instance of pocketmine\Player, none given, called in C:\Users\DanielYTK\Documents\PocketMine-MP\plugins\MCMMO\src\DanielYTK\MCMMO\Main.php on line 44" (EXCEPTION) in "/MCMMO/src/DanielYTK/MCMMO/Task" at line 15
    
    
     
  3. Aviv

    Aviv Baby Zombie

    Messages:
    156
    Show us how you called your task,
    the error says that argument 2 on the task constructor wasnt defined
     
  4. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    public function newTask(){
    $task = new Task($this);
    $h = $this->getServer()->getScheduler()->scheduleRepeatingTask($task, 20);
    $task->setHandler($h);
    $this->tasks[$task->getTaskId()] = $task->getTaskId();
    }
     
  5. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    I'm new with tasks u.u
     
  6. Aviv

    Aviv Baby Zombie

    Messages:
    156
    PHP:
    public function __construct(Main $pluginPlayer $player){
    PHP:
    new Task($this);
    you only defined 1 argument, the task wants the plugin and the player
     
  7. Aviv

    Aviv Baby Zombie

    Messages:
    156
    $this defines as $plugin in the task, now what defines $player?
    new method:
    PHP:
    public function newTask(Player $player){ //added player argument to put in the task
      
    $task = new Task($this$player);
      
    $h $this->getServer()->getScheduler()->scheduleRepeatingTask($task20);
      
    $task->setHandler($h);
      
    $this->tasks[$task->getTaskId()] = $task->getTaskId();
    }
     
  8. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    How should I do then?
    PHP:
     $task = new Task($this$player)
    ?
     
  9. Aviv

    Aviv Baby Zombie

    Messages:
    156
    yes, make sure $player is instance of a player
     
  10. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    ok, thanks :)
     
  11. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    look: new error .-.
    Code:
    Unhandled exception executing command 'status' in status: Argument 1 passed to DanielYTK\MCMMO\Main::createTask() must be an instance of pocketmine\Player, none given, called in C:\Users\DanielYTK\Documents\PocketMine-MP\plugins\MCMMO\src\DanielYTK\MCMMO\Main.php on line 79
    
     
  12. Aviv

    Aviv Baby Zombie

    Messages:
    156
    now to call the function do
    PHP:
    $this->createTask($player);
     
  13. Aviv

    Aviv Baby Zombie

    Messages:
    156
    if its a command then your player is the command sender $sender
     
  14. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    Code:
    Could not execute task DanielYTK\MCMMO\Task: Call to a member function sendMessage() on null
    [20:00:42] [Server thread/CRITICAL]: Error: "Call to a member function sendMessage() on null" (EXCEPTION) in "/MCMMO/src/DanielYTK/MCMMO/Task" at line 25
     
    ;-;
     
  15. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    it's work for me:
    PHP:
    public $player;
    public function 
    __construct(Main $pluginPlayer $player){
            
    parent::__construct($plugin);
              
    $this->plugin $plugin;
             
    $this->player $player;
        }
        public function 
    getServer(){
            return 
    $this->plugin;
        }
        public function 
    onRun($ticks){
            if(
    $this->player instanceof Player){
            
    $this->player->sendMessage("§eA task está em §b".$this->segundos);
            if(
    $this->segundos === 10){
                
    $this->player->sendMessage("§cA task chegou em 10s");
                
    $this->getServer()->removeTask($this->getTaskId());
            }
            }
            
    $this->segundos++;
        }
     
  16. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    Before:
    PHP:
    <?php
    namespace DanielYTK;

    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\Plugin;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    DanielYTK\MCMMO\Main;

    class 
    Task extends PluginTask{
      
        public 
    $segundos 0;
        public 
    $plugin;
      
        public function 
    __construct(Main $pluginPlayer $player){
            
    parent::__construct($plugin);
              
    $this->plugin $plugin;
             
    $this->player $player;
        }
        public function 
    getServer(){
            return 
    $this->plugin;
        }
        public function 
    onRun($ticks){
            
    $player->sendMessage("§eA task está em§b ".$this->seconds);
            if(
    $this->segundos === 10){
                
    $player->sendMessage("§cA task chegou em 10s");
                
    $this->getServer()->removeTask($this->getTaskId());
            }
            
    $this->segundos++;
        }
    }
    After:
    PHP:
    <?php
    namespace DanielYTK;

    use 
    pocketmine\Player;
    use 
    pocketmine\plugin\Plugin;
    use 
    pocketmine\scheduler\PluginTask;
    use 
    pocketmine\utils\TextFormat as TF;
    use 
    DanielYTK\MCMMO\Main;

    class 
    Task extends PluginTask{

        private 
    $segundos 0;
        private 
    $player//must register global player variable

        
    public function __construct(Main $pluginPlayer $player){
            
    parent::__construct($plugin); //this saves the owning plugin in a variable
            
    $this->player $player;
        }

        public function 
    onRun($ticks){
            
    $this->player->sendMessage(TF::GREEN."A task está em".TF::BLUE." ".$this->segundos); //changed seconds to segundos because seconds was unregistered
            
    if($this->segundos === 10) {
                
    $this->player->sendMessage(TF::RED."A task chegou em 10s"); //use TextFormat class instead of § and use $this->player instead of $player
                
    $this->getOwner()->removeTask($this->getTaskId()); //no need for getServer() because it is already built into PocketMine's API
            
    }
            
    $this->segundos++;
        }
    }
     
  17. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    Strictly speaking you don't have to declare a class property, it is just better practice to do so as it makes reading and understanding your code easier for others.
     
    jasonwynn10 likes this.
  18. KairusDarkSeeker

    KairusDarkSeeker Spider Jockey

    Messages:
    25
    GitHub:
    kairusdarkseeker
    Good thing I have a brain and a high common sense to create a task without asking how to do it LOLz
     
  19. DanielYTK

    DanielYTK Zombie

    Messages:
    227
    When people do not know how to do something, they ask how to do it, right?
     
    corytortoise and jasonwynn10 like this.
  20. jasonwynn10

    jasonwynn10 Moderator Poggit Reviewer

    Messages:
    1,489
    GitHub:
    jasonwynn10
    I actually try to figure it out on my own first... But asking works too!
     
  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.