Use NpcSpawned and despawned for getting cacheNpc

- Merge highlightedNpcs and npc tags to one list
- Update highlightedNpcs with npc tags on spawn/despawn and menu entry
click
- Add brackets to single line ifs, remove redundant code

Signed-off-by: Tomas Slusny <slusnucky@gmail.com>
This commit is contained in:
Tomas Slusny
2018-05-07 16:01:04 +02:00
parent 464f5057c3
commit 822bceacd7
3 changed files with 51 additions and 45 deletions

View File

@@ -63,16 +63,6 @@ public class NpcClickboxOverlay extends Overlay
renderNpcOverlay(graphics, npc, npc.getName(), config.getHighlightColor());
}
NPC[] npcs = client.getCachedNPCs();
for (int npcId : plugin.getNpcTags())
{
NPC npc = npcs[npcId];
if (npc != null && npc.getName() != null)
{
renderNpcOverlay(graphics, npc, npc.getName(), config.getHighlightColor());
}
}
return null;
}

View File

@@ -25,6 +25,7 @@
*/
package net.runelite.client.plugins.npchighlight;
import com.google.common.base.Splitter;
import com.google.common.eventbus.Subscribe;
import com.google.inject.Provides;
import java.util.ArrayList;
@@ -34,6 +35,7 @@ import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;
import javax.inject.Inject;
import lombok.AccessLevel;
import lombok.Getter;
@@ -59,7 +61,7 @@ public class NpcIndicatorsPlugin extends Plugin
private static final String TAG = "Tag";
// Regex for splitting the hidden items in the config.
private static final String DELIMITER_REGEX = "\\s*,\\s*";
private static final Splitter COMMA_SPLITTER = Splitter.on(Pattern.compile("\\s*,\\s*")).trimResults();
@Inject
private Client client;
@@ -83,13 +85,7 @@ public class NpcIndicatorsPlugin extends Plugin
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
* NPCs to highlight
*/
@Getter(AccessLevel.PACKAGE)
private final Set<NPC> highlightedNpcs = new HashSet<>();
@@ -99,14 +95,12 @@ public class NpcIndicatorsPlugin extends Plugin
*/
private List<String> highlights = new ArrayList<>();
private boolean hotKeyPressed = false;
/**
* NPC ids marked with the Tag option
*/
private final Set<Integer> npcTags = new HashSet<>();
private void toggleTag(int npcId)
{
boolean removed = npcTags.remove(npcId);
if (!removed)
npcTags.add(npcId);
}
private boolean hotKeyPressed = false;
@Provides
NpcIndicatorsConfig provideConfig(ConfigManager configManager)
@@ -144,12 +138,14 @@ public class NpcIndicatorsPlugin extends Plugin
private List<String> getHighlights()
{
String configNpcs = config.getNpcToHighlight().toLowerCase();
if (configNpcs.isEmpty())
return Collections.emptyList();
final String configNpcs = config.getNpcToHighlight().toLowerCase();
List<String> highlightedNpcs = Arrays.asList(configNpcs.split(DELIMITER_REGEX));
return highlightedNpcs;
if (configNpcs.isEmpty())
{
return Collections.emptyList();
}
return COMMA_SPLITTER.splitToList(configNpcs);
}
/**
@@ -168,6 +164,12 @@ public class NpcIndicatorsPlugin extends Plugin
continue;
}
if (npcTags.contains(npc.getIndex()))
{
highlightedNpcs.add(npc);
continue;
}
for (String highlight : highlights)
{
if (WildcardMatcher.matches(highlight, npcName))
@@ -193,16 +195,41 @@ public class NpcIndicatorsPlugin extends Plugin
public void onMenuObjectClicked(MenuOptionClicked click)
{
if (click.getMenuOption().equals(TAG))
toggleTag(click.getId());
{
final int id = click.getId();
final boolean removed = npcTags.remove(id);
final NPC[] cachedNPCs = client.getCachedNPCs();
final NPC npc = cachedNPCs[id];
if (npc != null && npc.getName() != null)
{
if (removed)
{
highlightedNpcs.remove(npc);
}
else
{
npcTags.add(id);
highlightedNpcs.add(npc);
}
}
}
}
@Subscribe
public void onNpcSpawned(NpcSpawned npcSpawned)
{
NPC npc = npcSpawned.getNpc();
String npcName = npc.getName();
final NPC npc = npcSpawned.getNpc();
final String npcName = npc.getName();
if (npcName != null)
{
if (npcTags.contains(npc.getIndex()))
{
highlightedNpcs.add(npc);
return;
}
for (String highlight : highlights)
{
if (WildcardMatcher.matches(highlight, npcName))
@@ -217,8 +244,7 @@ public class NpcIndicatorsPlugin extends Plugin
@Subscribe
public void onNpcDespawned(NpcDespawned npcDespawned)
{
NPC npc = npcDespawned.getNpc();
highlightedNpcs.remove(npc);
highlightedNpcs.remove(npcDespawned.getNpc());
}
@Override

View File

@@ -61,16 +61,6 @@ public class NpcMinimapOverlay extends Overlay
renderNpcOverlay(graphics, npc, npc.getName(), config.getHighlightColor());
}
NPC[] npcs = client.getCachedNPCs();
for (int npcId : plugin.getNpcTags())
{
NPC npc = npcs[npcId];
if (npc != null && npc.getName() != null)
{
renderNpcOverlay(graphics, npc, npc.getName(), config.getHighlightColor());
}
}
return null;
}