api: add npc accessor to menuentry

This commit is contained in:
Adam
2022-05-23 17:15:22 -04:00
parent c64fc72f9e
commit 7a1d958ee0
16 changed files with 78 additions and 82 deletions

View File

@@ -122,4 +122,11 @@ public interface MenuEntry
*/
@Nullable
Widget getWidget();
/**
* Get the {@link NPC} this menu entry is targeting, if any.
* @return
*/
@Nullable
NPC getNpc();
}

View File

@@ -25,6 +25,7 @@
package net.runelite.api.events;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.ToString;
import net.runelite.api.MenuEntry;
@@ -37,6 +38,7 @@ import net.runelite.api.MenuEntry;
@ToString(onlyExplicitlyIncluded = true)
public class MenuEntryAdded
{
@Getter
private final MenuEntry menuEntry;
/**

View File

@@ -52,6 +52,7 @@ public class MenuOptionClicked
/**
* The clicked menu entry
*/
@Getter
private final MenuEntry menuEntry;
/**

View File

@@ -118,9 +118,8 @@ public class CrowdsourcingThieving
{
if (event.getMenuOption().equals("Pickpocket") || event.getMenuOption().equals("Knock-Out"))
{
NPC[] cachedNPCs = client.getCachedNPCs();
NPC npc = cachedNPCs[event.getId()];
lastPickpocketTarget = npc.getId();
NPC npc = event.getMenuEntry().getNpc();
lastPickpocketTarget = npc != null ? npc.getId() : -1;
}
}
}

View File

@@ -472,7 +472,8 @@ public class DevToolsPlugin extends Plugin
if (action == MenuAction.EXAMINE_NPC)
{
NPC npc = client.getCachedNPCs()[identifier];
NPC npc = entry.getNpc();
assert npc != null;
info += npc.getId();
}
else

View File

@@ -114,8 +114,7 @@ class InteractHighlightOverlay extends Overlay
case NPC_FIFTH_OPTION:
case EXAMINE_NPC:
{
int id = entry.getIdentifier();
NPC npc = plugin.findNpc(id);
NPC npc = entry.getNpc();
if (npc != null && config.npcShowHover() && (npc != plugin.getInteractedTarget() || !config.npcShowInteract()))
{
Color highlightColor = menuAction == MenuAction.NPC_SECOND_OPTION

View File

@@ -167,9 +167,8 @@ public class InteractHighlightPlugin extends Plugin
case NPC_FOURTH_OPTION:
case NPC_FIFTH_OPTION:
{
int id = menuOptionClicked.getId();
interactedObject = null;
interactedNpc = findNpc(id);
interactedNpc = menuOptionClicked.getMenuEntry().getNpc();
attacked = menuOptionClicked.getMenuAction() == MenuAction.NPC_SECOND_OPTION ||
menuOptionClicked.getMenuAction() == MenuAction.WIDGET_TARGET_ON_NPC && WidgetInfo.TO_GROUP(client.getSelectedWidget().getId()) == WidgetID.SPELLBOOK_GROUP_ID;
clickTick = client.getTickCount();
@@ -242,11 +241,6 @@ public class InteractHighlightPlugin extends Plugin
return null;
}
NPC findNpc(int id)
{
return client.getCachedNPCs()[id];
}
@Nullable
Actor getInteractedTarget()
{

View File

@@ -717,7 +717,8 @@ public class MenuEntrySwapperPlugin extends Plugin
if (type == MenuAction.EXAMINE_NPC)
{
final NPC npc = client.getCachedNPCs()[id];
final NPC npc = entry.getNpc();
assert npc != null;
final NPCComposition composition = npc.getTransformedComposition();
final String[] actions = composition.getActions();
@@ -967,7 +968,8 @@ public class MenuEntrySwapperPlugin extends Plugin
if (NPC_MENU_TYPES.contains(menuAction))
{
final NPC npc = client.getCachedNPCs()[eventId];
final NPC npc = menuEntry.getNpc();
assert npc != null;
final NPCComposition composition = npc.getTransformedComposition();
Integer customOption = getNpcSwapConfig(composition.getId());

View File

@@ -26,7 +26,6 @@
package net.runelite.client.plugins.npchighlight;
import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ImmutableSet;
import com.google.inject.Provides;
import java.awt.Color;
import java.time.Instant;
@@ -48,7 +47,6 @@ import net.runelite.api.GraphicID;
import net.runelite.api.GraphicsObject;
import net.runelite.api.KeyCode;
import net.runelite.api.MenuAction;
import static net.runelite.api.MenuAction.MENU_ACTION_DEPRIORITIZE_OFFSET;
import net.runelite.api.MenuEntry;
import net.runelite.api.NPC;
import net.runelite.api.coords.WorldPoint;
@@ -89,10 +87,6 @@ public class NpcIndicatorsPlugin extends Plugin
private static final String TAG_ALL = "Tag-All";
private static final String UNTAG_ALL = "Un-tag-All";
private static final Set<MenuAction> NPC_MENU_ACTIONS = ImmutableSet.of(MenuAction.NPC_FIRST_OPTION, MenuAction.NPC_SECOND_OPTION,
MenuAction.NPC_THIRD_OPTION, MenuAction.NPC_FOURTH_OPTION, MenuAction.NPC_FIFTH_OPTION, MenuAction.WIDGET_TARGET_ON_NPC,
MenuAction.ITEM_USE_ON_NPC);
@Inject
private Client client;
@@ -239,46 +233,19 @@ public class NpcIndicatorsPlugin extends Plugin
@Subscribe
public void onMenuEntryAdded(MenuEntryAdded event)
{
int type = event.getType();
final MenuEntry menuEntry = event.getMenuEntry();
final MenuAction menuAction = menuEntry.getType();
final NPC npc = menuEntry.getNpc();
if (type >= MENU_ACTION_DEPRIORITIZE_OFFSET)
if (npc == null)
{
type -= MENU_ACTION_DEPRIORITIZE_OFFSET;
return;
}
final MenuAction menuAction = MenuAction.of(type);
if (NPC_MENU_ACTIONS.contains(menuAction))
{
NPC npc = client.getCachedNPCs()[event.getIdentifier()];
Color color = null;
if (npc.isDead())
{
color = config.deadNpcMenuColor();
}
if (color == null && highlightedNpcs.containsKey(npc) && config.highlightMenuNames() && (!npc.isDead() || !config.ignoreDeadNpcs()))
{
color = config.highlightColor();
}
if (color != null)
{
MenuEntry[] menuEntries = client.getMenuEntries();
final MenuEntry menuEntry = menuEntries[menuEntries.length - 1];
final String target = ColorUtil.prependColorTag(Text.removeTags(event.getTarget()), color);
menuEntry.setTarget(target);
}
}
else if (menuAction == MenuAction.EXAMINE_NPC && client.isKeyPressed(KeyCode.KC_SHIFT))
if (menuAction == MenuAction.EXAMINE_NPC && client.isKeyPressed(KeyCode.KC_SHIFT))
{
// Add tag and tag-all options
final int id = event.getIdentifier();
final NPC[] cachedNPCs = client.getCachedNPCs();
final NPC npc = cachedNPCs[id];
if (npc == null || npc.getName() == null)
if (npc.getName() == null)
{
return;
}
@@ -306,6 +273,25 @@ public class NpcIndicatorsPlugin extends Plugin
.setType(MenuAction.RUNELITE)
.onClick(this::tag);
}
else
{
Color color = null;
if (npc.isDead())
{
color = config.deadNpcMenuColor();
}
if (color == null && highlightedNpcs.containsKey(npc) && config.highlightMenuNames() && (!npc.isDead() || !config.ignoreDeadNpcs()))
{
color = config.highlightColor();
}
if (color != null)
{
final String target = ColorUtil.prependColorTag(Text.removeTags(event.getTarget()), color);
menuEntry.setTarget(target);
}
}
}
private void tag(MenuEntry entry)

View File

@@ -159,8 +159,7 @@ public class OpponentInfoPlugin extends Plugin
return;
}
int npcIndex = menuEntryAdded.getIdentifier();
NPC npc = client.getCachedNPCs()[npcIndex];
NPC npc = menuEntryAdded.getMenuEntry().getNpc();
if (npc == null)
{
return;

View File

@@ -160,7 +160,7 @@ public class RandomEventPlugin extends Plugin
&& event.getType() <= MenuAction.NPC_FIFTH_OPTION.getId()
&& EVENT_OPTIONS.contains(event.getOption()))
{
NPC npc = client.getCachedNPCs()[event.getIdentifier()];
NPC npc = event.getMenuEntry().getNpc();
if (npc != null && EVENT_NPCS.contains(npc.getId()) && npc != currentRandomEvent && config.removeMenuOptions())
{
client.setMenuEntries(Arrays.copyOf(client.getMenuEntries(), client.getMenuEntries().length - 1));

View File

@@ -279,7 +279,8 @@ public class WikiPlugin extends Plugin
case WIDGET_TARGET_ON_NPC:
{
type = "npc";
NPC npc = client.getCachedNPCs()[ev.getId()];
NPC npc = ev.getMenuEntry().getNpc();
assert npc != null;
NPCComposition nc = npc.getTransformedComposition();
id = nc.getId();
name = nc.getName();

View File

@@ -33,7 +33,6 @@ import java.util.List;
import javax.inject.Inject;
import net.runelite.api.Client;
import net.runelite.api.MenuAction;
import static net.runelite.api.MenuAction.CC_OP;
import static net.runelite.api.MenuAction.RUNELITE;
import net.runelite.api.MenuEntry;
import net.runelite.api.events.MenuEntryAdded;
@@ -99,13 +98,7 @@ public class MenuManagerTest
menuManager.addManagedCustomMenu(new WidgetMenuOption(second.getOption(), second.getTarget(), MINIMAP_WORLDMAP_OPTIONS), null);
menuManager.addManagedCustomMenu(new WidgetMenuOption(third.getOption(), third.getTarget(), MINIMAP_WORLDMAP_OPTIONS), null);
menuManager.onMenuEntryAdded(new MenuEntryAdded(
CANCEL.getOption(),
CANCEL.getTarget(),
CC_OP.getId(),
CANCEL.getIdentifier(),
CANCEL.getParam0(),
CANCEL.getParam1()));
menuManager.onMenuEntryAdded(new MenuEntryAdded(createMenuEntry("Cancel", "", MenuAction.CC_OP, MINIMAP_WORLDMAP_OPTIONS.getPackedId())));
verify(client, times(3)).createMenuEntry(anyInt());

View File

@@ -25,12 +25,13 @@
package net.runelite.client.menus;
import java.util.function.Consumer;
import javax.annotation.Nullable;
import lombok.EqualsAndHashCode;
import lombok.Setter;
import net.runelite.api.MenuAction;
import net.runelite.api.MenuEntry;
import net.runelite.api.NPC;
import net.runelite.api.widgets.Widget;
import org.jetbrains.annotations.Nullable;
@EqualsAndHashCode
public class TestMenuEntry implements MenuEntry
@@ -48,6 +49,8 @@ public class TestMenuEntry implements MenuEntry
private int itemId = -1;
@Setter
private Widget widget;
@Setter
private NPC npc;
@Override
public String getOption()
@@ -197,4 +200,11 @@ public class TestMenuEntry implements MenuEntry
{
return widget;
}
@Nullable
@Override
public NPC getNpc()
{
return npc;
}
}

View File

@@ -86,6 +86,7 @@ public class MenuEntrySwapperPluginTest
@Inject
MenuEntrySwapperPlugin menuEntrySwapperPlugin;
private NPC npc;
private MenuEntry[] entries;
@Before
@@ -96,10 +97,9 @@ public class MenuEntrySwapperPluginTest
when(client.getGameState()).thenReturn(GameState.LOGGED_IN);
when(client.getObjectDefinition(anyInt())).thenReturn(mock(ObjectComposition.class));
NPC npc = mock(NPC.class);
npc = mock(NPC.class);
NPCComposition composition = mock(NPCComposition.class);
when(npc.getTransformedComposition()).thenReturn(composition);
when(client.getCachedNPCs()).thenReturn(new NPC[] { npc });
when(client.getMenuEntries()).thenAnswer((Answer<MenuEntry[]>) invocationOnMock ->
{
@@ -117,18 +117,19 @@ public class MenuEntrySwapperPluginTest
menuEntrySwapperPlugin.setupSwaps();
}
private static MenuEntry menu(String option, String target, MenuAction menuAction)
private MenuEntry menu(String option, String target, MenuAction menuAction)
{
return menu(option, target, menuAction, 0);
}
private static MenuEntry menu(String option, String target, MenuAction menuAction, int identifier)
private MenuEntry menu(String option, String target, MenuAction menuAction, int identifier)
{
MenuEntry menuEntry = new TestMenuEntry();
TestMenuEntry menuEntry = new TestMenuEntry();
menuEntry.setOption(option);
menuEntry.setTarget(target);
menuEntry.setType(menuAction);
menuEntry.setIdentifier(identifier);
menuEntry.setNpc(npc);
return menuEntry;
}

View File

@@ -34,7 +34,6 @@ import java.util.concurrent.ScheduledExecutorService;
import javax.inject.Inject;
import net.runelite.api.Client;
import net.runelite.api.MenuAction;
import net.runelite.api.MenuEntry;
import net.runelite.api.NPC;
import net.runelite.api.events.MenuEntryAdded;
import net.runelite.api.events.NpcChanged;
@@ -107,11 +106,12 @@ public class NpcIndicatorsPluginTest
when(npc.isDead()).thenReturn(true);
npcIndicatorsPlugin.onNpcSpawned(new NpcSpawned(npc));
when(client.getCachedNPCs()).thenReturn(new NPC[]{npc}); // id 0
TestMenuEntry entry = new TestMenuEntry();
entry.setTarget("Goblin");
entry.setIdentifier(MenuAction.NPC_FIRST_OPTION.getId());
entry.setNpc(npc);
MenuEntry entry = new TestMenuEntry();
when(client.getMenuEntries()).thenReturn(new MenuEntry[]{entry});
MenuEntryAdded menuEntryAdded = new MenuEntryAdded("", "Goblin", MenuAction.NPC_FIRST_OPTION.getId(), 0, -1, -1);
MenuEntryAdded menuEntryAdded = new MenuEntryAdded(entry);
npcIndicatorsPlugin.onMenuEntryAdded(menuEntryAdded);
assertEquals("<col=ff0000>Goblin", entry.getTarget()); // red
@@ -130,11 +130,12 @@ public class NpcIndicatorsPluginTest
when(npc.getName()).thenReturn("Goblin");
npcIndicatorsPlugin.onNpcSpawned(new NpcSpawned(npc));
when(client.getCachedNPCs()).thenReturn(new NPC[]{npc}); // id 0
TestMenuEntry entry = new TestMenuEntry();
entry.setTarget("Goblin");
entry.setIdentifier(MenuAction.NPC_FIRST_OPTION.getId());
entry.setNpc(npc);
MenuEntry entry = new TestMenuEntry();
when(client.getMenuEntries()).thenReturn(new MenuEntry[]{entry});
MenuEntryAdded menuEntryAdded = new MenuEntryAdded("", "Goblin", MenuAction.NPC_FIRST_OPTION.getId(), 0, -1, -1);
MenuEntryAdded menuEntryAdded = new MenuEntryAdded(entry);
npcIndicatorsPlugin.onMenuEntryAdded(menuEntryAdded);
assertEquals("<col=0000ff>Goblin", entry.getTarget()); // blue