NPC indicators plugin: Generate less garbage objects, fix highlight (#1531)

This commit is contained in:
Lucwousin
2019-09-03 22:42:23 +02:00
committed by Ganom
parent 9d5adc9bb3
commit 86e5658fd8
3 changed files with 57 additions and 27 deletions

View File

@@ -24,15 +24,15 @@
*/ */
package net.runelite.api.events; package net.runelite.api.events;
import lombok.AllArgsConstructor;
import lombok.Data; import lombok.Data;
import lombok.RequiredArgsConstructor;
import net.runelite.api.MenuEntry; import net.runelite.api.MenuEntry;
/** /**
* An event when a new entry is added to a right-click menu. * An event when a new entry is added to a right-click menu.
*/ */
@Data @Data
@AllArgsConstructor @RequiredArgsConstructor
public class MenuEntryAdded implements Event public class MenuEntryAdded implements Event
{ {
/** /**
@@ -40,6 +40,15 @@ public class MenuEntryAdded implements Event
*/ */
private final MenuEntry menuEntry; private final MenuEntry menuEntry;
/**
* If this is set to true client mixin will update
* the menu entry with the modified values.
*
* Checks if count is the same, but doesn't check if there's
* been multiple changes
*/
private boolean wasModified;
public String getOption() public String getOption()
{ {
return menuEntry.getOption(); return menuEntry.getOption();

View File

@@ -31,7 +31,6 @@ import com.google.inject.Provides;
import java.awt.Color; import java.awt.Color;
import java.time.Instant; import java.time.Instant;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@@ -51,7 +50,6 @@ import net.runelite.api.GraphicID;
import net.runelite.api.GraphicsObject; import net.runelite.api.GraphicsObject;
import net.runelite.api.MenuOpcode; import net.runelite.api.MenuOpcode;
import static net.runelite.api.MenuOpcode.MENU_ACTION_DEPRIORITIZE_OFFSET; import static net.runelite.api.MenuOpcode.MENU_ACTION_DEPRIORITIZE_OFFSET;
import net.runelite.api.MenuEntry;
import net.runelite.api.NPC; import net.runelite.api.NPC;
import net.runelite.api.coords.WorldPoint; import net.runelite.api.coords.WorldPoint;
import net.runelite.api.events.ConfigChanged; import net.runelite.api.events.ConfigChanged;
@@ -72,6 +70,7 @@ import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor; import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.ui.overlay.OverlayManager; import net.runelite.client.ui.overlay.OverlayManager;
import net.runelite.api.util.Text; import net.runelite.api.util.Text;
import net.runelite.client.util.ColorUtil;
import net.runelite.client.util.WildcardMatcher; import net.runelite.client.util.WildcardMatcher;
@PluginDescriptor( @PluginDescriptor(
@@ -89,8 +88,13 @@ public class NpcIndicatorsPlugin extends Plugin
private static final String TAG = "Tag"; private static final String TAG = "Tag";
private static final String UNTAG = "Un-tag"; private static final String UNTAG = "Un-tag";
private static final Set<MenuOpcode> NPC_MENU_ACTIONS = ImmutableSet.of(MenuOpcode.NPC_FIRST_OPTION, MenuOpcode.NPC_SECOND_OPTION, private static final Set<MenuOpcode> NPC_MENU_ACTIONS = ImmutableSet.of(
MenuOpcode.NPC_THIRD_OPTION, MenuOpcode.NPC_FOURTH_OPTION, MenuOpcode.NPC_FIFTH_OPTION); MenuOpcode.NPC_FIRST_OPTION,
MenuOpcode.NPC_SECOND_OPTION,
MenuOpcode.NPC_THIRD_OPTION,
MenuOpcode.NPC_FOURTH_OPTION,
MenuOpcode.NPC_FIFTH_OPTION
);
@Inject @Inject
private Client client; private Client client;
@@ -294,8 +298,6 @@ public class NpcIndicatorsPlugin extends Plugin
private void onMenuEntryAdded(MenuEntryAdded event) private void onMenuEntryAdded(MenuEntryAdded event)
{ {
MenuEntry[] menuEntries = client.getMenuEntries();
String target = event.getTarget();
int type = event.getType(); int type = event.getType();
if (type >= MENU_ACTION_DEPRIORITIZE_OFFSET) if (type >= MENU_ACTION_DEPRIORITIZE_OFFSET)
@@ -307,22 +309,22 @@ public class NpcIndicatorsPlugin extends Plugin
NPC_MENU_ACTIONS.contains(MenuOpcode.of(type)) && NPC_MENU_ACTIONS.contains(MenuOpcode.of(type)) &&
highlightedNpcs.stream().anyMatch(npc -> npc.getIndex() == event.getIdentifier())) highlightedNpcs.stream().anyMatch(npc -> npc.getIndex() == event.getIdentifier()))
{ {
final MenuEntry menuEntry = menuEntries[menuEntries.length - 1]; final String target = ColorUtil.prependColorTag(Text.removeTags(event.getMenuEntry().getTarget()), this.getHighlightColor);
menuEntry.setTarget(target); event.getMenuEntry().setTarget(target);
client.setMenuEntries(menuEntries); event.setWasModified(true);
} }
else if (hotKeyPressed && type == MenuOpcode.EXAMINE_NPC.getId()) else if (hotKeyPressed && type == MenuOpcode.EXAMINE_NPC.getId())
{ {
// Add tag option // Add tag option
menuEntries = Arrays.copyOf(menuEntries, menuEntries.length + 1); client.insertMenuItem(
final MenuEntry tagEntry = menuEntries[menuEntries.length - 1] = new MenuEntry(); highlightedNpcs.stream().anyMatch(npc -> npc.getIndex() == event.getIdentifier()) ? UNTAG : TAG,
tagEntry.setOption(highlightedNpcs.stream().anyMatch(npc -> npc.getIndex() == event.getIdentifier()) ? UNTAG : TAG); event.getTarget(),
tagEntry.setTarget(event.getTarget()); MenuOpcode.RUNELITE.getId(),
tagEntry.setParam0(event.getActionParam0()); event.getIdentifier(),
tagEntry.setParam1(event.getActionParam1()); event.getActionParam0(),
tagEntry.setIdentifier(event.getIdentifier()); event.getActionParam1(),
tagEntry.setOpcode(MenuOpcode.RUNELITE.getId()); false
client.setMenuEntries(menuEntries); );
} }
} }

View File

@@ -717,21 +717,40 @@ public abstract class RSClientMixin implements RSClient
oldMenuEntryCount = newCount; oldMenuEntryCount = newCount;
final String[] options = client.getMenuOptions();
final String[] targets = client.getMenuTargets();
final int[] identifiers = client.getMenuIdentifiers();
final int[] opcodes = client.getMenuOpcodes();
final int[] arguments1 = client.getMenuArguments1();
final int[] arguments2 = client.getMenuArguments2();
final boolean[] forceLeftClick = client.getMenuForceLeftClick();
if (newCount == oldCount + 1) if (newCount == oldCount + 1)
{ {
MenuEntryAdded event = new MenuEntryAdded( MenuEntryAdded event = new MenuEntryAdded(
new MenuEntry( new MenuEntry(
client.getMenuOptions()[oldCount], options[oldCount],
client.getMenuTargets()[oldCount], targets[oldCount],
client.getMenuIdentifiers()[oldCount], identifiers[oldCount],
client.getMenuOpcodes()[oldCount], opcodes[oldCount],
client.getMenuArguments1()[oldCount], arguments1[oldCount],
client.getMenuArguments2()[oldCount], arguments2[oldCount],
client.getMenuForceLeftClick()[oldCount] forceLeftClick[oldCount]
) )
); );
client.getCallbacks().post(MenuEntryAdded.class, event); client.getCallbacks().post(MenuEntryAdded.class, event);
if (event.isWasModified() && client.getMenuOptionCount() == newCount)
{
options[oldCount] = event.getOption();
targets[oldCount] = event.getTarget();
identifiers[oldCount] = event.getIdentifier();
opcodes[oldCount] = event.getType();
arguments1[oldCount] = event.getActionParam0();
arguments2[oldCount] = event.getActionParam1();
forceLeftClick[oldCount] = event.isForceLeftClick();
}
} }
} }