/**
  * Gets the count of all the inventory items matching with any of the provided ids.
  *
  * @param includeStacks <tt>true</tt> to count the stack sizes of each item; otherwise
  *     <tt>false</tt>
  * @param itemIds the item ids to include
  * @return the count
  */
 public static int getCount(boolean includeStacks, int... itemIds) {
   int count = 0;
   Item[] items = getItems(itemIds);
   for (Item item : items) {
     if (item == null) continue;
     int itemId = item.getId();
     if (itemId != -1) {
       count += includeStacks ? item.getStackSize() : 1;
     }
   }
   return count;
 }
 /**
  * Gets the selected inventory item's index.
  *
  * @return the index of the current selected inventory item; otherwise -1 if none is selected
  */
 public static int getSelectedItemIndex() {
   Item[] items = getItems();
   for (Item item : items) {
     if (item == null) {
       continue;
     }
     Component comp = item.getComponent();
     if (comp.getBorderThickness() == 2) {
       return comp.getIndex();
     }
   }
   return -1;
 }
 /**
  * Gets the count of all the inventory items excluding any of the provided ids.
  *
  * @param includeStacks <tt>true</tt> to count the stack sizes of each item; otherwise
  *     <tt>false</tt>
  * @param ids the ids to exclude
  * @return the count
  */
 public static int getCountExcept(boolean includeStacks, int... ids) {
   int count = 0;
   Item[] items = getItems();
   outer:
   for (Item item : items) {
     if (item == null) continue;
     int itemId = item.getId();
     for (int id : ids) {
       if (itemId == id) continue outer;
     }
     count += includeStacks ? item.getStackSize() : 1;
   }
   return count;
 }
 /**
  * Uses an item on a game object.
  *
  * @param item the item to use
  * @param target the game object to be used on by the item
  * @return <tt>true</tt> if the "Use" action had been used on both the inventory item and the game
  *     object; otherwise <tt>false</tt>
  */
 public static boolean useItem(Item item, GameObject target) {
   if (item != null && target != null) {
     for (int i = 0, r = Random.nextInt(5, 8); i < r; i++) {
       if (!isItemSelected()) {
         if (item.interact("Use")) {
           for (int j = 0; j < 10 && !isItemSelected(); j++) {
             Task.sleep(100, 200);
           }
         } else {
           return false;
         }
       }
       // just make sure in case something bad happened
       if (isItemSelected()) {
         final String itemName = item.getName();
         final ObjectDefinition targetDef = target.getDef();
         final Model targetModel = target.getModel();
         if (targetDef != null && itemName != null && targetModel != null) {
           final String targetName = targetDef.getName();
           Mouse.move(targetModel.getNextPoint());
           final String action =
               "Use "
                   + itemName.replace("<col=ff9040>", "")
                   + " -> "
                   + targetName.replace("<col=ff9040>", "");
           for (int j = 0, s = Random.nextInt(5, 8); j < s; j++) {
             if (Menu.contains(action) && Menu.click(action)) {
               return true;
             } else {
               Mouse.move(targetModel.getNextPoint());
             }
           }
         }
         // kay, since that failed, let's try just use
         if (target.interact("Use")) {
           return true;
         }
       }
     }
   }
   return false;
 }
 /**
  * Drags an item to the specified inventory slot, which must be in the range of 0 and 27.
  *
  * @param item the inventory item
  * @param invSlot the inventory slot
  * @return <tt>true</tt> if dragged; otherwise <tt>false</tt>
  */
 public static boolean drag(Item item, int invSlot) {
   if (item != null) {
     if (invSlot >= 0 && invSlot <= 27) {
       Component slot = getComponent().getComponents()[invSlot];
       if (slot != null) {
         Rectangle slotRectangle = slot.getBoundingRect();
         Rectangle itemRectangle = item.getComponent().getContentRect();
         if (slotRectangle.contains(itemRectangle)) {
           return true;
         }
         Mouse.move((int) itemRectangle.getCenterX(), (int) itemRectangle.getCenterY(), 5, 5);
         Mouse.drag((int) slotRectangle.getCenterX(), (int) slotRectangle.getCenterY());
         return true;
       }
     }
   }
   return false;
 }
 /**
  * Uses an inventory item on either another inventory item or a game object.
  *
  * @param item the inventory item to use
  * @param target the inventory item or the game object to be used on by the inventory item
  * @return <tt>true</tt> if the "Use" action had been used on both the inventory item and the game
  *     object/other inventory item; otherwise <tt>false</tt>
  */
 private static boolean useItem(Item item, Object target) {
   if (isItemSelected()) {
     Item selectedItem = getSelectedItem();
     int selectedItemId = selectedItem.getId();
     if (item.getId() != selectedItemId) {
       if (!selectedItem.interact("Cancel")) {
         return false;
       }
     } else if (target instanceof Item) {
       Item t = (Item) target;
       if (selectedItemId != t.getId() && selectedItemId != item.getId()) {
         if (!selectedItem.interact("Cancel")) {
           return false;
         }
       }
     }
   }
   for (int i = 0, r = Random.nextInt(5, 8); i < r; i++) {
     if (isItemSelected()) {
       boolean success = false;
       for (int j = 0, k = Random.nextInt(5, 8); j < k; j++) {
         try {
           Item t = (Item) target;
           if (t.interact("Use")) {
             success = true;
             break;
           }
           Task.sleep(150, 300);
         } catch (final ClassCastException e) {
           return false;
         }
       }
       return success;
     }
     item.interact("Use");
     Task.sleep(150, 300);
   }
   return false;
 }
 /**
  * Clicks on the selected inventory item.
  *
  * @param leftClick <tt>true</tt> to left-click otherwise; <tt>false</tt> to right-click
  * @return <tt>true</tt> if the inventory item was clicked on; otherwise <tt>false</tt>
  */
 public static boolean clickSelectedItem(boolean leftClick) {
   Item item = getSelectedItem();
   return item != null && item.click(leftClick);
 }