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

Hunger Games Plugin: Teleporting to Spawn, Clearing Inventory, and using PlayerJoinEvent

Discussion in 'Facepalm' started by Haxley, Apr 30, 2017.

  1. Haxley

    Haxley Creeper

    Messages:
    3
    I'm trying to add two features to the Hunger Games Plugin https://poggit.pmmp.io/p/HungerGames/Build-16.2:
    1. [Unsolved] When a player joins, they are teleported to the spawn.
    2. [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
        
    }
     
    Last edited: May 2, 2017
  2. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    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().
     
  3. Haxley

    Haxley Creeper

    Messages:
    3
    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?
     
    Last edited: Apr 30, 2017
  4. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    :facepalm:
    Look at the class that PlayerJoinEvent extends.
     
  5. Haxley

    Haxley Creeper

    Messages:
    3
    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?
     
    Last edited: May 1, 2017
  6. corytortoise

    corytortoise Skeleton

    Messages:
    825
    GitHub:
    corytortoise
    We can't tell without knowing what $this->level is.
     
  7. Haxley

    Haxley Creeper

    Messages:
    3
    What code would you recommend to teleport $player to the spawnpoint?
     
  8. Jack Noordhuis

    Jack Noordhuis Zombie Pigman Poggit Reviewer

    Messages:
    618
    GitHub:
    JackNoordhuis
    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());
     
  9. Haxley

    Haxley Creeper

    Messages:
    3
    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();
        }
     
    Last edited: May 1, 2017
  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.