package common.packet;

import java.io.IOException;

import common.init.TileRegistry;
import common.nbt.NBTTagCompound;
import common.network.IClientPlayer;
import common.network.Packet;
import common.network.PacketBuffer;
import common.tileentity.TileEntity;
import common.util.BlockPos;

public class S35PacketUpdateTileEntity implements Packet<IClientPlayer>
{
    private BlockPos blockPos;
    private int type;
    private NBTTagCompound nbt;

    public S35PacketUpdateTileEntity()
    {
    }

    public S35PacketUpdateTileEntity(TileEntity tile)
    {
        this.blockPos = tile.getPos();
        this.type = TileRegistry.classToIdMap.get(tile.getClass());
        tile.writeToNBT(this.nbt = new NBTTagCompound());
    }

    /**
     * Reads the raw packet data from the data stream.
     */
    public void readPacketData(PacketBuffer buf) throws IOException
    {
        this.blockPos = buf.readBlockPos();
        this.type = buf.readUnsignedByte();
        this.nbt = buf.readNBTTagCompoundFromBuffer();
    }

    /**
     * Writes the raw packet data to the data stream.
     */
    public void writePacketData(PacketBuffer buf) throws IOException
    {
        buf.writeBlockPos(this.blockPos);
        buf.writeByte((byte)this.type);
        buf.writeNBTTagCompoundToBuffer(this.nbt);
    }

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

    public BlockPos getPos()
    {
        return this.blockPos;
    }

    public boolean isTileEntityType(TileEntity tile)
    {
        return this.type == TileRegistry.classToIdMap.get(tile.getClass());
    }

    public NBTTagCompound getNbtCompound()
    {
        return this.nbt;
    }
}
