Re-optimize wikiplugin, call contains on hashmultimap in menumanager
This commit is contained in:
@@ -266,21 +266,24 @@ public class MenuManager
|
|||||||
}
|
}
|
||||||
|
|
||||||
int widgetId = event.getParam1();
|
int widgetId = event.getParam1();
|
||||||
Collection<WidgetMenuOption> options = managedMenuOptions.get(widgetId);
|
if (managedMenuOptions.containsKey(widgetId))
|
||||||
|
|
||||||
for (WidgetMenuOption currentMenu : options)
|
|
||||||
{
|
{
|
||||||
if (!menuContainsCustomMenu(currentMenu))//Don't add if we have already added it to this widget
|
Collection<WidgetMenuOption> options = managedMenuOptions.get(widgetId);
|
||||||
|
|
||||||
|
for (WidgetMenuOption currentMenu : options)
|
||||||
{
|
{
|
||||||
client.insertMenuItem(
|
if (!menuContainsCustomMenu(currentMenu))//Don't add if we have already added it to this widget
|
||||||
currentMenu.getMenuOption(),
|
{
|
||||||
currentMenu.getMenuTarget(),
|
client.insertMenuItem(
|
||||||
MenuOpcode.RUNELITE.getId(),
|
currentMenu.getMenuOption(),
|
||||||
0,
|
currentMenu.getMenuTarget(),
|
||||||
0,
|
MenuOpcode.RUNELITE.getId(),
|
||||||
widgetId,
|
0,
|
||||||
false
|
0,
|
||||||
);
|
widgetId,
|
||||||
|
false
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,11 +25,8 @@
|
|||||||
package net.runelite.client.plugins.wiki;
|
package net.runelite.client.plugins.wiki;
|
||||||
|
|
||||||
import com.google.common.primitives.Ints;
|
import com.google.common.primitives.Ints;
|
||||||
import java.util.Arrays;
|
|
||||||
import java.util.stream.Stream;
|
|
||||||
import javax.inject.Inject;
|
import javax.inject.Inject;
|
||||||
import javax.inject.Provider;
|
import javax.inject.Provider;
|
||||||
import javax.inject.Singleton;
|
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import net.runelite.api.Client;
|
import net.runelite.api.Client;
|
||||||
import net.runelite.api.MenuEntry;
|
import net.runelite.api.MenuEntry;
|
||||||
@@ -53,18 +50,18 @@ import net.runelite.client.callback.ClientThread;
|
|||||||
import net.runelite.client.eventbus.Subscribe;
|
import net.runelite.client.eventbus.Subscribe;
|
||||||
import net.runelite.client.game.ItemManager;
|
import net.runelite.client.game.ItemManager;
|
||||||
import net.runelite.client.game.SpriteManager;
|
import net.runelite.client.game.SpriteManager;
|
||||||
import net.runelite.client.game.chatbox.ChatboxPanelManager;
|
|
||||||
import net.runelite.client.plugins.Plugin;
|
import net.runelite.client.plugins.Plugin;
|
||||||
import net.runelite.client.plugins.PluginDescriptor;
|
import net.runelite.client.plugins.PluginDescriptor;
|
||||||
import net.runelite.client.util.LinkBrowser;
|
import net.runelite.client.util.LinkBrowser;
|
||||||
import okhttp3.HttpUrl;
|
import okhttp3.HttpUrl;
|
||||||
|
import org.apache.commons.lang3.StringUtils;
|
||||||
|
import org.jetbrains.annotations.Nullable;
|
||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@PluginDescriptor(
|
@PluginDescriptor(
|
||||||
name = "Wiki",
|
name = "Wiki",
|
||||||
description = "Adds a Wiki button that takes you to the OSRS Wiki"
|
description = "Adds a Wiki button that takes you to the OSRS Wiki"
|
||||||
)
|
)
|
||||||
@Singleton
|
|
||||||
public class WikiPlugin extends Plugin
|
public class WikiPlugin extends Plugin
|
||||||
{
|
{
|
||||||
private static final int[] QUESTLIST_WIDGET_IDS = new int[]
|
private static final int[] QUESTLIST_WIDGET_IDS = new int[]
|
||||||
@@ -92,9 +89,6 @@ public class WikiPlugin extends Plugin
|
|||||||
@Inject
|
@Inject
|
||||||
private Client client;
|
private Client client;
|
||||||
|
|
||||||
@Inject
|
|
||||||
private ChatboxPanelManager chatboxPanelManager;
|
|
||||||
|
|
||||||
@Inject
|
@Inject
|
||||||
private ItemManager itemManager;
|
private ItemManager itemManager;
|
||||||
|
|
||||||
@@ -108,7 +102,6 @@ public class WikiPlugin extends Plugin
|
|||||||
@Override
|
@Override
|
||||||
public void startUp()
|
public void startUp()
|
||||||
{
|
{
|
||||||
|
|
||||||
spriteManager.addSpriteOverrides(WikiSprite.values());
|
spriteManager.addSpriteOverrides(WikiSprite.values());
|
||||||
clientThread.invokeLater(this::addWidgets);
|
clientThread.invokeLater(this::addWidgets);
|
||||||
}
|
}
|
||||||
@@ -199,123 +192,128 @@ public class WikiPlugin extends Plugin
|
|||||||
@Subscribe
|
@Subscribe
|
||||||
private void onMenuOptionClicked(MenuOptionClicked ev)
|
private void onMenuOptionClicked(MenuOptionClicked ev)
|
||||||
{
|
{
|
||||||
optarget:
|
if (ev.getMenuOpcode() == MenuOpcode.RUNELITE)
|
||||||
if (wikiSelected)
|
|
||||||
{
|
{
|
||||||
onDeselect();
|
checkQuestClicked(ev);
|
||||||
client.setSpellSelected(false);
|
}
|
||||||
ev.consume();
|
|
||||||
|
|
||||||
String type;
|
if (!wikiSelected)
|
||||||
int id;
|
{
|
||||||
String name;
|
return;
|
||||||
WorldPoint location;
|
}
|
||||||
|
|
||||||
switch (ev.getMenuOpcode())
|
onDeselect();
|
||||||
|
client.setSpellSelected(false);
|
||||||
|
ev.consume();
|
||||||
|
|
||||||
|
String type;
|
||||||
|
int id;
|
||||||
|
String name;
|
||||||
|
WorldPoint location;
|
||||||
|
|
||||||
|
switch (ev.getMenuOpcode())
|
||||||
|
{
|
||||||
|
case RUNELITE:
|
||||||
|
case CANCEL:
|
||||||
|
return;
|
||||||
|
case ITEM_USE_ON_WIDGET:
|
||||||
|
case SPELL_CAST_ON_GROUND_ITEM:
|
||||||
{
|
{
|
||||||
case RUNELITE:
|
type = "item";
|
||||||
// This is a quest widget op
|
id = itemManager.canonicalize(ev.getIdentifier());
|
||||||
break optarget;
|
name = itemManager.getItemDefinition(id).getName();
|
||||||
case CANCEL:
|
location = null;
|
||||||
return;
|
break;
|
||||||
case ITEM_USE_ON_WIDGET:
|
}
|
||||||
case SPELL_CAST_ON_GROUND_ITEM:
|
case SPELL_CAST_ON_NPC:
|
||||||
|
{
|
||||||
|
type = "npc";
|
||||||
|
NPC npc = client.getCachedNPCs()[ev.getIdentifier()];
|
||||||
|
NPCDefinition nc = npc.getTransformedDefinition();
|
||||||
|
id = nc.getId();
|
||||||
|
name = nc.getName();
|
||||||
|
location = npc.getWorldLocation();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case SPELL_CAST_ON_GAME_OBJECT:
|
||||||
|
{
|
||||||
|
type = "object";
|
||||||
|
ObjectDefinition lc = client.getObjectDefinition(ev.getIdentifier());
|
||||||
|
if (lc.getImpostorIds() != null)
|
||||||
|
{
|
||||||
|
lc = lc.getImpostor();
|
||||||
|
}
|
||||||
|
id = lc.getId();
|
||||||
|
name = lc.getName();
|
||||||
|
location = WorldPoint.fromScene(client, ev.getParam0(), ev.getParam1(), client.getPlane());
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
case SPELL_CAST_ON_WIDGET:
|
||||||
|
{
|
||||||
|
Widget w = getWidget(ev.getParam1(), ev.getParam0());
|
||||||
|
|
||||||
|
if (w.getType() == WidgetType.GRAPHIC && w.getItemId() != -1)
|
||||||
{
|
{
|
||||||
type = "item";
|
type = "item";
|
||||||
id = itemManager.canonicalize(ev.getIdentifier());
|
id = itemManager.canonicalize(w.getItemId());
|
||||||
name = itemManager.getItemDefinition(id).getName();
|
name = itemManager.getItemDefinition(id).getName();
|
||||||
location = null;
|
location = null;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case SPELL_CAST_ON_NPC:
|
|
||||||
{
|
|
||||||
type = "npc";
|
|
||||||
NPC npc = client.getCachedNPCs()[ev.getIdentifier()];
|
|
||||||
NPCDefinition nc = npc.getTransformedDefinition();
|
|
||||||
id = nc.getId();
|
|
||||||
name = nc.getName();
|
|
||||||
location = npc.getWorldLocation();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case SPELL_CAST_ON_GAME_OBJECT:
|
|
||||||
{
|
|
||||||
type = "object";
|
|
||||||
ObjectDefinition lc = client.getObjectDefinition(ev.getIdentifier());
|
|
||||||
if (lc.getImpostorIds() != null)
|
|
||||||
{
|
|
||||||
lc = lc.getImpostor();
|
|
||||||
}
|
|
||||||
id = lc.getId();
|
|
||||||
name = lc.getName();
|
|
||||||
location = WorldPoint.fromScene(client, ev.getParam0(), ev.getParam1(), client.getPlane());
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
case SPELL_CAST_ON_WIDGET:
|
|
||||||
Widget w = getWidget(ev.getParam1(), ev.getParam0());
|
|
||||||
|
|
||||||
if (w.getType() == WidgetType.GRAPHIC && w.getItemId() != -1)
|
|
||||||
{
|
|
||||||
type = "item";
|
|
||||||
id = itemManager.canonicalize(w.getItemId());
|
|
||||||
name = itemManager.getItemDefinition(id).getName();
|
|
||||||
location = null;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
// fallthrough
|
|
||||||
default:
|
|
||||||
log.info("Unknown menu option: {} {} {}", ev, ev.getMenuOpcode(), ev.getMenuOpcode() == MenuOpcode.CANCEL);
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
// fallthrough
|
||||||
name = Text.removeTags(name);
|
default:
|
||||||
HttpUrl.Builder urlBuilder = WIKI_BASE.newBuilder();
|
log.info("Unknown menu option: {} {} {}", ev, ev.getMenuOpcode(), ev.getMenuOpcode() == MenuOpcode.CANCEL);
|
||||||
urlBuilder.addPathSegments("w/Special:Lookup")
|
return;
|
||||||
.addQueryParameter("type", type)
|
|
||||||
.addQueryParameter("id", "" + id)
|
|
||||||
.addQueryParameter("name", name)
|
|
||||||
.addQueryParameter(UTM_SOURCE_KEY, UTM_SOURCE_VALUE);
|
|
||||||
|
|
||||||
if (location != null)
|
|
||||||
{
|
|
||||||
urlBuilder.addQueryParameter("x", "" + location.getX())
|
|
||||||
.addQueryParameter("y", "" + location.getY())
|
|
||||||
.addQueryParameter("plane", "" + location.getPlane());
|
|
||||||
}
|
|
||||||
|
|
||||||
HttpUrl url = urlBuilder.build();
|
|
||||||
|
|
||||||
LinkBrowser.browse(url.toString());
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (ev.getMenuOpcode() == MenuOpcode.RUNELITE)
|
name = Text.removeTags(name);
|
||||||
|
HttpUrl.Builder urlBuilder = WIKI_BASE.newBuilder();
|
||||||
|
urlBuilder.addPathSegments("w/Special:Lookup")
|
||||||
|
.addQueryParameter("type", type)
|
||||||
|
.addQueryParameter("id", "" + id)
|
||||||
|
.addQueryParameter("name", name)
|
||||||
|
.addQueryParameter(UTM_SOURCE_KEY, UTM_SOURCE_VALUE);
|
||||||
|
|
||||||
|
if (location != null)
|
||||||
{
|
{
|
||||||
boolean quickguide = false;
|
urlBuilder.addQueryParameter("x", "" + location.getX())
|
||||||
switch (ev.getOption())
|
.addQueryParameter("y", "" + location.getY())
|
||||||
{
|
.addQueryParameter("plane", "" + location.getPlane());
|
||||||
case MENUOP_QUICKGUIDE:
|
}
|
||||||
quickguide = true;
|
|
||||||
//fallthrough;
|
HttpUrl url = urlBuilder.build();
|
||||||
case MENUOP_GUIDE:
|
|
||||||
ev.consume();
|
LinkBrowser.browse(url.toString());
|
||||||
String quest = Text.removeTags(ev.getTarget());
|
}
|
||||||
HttpUrl.Builder ub = WIKI_BASE.newBuilder()
|
|
||||||
.addPathSegment("w")
|
private void checkQuestClicked(MenuOptionClicked ev)
|
||||||
.addPathSegment(quest)
|
{
|
||||||
.addQueryParameter(UTM_SOURCE_KEY, UTM_SOURCE_VALUE);
|
boolean quickguide = false;
|
||||||
if (quickguide)
|
switch (ev.getOption())
|
||||||
{
|
{
|
||||||
ub.addPathSegment("Quick_guide");
|
case MENUOP_QUICKGUIDE:
|
||||||
}
|
quickguide = true;
|
||||||
LinkBrowser.browse(ub.build().toString());
|
//fallthrough;
|
||||||
break;
|
case MENUOP_GUIDE:
|
||||||
case MENUOP_WIKI:
|
ev.consume();
|
||||||
LinkBrowser.browse(WIKI_BASE.newBuilder()
|
String quest = Text.removeTags(ev.getTarget());
|
||||||
.addPathSegment("w")
|
HttpUrl.Builder ub = WIKI_BASE.newBuilder()
|
||||||
.addPathSegment(Text.removeTags(ev.getTarget()))
|
.addPathSegment("w")
|
||||||
.addQueryParameter(UTM_SOURCE_KEY, UTM_SOURCE_VALUE)
|
.addPathSegment(quest)
|
||||||
.build().toString());
|
.addQueryParameter(UTM_SOURCE_KEY, UTM_SOURCE_VALUE);
|
||||||
}
|
if (quickguide)
|
||||||
|
{
|
||||||
|
ub.addPathSegment("Quick_guide");
|
||||||
|
}
|
||||||
|
LinkBrowser.browse(ub.build().toString());
|
||||||
|
break;
|
||||||
|
case MENUOP_WIKI:
|
||||||
|
LinkBrowser.browse(WIKI_BASE.newBuilder()
|
||||||
|
.addPathSegment("w")
|
||||||
|
.addPathSegment(Text.removeTags(ev.getTarget()))
|
||||||
|
.addQueryParameter(UTM_SOURCE_KEY, UTM_SOURCE_VALUE)
|
||||||
|
.build().toString());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -325,22 +323,11 @@ public class WikiPlugin extends Plugin
|
|||||||
.build();
|
.build();
|
||||||
}
|
}
|
||||||
|
|
||||||
private Widget getWidget(int wid, int index)
|
|
||||||
{
|
|
||||||
Widget w = client.getWidget(WidgetInfo.TO_GROUP(wid), WidgetInfo.TO_CHILD(wid));
|
|
||||||
if (index != -1)
|
|
||||||
{
|
|
||||||
w = w.getChild(index);
|
|
||||||
}
|
|
||||||
return w;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Subscribe
|
@Subscribe
|
||||||
private void onMenuEntryAdded(MenuEntryAdded event)
|
private void onMenuEntryAdded(MenuEntryAdded event)
|
||||||
{
|
{
|
||||||
int widgetIndex = event.getParam0();
|
int widgetIndex = event.getParam0();
|
||||||
int widgetID = event.getParam1();
|
int widgetID = event.getParam1();
|
||||||
MenuEntry[] menuEntries = client.getMenuEntries();
|
|
||||||
|
|
||||||
if (wikiSelected && event.getOpcode() == MenuOpcode.SPELL_CAST_ON_WIDGET.getId())
|
if (wikiSelected && event.getOpcode() == MenuOpcode.SPELL_CAST_ON_WIDGET.getId())
|
||||||
{
|
{
|
||||||
@@ -350,13 +337,16 @@ public class WikiPlugin extends Plugin
|
|||||||
// we don't support this widget
|
// we don't support this widget
|
||||||
// remove the last SPELL_CAST_ON_WIDGET; we can't blindly remove the top action because some other
|
// remove the last SPELL_CAST_ON_WIDGET; we can't blindly remove the top action because some other
|
||||||
// plugin might have added something on this same event, and we probably shouldn't remove that instead
|
// plugin might have added something on this same event, and we probably shouldn't remove that instead
|
||||||
MenuEntry[] oldEntries = menuEntries;
|
MenuEntry[] menuEntries = client.getMenuEntries();
|
||||||
menuEntries = Arrays.copyOf(menuEntries, menuEntries.length - 1);
|
for (int i = menuEntries.length - 1; i >= 0; i--)
|
||||||
for (int ourEntry = oldEntries.length - 1; ourEntry >= 2 && oldEntries[oldEntries.length - 1].getOpcode() != MenuOpcode.SPELL_CAST_ON_WIDGET.getId(); ourEntry--)
|
|
||||||
{
|
{
|
||||||
menuEntries[ourEntry - 1] = oldEntries[ourEntry];
|
if (menuEntries[i].getOpcode() == MenuOpcode.SPELL_CAST_ON_WIDGET.getId())
|
||||||
|
{
|
||||||
|
menuEntries[i] = null;
|
||||||
|
client.setMenuEntries(menuEntries);
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
client.setMenuEntries(menuEntries);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -396,9 +386,7 @@ public class WikiPlugin extends Plugin
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String action = Stream.of(w.getActions())
|
String action = firstAction(w);
|
||||||
.filter(s -> s != null && !s.isEmpty())
|
|
||||||
.findFirst().orElse(null);
|
|
||||||
if (action == null)
|
if (action == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@@ -424,9 +412,7 @@ public class WikiPlugin extends Plugin
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String action = Stream.of(w.getActions())
|
String action = firstAction(w);
|
||||||
.filter(s -> s != null && !s.isEmpty())
|
|
||||||
.findFirst().orElse(null);
|
|
||||||
if (action == null)
|
if (action == null)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
@@ -443,4 +429,28 @@ public class WikiPlugin extends Plugin
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private Widget getWidget(int wid, int index)
|
||||||
|
{
|
||||||
|
Widget w = client.getWidget(WidgetInfo.TO_GROUP(wid), WidgetInfo.TO_CHILD(wid));
|
||||||
|
if (index != -1)
|
||||||
|
{
|
||||||
|
w = w.getChild(index);
|
||||||
|
}
|
||||||
|
return w;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private static String firstAction(Widget widget)
|
||||||
|
{
|
||||||
|
for (String action : widget.getActions())
|
||||||
|
{
|
||||||
|
if (StringUtils.isNotEmpty(action))
|
||||||
|
{
|
||||||
|
return action;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return null;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user