package net.mcft.copy.betterstorage.inventory;

import net.mcft.copy.betterstorage.block.crate.CratePileData;
import net.mcft.copy.betterstorage.misc.ItemIdentifier;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.item.ItemStack;

/** An inventory interface built for machines accessing crate piles. */
public class InventoryCrateBlockView extends InventoryBetterStorage {
	
	// TODO: Rewrite this, some machines don't like the way this fake inventory works.
	// Suggestion: Do it similar to InventoryCratePlayerView, update internal
	//             inventory every tick instead of every access.
	
	private CratePileData data;
	
	public InventoryCrateBlockView(CratePileData data) {
		super("container.crate");
		this.data = data;
	}
	
	@Override
	public int getSizeInventory() {
		// Return lower inventory size so machines don't look at all the
		// empty slots, looking for specific item or stacks to merge with.
		return Math.min(data.getOccupiedSlots() + 10, data.getCapacity());
	}
	
	@Override
	public ItemStack getStackInSlot(int slot) {
		if (slot < 0 || slot >= getSizeInventory()) return null;
		// If there's space in the inventory, make the first slot be empty.
		// That way machines will easily find a slot to put items in.
		if (data.getFreeSlots() > 0 && --slot == -1) return null;
		if (slot >= data.getOccupiedSlots()) return null;
		ItemStack stack;
		int stackSize;
		int numItems = data.getNumItems();
		if (slot < numItems) {
			stack = data.getItemStack(slot);
			int stackLimit = stack.getItem().getItemStackLimit();
			stackSize = (stack.stackSize - 1) % stackLimit + 1;
		} else {
			slot -= numItems;
			int index = 0;
			do {
				stack = data.getItemStack(index);
				int numStacks = ItemIdentifier.calcNumStacks(stack) - 1;
				if (slot < numStacks) break;
				slot -= numStacks;
			} while (++index < data.getNumItems());
			stackSize = stack.getItem().getItemStackLimit();
		}
		stack = stack.copy();
		stack.stackSize = stackSize;
		return stack;
	}
	
	@Override
	public void setInventorySlotContents(int slot, ItemStack stack) {
		if (slot < 0 || slot >= getSizeInventory()) return;
		ItemStack oldStack = getStackInSlot(slot);
		if (oldStack != null) data.removeItems(oldStack);
		data.addItems(stack);
	}
	
	@Override
	public ItemStack decrStackSize(int slot, int amount) {
		ItemStack stack = getStackInSlot(slot);
		if (stack == null) return null;
		amount = Math.min(amount, stack.stackSize);
		return data.removeItems(stack, amount);
	}
	
	@Override
	public boolean isUseableByPlayer(EntityPlayer player) { return false; }
	
	@Override
	public void onInventoryChanged() { }
	@Override
	public void openChest() { }
	@Override
	public void closeChest() { }
	
}

@@ -395,618 +395,222 @@
 %0A%09%0A%09
-// TODO: Rewrite this, some machines don't like the way this fake inventory works.%0A%09// Suggestion: Do it similar to InventoryCratePlayerView, update internal%0A%09//             inventory every tick instead of every access.%0A%09%0A%09private CratePileData data;%0A%09%0A%09public InventoryCrateBlockView(CratePileData data) %7B%0A%09%09super(%22container.crate%22);%0A%09%09this.data = data;%0A%09%7D%0A%09%0A%09@Override%0A%09public int getSizeInventory() %7B%0A%09%09// Return lower inventory size so machines don't look at all the%0A%09%09// empty slots, looking for specific item or stacks to merge with.%0A%09%09return Math.min(data.getOccupiedSlots() + 10, data.getCapacity());%0A%09
+private CratePileData data;%0A%09%0A%09public InventoryCrateBlockView(CratePileData data) %7B%0A%09%09super(%22container.crate%22);%0A%09%09this.data = data;%0A%09%7D%0A%09%0A%09@Override%0A%09public int getSizeInventory() %7B return (data.getNumItems() + 1); 
 %7D%0A%09%0A
@@ -669,32 +669,33 @@
 ) %7B%0A%09%09if (slot %3C
+=
  0 %7C%7C slot %3E= ge
@@ -731,757 +731,97 @@
 ;%0A%09%09
-// If there's space in the inventory, make the first slot be empty.%0A%09%09// That way machines will easily find a slot to put items in.%0A%09%09if (data.getFreeSlots() %3E 0 && --slot == -1) return null;%0A%09%09if (slot %3E= data.getOccupiedSlots()) return null;%0A%09%09ItemStack stack;%0A%09%09int stackSize;%0A%09%09int numItems = data.getNumItems();%0A%09%09if (slot %3C numItems) %7B%0A%09%09%09stack = data.getItemStack(slot);%0A%09%09%09int stackLimit = stack.getItem().getItemStackLimit();%0A%09%09%09stackSize = (stack.stackSize - 1) %25 stackLimit + 1;%0A%09%09%7D else %7B%0A%09%09%09slot -= numItems;%0A%09%09%09int index = 0;%0A%09%09%09do %7B%0A%09%09%09%09stack = data.getItemStack(index);%0A%09%09%09%09int numStacks = ItemIdentifier.calcNumStacks(stack) - 1;%0A%09%09%09%09if (slot %3C numStacks) break;%0A%09%09%09%09slot -= numStacks;%0A%09%09%09%7D while (++index %3C data.getNumItems());%0A%09%09%09
+ItemStack stack = data.getItemStack(slot - 1).copy();%0A%09%09stack.stackSize = Math.min(stack.
 stac
@@ -821,26 +821,25 @@
 ck.stackSize
- =
+,
  stack.getIt
@@ -840,93 +840,20 @@
 .get
-Item().getItemStackLimit();%0A%09%09%7D%0A%09%09stack = stack.copy();%0A%09%09stack.stackSize = s
+MaxS
 tackSize
 ;%0A%09%09
@@ -848,16 +848,19 @@
 tackSize
+())
 ;%0A%09%09retu
@@ -1040,32 +1040,36 @@
 StackInSlot(slot
+ - 1
 );%0A%09%09if (oldStac
@@ -1375,32 +1375,162 @@
 %09%7D%0A%09%0A%09@Override%0A
+%09public boolean isStackValidForSlot(int slot, ItemStack stack) %7B%0A%09%09return (slot != 0 %7C%7C data.getFreeSlots() %3E 0);%0A%09%7D%0A%09%0A%09@Override%0A
 %09public boolean 

Simplify InventoryCrateBlockView