[SOLVED] Teleporting to an entity

Discussion in 'Spigot Plugin Development' started by robotballs, May 9, 2015.

  1. I have been coding plugins for about 1-2 months so I am kind of new at it. This will be my first public plugin it isn't meant to be useful it is mainly just for fun. I am trying to get the player to teleport to an egg. Here is what I have for my EventHandler.
    Code (Text):
        @EventHandler
        public void eggThrow(PlayerEggThrowEvent e) {
           
            Player player = e.getPlayer();
           
            if (player.hasPermission("enderegg.throw")) {
                Location origionalLocation = player.getLocation();
               
               

            }
    What would I do to get the location of the egg and move the player to that location?
     
  2. gigosaurus

    Supporter

    • Agree Agree x 1
  3. Thank you, it worked! When I teleport with the egg I face a random direction, Is there any way to change that?
     
  4. Change the yaw and pitch in the Location's constructor.
     
  5. gigosaurus

    Supporter

    The direction you are facing is determined by the yaw and pitch of your location. By setting the player's location to the egg's location, you're making the player face the same direction the egg is facing.

    If you wanted to make it so that the player remains facing the same direction, you should create a new Location object which contains the x, y, z values of the egg's location, but the yaw and pitch of the player's original location. Then teleport the player to this new location you created.
     
  6. Now i have this
    Code (Text):
        @EventHandler
        public void eggThrow(PlayerEggThrowEvent e) {
           
            Player player = e.getPlayer();
            Egg egg = e.getEgg();
           
            if (player.hasPermission("enderegg.throw")) {
                player.teleport(egg.getLocation());
                Location newLoc = egg.getLocation();
                Location old = player.getLocation();
                old.setPitch(newLoc.getPitch());
                old.setYaw(newLoc.getYaw());
            }
        }
    I can't seem to figure out how to get the old location and set the yaw and pitch to the new one.
     
  7. gigosaurus

    Supporter

    You were nearly there, just got your locations the wrong way around and the teleport wrong.

    Code (Java):
        @EventHandler
        public void eggThrow(PlayerEggThrowEvent e) {
         
            Player player = e.getPlayer();
            Egg egg = e.getEgg();
         
            if (player.hasPermission("enderegg.throw")) {
                Location newLoc = egg.getLocation();
                Location old = player.getLocation();
                newLoc.setPitch(old.getPitch());
                newLoc.setYaw(old.getYaw());
                player.teleport(newLoc);
            }
        }
     
  8. Player#getLocation() before teleporting them.
     
  9. gigosaurus

    Supporter

    Also, I just remembered about getDirection() and setDirection(), so you could use those instead of setting the pitch and yaw separately, although setting the pitch and yaw separately would be faster as getDirection() and setDirection() do some calculations with trig functions.
     
  10. Thanks it worked!