ge plugin: rewrite alt-click searching
This simplfies the code and improves performance by only needing to loop over the menu entries instead of the inventory and checking all types of inventories. - Fix exceptions being thrown when losing focus on the client when searching. There is still an exception when spam clicking, but that was always there to begin with. - Fix searching while using the GE window and while using a shop window.
This commit is contained in:
@@ -29,32 +29,22 @@ import java.awt.event.MouseEvent;
|
|||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.swing.SwingUtilities;
|
import javax.swing.SwingUtilities;
|
||||||
import net.runelite.api.Client;
|
import net.runelite.api.Client;
|
||||||
import net.runelite.api.ItemComposition;
|
import net.runelite.api.MenuEntry;
|
||||||
import net.runelite.api.Point;
|
|
||||||
import net.runelite.api.queries.BankItemQuery;
|
|
||||||
import net.runelite.api.widgets.Widget;
|
|
||||||
import net.runelite.api.widgets.WidgetInfo;
|
|
||||||
import net.runelite.api.widgets.WidgetItem;
|
|
||||||
import net.runelite.client.game.ItemManager;
|
|
||||||
import net.runelite.client.input.KeyListener;
|
import net.runelite.client.input.KeyListener;
|
||||||
import net.runelite.client.input.MouseListener;
|
import net.runelite.client.input.MouseListener;
|
||||||
import net.runelite.client.util.QueryRunner;
|
import static net.runelite.client.plugins.grandexchange.GrandExchangePlugin.SEARCH_GRAND_EXCHANGE;
|
||||||
|
import net.runelite.client.util.Text;
|
||||||
|
|
||||||
public class GrandExchangeInputListener extends MouseListener implements KeyListener
|
public class GrandExchangeInputListener extends MouseListener implements KeyListener
|
||||||
{
|
{
|
||||||
private final Client client;
|
private final Client client;
|
||||||
private final GrandExchangePlugin plugin;
|
private final GrandExchangePlugin plugin;
|
||||||
private final ItemManager itemManager;
|
|
||||||
private final QueryRunner queryRunner;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
GrandExchangeInputListener(Client client, GrandExchangePlugin plugin, ItemManager itemManager,
|
GrandExchangeInputListener(Client client, GrandExchangePlugin plugin)
|
||||||
QueryRunner queryRunner)
|
|
||||||
{
|
{
|
||||||
this.client = client;
|
this.client = client;
|
||||||
this.plugin = plugin;
|
this.plugin = plugin;
|
||||||
this.itemManager = itemManager;
|
|
||||||
this.queryRunner = queryRunner;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -63,36 +53,14 @@ public class GrandExchangeInputListener extends MouseListener implements KeyList
|
|||||||
// Check if left click + alt
|
// Check if left click + alt
|
||||||
if (e.getButton() == MouseEvent.BUTTON1 && e.isAltDown())
|
if (e.getButton() == MouseEvent.BUTTON1 && e.isAltDown())
|
||||||
{
|
{
|
||||||
Widget inventoryWidget = client.getWidget(WidgetInfo.INVENTORY);
|
final MenuEntry[] menuEntries = client.getMenuEntries();
|
||||||
if (inventoryWidget != null && !inventoryWidget.isHidden())
|
for (final MenuEntry menuEntry : menuEntries)
|
||||||
{
|
{
|
||||||
if (findAndSearch(inventoryWidget.getWidgetItems().toArray(new WidgetItem[0])))
|
if (menuEntry.getOption().equals(SEARCH_GRAND_EXCHANGE))
|
||||||
{
|
{
|
||||||
|
search(Text.removeTags(menuEntry.getTarget()));
|
||||||
e.consume();
|
e.consume();
|
||||||
return super.mouseClicked(e);
|
break;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check the inventory when the bank is open aswell
|
|
||||||
Widget bankInventoryWidget = client.getWidget(WidgetInfo.BANK_INVENTORY_ITEMS_CONTAINER);
|
|
||||||
if (bankInventoryWidget != null && !bankInventoryWidget.isHidden())
|
|
||||||
{
|
|
||||||
if (findAndSearch(bankInventoryWidget.getDynamicChildren()))
|
|
||||||
{
|
|
||||||
e.consume();
|
|
||||||
return super.mouseClicked(e);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
Widget bankWidget = client.getWidget(WidgetInfo.BANK_ITEM_CONTAINER);
|
|
||||||
if (bankWidget != null && !bankWidget.isHidden())
|
|
||||||
{
|
|
||||||
// Use bank item query for only checking the active tab
|
|
||||||
WidgetItem[] items = queryRunner.runQuery(new BankItemQuery());
|
|
||||||
if (findAndSearch(items))
|
|
||||||
{
|
|
||||||
e.consume();
|
|
||||||
return super.mouseClicked(e);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -100,42 +68,7 @@ public class GrandExchangeInputListener extends MouseListener implements KeyList
|
|||||||
return super.mouseClicked(e);
|
return super.mouseClicked(e);
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean findAndSearch(Widget[] widgets)
|
private void search(final String itemName)
|
||||||
{
|
|
||||||
Point mousePosition = client.getMouseCanvasPosition();
|
|
||||||
for (Widget widget : widgets)
|
|
||||||
{
|
|
||||||
if (widget.getBounds().contains(mousePosition.getX(), mousePosition.getY()))
|
|
||||||
{
|
|
||||||
ItemComposition itemComposition = itemManager.getItemComposition(widget.getItemId());
|
|
||||||
search(itemComposition);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Finds the item clicked based on the mouse location
|
|
||||||
* @param items
|
|
||||||
* @return true if an item is found, false otherwise
|
|
||||||
*/
|
|
||||||
private boolean findAndSearch(WidgetItem[] items)
|
|
||||||
{
|
|
||||||
Point mousePosition = client.getMouseCanvasPosition();
|
|
||||||
for (WidgetItem item : items)
|
|
||||||
{
|
|
||||||
if (item.getCanvasBounds().contains(mousePosition.getX(), mousePosition.getY()))
|
|
||||||
{
|
|
||||||
ItemComposition itemComposition = itemManager.getItemComposition(item.getId());
|
|
||||||
search(itemComposition);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
private void search(ItemComposition itemComposition)
|
|
||||||
{
|
{
|
||||||
SwingUtilities.invokeLater(() ->
|
SwingUtilities.invokeLater(() ->
|
||||||
{
|
{
|
||||||
@@ -146,7 +79,7 @@ public class GrandExchangeInputListener extends MouseListener implements KeyList
|
|||||||
plugin.getButton().getOnSelect().run();
|
plugin.getButton().getOnSelect().run();
|
||||||
}
|
}
|
||||||
|
|
||||||
plugin.getPanel().getSearchPanel().priceLookup(itemComposition.getName());
|
plugin.getPanel().getSearchPanel().priceLookup(itemName);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -44,6 +44,7 @@ import net.runelite.api.Client;
|
|||||||
import net.runelite.api.GameState;
|
import net.runelite.api.GameState;
|
||||||
import net.runelite.api.GrandExchangeOffer;
|
import net.runelite.api.GrandExchangeOffer;
|
||||||
import net.runelite.api.ItemComposition;
|
import net.runelite.api.ItemComposition;
|
||||||
|
import net.runelite.api.MenuAction;
|
||||||
import net.runelite.api.MenuEntry;
|
import net.runelite.api.MenuEntry;
|
||||||
import net.runelite.api.events.ChatMessage;
|
import net.runelite.api.events.ChatMessage;
|
||||||
import net.runelite.api.events.ConfigChanged;
|
import net.runelite.api.events.ConfigChanged;
|
||||||
@@ -82,6 +83,8 @@ public class GrandExchangePlugin extends Plugin
|
|||||||
private static final int OFFER_DEFAULT_ITEM_ID = 6512;
|
private static final int OFFER_DEFAULT_ITEM_ID = 6512;
|
||||||
private static final GrandExchangeClient CLIENT = new GrandExchangeClient();
|
private static final GrandExchangeClient CLIENT = new GrandExchangeClient();
|
||||||
|
|
||||||
|
static final String SEARCH_GRAND_EXCHANGE = "Search Grand Exchange";
|
||||||
|
|
||||||
@Getter(AccessLevel.PACKAGE)
|
@Getter(AccessLevel.PACKAGE)
|
||||||
private NavigationButton button;
|
private NavigationButton button;
|
||||||
|
|
||||||
@@ -247,7 +250,10 @@ public class GrandExchangePlugin extends Plugin
|
|||||||
}
|
}
|
||||||
case WidgetID.INVENTORY_GROUP_ID:
|
case WidgetID.INVENTORY_GROUP_ID:
|
||||||
case WidgetID.BANK_INVENTORY_GROUP_ID:
|
case WidgetID.BANK_INVENTORY_GROUP_ID:
|
||||||
menuEntry.setOption("Search Grand Exchange");
|
case WidgetID.GRAND_EXCHANGE_INVENTORY_GROUP_ID:
|
||||||
|
case WidgetID.SHOP_INVENTORY_GROUP_ID:
|
||||||
|
menuEntry.setOption(SEARCH_GRAND_EXCHANGE);
|
||||||
|
menuEntry.setType(MenuAction.RUNELITE.getId());
|
||||||
client.setMenuEntries(entries);
|
client.setMenuEntries(entries);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -273,8 +273,11 @@ class GrandExchangeSearchPanel extends JPanel
|
|||||||
constraints.gridy++;
|
constraints.gridy++;
|
||||||
}
|
}
|
||||||
|
|
||||||
// remove focus from the search bar
|
// if exactMatch was set, then it came from the applet, so don't lose focus
|
||||||
searchItemsPanel.requestFocusInWindow();
|
if (!exactMatch)
|
||||||
|
{
|
||||||
|
searchItemsPanel.requestFocusInWindow();
|
||||||
|
}
|
||||||
searchBox.setEditable(true);
|
searchBox.setEditable(true);
|
||||||
|
|
||||||
// Remove searching label after search is complete
|
// Remove searching label after search is complete
|
||||||
|
|||||||
Reference in New Issue
Block a user