public int capShoppingAmount( int offerId, int wantedAmount, IInventory inv, IFluidHandler fluidHandler) { AmadronOffer offer = offers.get(offerId); if (offer.getInput() instanceof ItemStack) { if (inv != null) { ItemStack searchingItem = (ItemStack) offer.getInput(); int count = 0; for (int i = 0; i < inv.getSizeInventory(); i++) { if (inv.getStackInSlot(i) != null && inv.getStackInSlot(i).isItemEqual(searchingItem) && ItemStack.areItemStackTagsEqual(inv.getStackInSlot(i), searchingItem)) { count += inv.getStackInSlot(i).stackSize; } } int maxAmount = count / ((ItemStack) offer.getInput()).stackSize; if (wantedAmount > maxAmount) { problemState = EnumProblemState.NOT_ENOUGH_ITEMS; wantedAmount = maxAmount; } } else { wantedAmount = 0; problemState = EnumProblemState.NO_ITEM_PROVIDER; } } else { if (fluidHandler != null) { FluidStack searchingFluid = ((FluidStack) offer.getInput()).copy(); searchingFluid.amount = Integer.MAX_VALUE; FluidStack extracted = fluidHandler.drain(ForgeDirection.UP, searchingFluid, false); int maxAmount = 0; if (extracted != null) maxAmount = extracted.amount / ((FluidStack) offer.getInput()).amount; if (wantedAmount > maxAmount) { problemState = EnumProblemState.NOT_ENOUGH_FLUID; wantedAmount = maxAmount; } } else { wantedAmount = 0; problemState = EnumProblemState.NO_FLUID_PROVIDER; } } if (offer.getOutput() instanceof ItemStack) { if (inv != null) { ItemStack providingItem = ((ItemStack) offer.getOutput()).copy(); providingItem.stackSize *= wantedAmount; ItemStack remainder = IOHelper.insert(inv, providingItem.copy(), 0, true); if (remainder != null) { int maxAmount = (providingItem.stackSize - remainder.stackSize) / ((ItemStack) offer.getOutput()).stackSize; if (wantedAmount > maxAmount) { wantedAmount = maxAmount; problemState = EnumProblemState.NOT_ENOUGH_ITEM_SPACE; } } } else { wantedAmount = 0; problemState = EnumProblemState.NO_ITEM_PROVIDER; } } else { if (fluidHandler != null) { FluidStack providingFluid = ((FluidStack) offer.getOutput()).copy(); providingFluid.amount *= wantedAmount; int amountFilled = fluidHandler.fill(ForgeDirection.UP, providingFluid, false); int maxAmount = amountFilled / ((FluidStack) offer.getOutput()).amount; if (wantedAmount > maxAmount) { wantedAmount = maxAmount; problemState = EnumProblemState.NOT_ENOUGH_FLUID_SPACE; } } else { wantedAmount = 0; problemState = EnumProblemState.NO_FLUID_PROVIDER; } } return wantedAmount; }