Jump to content

Selecting the Closest Battlestaff in crafting script


Recommended Posts

Posted

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.");
        }

 

Posted
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)

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

 
Posted (edited)

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

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