Jump to content

Using an item on an other item


barackogama

Recommended Posts

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.

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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... */
}

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

  • Recently Browsing   0 members

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