package common.packet;

import java.io.IOException;

import common.network.IPlayer;
import common.network.Packet;
import common.network.PacketBuffer;
import common.util.BlockPos;

public class CPacketComplete implements Packet<IPlayer>
{
    private String message;
    private int entityId;
    private BlockPos position;

    public CPacketComplete()
    {
    }

    public CPacketComplete(String msg, int entity, BlockPos pos)
    {
        this.message = msg;
        this.entityId = entity;
        this.position = pos;
    }
    
    public void readPacketData(PacketBuffer buf) throws IOException
    {
        this.message = buf.readString(32767);
        this.entityId = buf.readVarInt();
        if(buf.readBoolean())
        	this.position = buf.readBlockPos();
    }
    
    public void writePacketData(PacketBuffer buf) throws IOException
    {
        buf.writeString(this.message.length() > 32767 ? this.message.substring(0, 32767) : this.message);
        buf.writeVarInt(this.entityId);
        buf.writeBoolean(this.position != null);
        if(this.position != null)
        	buf.writeBlockPos(this.position);
    }
    
    public void processPacket(IPlayer handler)
    {
        handler.processComplete(this);
    }

    public String getMessage()
    {
        return this.message;
    }

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

    public BlockPos getPosition()
    {
        return this.position;
    }
}
