package net.glowstone.net.handler.play.player;

import com.flowpowered.networking.MessageHandler;
import net.glowstone.net.GlowSession;
import net.glowstone.net.message.play.player.PlayerUpdateMessage;
import org.bukkit.Location;

public final class PlayerUpdateHandler implements MessageHandler<GlowSession, PlayerUpdateMessage> {

    @Override
    public void handle(GlowSession session, PlayerUpdateMessage message) {
        Location original = session.getPlayer().getLocation();
        Location newLoc = original.clone();
        message.update(newLoc);

        // don't let players move more than 16 blocks in a single packet.
        // this is NOT robust hack prevention - only to prevent client
        // confusion about where its actual location is (e.g. during login)
        if (newLoc.distanceSquared(original) > 16 * 16) {
            return;
        }

        // do stuff with onGround if we need to
        session.getPlayer().setRawLocation(newLoc);
    }
}

@@ -116,23 +116,126 @@
 one.
-net.GlowSession
+EventFactory;%0Aimport net.glowstone.net.GlowSession;%0Aimport net.glowstone.net.message.play.game.PositionRotationMessage
 ;%0Aim
@@ -325,16 +325,64 @@
 ocation;
+%0Aimport org.bukkit.event.player.PlayerMoveEvent;
 %0A%0Apublic
@@ -669,24 +669,60 @@
 al.clone();%0A
+        Location finalLoc = newLoc;%0A
         mess
@@ -1052,16 +1052,551 @@
      %7D%0A%0A
+        if (!original.equals(newLoc)) %7B%0A            final PlayerMoveEvent event = EventFactory.callEvent(new PlayerMoveEvent(session.getPlayer(), original, newLoc));%0A%0A            if (event.isCancelled()) %7B%0A                finalLoc = original;%0A            %7D else if (!event.getTo().equals(newLoc)) %7B%0A                finalLoc = event.getTo();%0A            %7D%0A%0A            if (!newLoc.equals(finalLoc)) %7B%0A                session.send(new PositionRotationMessage(finalLoc, session.getPlayer().getEyeHeight(true)));%0A            %7D%0A        %7D%0A%0A%0A
         
@@ -1678,19 +1678,21 @@
 ocation(
-new
+final
 Loc);%0A  

Call PlayerMoveEvent.