/** * 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 all the inventory items (including empty ones). * * @return an array instance of <code>Item</code> of the current inventory items */ public static Item[] getAllItems() { Item[] items = new Item[28]; Component invIface = getComponent(); if (invIface != null) { Component[] comps = invIface.getComponents(); if (comps.length > 27) { for (int i = 0; i < 28; ++i) { items[i] = new Item(comps[i]); } } } return items; }
/** * Gets the inventory component. * * @return the inventory component */ public static Component getComponent() { for (int widget : ALT_WIDGETS) { Component inventory = Widgets.getComponent(widget, 0); if (inventory != null && inventory.getAbsLocation().x > 50) { return inventory; } } // Tab has to be open for us to get its contents openTab(); return Widgets.getComponent(WIDGET, 0); }
/** * Gets all the valid inventory items. * * @return an array instance of <code>Item</code> of the current valid inventory items */ public static Item[] getItems() { Component invIface = getComponent(); if (invIface != null) { Component[] comps = invIface.getComponents(); if (comps.length > 27) { List<Item> items = new LinkedList<Item>(); for (int i = 0; i < 28; ++i) { if (comps[i].getItemId() != -1) { items.add(new Item(comps[i])); } } return items.toArray(new Item[items.size()]); } } return new Item[0]; }
/** * 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; }
/** * Gets the inventory item at the specified index. * * @param index the index of the inventory item * @return the <code>Item</code>; otherwise <code>null</code> if invalid */ public static Item getItemAt(int index) { Component comp = getComponent().getComponent(index); return 0 <= index && index < 28 && comp != null && comp.getItemId() != -1 ? new Item(comp) : null; }