Solved Teleport player at specific coordinates

Discussion in 'Plugin Development' started by WeeRox, Aug 11, 2014.

Thread Status:
Not open for further replies.
  1. Offline


    I am making a parkour plugin for my future Minigame server.
    I wish that when you fall of the parkour and hit the ground you will be teleported to the beginning of the parkour. But I can not figure out how to do this. Here is my code so far:
    1. package;
    3. import java.util.logging.Logger;
    5. import org.bukkit.Location;
    6. import org.bukkit.entity.Player;
    7. import org.bukkit.event.player.PlayerMoveEvent;
    8. import org.bukkit.plugin.PluginDescriptionFile;
    9. import;
    11. public class Plugin extends JavaPlugin {
    12. public static Plugin plugin;
    13. public final Logger logger = Logger.getLogger("Minecraft");
    15. @Override
    16. public void onDisable() {
    17. PluginDescriptionFile pdfFile = this.getDescription();
    18. + "Has Been Disabled");
    19. }
    21. @Override
    22. public void onEnable() {
    23. PluginDescriptionFile pdfFile = this.getDescription();
    24. + " Version " + pdfFile.getVersion()
    25. + " Has Been Enabled");
    26. }
    28. public void onPlayerMove(PlayerMoveEvent event) {
    29. Player player = event.getPlayer();
    31. double x = 107; //This is a coordinate I tried with
    32. double y = 3; //This is a coordinate I tried with
    33. double z = 416; //This is a coordinate I tried with
    34. double sx = 124; //This is the spawn coordinates
    35. double sy = 24; //This is the spawn coordinates
    36. double sz = 392; //This is the spawn coordinates
    38. Location p = new Location(event.getPlayer().getWorld(), x, y, z); //Try location
    39. Location spawn = new Location(event.getPlayer().getWorld(), sx, sy, sz); //Spawn location
    41. if (player.getLocation().getBlock().getLocation() == p) { //Checks if player is on the specific coordinates
    42. player.teleport(spawn); //This should have teleported the player to spawn
    43. }
    44. }
    45. }

    My idea was to first take a coordinate and when the player reaches it they will be teleported back to spawn, but this does not work.

    Please help, thanks :)
  2. Offline


    Looks like you copied some plugin template, which teaches very bad practices.
    1. Do not use minecraft logger, get the plugin logger.
    2. Do not make plugin static.
    3. Do not print messages on enable and disable, bukkit already does so.

    4. Do not compare objects with ==, this will only be true if both variables point to exact same instance. Compare X Y Z coords and world instead.
  3. Offline


    Thanks for you answer.
    1. I did not know that.
    2. Thanks for that tip.
    3. Is that connected with the plugin logger?
    4. Will it work then?
    Thanks for your answer. The Plugin Template thing: the truth is that I was looking at a tutorial on YouTube. I have some basic knowledge of Java already
  4. Offline


    You're missing @EventHandler in your onPlayerMove event.

    Also, my method for these types of stuff to compare a location with another location is using the .distance() feature.
    It will return a double of the distance between the compared locations.
    For example,

    1. if(locA.distance(locB) <= 1){
    2. // Code
    3. }
  5. Offline


    why not using commandblocks?
    thats easyer, and not dependig on versions
  6. Offline


    You can always set up a scheduler to constantly loop through all of the players that are online and check if their y position is less then or equal to 5 and then teleport them to the spawn location.

    1. for(Player player : Bukkit.getOnlinePlayers()) {
    2. if(player.getLocation().getBlockY() <= 5) {
    3. //spawn location
    4. player.teleport(world, x, y, z);
    5. }
    6. }
  7. Offline


    This isn't either :p. That's why the Bukkit API exists.
  8. Offline


    sorry, i mean that when the api changes, the plugin will break.
  9. Offline


  10. Offline


    Thanks for all answers. Stupid me forgotten to add the line
    1. this.getServer().getPluginManager().registerEvents(this, this);

    in the onEnable :p
Thread Status:
Not open for further replies.

Share This Page