From b29f2ff0ed7cd394f875f2489ae8bac42ff2654b Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 6 May 2018 20:40:30 -0400 Subject: [PATCH 1/2] npc indicators: remove unnecessary tagged npcs list --- .../npchighlight/NpcClickboxOverlay.java | 10 +++++--- .../npchighlight/NpcIndicatorsPlugin.java | 23 +++++-------------- .../npchighlight/NpcMinimapOverlay.java | 14 ++++++++--- 3 files changed, 24 insertions(+), 23 deletions(-) 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..1446b7d837 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 @@ -65,10 +65,14 @@ public class NpcClickboxOverlay extends Overlay renderNpcOverlay(graphics, npc, npcMap.get(npc), 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..f6723c8e31 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 @@ -27,7 +27,6 @@ package net.runelite.client.plugins.npchighlight; import com.google.common.eventbus.Subscribe; import com.google.inject.Provides; -import java.util.ArrayList; import java.util.Arrays; import java.util.Collection; import java.util.Collections; @@ -82,12 +81,15 @@ public class NpcIndicatorsPlugin extends Plugin @Inject private KeyManager keyManager; + /** + * NPCs tagged with the Tag option + */ @Getter(AccessLevel.PACKAGE) private final Set npcTags = new HashSet<>(); - @Getter(AccessLevel.PACKAGE) - private final List taggedNpcs = new ArrayList<>(); - + /** + * NPCs tagged due to highlight in the config + */ @Getter(AccessLevel.PACKAGE) private Map highlightedNpcs = new HashMap<>(); @@ -116,7 +118,6 @@ public class NpcIndicatorsPlugin extends Plugin protected void shutDown() throws Exception { npcTags.clear(); - taggedNpcs.clear(); keyManager.unregisterKeyListener(inputListener); } @@ -141,18 +142,6 @@ public class NpcIndicatorsPlugin extends Plugin public void onGameTick(GameTick tick) { highlightedNpcs = buildNpcsToHighlight(); - taggedNpcs.clear(); - if (npcTags.isEmpty() || !config.isTagEnabled()) - { - return; - } - for (NPC npc : client.getNpcs()) - { - if (npcTags.contains(npc.getIndex()) && npc.getName() != null) - { - taggedNpcs.add(npc); - } - } } @Override 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..0f4729f609 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 @@ -30,6 +30,7 @@ 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 +40,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); @@ -60,9 +63,14 @@ public class NpcMinimapOverlay extends Overlay renderNpcOverlay(graphics, npc, npcMap.get(npc), 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; From 40d0fab0b5c9dd0ead076026557b4fc3f0e6019a Mon Sep 17 00:00:00 2001 From: Adam Date: Sun, 6 May 2018 21:12:24 -0400 Subject: [PATCH 2/2] npc indicators: use events for highlighted npcs --- .../npchighlight/NpcClickboxOverlay.java | 6 +- .../npchighlight/NpcIndicatorsPlugin.java | 119 ++++++++++++------ .../npchighlight/NpcMinimapOverlay.java | 6 +- 3 files changed, 87 insertions(+), 44 deletions(-) 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 1446b7d837..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,10 +58,9 @@ 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()); } NPC[] npcs = client.getCachedNPCs(); 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 f6723c8e31..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 @@ -27,22 +27,23 @@ package net.runelite.client.plugins.npchighlight; import com.google.common.eventbus.Subscribe; import com.google.inject.Provides; +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; @@ -91,7 +92,12 @@ public class NpcIndicatorsPlugin extends Plugin * NPCs tagged due to highlight in the config */ @Getter(AccessLevel.PACKAGE) - private Map highlightedNpcs = new HashMap<>(); + private final Set highlightedNpcs = new HashSet<>(); + + /** + * Highlight strings from the configuration + */ + private List highlights = new ArrayList<>(); private boolean hotKeyPressed = false; @@ -112,15 +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(); + 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) { @@ -139,9 +197,28 @@ public class NpcIndicatorsPlugin extends Plugin } @Subscribe - public void onGameTick(GameTick tick) + public void onNpcSpawned(NpcSpawned npcSpawned) { - highlightedNpcs = buildNpcsToHighlight(); + NPC npc = npcSpawned.getNpc(); + String npcName = npc.getName(); + if (npcName != null) + { + for (String highlight : highlights) + { + if (WildcardMatcher.matches(highlight, npcName)) + { + highlightedNpcs.add(npc); + break; + } + } + } + } + + @Subscribe + public void onNpcDespawned(NpcDespawned npcDespawned) + { + NPC npc = npcDespawned.getNpc(); + highlightedNpcs.remove(npc); } @Override @@ -150,36 +227,6 @@ public class NpcIndicatorsPlugin extends Plugin 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 0f4729f609..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,7 +28,6 @@ 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; @@ -57,10 +56,9 @@ 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()); } NPC[] npcs = client.getCachedNPCs();