package common.inventory;

import common.entity.npc.EntityNPC;
import common.init.Blocks;
import common.item.ItemStack;
import common.nbt.NBTTagCompound;
import common.nbt.NBTTagList;
import common.util.BlockPos;

public class InventoryWarpChest extends InventoryBasic
{
    private BlockPos associatedChest;

    public InventoryWarpChest()
    {
        super("Warptruhe", false, 27);
    }

    public void setChestTileEntity(BlockPos chestTileEntity)
    {
        this.associatedChest = chestTileEntity;
    }

    public void loadInventoryFromNBT(NBTTagList p_70486_1_)
    {
        for (int i = 0; i < this.getSizeInventory(); ++i)
        {
            this.setInventorySlotContents(i, (ItemStack)null);
        }

        for (int k = 0; k < p_70486_1_.tagCount(); ++k)
        {
            NBTTagCompound nbttagcompound = p_70486_1_.getCompoundTagAt(k);
            int j = nbttagcompound.getByte("Slot") & 255;

            if (j >= 0 && j < this.getSizeInventory())
            {
                this.setInventorySlotContents(j, ItemStack.loadItemStackFromNBT(nbttagcompound));
            }
        }
    }

    public NBTTagList saveInventoryToNBT()
    {
        NBTTagList nbttaglist = new NBTTagList();

        for (int i = 0; i < this.getSizeInventory(); ++i)
        {
            ItemStack itemstack = this.getStackInSlot(i);

            if (itemstack != null)
            {
                NBTTagCompound nbttagcompound = new NBTTagCompound();
                nbttagcompound.setByte("Slot", (byte)i);
                itemstack.writeToNBT(nbttagcompound);
                nbttaglist.appendTag(nbttagcompound);
            }
        }

        return nbttaglist;
    }
    
    public boolean isUseableByPlayer(EntityNPC player)
    {
        return (this.associatedChest == null || (player.worldObj.getState(this.associatedChest).getBlock() == Blocks.warp_chest && player.getDistanceSq((double)this.associatedChest.getX() + 0.5D, (double)this.associatedChest.getY() + 0.5D, (double)this.associatedChest.getZ() + 0.5D) <= 64.0D)) && super.isUseableByPlayer(player);
    }

//    public void openInventory(EntityNPC player)
//    {
//        if (this.associatedChest != null)
//        {
//            this.associatedChest.openChest();
//        }
//
//        super.openInventory(player);
//    }

    public void closeInventory(EntityNPC player)
    {
//        if (this.associatedChest != null)
//        {
//            this.associatedChest.closeChest();
//        }

        super.closeInventory(player);
        this.associatedChest = null;
    }
}
