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

Solved Problem with repeating task

Comments in 'Plugin Development' started by Blabla, Oct 16, 2016.

  1. Blabla
    Offline

    Blabla Active Member

    Joined:
    Sep 19, 2015
    Posts:
    82
    Minecraft User:
    The_Beast_Inside
    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($t10);
            
    $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$posPlayer $p) {
      
    parent::__construct($plugin);
      
    $this->pos $pos;
      
    $this->plugin $plugin;
      
    $this->seconds 0;
      
    $this->$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! :)
  2. MyNameIsTriXz
    Offline

    MyNameIsTriXz Notable Member

    Joined:
    Aug 17, 2015
    Posts:
    538
    Minecraft User:
    MyNameIsTriXz
    Not sure if it helps, but you dont construct with an event listener, you construct with a plugin.
  3. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    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($t10);
    $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($t10);
    # Should be 
    scheduleRepeatingTask($t20);
    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
  4. Blabla
    Offline

    Blabla Active Member

    Joined:
    Sep 19, 2015
    Posts:
    82
    Minecraft User:
    The_Beast_Inside
    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.
  5. Blabla
    Offline

    Blabla Active Member

    Joined:
    Sep 19, 2015
    Posts:
    82
    Minecraft User:
    The_Beast_Inside
    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:
    <?php
    namespace 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 {
  6. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Why it's a PluginTask?
    Blabla likes this.
  7. Blabla
    Offline

    Blabla Active Member

    Joined:
    Sep 19, 2015
    Posts:
    82
    Minecraft User:
    The_Beast_Inside
    Shouldn't it be?
  8. Primus
    Offline

    Primus Notable Member

    Joined:
    Apr 7, 2015
    Posts:
    1,470
    Minecraft User:
    PrimusLV
    Isn't that obvious? FireCageTask::getPlugin() returns Listener :eek:
    Blabla likes this.
  9. Blabla
    Offline

    Blabla Active Member

    Joined:
    Sep 19, 2015
    Posts:
    82
    Minecraft User:
    The_Beast_Inside
    That was the hint I needed, I constructed the wrong parent. I had to make it $plugin->getOwner() instead of just $plugin (thx EssentialsPE :D) Thanks a lot for your help @Primus !

Share This Page

Advertisement