Solved Teleport player after 3 seconds of not moving

Discussion in 'Plugin Development' started by NonameSL, Jun 19, 2014.

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

    NonameSL

    I am trying to use the following code to make a player teleport to a certain location after 3 seconds, as long he wasn't moving, and when he moves (if he moves), to cancel the countdown and send the player a message.
    All is going well, but the player gets a message he moved when he types the command. Why?
    (To check if he moves, I am comparing locations.)

    Code:java
    1. Player p = (Player) sender;
    2. Location l = p.getLocation();
    3. sender.sendMessage(ChatColor.RED + "Teleporting you to your base in 3 seconds, do not move!");
    4. int seconds = Calendar.getInstance().get(Calendar.SECOND);
    5. while(seconds-Calendar.getInstance().get(Calendar.SECOND)<=3){
    6. if(l!=p.getLocation()){
    7. p.sendMessage(CommandManager.PREFIX+"§cYou moved! Teleportation failed.");
    8. break;
    9. }
    10. if(seconds-Calendar.getInstance().get(Calendar.SECOND)==3){
    11. //Getting a location, teleporting the player to it.
    12. }
    13. }

    Thanks in advance![pig] [pig]
     
  2. NonameSL This current method will, ideally, freeze the entire server for 3 seconds until the player is allowed to teleport. You should schedule a delayed task and cancel it if they move. Also, PlayerMoveEvent fires/Location changes if the Player so much as turns
     
  3. Offline

    NonameSL

    AdamQpzm Okay, what about this?
    Code:java
    1.  
    2. final Player p = (Player) sender;
    3. final Location l = p.getLocation();
    4. count = 0;
    5. sender.sendMessage(ChatColor.RED + "Teleporting you to your base in 3 seconds, do not move!");
    6. task = Bukkit.getServer().getScheduler().scheduleSyncRepeatingTask(pl, new Runnable(){
    7. @Override
    8. public void run() {
    9. if(l!=p.getLocation()){
    10. p.sendMessage(CommandManager.PREFIX+"§cYou moved! Teleportation failed.");
    11. Bukkit.getServer().getScheduler().cancelTask(task);
    12. }
    13. if(count==3){
    14. //Code for teleporting
    15. count=0;
    16. }
    17. count+=0.1;
    18. }
    19.  
    20. }, 2L, 2L);

    But it instantly tells the player that he moved, and the teleportation was failed.
    What's wrong?
    EDIT: I fixed it!
    I used this compareLoaction method to compare between 2 locations:
    Code:
    private boolean compareLocation(Location l1, Location l2){
    return l1.getX()==l2.getX()&&l1.getY()==l2.getY()&&l1.getZ()==l2.getZ()&&l1.getYaw()==l2.getYaw()&&l1.getPitch()==l2.getPitch();
    }
    And at the end of the code of teleporting I canceled the task.
     
    AdamQpzm likes this.
Thread Status:
Not open for further replies.

Share This Page