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

Solved EntityTeleportEvent

Discussion in 'Development' started by TheClimbing, Mar 23, 2019.

  1. TheClimbing

    TheClimbing Spider Jockey

    Messages:
    39
    GitHub:
    theclimbing
    PHP:
    public function onPlayerTeleport (EntityTeleportEvent $event)
            {
                if (
    $event->getEntity() instanceof Player) {
                 
    $world $event->getTo()->getLevel()->getName();
                }
            }
    After the most resent release it started throwing error "getName() on null" when a Player joins ,is this an issue
    since release 3.6.5 didn't throw such errors? Or am I mis-understanding things
     
  2. dktapps

    dktapps Administrator Staff Member PMMP Team

    Messages:
    774
    GitHub:
    dktapps
    3.6.5 and 3.7.0 are functionally identical, as stated by the release notes.

    Regardless of the cause, you haven't provided sufficient information for anyone to help you.
     
  3. MalakasPlayzMCPE

    MalakasPlayzMCPE Zombie Pigman

    Messages:
    667
    $event->getTo()->getLevel() returns null.
     
  4. TheClimbing

    TheClimbing Spider Jockey

    Messages:
    39
    GitHub:
    theclimbing
    Okay so I haven't tested it on without any other code but still the code provided above shouldn't be affected by anything as it's an Event called by the Server itself. The event is registered and I don't know if intentional or not it is called when a player joins the server. It then throws getName() on null and crashes. getTo() doesn't have level for some reason
     
  5. TheClimbing

    TheClimbing Spider Jockey

    Messages:
    39
    GitHub:
    theclimbing
    Alright here is the code that magically makes the level null:
    Solution on the bottom of the snippet:
    PHP:
    class Main extends \pocketmine\plugin\PluginBase implements \pocketmine\event\Listener
        
    {
            public 
    $defaultSpawn;
            public function 
    onEnable ()
            {
                
    $this->getServer()->getPluginManager()->registerEvents($this$this);
                
    $this->getScheduler()->scheduleDelayedTask(new Task($this), 60);
            }
            public function 
    onPlayerLogin (\pocketmine\event\player\PlayerLoginEvent $player_login_event)
            {
                
    $player_login_event->getPlayer()->teleport($this->defaultSpawn);
            }
            public function 
    onPlayerTeleport (\pocketmine\event\entity\EntityTeleportEvent $event)
            {
                if (
    $event->getEntity() instanceof \pocketmine\Player) {
                    
    $this->getLogger()->debug(print_r($event->getTo()));
                    
    $this->getLogger()->debug(print_r($event->getTo()->getLevel()));
                    
    $this->getLogger()->debug(print_r($event->getTo()->getLevel()->getName()));
                }
            }
        }
        class 
    Task extends \pocketmine\scheduler\Task
        
    {
            private 
    $main;
            public function 
    __construct (Main $main)
            {
                
    $this->main $main;
     
            }
            public function 
    onRun (int $currentTick)
            {
                
    $defaultSpawn $this->main->getServer()->getDefaultLevel()->getSafeSpawn();
                
    $this->main->defaultSpawn = new \pocketmine\level\Position($defaultSpawn->getX() - 0.5$defaultSpawn->getY(),
    $defaultSpawn->getZ() - 0.5,
    $defaultSpawn->getLevel());
    //$defaultSpawn->getLevel() wasn't  specified so getTo() in EntityTeleportEvent was receiving null
            
    }
        }
    What did I do wrong? @dktapps
     
    Last edited: Mar 23, 2019
  6. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    level is not specified in Main::$defaultSpawn
     
    TheClimbing likes this.
  7. TheClimbing

    TheClimbing Spider Jockey

    Messages:
    39
    GitHub:
    theclimbing
    It is after 60 ticks um 3 seconds I think and the error occurs in the EntityTeleportEvent function which I named playerTeleportEvent. Then again the function itself has nothing to do with $main->defaultSpawn

    Edit:
    It wasn't specified I'm sorry, and yes that fixed my issue. Now I understand what was going on....And I feel dumb
     
    Last edited: Mar 23, 2019
  8. KielKing

    KielKing Zombie

    Messages:
    245
    GitHub:
    kielking
    no problem :)
     
  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.