package net.blay09.mods.cookingbook.block;

import net.blay09.mods.cookingbook.CookingBook;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.nbt.NBTTagList;
import net.minecraft.network.NetworkManager;
import net.minecraft.network.Packet;
import net.minecraft.network.play.server.S35PacketUpdateTileEntity;
import net.minecraft.tileentity.TileEntity;
import net.minecraft.util.AxisAlignedBB;
import net.minecraftforge.common.util.Constants;

public class TileEntityFridge extends TileEntity implements IInventory {

    private ItemStack[] inventory = new ItemStack[27];
    private TileEntityFridge neighbourFridge;
    private int fridgeColor;
    private float prevDoorAngle;
    private float doorAngle;
    private int numPlayersUsing;

    public void findNeighbourFridge() {
        if(worldObj.getBlock(xCoord, yCoord + 1, zCoord) == CookingBook.blockFridge) {
            neighbourFridge = (TileEntityFridge) worldObj.getTileEntity(xCoord, yCoord + 1, zCoord);
        } else if(worldObj.getBlock(xCoord, yCoord - 1, zCoord) == CookingBook.blockFridge) {
            neighbourFridge = (TileEntityFridge) worldObj.getTileEntity(xCoord, yCoord - 1, zCoord);
        } else {
            neighbourFridge = null;
        }
    }

    @Override
    public int getSizeInventory() {
        return inventory.length + (neighbourFridge != null ? neighbourFridge.getSizeInventory() : 0);
    }

    @Override
    public ItemStack getStackInSlot(int i) {
        if(i < inventory.length) {
            return inventory[i];
        } else if(neighbourFridge != null) {
            return neighbourFridge.getStackInSlot(i - inventory.length);
        }
        return null;
    }

    @Override
    public ItemStack decrStackSize(int i, int count) {
        if(i < inventory.length) {
            if(inventory[i] != null) {
                ItemStack itemStack;
                if (inventory[i].stackSize <= count) {
                    itemStack = inventory[i];
                    inventory[i] = null;
                    return itemStack;
                } else {
                    itemStack = inventory[i].splitStack(count);
                    if (inventory[i].stackSize == 0) {
                        inventory[i] = null;
                    }
                    return itemStack;
                }
            }
        } else if(neighbourFridge != null) {
            return neighbourFridge.decrStackSize(i - inventory.length, count);
        }
        return null;
    }

    @Override
    public ItemStack getStackInSlotOnClosing(int i) {
        if (inventory[i] != null) {
            ItemStack itemstack = inventory[i];
            inventory[i] = null;
            return itemstack;
        } else {
            return null;
        }
    }

    @Override
    public void setInventorySlotContents(int i, ItemStack itemStack) {
        if(i < inventory.length) {
            inventory[i] = itemStack;
        } else if(neighbourFridge != null) {
            neighbourFridge.setInventorySlotContents(i - inventory.length, itemStack);
        }
    }

    @Override
    public void updateEntity() {
        super.updateEntity();

        prevDoorAngle = doorAngle;
        if(numPlayersUsing > 0) {
            final float doorSpeed = 0.2f;
            doorAngle = Math.min(1f, doorAngle + doorSpeed);
            if(doorAngle == 1f) {
                numPlayersUsing = 0;
            }
        } else {
            final float doorSpeed = 0.1f;
            doorAngle = Math.max(0f, doorAngle - doorSpeed);
            if(doorAngle == 0f) {
                numPlayersUsing = 1;
            }
        }
    }

    @Override
    public String getInventoryName() {
        return "container.cookingbook:fridge";
    }

    @Override
    public boolean hasCustomInventoryName() {
        return false;
    }

    @Override
    public int getInventoryStackLimit() {
        return 64;
    }

    @Override
    public boolean isUseableByPlayer(EntityPlayer player) {
        return true;
    }

    @Override
    public boolean receiveClientEvent(int id, int value) {
        if(id == 1) {
            numPlayersUsing = value;
            return true;
        }
        return super.receiveClientEvent(id, value);
    }

    @Override
    public void openInventory() {
        numPlayersUsing++;
        worldObj.addBlockEvent(xCoord, yCoord, zCoord, getBlockType(), 1, numPlayersUsing);
    }

    @Override
    public void closeInventory() {
        numPlayersUsing--;
        worldObj.addBlockEvent(xCoord, yCoord, zCoord, getBlockType(), 1, numPlayersUsing);
    }

    @Override
    public boolean isItemValidForSlot(int i, ItemStack itemStack) {
        return true;
    }

    public boolean isLargeFridge() {
        return neighbourFridge != null;
    }

    @Override
    public AxisAlignedBB getRenderBoundingBox() {
        return AxisAlignedBB.getBoundingBox(xCoord, yCoord, zCoord, xCoord + 1, yCoord + 2, zCoord + 1);
    }

    public void setFridgeColor(int fridgeColor) {
        this.fridgeColor = fridgeColor;
        markDirty();
    }

    public int getFridgeColor() {
        return fridgeColor;
    }

    @Override
    public void readFromNBT(NBTTagCompound tagCompound) {
        super.readFromNBT(tagCompound);

        NBTTagList tagList = tagCompound.getTagList("Items", Constants.NBT.TAG_COMPOUND);
        for(int i = 0; i < tagList.tagCount(); i++) {
            NBTTagCompound itemCompound = tagList.getCompoundTagAt(i);
            inventory[itemCompound.getByte("Slot")] = ItemStack.loadItemStackFromNBT(itemCompound);
        }
        fridgeColor = tagCompound.getByte("FridgeColor");
    }

    @Override
    public void writeToNBT(NBTTagCompound tagCompound) {
        super.writeToNBT(tagCompound);

        NBTTagList tagList = new NBTTagList();
        for(int i = 0; i < inventory.length; i++) {
            if(inventory[i] != null) {
                NBTTagCompound itemCompound = new NBTTagCompound();
                itemCompound.setByte("Slot", (byte) i);
                inventory[i].writeToNBT(itemCompound);
            }
        }
        tagCompound.setTag("Items", tagList);
        tagCompound.setByte("FridgeColor", (byte) fridgeColor);
    }

    @Override
    public void onDataPacket(NetworkManager net, S35PacketUpdateTileEntity pkt) {
        super.onDataPacket(net, pkt);
        NBTTagCompound tagCompound = pkt.func_148857_g();
        readFromNBT(tagCompound);
    }

    @Override
    public Packet getDescriptionPacket() {
        NBTTagCompound tagCompound = new NBTTagCompound();
        writeToNBT(tagCompound);
        return new S35PacketUpdateTileEntity(xCoord, yCoord, zCoord, 0, tagCompound);
    }

    public TileEntityFridge getNeighbourFridge() {
        return neighbourFridge;
    }

    public float getDoorAngle() {
        return doorAngle;
    }

    public float getPrevDoorAngle() {
        return prevDoorAngle;
    }
}

@@ -3450,93 +3450,8 @@
 d);%0A
-            if(doorAngle == 1f) %7B%0A                numPlayersUsing = 0;%0A            %7D%0A
     
@@ -3570,93 +3570,8 @@
 d);%0A
-            if(doorAngle == 0f) %7B%0A                numPlayersUsing = 1;%0A            %7D%0A
     

Disable door madness