001/*
002 * Trident - A Multithreaded Server Alternative
003 * Copyright 2014 The TridentSDK Team
004 *
005 * Licensed under the Apache License, Version 2.0 (the "License");
006 * you may not use this file except in compliance with the License.
007 * You may obtain a copy of the License at
008 *
009 *    http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017
018package net.tridentsdk.server.packets.play.in;
019
020import io.netty.buffer.ByteBuf;
021import net.tridentsdk.base.Position;
022import net.tridentsdk.server.netty.ClientConnection;
023import net.tridentsdk.server.netty.packet.InPacket;
024import net.tridentsdk.server.netty.packet.Packet;
025import net.tridentsdk.server.player.PlayerConnection;
026import net.tridentsdk.server.player.TridentPlayer;
027
028/**
029 * This packet is sent when the player wishes updates the player's XYZ position on the server.
030 */
031public class PacketPlayInPlayerMove extends InPacket {
032    /**
033     * Updated location, Y is the feet location
034     */
035    protected Position location;
036    /**
037     * Whether the player is on the ground or not
038     */
039    protected boolean onGround;
040
041    @Override
042    public int id() {
043        return 0x0C;
044    }
045
046    @Override
047    public Packet decode(ByteBuf buf) {
048        double x = buf.readDouble();
049        double y = buf.readDouble();
050        double z = buf.readDouble();
051
052        this.location = Position.create(null, x, y, z);
053
054        this.onGround = buf.readBoolean();
055
056        return this;
057    }
058
059    public Position location() {
060        return this.location;
061    }
062
063    public boolean onGround() {
064        return this.onGround;
065    }
066
067    @Override
068    public void handleReceived(ClientConnection connection) {
069        TridentPlayer player = ((PlayerConnection) connection).player();
070        this.location.setWorld(player.world());
071        this.location.setPitch(player.position().pitch());
072        this.location.setYaw(player.position().yaw());
073        player.setPosition(location());
074    }
075}