package net.mcft.copy.betterstorage.container;

import net.mcft.copy.betterstorage.inventory.InventoryCratePlayerView;
import net.mcft.copy.betterstorage.tile.crate.CratePileData;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.InventoryBasic;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;
import net.minecraft.network.packet.Packet103SetSlot;
import cpw.mods.fml.common.network.PacketDispatcher;
import cpw.mods.fml.common.network.Player;
import cpw.mods.fml.relauncher.Side;
import cpw.mods.fml.relauncher.SideOnly;

public class ContainerCrate extends ContainerBetterStorage {
	
	private InventoryCratePlayerView playerView;
	
	public int fullness = 0;
	private int lastFullness = 0;
	
	public ContainerCrate(EntityPlayer player, InventoryCratePlayerView inventory) {
		super(player, inventory, 9, inventory.getSizeInventory() / 9);
		this.playerView = inventory;
		CratePileData data = inventory.data;
		fullness = data.getOccupiedSlots() * 255 / data.getCapacity();
	}
	public ContainerCrate(EntityPlayer player, int rows, String name, boolean localized) {
		super(player, new InventoryBasic(name, !localized, 9 * rows), 9, rows);
	}
	
	@Override
	public void detectAndSendChanges() {
		if (playerView == null) return;
		super.detectAndSendChanges();
		
		CratePileData data = playerView.data;
		// detectAndSendChanges gets called after a crate gets broken.
		// So to prevent a division by zero, just return.
		if (data.getNumCrates() <= 0) return;
		fullness = data.getOccupiedSlots() * 255 / data.getCapacity();
		sendUpdateIfChanged(0, fullness, lastFullness);
		lastFullness = fullness;
	}
	
	@Override
	@SideOnly(Side.CLIENT)
	public void updateProgressBar(int id, int val) {
		if (id == 0) fullness = val;
	}
	
	@Override
	public ItemStack transferStackInSlot(EntityPlayer player, int slotId) {
		Slot slot = (Slot)inventorySlots.get(slotId);
		ItemStack slotStack = slot.getStack();
		if (slotStack == null) return null;
		ItemStack stackBefore = slotStack.copy();
		// If slot is in the container inventory, try to transfer the item to the player.
		if (slotId < getColumns() * getRows()) {
			if (playerView == null)
				return super.transferStackInSlot(player, slotId);
			int count = slotStack.stackSize;
			boolean success = mergeItemStack(slotStack, playerView.getSizeInventory(), inventorySlots.size(), true);
			int amount = count - slotStack.stackSize;
			slotStack.stackSize = count;
			playerView.decrStackSize(slotId, amount);
			if (!success) return null;
		// If slot is in the player inventory, try to transfer the item to the container.
		} else {
			if (playerView != null) {
				boolean success = playerView.canFitSome(slotStack);
				ItemStack overflow = playerView.data.addItems(slotStack);
				slot.putStack(overflow);
				// Send slot contents to player if it doesn't match the calculated overflow.
				PacketDispatcher.sendPacketToPlayer(new Packet103SetSlot(player.openContainer.windowId, slotId, overflow), (Player)player);
				if (!success) return null;
			} else {
				
			}
		}
		return stackBefore;
	}
	
}

@@ -1861,16 +1861,201 @@
 otId) %7B%0A
+%09%09// On the client, don't do anything fancy, just%0A%09%09// use ContainerBetterStorage.transferStackInSlot.%0A%09%09if (playerView == null)%0A%09%09%09return super.transferStackInSlot(player, slotId);%0A%09%09%0A
 %09%09Slot s
@@ -2094,16 +2094,16 @@
 lotId);%0A
-
 %09%09ItemSt
@@ -2217,16 +2217,19 @@
 copy();%0A
+%09%09%0A
 %09%09// If 
@@ -2351,89 +2351,8 @@
 ) %7B%0A
-%09%09%09if (playerView == null)%0A%09%09%09%09return super.transferStackInSlot(player, slotId);%0A
 %09%09%09i
@@ -2643,16 +2643,19 @@
 n null;%0A
+%09%09%0A
 %09%09// If 
@@ -2745,38 +2745,8 @@
 e %7B%0A
-%09%09%09if (playerView != null) %7B%0A%09
 %09%09%09b
@@ -2796,17 +2796,16 @@
 Stack);%0A
-%09
 %09%09%09ItemS
@@ -2860,17 +2860,16 @@
 ck);%0A%09%09%09
-%09
 slot.put
@@ -2885,17 +2885,16 @@
 rflow);%0A
-%09
 %09%09%09// Se
@@ -2968,17 +2968,16 @@
 low.%0A%09%09%09
-%09
 PacketDi
@@ -3095,17 +3095,16 @@
 er);%0A%09%09%09
-%09
 if (!suc
@@ -3126,30 +3126,8 @@
 ll;%0A
-%09%09%09%7D else %7B%0A%09%09%09%09%0A%09%09%09%7D%0A
 %09%09%7D%0A

Fix #190, crate container not transfering items