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.out;
019
020import io.netty.buffer.ByteBuf;
021import net.tridentsdk.base.Position;
022import net.tridentsdk.entity.living.Player;
023import net.tridentsdk.server.data.ProtocolMetadata;
024import net.tridentsdk.server.netty.Codec;
025import net.tridentsdk.server.netty.packet.OutPacket;
026
027import java.util.UUID;
028
029public class PacketPlayOutSpawnPlayer extends OutPacket {
030    protected int entityId;
031    protected Player player;
032    protected ProtocolMetadata metadata;
033
034    @Override
035    public int id() {
036        return 0x05;
037    }
038
039    public int entityId() {
040        return this.entityId;
041    }
042
043    public Player player() {
044        return this.player;
045    }
046
047    public ProtocolMetadata metadata() {
048        return metadata;
049    }
050
051    @Override
052    public void encode(ByteBuf buf) {
053        Position loc = this.player.position();
054        UUID id = this.player.uniqueId();
055
056        Codec.writeVarInt32(buf, this.entityId);
057
058        buf.writeLong(id.getMostSignificantBits());
059        buf.writeLong(id.getLeastSignificantBits());
060
061        buf.writeInt((int) loc.x() * 32);
062        buf.writeInt((int) loc.y() * 32);
063        buf.writeInt((int) loc.z() * 32);
064
065        buf.writeByte((int) (byte) loc.yaw());
066        buf.writeByte((int) (byte) loc.pitch());
067
068        buf.writeShort(player.heldItem().id());
069        metadata.write(buf);
070    }
071}