Hello! I'm trying to call a task in a class that is not my main class. The code all the way to the task works, but when the task gets called it gives a weird error I can not place, it doesn't tell me the line in the error, nor does it tell me if it's actually in the task or the event listener. (though I found out it actually is in the class) I've spent literally 2 hours straight trying to fix this error, but no positive results so far. Error: Code: [03:10:06] [Server thread/CRITICAL]: "Could not pass event 'pocketmine\event\player\PlayerInteractEvent' to 'PocketRPG v0.3.0': Call to a member function getName() on null on PocketRPG\eventlistener\EventListener [03:10:06] [Server thread/CRITICAL]: Error: "Call to a member function getName() on null" (EXCEPTION) in "/src/pocketmine/plugin/PluginBase" at line 277 Event Listener event code I call the repeating task in: PHP: public function onInteract(PlayerInteractEvent $event) { $p = $event->getPlayer(); if($p->getItemInHand()->getId() == Item::BLAZE_POWDER) { foreach($this->getOwner()->getServer()->getOnlinePlayers() as $ps) { if($p->distance($ps) <= 10) { $pos = $ps->getPosition(); $t = new FireCageTask($this, $pos, $ps); $h = $this->getOwner()->getServer()->getScheduler()->scheduleRepeatingTask($t, 10); $t->setHandler($h); $this->tasks[$task->getTaskId()] = $task->getTaskId(); } } }} And lastly, the task I'm executing. I've removed quite literally pretty much anything I thought of could be a problem, but nothing. PHP: public $plugin;public $seconds;public function __construct(EventListener $plugin, $pos, Player $p) { parent::__construct($plugin); $this->pos = $pos; $this->plugin = $plugin; $this->seconds = 0; $this->p = $p;}public function getPlugin() { return $this->plugin;}public function onRun($tick) { if($this->seconds === 10) { unset($this->tasks[$this->getTaskId()]); $this->getOwner()->getServer()->getScheduler()->cancelTask($this->getTaskId()); }$this->seconds++;} I'm completely hopeless, I really don't understand the error because there's no getName() in all of that code. Hope you guys can help me out!
That's not the full code. Show your class definition Undefined variable '$task' in this scope PHP: $this->tasks[$task->getTaskId()] = $task->getTaskId(); Complete non-sense here. Obviously those function calls below require different parameter types PHP: $t = new FireCageTask($this, $pos, $ps);$h = $this->getOwner()->getServer()->getScheduler()->scheduleRepeatingTask($t, 10);$t->setHandler($h); You know, instead of 10 second task life-time you'll get 5 seconds? Second parameter is interval in ticks Code: 1 tick = 1 second / 20 = 0.05s 20 tick = 1 second 10 tick = 0.5s PHP: scheduleRepeatingTask($t, 10);# Should be scheduleRepeatingTask($t, 20); And the error never said that the error was thrown in your code. Code: [03:10:06] [Server thread/CRITICAL]: Error: "Call to a member function getName() on null" (EXCEPTION) in "/src/pocketmine/plugin/PluginBase" at line 277 I don't know which build or version of PocketMine you're using but here's the code from official master branch https://github.com/PocketMine/PocketMine-MP/blob/master/src/pocketmine/plugin/PluginBase.php#L278
Okay first, thanks for that undefined variable, second, I know that the 10 is the time in ticks. I did this on purpose because in my opinion flame particles don't last long enough to do it every second, thus I made them appear ever half second. I want it to last 5 seconds, that is not my problem. Third, the error says its in the playerinteractevent, which is when I execute the task. When I remove the task, it does not show this error. I will test if it shows the error again if I fix the undefined variable and let you know. Thanks.
If I construct it to my main class it gives an error about property stuff, the task gets called in the EventListener. The plugin still gives the same error. Don't think there's an error in this: PHP: <?phpnamespace PocketRPG\tasks;use PocketRPG\eventlistener\EventListener;use pocketmine\scheduler\PluginTask;use pocketmine\Player;use pocketmine\Server;use pocketmine\level\particle\FlameParticle;use pocketmine\math\Vector3;class FireCageTask extends PluginTask {
That was the hint I needed, I constructed the wrong parent. I had to make it $plugin->getOwner() instead of just $plugin (thx EssentialsPE ) Thanks a lot for your help @Primus !