package common.packet;

import java.io.IOException;

import common.init.TileRegistry;
import common.network.IClientPlayer;
import common.network.Packet;
import common.network.PacketBuffer;
import common.tags.TagObject;
import common.tileentity.TileEntity;
import common.util.BlockPos;

public class SPacketUpdateTileEntity implements Packet<IClientPlayer>
{
    private BlockPos blockPos;
    private int type;
    private TagObject tag;

    public SPacketUpdateTileEntity()
    {
    }

    public SPacketUpdateTileEntity(TileEntity tile)
    {
        this.blockPos = tile.getPos();
        this.type = TileRegistry.CLASS_TO_ID.get(tile.getClass());
        tile.writeTags(this.tag = new TagObject());
    }

    /**
     * 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.tag = buf.readTag();
    }

    /**
     * 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.writeTag(this.tag);
    }

    /**
     * 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.CLASS_TO_ID.get(tile.getClass());
    }

    public TagObject getTags()
    {
        return this.tag;
    }
}
