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

Solved PlayerRespawnEvent - issue with set coordinates.

Discussion in 'Development' started by Ver24436, Nov 21, 2019.

  1. Ver24436

    Ver24436 Creeper

    Messages:
    3
    I have this little issue, I'd like to have players always respawn at a set location in the default world.
    The code below works somewhat but seems to always round the coordinates and the player spawn at 202 69 23 - rather than use the 201.5, 69, 22.5 coordinates that were set.

    PHP:
     public function onRespawn(PlayerRespawnEvent $event) {
            
    $level $this->getServer()->getDefaultLevel();
            
    $pos = new Location(201.56922.51800$level);
            
    $event->setRespawnPosition($pos);
    }
    The code I used in the PlayerJoinEvent works as expected and teleports the player in the center of a single block.

    PHP:
        public function onJoin(PlayerJoinEvent $event) {
         
           
    $player $event->getPlayer();
           
    $level $this->getServer()->getDefaultLevel();
           
    $player->teleport(new Location(201.56922.51800$level));      
        }
    I know it's a minor thing, but I'm new to coding and trying to understand why it doesn't work and want to learn how to solve it.

    Thanks in advance.
     
  2. HimbeersaftLP

    HimbeersaftLP Fish

    Messages:
    2,402
    GitHub:
    HimbeersaftLP
    This happens for a very, very funny reason (I literally grinned when I saw it):
    https://github.com/pmmp/PocketMine-...53562bb41ece1/src/pocketmine/Player.php#L3686
    Here's an excerpt of that file:
    PHP:
    protected function respawn() : void{
    if(
    $this->server->isHardcore()){
    $this->setBanned(true);
    return;
    }
    $ev = new PlayerRespawnEvent($this$this->getSpawn());
    $ev->call();
    $realSpawn Position::fromObject($ev->getRespawnPosition()->add(0.500.5), $ev->getRespawnPosition()->getLevel()); // take a look at this line
    $this->teleport($realSpawn);
    $this->setSprinting(false);
    $this->setSneaking(false);
    PocketMine adds 0.5 to the coordinate lol

    (btw you don't need to bother with yaw and pitch as setRespawnPosition only uses a Position object which only has x, y, z and level)
     
    Ver24436 likes this.
  3. Ver24436

    Ver24436 Creeper

    Messages:
    3
    Wow, lol so simple. I was looking in the wrong place but makes perfect sense now. Thank you for taking the time to explain. I appreciate it.
     
    HimbeersaftLP likes 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.