diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcClickboxOverlay.java b/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcClickboxOverlay.java index 339b74f797..12e1dcec34 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcClickboxOverlay.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcClickboxOverlay.java @@ -30,7 +30,6 @@ import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; import java.awt.Polygon; -import java.util.Map; import javax.inject.Inject; import net.runelite.api.Client; import net.runelite.api.NPC; @@ -59,16 +58,19 @@ public class NpcClickboxOverlay extends Overlay @Override public Dimension render(Graphics2D graphics) { - Map npcMap = plugin.getHighlightedNpcs(); - for (NPC npc : npcMap.keySet()) + for (NPC npc : plugin.getHighlightedNpcs()) { - renderNpcOverlay(graphics, npc, npcMap.get(npc), config.getNpcColor()); + renderNpcOverlay(graphics, npc, npc.getName(), config.getNpcColor()); } - for (NPC npc : plugin.getTaggedNpcs()) + NPC[] npcs = client.getCachedNPCs(); + for (int npcId : plugin.getNpcTags()) { - String npcName = npc.getName(); - renderNpcOverlay(graphics, npc, npcName, config.getTagColor()); + NPC npc = npcs[npcId]; + if (npc != null && npc.getName() != null) + { + renderNpcOverlay(graphics, npc, npc.getName(), config.getTagColor()); + } } return null; diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcIndicatorsPlugin.java b/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcIndicatorsPlugin.java index 3b0fc692d5..c379efb4a4 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcIndicatorsPlugin.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcIndicatorsPlugin.java @@ -31,19 +31,19 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; -import java.util.HashMap; import java.util.HashSet; import java.util.List; -import java.util.Map; import java.util.Set; import javax.inject.Inject; import lombok.AccessLevel; import lombok.Getter; import net.runelite.api.Client; import net.runelite.api.NPC; +import net.runelite.api.events.ConfigChanged; import net.runelite.api.events.FocusChanged; -import net.runelite.api.events.GameTick; import net.runelite.api.events.MenuOptionClicked; +import net.runelite.api.events.NpcDespawned; +import net.runelite.api.events.NpcSpawned; import net.runelite.client.config.ConfigManager; import net.runelite.client.input.KeyManager; import net.runelite.client.menus.MenuManager; @@ -82,14 +82,22 @@ public class NpcIndicatorsPlugin extends Plugin @Inject private KeyManager keyManager; + /** + * NPCs tagged with the Tag option + */ @Getter(AccessLevel.PACKAGE) private final Set npcTags = new HashSet<>(); + /** + * NPCs tagged due to highlight in the config + */ @Getter(AccessLevel.PACKAGE) - private final List taggedNpcs = new ArrayList<>(); + private final Set highlightedNpcs = new HashSet<>(); - @Getter(AccessLevel.PACKAGE) - private Map highlightedNpcs = new HashMap<>(); + /** + * Highlight strings from the configuration + */ + private List highlights = new ArrayList<>(); private boolean hotKeyPressed = false; @@ -110,16 +118,67 @@ public class NpcIndicatorsPlugin extends Plugin protected void startUp() throws Exception { keyManager.registerKeyListener(inputListener); + highlights = getHighlights(); + rebuildNpcs(); } @Override protected void shutDown() throws Exception { npcTags.clear(); - taggedNpcs.clear(); + highlightedNpcs.clear(); keyManager.unregisterKeyListener(inputListener); } + @Subscribe + public void onConfigChanged(ConfigChanged configChanged) + { + if (!configChanged.getGroup().equals("npcindicators")) + { + return; + } + + highlights = getHighlights(); + rebuildNpcs(); + } + + private List getHighlights() + { + String configNpcs = config.getNpcToHighlight().toLowerCase(); + if (configNpcs.isEmpty()) + return Collections.emptyList(); + + List highlightedNpcs = Arrays.asList(configNpcs.split(DELIMITER_REGEX)); + return highlightedNpcs; + } + + /** + * Rebuild highlighted npcs + */ + private void rebuildNpcs() + { + highlightedNpcs.clear(); + + for (NPC npc : client.getNpcs()) + { + String npcName = npc.getName(); + + if (npcName == null) + { + continue; + } + + for (String highlight : highlights) + { + if (WildcardMatcher.matches(highlight, npcName)) + { + highlightedNpcs.add(npc); + break; + } + } + } + } + @Subscribe public void onFocusChanged(FocusChanged focusChanged) { @@ -138,59 +197,36 @@ public class NpcIndicatorsPlugin extends Plugin } @Subscribe - public void onGameTick(GameTick tick) + public void onNpcSpawned(NpcSpawned npcSpawned) { - highlightedNpcs = buildNpcsToHighlight(); - taggedNpcs.clear(); - if (npcTags.isEmpty() || !config.isTagEnabled()) + NPC npc = npcSpawned.getNpc(); + String npcName = npc.getName(); + if (npcName != null) { - return; - } - for (NPC npc : client.getNpcs()) - { - if (npcTags.contains(npc.getIndex()) && npc.getName() != null) + for (String highlight : highlights) { - taggedNpcs.add(npc); + if (WildcardMatcher.matches(highlight, npcName)) + { + highlightedNpcs.add(npc); + break; + } } } } + @Subscribe + public void onNpcDespawned(NpcDespawned npcDespawned) + { + NPC npc = npcDespawned.getNpc(); + highlightedNpcs.remove(npc); + } + @Override public Collection getOverlays() { return Arrays.asList(npcClickboxOverlay, npcMinimapOverlay); } - private Map buildNpcsToHighlight() - { - String configNpcs = config.getNpcToHighlight().toLowerCase(); - if (configNpcs.isEmpty()) - return Collections.EMPTY_MAP; - - Map npcMap = new HashMap<>(); - List highlightedNpcs = Arrays.asList(configNpcs.split(DELIMITER_REGEX)); - - for (NPC npc : client.getNpcs()) - { - String npcName = npc.getName(); - - if (npcName == null) - { - continue; - } - - for (String highlight : highlightedNpcs) - { - if (WildcardMatcher.matches(highlight, npcName)) - { - npcMap.put(npc, npcName); - } - } - } - - return npcMap; - } - void updateNpcMenuOptions(boolean pressed) { if (!config.isTagEnabled()) diff --git a/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcMinimapOverlay.java b/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcMinimapOverlay.java index c06399d45f..acacb232b8 100644 --- a/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcMinimapOverlay.java +++ b/runelite-client/src/main/java/net/runelite/client/plugins/npchighlight/NpcMinimapOverlay.java @@ -28,8 +28,8 @@ package net.runelite.client.plugins.npchighlight; import java.awt.Color; import java.awt.Dimension; import java.awt.Graphics2D; -import java.util.Map; import javax.inject.Inject; +import net.runelite.api.Client; import net.runelite.api.NPC; import net.runelite.api.Point; import net.runelite.client.ui.overlay.Overlay; @@ -39,12 +39,14 @@ import net.runelite.client.ui.overlay.OverlayUtil; public class NpcMinimapOverlay extends Overlay { + private final Client client; private final NpcIndicatorsConfig config; private final NpcIndicatorsPlugin plugin; @Inject - NpcMinimapOverlay(NpcIndicatorsConfig config, NpcIndicatorsPlugin plugin) + NpcMinimapOverlay(Client client, NpcIndicatorsConfig config, NpcIndicatorsPlugin plugin) { + this.client = client; this.config = config; this.plugin = plugin; setPosition(OverlayPosition.DYNAMIC); @@ -54,15 +56,19 @@ public class NpcMinimapOverlay extends Overlay @Override public Dimension render(Graphics2D graphics) { - Map npcMap = plugin.getHighlightedNpcs(); - for (NPC npc : npcMap.keySet()) + for (NPC npc : plugin.getHighlightedNpcs()) { - renderNpcOverlay(graphics, npc, npcMap.get(npc), config.getNpcColor()); + renderNpcOverlay(graphics, npc, npc.getName(), config.getNpcColor()); } - for (NPC npc : plugin.getTaggedNpcs()) + NPC[] npcs = client.getCachedNPCs(); + for (int npcId : plugin.getNpcTags()) { - renderNpcOverlay(graphics, npc, npc.getName(), config.getTagColor()); + NPC npc = npcs[npcId]; + if (npc != null && npc.getName() != null) + { + renderNpcOverlay(graphics, npc, npc.getName(), config.getTagColor()); + } } return null;