public static InvStack takeDefinedItem(IInventory inventory, int side, ItemStack type, int min, int max)
{
inventory = checkChestInv(inventory);
InvStack ret = new InvStack(inventory);
if(!(inventory instanceof ISidedInventory))
{
for(int i = inventory.getSizeInventory() - 1; i >= 0; i--)
{
if(inventory.getStackInSlot(i) != null && StackUtils.equalsWildcard(inventory.getStackInSlot(i), type))
{
ItemStack stack = inventory.getStackInSlot(i);
int current = ret.getStack() != null ? ret.getStack().stackSize : 0;
if(current+stack.stackSize <= max)
{
ret.appendStack(i, stack.copy());
}
else {
ItemStack copy = stack.copy();
copy.stackSize = max-current;
ret.appendStack(i, copy);
}
if(ret.getStack() != null && ret.getStack().stackSize == max)
{
return ret;
}
}
}
}
else {
ISidedInventory sidedInventory = (ISidedInventory)inventory;
int[] slots = sidedInventory.getAccessibleSlotsFromSide(ForgeDirection.OPPOSITES[side]);
if(slots != null && slots.length != 0)
{
for(int get = slots.length - 1; get >= 0; get--)
{
int slotID = slots[get];
if(sidedInventory.getStackInSlot(slotID) != null && StackUtils.equalsWildcard(inventory.getStackInSlot(slotID), type))
{
ItemStack stack = sidedInventory.getStackInSlot(slotID);
int current = ret.getStack() != null ? ret.getStack().stackSize : 0;
if(current+stack.stackSize <= max)
{
ItemStack copy = stack.copy();
if(sidedInventory.canExtractItem(slotID, copy, ForgeDirection.OPPOSITES[side]))
{
ret.appendStack(slotID, copy);
}
}
else {
ItemStack copy = stack.copy();
if(sidedInventory.canExtractItem(slotID, copy, ForgeDirection.OPPOSITES[side]))
{
copy.stackSize = max-current;
ret.appendStack(slotID, copy);
}
}
if(ret.getStack() != null && ret.getStack().stackSize == max)
{
return ret;
}
}
}
}
}
if(ret != null && ret.getStack() != null && ret.getStack().stackSize >= min)
{
return ret;
}
return null;