Jump to content

Selecting the Closest Battlestaff in crafting script


timepudding

Recommended Posts

I’m working on a script that crafts earth battlestaffs. The script currently selects an orb and then a battlestaff from my inventory. However, I’m facing an issue where the script always selects the first battlestaff in the inventory instead of the one closest to the orb.

the current script interacts with first one

image.png.fc12b7c99a856fc6232cfb196e534aab.png

I want it to interact with the last one

image.png.8464e8709e50b30d544acf82404bad04.png

 

Item battleStaff = inventory.getItem(ITEM_BATTLESTAFF);
Item earthOrb = inventory.getItem(ITEM_EARTHORB);

if (battleStaff.interact(ACTION_SELECTITEM)) {
            sleep(random(400, 600));
            if (earthOrb.interact(ACTION_SELECTITEM)) {
                sleep(random(700, 900));
                return true;
            } else {
                log("Error: Failed to interact with Earth Orb.");
            }
        } else {
            log("Error: Failed to interact with Battle Staff.");
        }

 

Link to comment
Share on other sites

private Item selectItemFromInventory(String itemName) {
    List<Integer> itemSlots = new ArrayList<>();
    for (int i = 1; i <= 28; i++) {
        if (getInventory().getItemInSlot(i) != null &&
                getInventory().getItemInSlot(i).getName().equals(itemName)) {
            itemSlots.add(i);
        }
    }
                                                
    // If the item exists in any slot, return the last one
    if (!itemSlots.isEmpty()) {
        int lastSlotIndex = itemSlots.get(itemSlots.size() - 1); // Get the last index
        return getInventory().getItemInSlot(lastSlotIndex); // Return the last item
    }
    
    // Return null if no item was found
    return null;
}

Just off the top of my head (untested)

Link to comment
Share on other sites

10 hours ago, Fanny said:
private Item selectItemFromInventory(String itemName) {
    List<Integer> itemSlots = new ArrayList<>();
    for (int i = 1; i <= 28; i++) {
        if (getInventory().getItemInSlot(i) != null &&
                getInventory().getItemInSlot(i).getName().equals(itemName)) {
            itemSlots.add(i);
        }
    }
                                                
    // If the item exists in any slot, return the last one
    if (!itemSlots.isEmpty()) {
        int lastSlotIndex = itemSlots.get(itemSlots.size() - 1); // Get the last index
        return getInventory().getItemInSlot(lastSlotIndex); // Return the last item
    }
    
    // Return null if no item was found
    return null;
}

Just off the top of my head (untested)

nah, it still selects first battle staff.

I tried using

Item battleStaff = inventory.getItemInSlot(13);

, but it still selects the first battlestaff.

 
Link to comment
Share on other sites

Use ```inventory.interact(13,"Use"))``` To select slot 13. followed by ```inventory.interact(14, USE)```.

Here is how I dod mine

private boolean combineComponents() throws InterruptedException {
        int[] slotPair = getInvSlotPair();
        Item item1 = inventory.getItemInSlot(slotPair[0]);
        Item item2 = inventory.getItemInSlot(slotPair[1]);

        boolean notNull = item1 != null && item2 != null;
        boolean notSameItem = notNull && item1.getId() != item2.getId();
        // Assert Item1 and 2 are the equivalent items determined at script start (ItemA and B).
        // Not foolproof, but a simple method
        boolean sumTo0 = notNull && (item1.getId() + item2.getId() - itemA.getId() - itemB.getId() == 0);
        boolean canUseSlotPair = notSameItem && sumTo0;


        if (canUseSlotPair && inventory.interact(slotPair[0], USE)) {
            ScriptPaint.setStatus("ItemA -> ItemB");
            sleep(randomGaussian(300, 100));
            return inventory.isItemSelected() && inventory.interact(slotPair[1], USE);
        } else {
            ScriptPaint.setStatus("ItemA -> ItemB w/ backup interaction");
            if (inventory.interact(USE, itemA.getId())) {
                sleep(randomGaussian(300, 100));
                return inventory.isItemSelected() && inventory.interact(USE, itemB.getId());
            }
        }
        return false;
    }

 

Edited by yfoo
Link to comment
Share on other sites

On 9/17/2024 at 6:53 PM, yfoo said:

Use ```inventory.interact(13,"Use"))``` To select slot 13. followed by ```inventory.interact(14, USE)```.

Here is how I dod mine

private boolean combineComponents() throws InterruptedException {
        int[] slotPair = getInvSlotPair();
        Item item1 = inventory.getItemInSlot(slotPair[0]);
        Item item2 = inventory.getItemInSlot(slotPair[1]);

        boolean notNull = item1 != null && item2 != null;
        boolean notSameItem = notNull && item1.getId() != item2.getId();
        // Assert Item1 and 2 are the equivalent items determined at script start (ItemA and B).
        // Not foolproof, but a simple method
        boolean sumTo0 = notNull && (item1.getId() + item2.getId() - itemA.getId() - itemB.getId() == 0);
        boolean canUseSlotPair = notSameItem && sumTo0;


        if (canUseSlotPair && inventory.interact(slotPair[0], USE)) {
            ScriptPaint.setStatus("ItemA -> ItemB");
            sleep(randomGaussian(300, 100));
            return inventory.isItemSelected() && inventory.interact(slotPair[1], USE);
        } else {
            ScriptPaint.setStatus("ItemA -> ItemB w/ backup interaction");
            if (inventory.interact(USE, itemA.getId())) {
                sleep(randomGaussian(300, 100));
                return inventory.isItemSelected() && inventory.interact(USE, itemB.getId());
            }
        }
        return false;
    }

 

Your solution worked. Thanks.

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...