Merge pull request #2426 from Adam-/npcindicators

Cleanup npcindicators
This commit is contained in:
Adam
2018-05-07 09:43:09 -04:00
committed by GitHub
3 changed files with 105 additions and 61 deletions

View File

@@ -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<NPC, String> 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;

View File

@@ -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<Integer> npcTags = new HashSet<>();
/**
* NPCs tagged due to highlight in the config
*/
@Getter(AccessLevel.PACKAGE)
private final List<NPC> taggedNpcs = new ArrayList<>();
private final Set<NPC> highlightedNpcs = new HashSet<>();
@Getter(AccessLevel.PACKAGE)
private Map<NPC, String> highlightedNpcs = new HashMap<>();
/**
* Highlight strings from the configuration
*/
private List<String> 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<String> getHighlights()
{
String configNpcs = config.getNpcToHighlight().toLowerCase();
if (configNpcs.isEmpty())
return Collections.emptyList();
List<String> 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<Overlay> getOverlays()
{
return Arrays.asList(npcClickboxOverlay, npcMinimapOverlay);
}
private Map<NPC, String> buildNpcsToHighlight()
{
String configNpcs = config.getNpcToHighlight().toLowerCase();
if (configNpcs.isEmpty())
return Collections.EMPTY_MAP;
Map<NPC, String> npcMap = new HashMap<>();
List<String> 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())

View File

@@ -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<NPC, String> 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;