Jump to content

Using an item on an other item


Recommended Posts

Posted

Probably a very simple solution but I can't seem to find it while searching through osbot API. I simply want to use an item on another (for example using a needle on leather or knife on logs).

I found the following method but how do I make it click on a certain item after?

getInventory().getItem("Needle").interact("Use")

Thankful for answers.

Posted
2 hours ago, barackogama said:

Probably a very simple solution but I can't seem to find it while searching through osbot API. I simply want to use an item on another (for example using a needle on leather or knife on logs).

I found the following method but how do I make it click on a certain item after?


getInventory().getItem("Needle").interact("Use")

Thankful for answers.

 

if (!"Needle".equals(getInventory().getSelectedItemName())) {
    if (getInventory().isItemSelected()) {
        getInventory().deselectItem();
    } else {
        getInventory().interact("Use", "Needle");
    }
} else if (getInventory().getItem("Leather").interact()) {
    new ConditionalSleep(2000) {
        @Override
        public boolean condition() throws InterruptedException {
            return getWidgets().getWidgetContainingText("Choose a quantity, then click an item to begin") != null;
        }
    }.sleep();
}


 

  • Like 1
Posted

I wrote this ad-hoc and untested function:

public boolean useItemOnItem(String itemA, String itemB)
	throws IllegalArgumentException, RuntimeException {
	
	/* Variables */
	boolean successful = false;
	String selectedItemName;
	
	/* Validation */
	if (itemA == null || itemA.isEmpty()) {
		throw new IllegalArgumentException("Parameter \"itemA\" is invalid!");
	} else if (itemB == null || itemB.isEmpty()) {
		throw new IllegalArgumentException("Parameter \"itemB\" is invalid!");
	} else if (itemA == itemB) {
		throw new IllegalArgumentException("Parameter \"itemA\" and \"itemB\" must be different!");
	} else if (!inventory.contains(itemA)) {
		throw new RuntimeException("Inventory doesn't contain: " + itemA);
	} else if (!inventory.contains(itemb)) {
		throw new RuntimeException("Inventory doesn't contain: " + itemB);
	}
	
	/* Process */
	selectedItemName = inventory.getSelectedItemName();
	if (selectedItemName != null) {
		if (selectedItemName == itemA) {
			/* Now click on item B */
			successful = inventory.getItem(itemB).interact();
		} else if (selectedItemName == itemB) {
			/* Now click on item A */
			successful = inventory.getItem(itemA).interact();
		} else {
			/* Deselect and then re-try (safe recursion) */
			if (inventory.deselectItem()) {
				useItemOnItem(itemA, itemB);
			} else {
				throw new RuntimeException("Failed to deselect: " + selectedItemName);
			}
		}
	} else {
		/* Select item and re-try (safe recursion) */
		if (inventory.interact("Use", itemA)) {
			successful = useItemOnItem(itemA, itemB);
		} else {
			throw new RuntimeException("Failed to select: " + itemA);
		}
	}
	
	return successful;
}

Then you could do:

try {
    if (useItemOnItem("Needle", "Thread")) {
        /* Conditional sleep */
    }
} catch (IllegalArgumentException e) {
    /* I should have checked we have items before executing the function */
} catch (RuntimeException e) {
    /* Something bad happened... */
}

 

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

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