package common.packet;

import java.io.IOException;

import common.entity.Entity;
import common.network.IClientPlayer;
import common.network.Packet;
import common.network.PacketBuffer;
import common.util.ExtMath;

public class SPacketSpawnGlobalEntity implements Packet<IClientPlayer>
{
    private int entityId;
    private int x;
    private int y;
    private int z;
    private int type;
    private int data;

    public SPacketSpawnGlobalEntity()
    {
    }

    public SPacketSpawnGlobalEntity(Entity entityIn, int type, int data)
    {
        this.entityId = entityIn.getId();
        this.x = ExtMath.floord(entityIn.posX * 32.0D);
        this.y = ExtMath.floord(entityIn.posY * 32.0D);
        this.z = ExtMath.floord(entityIn.posZ * 32.0D);
        this.type = type;
        this.data = data;
    }

    /**
     * Reads the raw packet data from the data stream.
     */
    public void readPacketData(PacketBuffer buf) throws IOException
    {
        this.entityId = buf.readVarInt();
        this.type = buf.readByte();
        this.x = buf.readInt();
        this.y = buf.readInt();
        this.z = buf.readInt();
        this.data = buf.readVarInt();
    }

    /**
     * Writes the raw packet data to the data stream.
     */
    public void writePacketData(PacketBuffer buf) throws IOException
    {
        buf.writeVarInt(this.entityId);
        buf.writeByte(this.type);
        buf.writeInt(this.x);
        buf.writeInt(this.y);
        buf.writeInt(this.z);
        buf.writeVarInt(this.data);
    }

    /**
     * Passes this Packet on to the NetHandler for processing.
     */
    public void processPacket(IClientPlayer handler)
    {
        handler.handleSpawnGlobalEntity(this);
    }

    public int getEntityId()
    {
        return this.entityId;
    }

    public int getEncodedX()
    {
        return this.x;
    }

    public int getEncodedY()
    {
        return this.y;
    }

    public int getEncodedZ()
    {
        return this.z;
    }

    public int getType()
    {
        return this.type;
    }

    public int getData()
    {
        return this.data;
    }
}
