The tittle say all. PHP: <?phpnamespace 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 $plugin, Player $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
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
Show us how you called your task, the error says that argument 2 on the task constructor wasnt defined
public function newTask(){ $task = new Task($this); $h = $this->getServer()->getScheduler()->scheduleRepeatingTask($task, 20); $task->setHandler($h); $this->tasks[$task->getTaskId()] = $task->getTaskId(); }
PHP: public function __construct(Main $plugin, Player $player){ PHP: new Task($this); you only defined 1 argument, the task wants the plugin and the player
$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($task, 20); $task->setHandler($h); $this->tasks[$task->getTaskId()] = $task->getTaskId();}
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
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 ;-;
it's work for me: PHP: public $player;public function __construct(Main $plugin, Player $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++; }
Before: PHP: <?phpnamespace 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 $plugin, Player $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: <?phpnamespace 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 $plugin, Player $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++; }}
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.