001package net.minecraft.inventory;
002
003import net.minecraft.entity.player.EntityPlayer;
004import net.minecraft.item.ItemStack;
005import net.minecraft.nbt.NBTTagCompound;
006import net.minecraft.nbt.NBTTagList;
007import net.minecraft.tileentity.TileEntityEnderChest;
008
009public class InventoryEnderChest extends InventoryBasic
010{
011    private TileEntityEnderChest associatedChest;
012
013    public InventoryEnderChest()
014    {
015        super("container.enderchest", 27);
016    }
017
018    public void setAssociatedChest(TileEntityEnderChest par1TileEntityEnderChest)
019    {
020        this.associatedChest = par1TileEntityEnderChest;
021    }
022
023    public void loadInventoryFromNBT(NBTTagList par1NBTTagList)
024    {
025        int var2;
026
027        for (var2 = 0; var2 < this.getSizeInventory(); ++var2)
028        {
029            this.setInventorySlotContents(var2, (ItemStack)null);
030        }
031
032        for (var2 = 0; var2 < par1NBTTagList.tagCount(); ++var2)
033        {
034            NBTTagCompound var3 = (NBTTagCompound)par1NBTTagList.tagAt(var2);
035            int var4 = var3.getByte("Slot") & 255;
036
037            if (var4 >= 0 && var4 < this.getSizeInventory())
038            {
039                this.setInventorySlotContents(var4, ItemStack.loadItemStackFromNBT(var3));
040            }
041        }
042    }
043
044    public NBTTagList saveInventoryToNBT()
045    {
046        NBTTagList var1 = new NBTTagList("EnderItems");
047
048        for (int var2 = 0; var2 < this.getSizeInventory(); ++var2)
049        {
050            ItemStack var3 = this.getStackInSlot(var2);
051
052            if (var3 != null)
053            {
054                NBTTagCompound var4 = new NBTTagCompound();
055                var4.setByte("Slot", (byte)var2);
056                var3.writeToNBT(var4);
057                var1.appendTag(var4);
058            }
059        }
060
061        return var1;
062    }
063
064    /**
065     * Do not make give this method the name canInteractWith because it clashes with Container
066     */
067    public boolean isUseableByPlayer(EntityPlayer par1EntityPlayer)
068    {
069        return this.associatedChest != null && !this.associatedChest.isUseableByPlayer(par1EntityPlayer) ? false : super.isUseableByPlayer(par1EntityPlayer);
070    }
071
072    public void openChest()
073    {
074        if (this.associatedChest != null)
075        {
076            this.associatedChest.openChest();
077        }
078
079        super.openChest();
080    }
081
082    public void closeChest()
083    {
084        if (this.associatedChest != null)
085        {
086            this.associatedChest.closeChest();
087        }
088
089        super.closeChest();
090        this.associatedChest = null;
091    }
092}