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;
 }
  @Override
  public void handleGUIButtonPress(int guiID, EntityPlayer player) {
    super.handleGUIButtonPress(guiID, player);
    if (guiID == 1) {
      for (int i = 0; i < shoppingItems.length; i++) {
        if (shoppingItems[i] >= 0) {
          AmadronOffer offer = offers.get(shoppingItems[i]);
          if (offer.getInput() instanceof ItemStack) {
            ItemStack queryingItems = (ItemStack) offer.getInput();
            int amount = queryingItems.stackSize * shoppingAmounts[i];
            List<ItemStack> stacks = new ArrayList<ItemStack>();
            while (amount > 0) {
              ItemStack stack = queryingItems.copy();
              stack.stackSize = Math.min(amount, stack.getMaxStackSize());
              stacks.add(stack);
              amount -= stack.stackSize;
            }
            ChunkPosition pos =
                ItemAmadronTablet.getItemProvidingLocation(player.getCurrentEquippedItem());
            World world = null;
            if (pos == null) {
              pos = new ChunkPosition((int) player.posX, (int) player.posY, (int) player.posZ);
              world = player.worldObj;
            } else {
              world =
                  ItemAmadronTablet.getWorldForDimension(
                      ItemAmadronTablet.getItemProvidingDimension(player.getCurrentEquippedItem()));
            }
            EntityDrone drone =
                (EntityDrone)
                    PneumaticRegistry.getInstance()
                        .retrieveItemsAmazonStyle(
                            world,
                            pos.chunkPosX,
                            pos.chunkPosY,
                            pos.chunkPosZ,
                            stacks.toArray(new ItemStack[stacks.size()]));
            drone.setHandlingOffer(offer, shoppingAmounts[i], player.getCurrentEquippedItem());
          } else {
            FluidStack queryingFluid = ((FluidStack) offer.getInput()).copy();
            queryingFluid.amount *= shoppingAmounts[i];

            ChunkPosition pos =
                ItemAmadronTablet.getLiquidProvidingLocation(player.getCurrentEquippedItem());
            if (pos != null) {
              World world =
                  ItemAmadronTablet.getWorldForDimension(
                      ItemAmadronTablet.getLiquidProvidingDimension(
                          player.getCurrentEquippedItem()));
              EntityDrone drone =
                  (EntityDrone)
                      PneumaticRegistry.getInstance()
                          .retrieveFluidAmazonStyle(
                              world, pos.chunkPosX, pos.chunkPosY, pos.chunkPosZ, queryingFluid);
              drone.setHandlingOffer(offer, shoppingAmounts[i], player.getCurrentEquippedItem());
            }
          }
        }
      }
      Arrays.fill(shoppingAmounts, 0);
      Arrays.fill(shoppingItems, -1);
    } else if (guiID == 2) {
      player.openGui(
          PneumaticCraft.instance,
          CommonProxy.EnumGuiId.AMADRON_ADD_TRADE.ordinal(),
          player.worldObj,
          0,
          0,
          0);
    }
  }