api: pass menu entry to menu clicked event and forward accessors

This commit is contained in:
Adam
2022-04-16 16:28:38 -04:00
parent 802f8c654a
commit fcb933eeef
4 changed files with 75 additions and 36 deletions

View File

@@ -24,8 +24,11 @@
*/
package net.runelite.api.events;
import lombok.Data;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import net.runelite.api.MenuAction;
import net.runelite.api.MenuEntry;
import net.runelite.api.widgets.Widget;
/**
* An event where a menu option has been clicked.
@@ -38,42 +41,77 @@ import net.runelite.api.MenuAction;
* By default, when there is no action performed when left-clicking,
* it seems that this event still triggers with the "Cancel" action.
*/
@Data
@RequiredArgsConstructor
public class MenuOptionClicked
{
/**
* Action parameter 0. Its value depends on the menuAction.
* The clicked menu entry
*/
private int param0;
/**
* Action parameter 1. Its value depends on the menuAction.
*/
private int param1;
/**
* The option text added to the menu.
*/
private String menuOption;
/**
* The target of the action.
*/
private String menuTarget;
/**
* The action performed.
*/
private MenuAction menuAction;
/**
* The ID of the object, actor, or item that the interaction targets.
*/
private int id;
/**
* The selected item index at the time of the option click.
*/
private int selectedItemIndex;
private final MenuEntry menuEntry;
/**
* Whether or not the event has been consumed by a subscriber.
*/
@Getter
private boolean consumed;
/**
* Action parameter 0. Its value depends on the menuAction.
*/
public int getParam0()
{
return menuEntry.getParam0();
}
/**
* Action parameter 1. Its value depends on the menuAction.
*/
public int getParam1()
{
return menuEntry.getParam1();
}
/**
* The option text added to the menu.
*/
public String getMenuOption()
{
return menuEntry.getOption();
}
/**
* The target of the action.
*/
public String getMenuTarget()
{
return menuEntry.getTarget();
}
/**
* The action performed.
*/
public MenuAction getMenuAction()
{
return menuEntry.getType();
}
/**
* The ID of the object, actor, or item that the interaction targets.
*/
public int getId()
{
return menuEntry.getIdentifier();
}
/**
* Get the widget this menu entry is on, if this is a menu entry
* with an associated widget. Such as eg, CC_OP.
* @return
*/
public Widget getWidget()
{
return menuEntry.getWidget();
}
/**
* Marks the event as having been consumed.
* <p>
@@ -89,12 +127,12 @@ public class MenuOptionClicked
@Deprecated
public int getActionParam()
{
return param0;
return menuEntry.getParam0();
}
@Deprecated
public int getWidgetId()
{
return param1;
return menuEntry.getParam1();
}
}

View File

@@ -286,7 +286,7 @@ public class CannonPlugin extends Plugin
// Check if cannonballs are being used on the cannon
if (event.getMenuAction() == MenuAction.ITEM_USE_ON_GAME_OBJECT)
{
final int idx = event.getSelectedItemIndex();
final int idx = client.getSelectedItemIndex();
final ItemContainer items = client.getItemContainer(InventoryID.INVENTORY);
if (items == null)
{

View File

@@ -703,7 +703,7 @@ public class GroundItemsPlugin extends Plugin
return;
}
final Item clickedItem = inventory.getItem(menuOptionClicked.getSelectedItemIndex());
final Item clickedItem = inventory.getItem(client.getSelectedItemIndex());
if (clickedItem == null)
{
return;

View File

@@ -63,6 +63,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import static org.mockito.ArgumentMatchers.any;
import org.mockito.Mock;
import static org.mockito.Mockito.lenient;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
@@ -164,10 +165,10 @@ public class ClueScrollPluginTest
plugin.onGameTick(new GameTick());
// Simulate clicking on the STASH
MenuOptionClicked menuOptionClicked = new MenuOptionClicked();
menuOptionClicked.setMenuOption("Search");
menuOptionClicked.setMenuTarget("<col=ffff>STASH unit (easy)");
menuOptionClicked.setId(NullObjectID.NULL_28983);
MenuOptionClicked menuOptionClicked = mock(MenuOptionClicked.class);
when(menuOptionClicked.getMenuOption()).thenReturn("Search");
lenient().when(menuOptionClicked.getMenuTarget()).thenReturn("<col=ffff>STASH unit (easy)");
when(menuOptionClicked.getId()).thenReturn(NullObjectID.NULL_28983);
plugin.onMenuOptionClicked(menuOptionClicked);
// Check that the STASH is stored after withdrawing