I'm trying to add two features to the Hunger Games Plugin https://poggit.pmmp.io/p/HungerGames/Build-16.2: [Unsolved] When a player joins, they are teleported to the spawn. [Solved] When a player joins, their inventory is cleared. I've tried implementing AlwaysSpawn's code into the Hunger Games plugin's EventListener.php: PHP: public function onSpawn(PlayerJoinEvent $event){ $player->teleport($this->level->getSpawn()); } But this delivers the error when a player joins the server: Code: Call to a member function teleport() on null" (EXCEPTION) As a noob to PMMP and programming in general, my question is how do I properly teleport a player and clear their inventory? PHP: public function onSpawn(PlayerJoinEvent $event){ #Teleport to spawn code #Clear inventory code }
To use $player->teleport(), you have to define $player. You should be able to do it with $event->getPlayer(). You can clear a player's inventory with $player->getInventory()->clearAll().
I was thinking about that but PlayerJoinEvent.php doesn't have a getter method for the player and I don't think I should edit main PMMP source code to achieve teleporting and clearing inventory on login. Maybe I'll do a pull request to add getter methods to PlayerJoinEvent.php like: PHP: /** * @return Player */ public function getEntity(){ return $this->entity; } /** * @return Player */ public function getPlayer(){ return $this->entity; } But is there a different way to teleport and clear inventory upon login without changing PMMP source?
LOL I just assumed it didn't have it, because all the other events used in the Hunger Games plugin EventListener.php have getPlayer() and getEntity() in the child class. Now let's see if I can get this to work. UPDATE: OK clear inventory works great, thanks Cory and Jack. getSpawn() doesn't seem to work though, in: PHP: $player->teleport($this->level->getSpawn()); What method is getSpawn() supposed to be changed to, or am I not using a needed file, or is it something else?
PHP: // Teleport player to the spawn of their current world $player->teleport($player->getLevel()->getSafeSpawn());// Teleport the player to the spawn of the default level$level = $player->getServer()->getDefaultLevel();$player->setLevel($level);$player->teleport($level->getSafeSpawn());
UPDATE: In testing, it appears that they are at the spawn when they login but immediately teleport to where they were when they logged off. This can only be observed through the eyes of another player watching a person log in. From the perspective of the player who just logged in, they're always where they last logged off. Neither of those seemed to work when I added them to onSpawn(), but they didn't throw any errors. Player just logged in where they logged off. This is what I used: PHP: public function onSpawn(PlayerJoinEvent $e){ $p = $e->getPlayer(); $p->teleport($p->getLevel()->getSafeSpawn()); $p->getInventory()->clearAll(); }