package squeek.spiceoflife.helpers;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import net.minecraft.inventory.IInventory;
import net.minecraft.item.ItemStack;
import net.minecraft.tileentity.TileEntityHopper;
import net.minecraft.world.World;
import net.minecraftforge.common.util.ForgeDirection;
import cpw.mods.fml.relauncher.ReflectionHelper;

public class InventoryHelper
{
	public static final Method hopperInsertIntoInventory = ReflectionHelper.findMethod(TileEntityHopper.class, null, new String[]{"func_145899_c", "c"}, IInventory.class, ItemStack.class, int.class, int.class);

	public static IInventory getInventoryAtLocation(World world, int x, int y, int z)
	{
		return TileEntityHopper.func_145893_b(world, x, y, z);
	}

	public static ItemStack insertStackIntoInventory(ItemStack itemStack, IInventory inventory)
	{
		return TileEntityHopper.func_145889_a(inventory, itemStack, ForgeDirection.DOWN.ordinal());
	}

	/**
	 * Only fill a maximum of one slot
	 * @return The remainder
	 */
	public static ItemStack insertStackIntoInventoryOnce(ItemStack itemStack, IInventory inventory)
	{
		int originalStackSize = itemStack.stackSize;

		for (int l = 0; l < inventory.getSizeInventory(); ++l)
		{
			try
			{
				itemStack = (ItemStack) hopperInsertIntoInventory.invoke(null, inventory, itemStack, l, ForgeDirection.DOWN.ordinal());
			}
			catch (Exception e)
			{
				e.printStackTrace();
			}

			if (itemStack == null || itemStack.stackSize != originalStackSize)
				break;
		}

		if (itemStack != null && itemStack.stackSize == 0)
		{
			itemStack = null;
		}

		return itemStack;
	}
	
	public static List<Integer> getNonEmptySlotsInInventory(IInventory inventory)
	{
		List<Integer> nonEmptySlotIndexes = new ArrayList<Integer>();
		for (int slotNum = 0; slotNum < inventory.getSizeInventory(); slotNum++)
		{
			if (inventory.getStackInSlot(slotNum) != null)
				nonEmptySlotIndexes.add(slotNum);
		}
		return nonEmptySlotIndexes;
	}

	public static int getRandomNonEmptySlotInInventory(IInventory inventory, Random random)
	{
		List<Integer> nonEmptySlots = getNonEmptySlotsInInventory(inventory);

		if (nonEmptySlots.size() > 0)
			return nonEmptySlots.get(random.nextInt(nonEmptySlots.size()));
		else
			return 0;
	}

	public static ItemStack removeRandomSingleItemFromInventory(IInventory inventory, Random random)
	{
		int randomNonEmptySlotIndex = getRandomNonEmptySlotInInventory(inventory, random);

		if (inventory.getStackInSlot(randomNonEmptySlotIndex) != null)
			return inventory.decrStackSize(randomNonEmptySlotIndex, 1);
		else
			return null;
	}
}

@@ -959,33 +959,36 @@
  ForgeDirection.
-D
+UNKN
 OWN.ordinal());%0A
@@ -1391,17 +1391,20 @@
 rection.
-D
+UNKN
 OWN.ordi

Use ForgeDirection.UNKNOWN when inserting into an inventory from a food container