Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
Expand Down Expand Up @@ -84,7 +85,7 @@ private static boolean hasDeathContinue() {
*
* @return true if the "Continue" option is visible in either sprite-based dialogue, false otherwise.
*/

private static boolean hasSpriteContinue() {
return Rs2Widget.isWidgetVisible(InterfaceID.DIALOG_SPRITE, 0) || Rs2Widget.isWidgetVisible(InterfaceID.DIALOG_SPRITE, 3) || Rs2Widget.isWidgetVisible(InterfaceID.DIALOG_DOUBLE_SPRITE, 4);
}
Expand Down Expand Up @@ -138,10 +139,10 @@ private static boolean hasSpellFilterContinue() {
public static boolean hasSelectAnOption() {
boolean isWidgetVisible = Rs2Widget.isWidgetVisible(InterfaceID.DIALOG_OPTION, 1);
if (!isWidgetVisible) return false;

Widget widget = Rs2Widget.getWidget(InterfaceID.DIALOG_OPTION, 1);
if (widget == null) return false;

return widget.getDynamicChildren() != null;
}

Expand Down Expand Up @@ -337,6 +338,36 @@ public static boolean keyPressForDialogueOption(int index) {
return true;
}

/**
* Attempts to click on a dialogue option based on the specified text(s). The method
* performs a partial matching depending on the provided parameter and will return
* whether the operation was successful.
*
* @param texts varargs parameter representing the*/
public static boolean clickOption(String... texts){
return clickOption(false, texts);
}

/**
* Attempts to click on a dialogue option based on the specified text(s). The method can
* perform an exact or partial matching depending on the provided parameter and will return
* whether the operation was successful.
*
* @param exact specifies whether the matching should be exact (true) or partial (false).
* @param texts varargs parameter representing the*/
public static boolean clickOption(boolean exact, String... texts){
if (!hasSelectAnOption()) return false;
List<Widget> options = getDialogueOptions();
if(options.isEmpty()) return false;

Widget dialogueOption = options.stream()
.filter(dialop -> exact ? Arrays.stream(texts).anyMatch(t -> dialop.getText().equalsIgnoreCase(t)) : Arrays.stream(texts).anyMatch(t -> dialop.getText().toLowerCase().contains(t.toLowerCase())))
.findFirst()
.orElse(null);
if (dialogueOption == null) return false;
return Rs2Widget.clickWidget(dialogueOption);
}

/**
* Attempts to click on a dialogue option widget with the specified text.
*
Expand Down Expand Up @@ -449,7 +480,7 @@ public static boolean sleepUntilHasQuestion(String text, boolean exact) {
public static boolean sleepUntilHasQuestion(String text) {
return sleepUntilHasQuestion(text, false);
}

/**
* Checks if the combination dialogue widget is currently visible.
*
Expand Down Expand Up @@ -597,11 +628,11 @@ public static boolean clickCombinationOption(String text, boolean exact) {
if (!hasCombinationDialogue()) return false;

Widget option = getCombinationOption(text, exact);

if (option == null) return false;

return Rs2Widget.clickWidget(option);

}

/**
Expand All @@ -627,7 +658,7 @@ public static boolean sleepUntilHasCombinationDialogue() {
* Pauses the current thread until a specific combination dialogue option becomes available.
*
* <p>This method continuously checks for a combination dialogue option that matches the specified
* text. If an exact match is required, it will search for an option that exactly matches the text;
* text. If an exact match is required, it will search for an option that exactly matches the text;
* otherwise, it will look for an option containing the text.
*
* @param text the text to search for within the combination dialogue options.
Expand All @@ -649,7 +680,7 @@ public static boolean sleepUntilHasCombinationOption(String text, boolean exact)
public static boolean sleepUntilHasCombinationOption(String text) {
return sleepUntilHasCombinationOption(text, false);
}

/**
* Determines whether the game is currently in a cutscene.
* <p>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,7 @@
import org.slf4j.event.Level;

import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Objects;
import java.util.Set;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Stream;

Expand Down Expand Up @@ -316,6 +311,7 @@ public static void invokeMenu(Rs2ItemModel rs2Item, String action) {
int param0 = -1;
int param1 = -1;
int identifier;
String target = rs2Item.getName();
MenuAction menuAction = MenuAction.CC_OP;
if (action.equalsIgnoreCase("remove")) {
identifier = 1;
Expand All @@ -328,9 +324,20 @@ public static void invokeMenu(Rs2ItemModel rs2Item, String action) {
break;
}
}
// We could not find the action in the equipment actions, so we try to find it in the sub-menu actions
if (identifier == -1) {
Microbot.log("Item=" + rs2Item.getName() + " does not have action=" + action + ". Actions=" + Arrays.toString(actions.stream().filter(Objects::nonNull).toArray()), Level.ERROR);
return;
Map.Entry<String, Integer> subMenuEntry = rs2Item.getIndexOfSubAction(action);
if (subMenuEntry == null) {
Microbot.log("Item=" + rs2Item.getName() + " does not have a subaction=" + action, Level.ERROR);
return;
}
int mainMenuIndex = actions.indexOf(subMenuEntry.getKey());
if (mainMenuIndex < 0) {
Microbot.log("Cannot find action=%s, in main actions=%s, mainMenuIndex=%s", subMenuEntry.getKey(), String.join(", ", actions), mainMenuIndex, Level.ERROR);
return;
}
target = "";
identifier = NewMenuEntry.findIdentifier(subMenuEntry.getValue() + 1, mainMenuIndex + 2);
}
}
Rectangle rectangle = new Rectangle(1, 1, Microbot.getClient().getCanvasWidth(), Microbot.getClient().getCanvasHeight());
Expand Down Expand Up @@ -368,6 +375,7 @@ public static void invokeMenu(Rs2ItemModel rs2Item, String action) {
param1 = 25362456;
rectangle = getSafeBounds(InterfaceID.WORNITEMS,24);
}

Microbot.doInvoke(new NewMenuEntry(param0, param1, menuAction.getId(), identifier, -1, rs2Item.getName()), rectangle);
//Rs2Reflection.invokeMenu(param0, param1, menuAction.getId(), identifier, rs2Item.id, action, target, -1, -1);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1206,20 +1206,6 @@ public static boolean interact(int id) {
public static boolean interact(int id, String action) {
return interact(get(id), action);
}
/**
* Interacts with an item with the specified ID in the inventory using the specified action.
*
* @param id The ID of the item to interact with.
* @param action The action to perform on the item.
*
* @return True if the interaction was successful, false otherwise.
*/
public static boolean interact(int id, String action, int identifier) {
final Rs2ItemModel rs2Item = get(id);
if (rs2Item == null) return false;
invokeMenu(rs2Item, action, identifier);
return true;
}

/**
* Interacts with an item with the specified name in the inventory using the first available action.
Expand Down Expand Up @@ -1888,17 +1874,16 @@ public static boolean hasRunePouch() {
*
* @param rs2Item The current item to interact with.
* @param action The action to be used on the item.
* @param providedIdentifier The identifier to use; if -1, compute using the old logic.
*/
private static void invokeMenu(Rs2ItemModel rs2Item, String action, int providedIdentifier) {
private static void invokeMenu(Rs2ItemModel rs2Item, String action) {
if (rs2Item == null) return;

Rs2Tab.switchToInventoryTab();
Microbot.status = action + " " + rs2Item.getName();

int param0;
int param1;
int identifier = -1;
String target = rs2Item.getName();
MenuAction menuAction = MenuAction.CC_OP;
Widget[] inventoryWidgets;
param0 = rs2Item.getSlot();
Expand Down Expand Up @@ -1939,7 +1924,21 @@ private static void invokeMenu(Rs2ItemModel rs2Item, String action, int provided
itemWidget.getActions() :
rs2Item.getInventoryActions();

identifier = providedIdentifier == -1 ? indexOfIgnoreCase(stripColTags(actions), action) + 1 : providedIdentifier;
int simpleIndex = indexOfIgnoreCase(stripColTags(actions), action);
if (simpleIndex != -1) {
identifier = simpleIndex + 1;
} else {
// We could not find the action in the item widget's actions, so we try to find it in the sub-menu actions
Map.Entry<String, Integer> subActionMap = rs2Item.getIndexOfSubAction(action);
if (subActionMap == null) {
Microbot.log("Item=" + rs2Item.getName() + " does not have action=" + action, Level.ERROR);
return;
}
// The main menu index depends on the inventory interface from which this item is interacted with
int mainMenuIndex = java.util.Arrays.asList(actions).indexOf(subActionMap.getKey());
identifier = NewMenuEntry.findIdentifier(subActionMap.getValue() + 1, mainMenuIndex + 1);
target = "";
}
}


Expand All @@ -1951,7 +1950,7 @@ private static void invokeMenu(Rs2ItemModel rs2Item, String action, int provided
menuAction = MenuAction.WIDGET_TARGET_ON_WIDGET;
}

Microbot.doInvoke(new NewMenuEntry(action, param0, param1, menuAction.getId(), identifier, rs2Item.getId(), rs2Item.getName()), (itemBounds(rs2Item) == null) ? new Rectangle(1, 1) : itemBounds(rs2Item));
Microbot.doInvoke(new NewMenuEntry(action, param0, param1, menuAction.getId(), identifier, rs2Item.getId(), target), (itemBounds(rs2Item) == null) ? new Rectangle(1, 1) : itemBounds(rs2Item));

if (action.equalsIgnoreCase("destroy")) {
sleepUntil(() -> Rs2Widget.isWidgetVisible(584, 0));
Expand All @@ -1960,16 +1959,6 @@ private static void invokeMenu(Rs2ItemModel rs2Item, String action, int provided
}


/**
* Method executes menu actions
*
* @param rs2Item Current item to interact with
* @param action Action used on the item
*/
private static void invokeMenu(Rs2ItemModel rs2Item, String action) {
invokeMenu(rs2Item, action, -1);
}

public static Widget getInventory() {
final int BANK_PIN_INVENTORY_ITEM_CONTAINER = 17563648;
final int SHOP_INVENTORY_ITEM_CONTAINER = 19726336;
Expand Down
Loading