package net.mcft.copy.betterstorage.utils;

import java.util.ArrayList;
import java.util.List;

import net.minecraft.inventory.IInventory;
import net.minecraft.item.Item;
import net.minecraft.item.ItemStack;

public class InventoryUtils {
	
	/** Standard decrStackSize which modifies stackSize directly. */
	public static ItemStack unsafeDecreaseStackSize(IInventory inventory, int slot, int amount) {
		ItemStack stack = inventory.getStackInSlot(slot);
		if (stack == null) return null;
		amount = Math.min(amount, stack.stackSize);
		if (amount < stack.stackSize) {
			stack.stackSize -= amount;
			stack = stack.copy();
			stack.stackSize = amount;
		} else inventory.setInventorySlotContents(slot, null);
		return stack;
	}
	
	/** Returns if the inventory has a specific item. */
	public static boolean hasItem(IInventory inventory, Item item) {
		for (int i = 0; i < inventory.getSizeInventory(); i++) {
			ItemStack stack = inventory.getStackInSlot(i);
			if (stack != null && stack.getItem() == item) return true;
		}
		return false;
	}
	/** Finds and returns a specific item in the inventory, null if none found. */
	public static ItemStack findItem(IInventory inventory, Item item) {
		for (int i = 0; i < inventory.getSizeInventory(); i++) {
			ItemStack stack = inventory.getStackInSlot(i);
			if (stack != null && stack.getItem() == item) return stack;
		}
		return null;
	}
	/** Finds and returns the slot a specific item is in, -1 if none found. */
	public static int findItemSlot(IInventory inventory, Item item) {
		for (int i = 0; i < inventory.getSizeInventory(); i++) {
			ItemStack stack = inventory.getStackInSlot(i);
			if (stack != null && stack.getItem() == item) return i;
		}
		return -1;
	}

	/** Returns a list of items of the specific type in the inventory. */
	public static int countItems(IInventory inventory, Item item) {
		int count = 0;
		for (int i = 0; i < inventory.getSizeInventory(); i++) {
			ItemStack stack = inventory.getStackInSlot(i);
			if (stack != null && stack.getItem() == item) count++;
		}
		return count;
	}
	/** Returns a list of items of the specific type in the inventory. */
	public static List<ItemStack> findItems(IInventory inventory, Item item) {
		List<ItemStack> list = new ArrayList<ItemStack>();
		for (int i = 0; i < inventory.getSizeInventory(); i++) {
			ItemStack stack = inventory.getStackInSlot(i);
			if (stack != null && stack.getItem() == item) list.add(stack);
		}
		return list;
	}
	/** Returns a list of slot indices which hold a specific item type in the inventory. */
	public static List<Integer> findItemSlots(IInventory inventory, Item item) {
		List<Integer> list = new ArrayList<Integer>();
		for (int i = 0; i < inventory.getSizeInventory(); i++) {
			ItemStack stack = inventory.getStackInSlot(i);
			if (stack != null && stack.getItem() == item) list.add(i);
		}
		return list;
	}
	
}

@@ -200,16 +200,57 @@
 emStack;
+%0Aimport net.minecraft.nbt.NBTTagCompound;
 %0A%0Apublic
@@ -2904,10 +2904,293 @@
 t;%0A%09%7D%0A%09%0A
+%09public static ItemStack copyStack(ItemStack stack, int stackSize) %7B%0A%09%09ItemStack copy = new ItemStack(stack.itemID, stackSize, stack.getItemDamage());%0A%09%09if (stack.stackTagCompound != null)%0A%09%09%09copy.stackTagCompound = (NBTTagCompound)stack.stackTagCompound.copy();%0A%09%09return copy;%0A%09%7D%0A%09%0A
 %7D%0A

Add InventoryUtils.copyStack