package common.packet;

import java.io.IOException;

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

public class CPacketPlace implements Packet<IPlayer>
{
    private static final BlockPos DUMMY_POS = new BlockPos(-1, -1, -1);
    
    private BlockPos position;
    private int placedBlockDirection;
    private ItemStack stack;
    private float facingX;
    private float facingY;
    private float facingZ;

    public CPacketPlace()
    {
    }

    public CPacketPlace(ItemStack stackIn)
    {
        this(DUMMY_POS, 255, stackIn, 0.0F, 0.0F, 0.0F);
    }

    public CPacketPlace(BlockPos positionIn, int placedBlockDirectionIn, ItemStack stackIn, float facingXIn, float facingYIn, float facingZIn)
    {
        this.position = positionIn;
        this.placedBlockDirection = placedBlockDirectionIn;
        this.stack = stackIn != null ? stackIn.copy() : null;
        this.facingX = facingXIn;
        this.facingY = facingYIn;
        this.facingZ = facingZIn;
    }
    
    public void readPacketData(PacketBuffer buf) throws IOException
    {
        this.position = buf.readBlockPos();
        this.placedBlockDirection = buf.readUnsignedByte();
        this.stack = buf.readItemStackFromBuffer();
        this.facingX = (float)buf.readUnsignedByte() / 16.0F;
        this.facingY = (float)buf.readUnsignedByte() / 16.0F;
        this.facingZ = (float)buf.readUnsignedByte() / 16.0F;
    }
    
    public void writePacketData(PacketBuffer buf) throws IOException
    {
        buf.writeBlockPos(this.position);
        buf.writeByte(this.placedBlockDirection);
        buf.writeItemStackToBuffer(this.stack);
        buf.writeByte((int)(this.facingX * 16.0F));
        buf.writeByte((int)(this.facingY * 16.0F));
        buf.writeByte((int)(this.facingZ * 16.0F));
    }
    
    public void processPacket(IPlayer handler)
    {
        handler.processPlace(this);
    }

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

    public int getPlacedBlockDirection()
    {
        return this.placedBlockDirection;
    }

    public ItemStack getStack()
    {
        return this.stack;
    }
    
    public float getPlacedBlockOffsetX()
    {
        return this.facingX;
    }
    
    public float getPlacedBlockOffsetY()
    {
        return this.facingY;
    }
    
    public float getPlacedBlockOffsetZ()
    {
        return this.facingZ;
    }
}
