Scanning the location of the player and the Teleport user [Bukkit]
I searched, but I didn't find exactly what I want ... I want to get the player's location and if it matches a certain location teleport the player to another location. This is what I get. No error is displayed in the console, but nothing happens when the player is at X = 300.
public void onPlayerInteract(PlayerInteractEvent e)
{
if(e.getPlayer().getLocation().getX()==300)
{
e.getPlayer().teleport(new Location(Bukkit.getServer().getWorld("world"), 310, 75, 300));
}
}
source to share
There are three mistakes in doing this and getting them to work correctly. I corrected my code:
public void onPlayerMove(PlayerMoveEvent e)
{ // On player movement!
if((int) e.getPlayer().getLocation().getX() == 300)
// If you cast as int 3.9 or 3.01 it will return 3
{
e.getPlayer().teleport(new Location(Bukkit.getWorld("world"), 310, 75, 300));
/* There is a bug in bukkit plugins since 1.6.4.
* You need to get the world just by getWorld(); not
* getServer().getWorld();
*/
}
}
This works at 10/10 :)
source to share
You are checking if the x coordinate of the player is exactly 300, which probably never will. It looks more like a decimal number like 300.05456
.
What you probably wanted to do was check if the player is on block with x = 300
. If so, then this is what you should do instead.
if (e.getPlayer().getLocation().getBlockX() == 300) {
// Do stuff
}
source to share