Jump to content

Custom inventory interact method


Nezz

Recommended Posts

	/*
	 * public boolean interactInv will return true/false whether it was capable of interacting with an inventory item given the specified interaction.
	 */
	public boolean interactInv(String itemName, String interact) throws InterruptedException{
                //create an inventory variable for less typing.
                Inventory in = client.getInventory();
		//check if inventory contains item
		if(in.contains(itemName)){
			//create a mouse destination based off of a rectangle created around the slot of the item
			MouseDestination md = new RectangleDestination(in.getDestinationForSlot(in.getSlotForName(itemName)));
			//move the mouse over the slot
			if(!client.moveMouseTo(md, false, false, false))
                                return false;
			//create a list of the options available in the menu, cycle through them to find specified interaction
			List<Option> menu_option = client.getMenu();
			int use = 999;
			for(Option opt : menu_option){
				if(opt != null){
					if(opt.action.compareToIgnoreCase(interact) == 0){
						use = menu_option.indexOf(opt);
						break;
					}
				}
			}
			//if the option was the first in the list, left click it and return true.
			if(use == 0){
				return client.moveMouseTo(md, false, true, false);
			}
			//if use != 999, it was found in the options list.
			if(use != 999){
				//move mouse over the slot, right click it. (could be replaced with client.moveMouse(md,true))
				if(!client.moveMouseTo(md, false, true, true))
                                        return false;
				//create a new mouse destination based a new rectangle created over interact option of the menu
				md = new RectangleDestination(new Rectangle(client.getMenuX(), client.getMenuY() + 19 + use * 15, client.getMenuWidth(), 14));
				//click the mouse
				return client.moveMouseTo(md, false, true, false);
			}
			else{
				//the interact option wasn't found in the list, return false
				sleep(25);
				return false;
			}
		}
		else
                        return false;
	}

This can be adapted for NPC's, RS2Object's, items in a bank, etc.

Any improvements/suggestions are appreciated. biggrin.png

Edited by Nezz
Link to comment
Share on other sites

A couple things:

  1. Remove the random "sleep(50)" you have when right clicking an item as the menu opens on mouse press and not release.
  2. You can create a RectangleDestination to move your mouse to in one line instead of creating two different variables like you're doing, just pass a RectangleDestination the bounds of the slot you need (aka a Rectangle).
  3. moveMouse returns a boolean for a reason. Return the value returned by the method to see if it really did complete successfully.
  4. Arrays.asList is a great method to use for things like this. Just use the get(0) method to see if the action you want is the first one instead of iterating through them.
  5. Instead of an enhanced for loop just increment until your value is equal to the array size, then break because the action would not have been found.

I wrote this a few days ago for a friend. Haven't tested it out but it should be beneficial if you don't understand my points :)

public boolean interactInventoryItem(String itemName, String interaction) throws InterruptedException {
        Item[] items = client.getInventory().getItems();
        if (items == null)
                return false;
        RectangleDestination itemDestination = new RectangleDestination(new Rectangle(client.getInventory().getDestinationForSlot(client.getInventory().getSlotForName(itemName)).getBounds()));
        for (Item currentItem : items) {
                if (currentItem.getName().equalsIgnoreCase(itemName)) {
                        String[] itemActions = currentItem.getDefinition().getActions();
                        if (itemActions[0].equalsIgnoreCase(interaction))
                                return client.moveMouseTo(itemDestination, false, true, false);
                        else {
                                for (int i = 1; i < itemActions.length; i++) {
                                        if (itemActions[i].equalsIgnoreCase(interaction)) {
                                                client.moveMouseTo(itemDestination, false, true, true);
                                                if (client.isMenuOpen()) {
                                                        List<Option> options = client.getMenu();
                                                        for (int j = 0; j < options.size(); j++) {
                                                                if (options.get(i).action.equalsIgnoreCase(interaction)) {
                                                                        return client.moveMouseTo(new RectangleDestination(client.getMenuX(), client.getMenuY()+21+i*14, client.getMenuWidth(), 10), false, true, false);
                                                                }
                                                        }
                                                }
                                        }
                                }
                        }
                }
        }
        return false;
}
  • Like 2
Link to comment
Share on other sites

Guest
This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...