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

    WeeRox

    Hi!
    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:
    Code:java
    1. package com.gmail.bathingrad;
    2.  
    3. import java.util.logging.Logger;
    4.  
    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 org.bukkit.plugin.java.JavaPlugin;
    10.  
    11. public class Plugin extends JavaPlugin {
    12. public static Plugin plugin;
    13. public final Logger logger = Logger.getLogger("Minecraft");
    14.  
    15. @Override
    16. public void onDisable() {
    17. PluginDescriptionFile pdfFile = this.getDescription();
    18. this.logger.info(pdfFile.getName() + "Has Been Disabled");
    19. }
    20.  
    21. @Override
    22. public void onEnable() {
    23. PluginDescriptionFile pdfFile = this.getDescription();
    24. this.logger.info(pdfFile.getName() + " Version " + pdfFile.getVersion()
    25. + " Has Been Enabled");
    26. }
    27.  
    28. public void onPlayerMove(PlayerMoveEvent event) {
    29. Player player = event.getPlayer();
    30.  
    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
    37.  
    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
    40.  
    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. }
    46.  


    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

    Necrodoom

    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.

    Also:
    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

    WeeRox

    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

    stormneo7

    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,

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

    dodo_hacker

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

    Skionz

    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.

    Code:java
    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

    fireblast709

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

    dodo_hacker

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

    GameplayJDK

  10. Offline

    WeeRox

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

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

Share This Page