# How to get x,y,z of player and teleport

Discussion in 'Plugin Development' started by jacklin213, Nov 19, 2012.

Not open for further replies.
1. Offline

### jacklin213

I would like to know how to get x,y,z of a player and the direction hes facing in. and when someone uses a command they teleport to that location facing the same way

#1
2. Offline

### ZeusAllMighty11Retired Staff

Code:
```// Is player target or player player? confused
Double pX = target.getLocation().getX();
Double pY = target.getLocation().getY();
Double pZ = target.getLocation().getZ();
Double pPitch = target.getLocation().getPitch();
Double pYaw = target.getLocation().getYaw();
World pWorld = target.getWorld();

Location pLoc = new Location(pWorld,pX,pY,pZ);
pLoc.setPitch(pPitch);
pLoc.setYaw(pYaw);
// if commands say something
// if it's the command you want to teleport

player.teleport(pLoc);
```

3. Offline

### jacklin213

wats pitach and yaw?
EDIT: no work
The constructor Location(World, double, double, double, double, double) is undefined

#3
4. Offline

### ZeusAllMighty11Retired Staff

Fixed the code. Sorry it's 3am lol.

Also, Pitch and Yaw are like the angles and direction player is facing I think.. I don't know how to explain

here is a thread I found on it

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: May 30, 2016
5. Offline

### desht

Why not just do:
PHP:
``` player.teleport(target.getLocation()); ```
?

Pitch is the "up/down" angle of the player's head: to be precise, the angle of the player's head around the horizontal axis perpendicular to the player's facing direction.

Yaw is the "left/right" angle of the player's head: to be precise, the angle of the player's head around the vertical axis.

#5
6. Offline

### ZeusAllMighty11Retired Staff

Well he said get the xyz AND teleport, so he might be using the coords for something later

7. Offline

### jacklin213

ah they are floats as well apprently

dont know y this isnt working

Code:java
`@EventHandler    public void onJoinEvent(PlayerJoinEvent event) {        Player p = event.getPlayer();        p.teleport(pos);     }`

Code:java
`package me.jacklin213.tplogin; import java.util.logging.Logger; import org.bukkit.Bukkit;import org.bukkit.ChatColor;import org.bukkit.Location;import org.bukkit.World;import org.bukkit.command.Command;import org.bukkit.command.CommandSender;import org.bukkit.entity.Player;import org.bukkit.plugin.java.JavaPlugin; public class TPLogin extends JavaPlugin {     public final Logger logger = Logger.getLogger("Minecraft");    public TPLListener tpl = new TPLListener(this);    public static TPLogin plugin;    public double x;    public double y;    public double z;    public float pitch;    public float yaw;    public World world = Bukkit.getWorld("world");    public Location pos;      public void onDisable()  {        logger.info(String.format("[%s] Disabled Version %s", getDescription()                .getName(), getDescription().getVersion()));    }     public void onEnable() {        logger.info(String.format("[%s] Enabled Version %s by jacklin213",                getDescription().getName(), getDescription().getVersion()));    }     public boolean onCommand(CommandSender sender, Command cmd, String commandLabel, String args[]){        if (sender instanceof Player){            if (commandLabel.equalsIgnoreCase("tplogin")){                if (args.length == 1){                    if(args[0].equalsIgnoreCase("set")){                        if (sender.hasPermission("tplogin.set")){                            Location player = (Location) sender;                            x = player.getX();                            y = player.getY();                            z = player.getZ();                            pitch = player.getPitch();                            yaw = player.getYaw();                            Location pos = new Location(world, x ,y ,z);                            pos.setPitch(pitch);                            pos.setYaw(yaw);                            return true;                        } else {                            sender.sendMessage(ChatColor.RED + "You do not have the permission to do this!");                            return true;                        }                    }                } else if (args.length > 1 || args.length == 0) {                    sender.sendMessage(ChatColor.RED + "Not enough arguments!");                    return true;                }            }        }          return false;     } } `

EDIT by Moderator: merged posts, please use the edit button instead of double posting.

Last edited by a moderator: May 30, 2016
#7
8. Offline

### desht

Try using a sync delayed task with a zero delay to teleport the player.

#8
9. Offline

### jacklin213

erm sorry how do i do that?

#9
10. Offline

#10
11. Offline

### jacklin213

been there dont under stand it
i would b greatfull if there was an example ty

#11
12. Offline

### V10lator

#12
Cowboys1919 likes this.
13. Offline

### desht

Right - I really couldn't post any example which illustrates it better than the code in that link. jacklin213 if you don't understand it, tell us what you don't understand. In the long run, that's better for everyone.

#13
14. Offline

### jacklin213

Do I put my pos.teleport inside run()?

#14
15. Offline

### fireblast709

yes, you do the p.teleport(pos) in there :3 (inside the public void run(), that is)

#15
16. Offline

### jacklin213

so this delay thingy will make my plugin work?

#16
17. Offline

### RealDope

You should probably just try it and see...

#17
Cowboys1919 likes this.