package link.infra.simpleprocessors.blocks.programmer;

import javax.annotation.Nullable;

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 net.minecraftforge.items.ItemStackHandler;
import net.minecraftforge.items.SlotItemHandler;

public class ProgrammerContainer extends Container {

	private ProgrammerTileEntity te;
	private Slot processorSlot;
	private boolean usable = true;
	
	private ItemStackHandler inputStackHandler = new ItemStackHandler(1);

	public ProgrammerContainer(IInventory playerInventory, ProgrammerTileEntity te) {
		this.te = te;
		
		addOwnSlots();
		addPlayerSlots(playerInventory);
	}


	private void addPlayerSlots(IInventory playerInventory) {
		// Slots for the main inventory
		for (int row = 0; row < 3; ++row) {
			for (int col = 0; col < 9; ++col) {
				int x = 9 + col * 18;
				int y = row * 18 + 70;
				this.addSlotToContainer(new Slot(playerInventory, col + row * 9 + 10, x, y));
			}
		}

		// Slots for the hotbar
		for (int row = 0; row < 9; ++row) {
			int x = 9 + row * 18;
			int y = 58 + 70;
			this.addSlotToContainer(new Slot(playerInventory, row, x, y));
		}
	}

	private void addOwnSlots() {
		processorSlot = new SlotItemHandler(inputStackHandler, 0, 146, 41);
		addSlotToContainer(processorSlot);
	}

	@Nullable
	@Override
	public ItemStack transferStackInSlot(EntityPlayer playerIn, int index) {
		ItemStack itemstack = ItemStack.EMPTY;
		Slot slot = this.inventorySlots.get(index);

		if (usable && slot != null && slot.getHasStack()) {
			ItemStack itemstack1 = slot.getStack();
			itemstack = itemstack1.copy();

			if (index < 1) {
				if (!this.mergeItemStack(itemstack1, 1, this.inventorySlots.size(), true)) {
					return ItemStack.EMPTY;
				}
			} else if (!this.mergeItemStack(itemstack1, 0, 1, false)) {
				return ItemStack.EMPTY;
			}

			if (itemstack1.isEmpty()) {
				slot.putStack(ItemStack.EMPTY);
			} else {
				slot.onSlotChanged();
			}
		}

		return itemstack;
	}

	@Override
	public boolean canInteractWith(EntityPlayer playerIn) {
		return te.canInteractWith(playerIn);
	}
	
	public void setUsable(boolean usable) {
		this.usable = usable;
		if (usable) {
			processorSlot.xPos = 146;
		} else {
			processorSlot.xPos = -999; // make slot invisible
		}
	}

}

@@ -129,24 +129,75 @@
 tityPlayer;%0A
+import net.minecraft.entity.player.EntityPlayerMP;%0A
 import net.m
@@ -2444,12 +2444,451 @@
 e%0A%09%09%7D%0A%09%7D
+%0A%09%0A%09@Override%0A%09public void onContainerClosed(EntityPlayer playerIn) %7B%0A%09%09super.onContainerClosed(playerIn);%0A%09%09if (!playerIn.isEntityAlive() %7C%7C playerIn instanceof EntityPlayerMP && ((EntityPlayerMP)playerIn).hasDisconnected()) %7B%0A%09%09%09playerIn.dropItem(inputStackHandler.extractItem(0, 64, false), false);%0A%09%09%7D else %7B%0A%09%09%09playerIn.inventory.placeItemBackInInventory(playerIn.getEntityWorld(), inputStackHandler.extractItem(0, 64, false));%0A%09%09%7D%0A%09%7D
 %0A%0A%7D%0A

Drop item or place item back if inventory closed