I'm using $player->teleport() on PlayerJoinEvent, but it doesn't work. Why doesn't it and how can I fix it?
PHP: $player = $event->getPlayer(); $x = $this->getConfig()->getNested("lobby.x"); $y = $this->getConfig()->getNested("lobby.y"); $z = $this->getConfig()->getNested("lobby.z"); $player->teleport(new Vector3($x, $y, $z));
I had this issue when developing SpawnMgr. Pocketmine does a teleport AFTER the join event. So whatever teleport you did is lost. What I did was to remove the teleport in the join event handler instead that would trigger a delayed task. Then the delayed task wold do the teleport.
Below is the code directly out of my AlwaysSpawn plugin: PHP: <?php namespace philipshilling\alwaysspawn; use pocketmine\plugin\PluginBase as Plugin; use pocketmine\event\Listener; use pocketmine\event\player\PlayerLoginEvent; use pocketmine\math\Vector3; class Loader extends Plugin implements Listener { public function onEnable() { $this->getServer()->getPluginManager()->registerEvents($this, $this); $this->getServer()->getLogger()->info("AlwaysSpawn Enabled!"); } 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(); $player->teleport(new Vector3($x, $y, $z, $level)); } public function onDisable() { $this->getServer()->getLogger()->info("AlwaysSpawn is no longer enabled! Did the server stop?"); } } EDIT: If you decide to use my code to ease development, don't copy-paste it. Edit it a little to make it your own.
I dont know if im right about this but I think Join is called when the client and server are communicating then Login is called right before (or maybe during idk) the player spawns.
@shoghicp recommended to use PlayerRespawnEvent the respawn position setter, which is also called when player joins.