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

Teleport on login doesn't work

Discussion in 'Development' started by udwarf, Jul 20, 2017.

  1. udwarf

    udwarf Silverfish

    Messages:
    21
    Hi!
    After one of updates AlwasySpawn plugin stops to work
    He is trying to teleport player to world spawn position right from onPlayerLogin event.
    PHP:
            public function onPlayerLogin(PlayerLoginEvent $event) {
                
    $player $event->getPlayer();
                
    $x $this->getServer()->getDefaultLevel()->getSafeSpawn()->getX();
                
    $y $this->getServer()->getDefaultLevel()->getSafeSpawn()-> getY();
                
    $z $this->getServer()->getDefaultLevel()->getSafeSpawn()->getZ();
                
    $level $this->getServer()->getDefaultLevel();
                
    $pos = new Position($x$y$z$level);
                
    $player->teleport($pos);
                 }
    But now it doesn't work

    What is the correct way now to force player to appear always on the particular position after the relogin?
     
  2. Kyd

    Kyd Zombie Pigman

    Messages:
    678
    GitHub:
    boi1216
    You dont want to check x,y,z of spawn position getSafeSpawn already return position
    PHP:
    $player->teleport($level->getSafeSpawn());
     
    Matthew likes this.
  3. udwarf

    udwarf Silverfish

    Messages:
    21
    Does not matter. It does not work anyway.
     
  4. Matthew

    Matthew Baby Zombie

    Messages:
    167
    GitHub:
    matthww
    What Kyd said does work:
    PHP:
    public function onPlayerLogin(PlayerLoginEvent $event) {
        
    $player $event->getPlayer();
        
    $level $this->getServer()->getDefaultLevel();
        
    $player->teleport($level->getSafeSpawn());
    }
    I tested it several times. And it works perfectly fine
     
    jasonwynn10 likes this.
  5. Matthew

    Matthew Baby Zombie

    Messages:
    167
    GitHub:
    matthww
    But he/she wanted to teleport people when they login to spawn. Not if they respawn
     
  6. udwarf

    udwarf Silverfish

    Messages:
    21
    Well,
    I've checked all events that occurs wneh player connects to the server and tryied to setSpawn there
    1. PlayerCreationEvent - we cannot do anything yet. Player object not constructed yet
    2. PlayerPreLoginEvent - same thing: got Exception: "Attempted to send pocketmine\network\mcpe\protocol\SetSpawnPositionPacket to Steve too early
    3. EntitySpawnEvent - SetSpawnPositionPacket sent to the player but no effect
    4. PlayerLoginEvent - same
    But looks like I found solution. In the EntitySpawnEvent I also call $player->setPosition($spawnPos) and it works!
    Hope there will not be any side effect
     
  7. Muqsit

    Muqsit Chicken

    Messages:
    1,548
    GitHub:
    muqsit
    jasonwynn10 and SOFe like this.
  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.