what does player.teleport() do?

Discussion in 'Plugin Development' started by Coffox, Apr 8, 2011.

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

    Coffox

    Hi

    i made a plugin that prevents players from walking too far away. when they are about 100 meters away from the worlds center, they recieve a warning and when they are 120 meters away, they are teleported back, so they cant go any further.
    the plugin works, but the player is only teleported back about 0,5 meters instead of 15 meters.

    every time sb is teleportet back the console gives this message:
    2011-04-08 15:00:33 [WARNING] Coffox moved wrongly!
    2011-04-08 15:00:33 [INFO] Got position 84.01986542252229, 64.0, 0.39465623923862014
    2011-04-08 15:00:33 [INFO] Expected 116.30000001192093, 64.0, 0.39465623923862014
    code:
    Code:
        public void onPlayerMove(PlayerMoveEvent event) {
            double X = event.getTo().getX();
            double Z = event.getTo().getZ();
            int dist = (int) Math.sqrt( (X*X)+(Z*Z) );
    
            if (dist > 100 && dist < 105) {
                    event.getPlayer().sendMessage( ChatColor.YELLOW + "Du befindest dich am Rand der Welt. Kehre also um!" ); //<- Warning for player
    
            } else if (dist >= 120) {
                Location targetLoc = new Location( event.getPlayer().getWorld() ,
                                             X * 0.88d ,
                                             event.getTo().getY() ,
                                             Z * 0.88d );
    
                event.getPlayer().sendMessage( ChatColor.RED + "Hier gehts nicht weiter!!! " );
                event.getPlayer().teleportTo( targetLoc );
    }
    btw: whats the difference between teleport(); and teleportTo(); ?
     
  2. Offline

    MrChick

  3. Offline

    Evenprime

    Replace
    Code:
    event.getPlayer().teleportTo (targetLoc );
    with
    Code:
    event.setTo(targetLoc);
    event.setFrom(targetLoc);
    event.setCancelled(true);
    event.getPlayer().teleport( targetLoc );
    
    this should reduce your problems with teleporting a lot. Why? Because the following happens if you don't do:

    1. MoveEvent A starts
    2. You capture MoveEvent A
    3. You start a TeleportEvent B
    4. TeleportEvent B is executed (Player is moved and gets notified about his new coordinates)
    5. MoveEvent A is Executed (Player gets moved to the event.getTo() location)

    You definitely don't want 5 to happen, but bukkit is a bit weird when it comes to cancelling MoveEvents. Therefore cancelling the event (to prevent other plugins from accidentally handling it after your plugin) plus modifying it such that there is no way the player will end up at a different location than the "targetLoc" is the safest way to go. I use it and it works very well.
     
Thread Status:
Not open for further replies.

Share This Page