package extracells.container;

import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.inventory.Container;
import net.minecraft.inventory.IInventory;
import net.minecraft.inventory.Slot;
import net.minecraft.item.ItemStack;

import extracells.container.slot.SlotRespective;
import extracells.part.PartDrive;

public class ContainerDrive extends Container {
	PartDrive part;

	public ContainerDrive(PartDrive part, EntityPlayer player) {
		this.part = part;
		for (int i = 0; i < 2; i++) {
			for (int j = 0; j < 3; j++) {
				addSlotToContainer(new SlotRespective(part.getInventory(), j
					+ i * 3, 18 + 71 - i * 18, j * 18 - 4));
			}
		}
		bindPlayerInventory(player.inventory);
	}

	protected void bindPlayerInventory(IInventory inventoryPlayer) {
		for (int i = 0; i < 3; i++) {
			for (int j = 0; j < 9; j++) {
				addSlotToContainer(new Slot(inventoryPlayer, j + i * 9 + 9,
					8 + j * 18, i * 18 + 63));
			}
		}

		for (int i = 0; i < 9; i++) {
			addSlotToContainer(new Slot(inventoryPlayer, i, 8 + i * 18, 121));
		}
	}

	@Override
	public boolean canInteractWith(EntityPlayer entityplayer) {
		return part.isValid();
	}

	@Override
	public ItemStack transferStackInSlot(EntityPlayer player, int slotnumber) {
		ItemStack transferStack = ItemStack.EMPTY;
		Slot slot = this.inventorySlots.get(slotnumber);

		if (slot != null && slot.getHasStack()) {
			ItemStack itemStack = slot.getStack();
			transferStack = itemStack.copy();

			if (slotnumber < 36) {
				if (!mergeItemStack(itemStack, 36, this.inventorySlots.size(), true)) {
					return ItemStack.EMPTY;
				}
			} else if (!mergeItemStack(itemStack, 0, 36, false)) {
				return ItemStack.EMPTY;
			}

			if (itemStack.getCount() == 0) {
				slot.putStack(ItemStack.EMPTY);
			} else {
				slot.onSlotChanged();
			}
		}

		return transferStack;
	}
}

@@ -487,17 +487,17 @@
  0; i %3C 
-2
+3
 ; i++) %7B
@@ -520,17 +520,17 @@
  0; j %3C 
-3
+2
 ; j++) %7B
@@ -597,17 +597,17 @@
 (), 
-j
+i
 %0A%09%09%09%09%09+ 
 i * 
@@ -602,17 +602,30 @@
 %0A%09%09%09%09%09+ 
-i
+(j * (-1) + 1)
  * 3, 18
@@ -636,17 +636,30 @@
 1 - 
-i
+(j * (-1) + 1)
  * 18, 
-j
+i
  * 1
@@ -1498,17 +1498,16 @@
 umber %3C 
-3
 6) %7B%0A%09%09%09
@@ -1538,17 +1538,16 @@
 mStack, 
-3
 6, this.
@@ -1573,11 +1573,205 @@
 (), 
-tru
+false)) %7B%0A%09%09%09%09%09return ItemStack.EMPTY;%0A%09%09%09%09%7D%0A%09%09%09%7D else if (slotnumber %3C 33)%7B%0A%09%09%09%09if (!mergeItemStack(itemStack, 0, 6, false) &&%0A%09%09%09%09%09%09!mergeItemStack(itemStack, 33, this.inventorySlots.size(), fals
 e)) 
@@ -1855,10 +1855,58 @@
  0, 
-3
 6
+, false) &&%0A%09%09%09%09%09!mergeItemStack(itemStack, 6, 33
 , fa

fix #494