slayer plugin: move target highlight building to use events

This commit is contained in:
Adam
2018-07-07 20:50:22 -04:00
parent c216615d56
commit 62b069eab3
2 changed files with 83 additions and 61 deletions

View File

@@ -32,7 +32,6 @@ import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@@ -54,6 +53,8 @@ import net.runelite.api.events.ConfigChanged;
import net.runelite.api.events.ExperienceChanged;
import net.runelite.api.events.GameStateChanged;
import net.runelite.api.events.GameTick;
import net.runelite.api.events.NpcDespawned;
import net.runelite.api.events.NpcSpawned;
import net.runelite.api.widgets.Widget;
import net.runelite.api.widgets.WidgetInfo;
import net.runelite.client.Notifier;
@@ -159,6 +160,7 @@ public class SlayerPlugin extends Plugin
private int cachedXp;
private Instant infoTimer;
private boolean loginFlag;
private List<String> targetNames = new ArrayList<>();
@Override
protected void startUp() throws Exception
@@ -232,6 +234,23 @@ public class SlayerPlugin extends Plugin
config.slaughter(slaughterChargeCount);
}
@Subscribe
public void onNpcSpawned(NpcSpawned npcSpawned)
{
NPC npc = npcSpawned.getNpc();
if (isTarget(npc))
{
highlightedTargets.add(npc);
}
}
@Subscribe
public void onNpcDespawned(NpcDespawned npcDespawned)
{
NPC npc = npcDespawned.getNpc();
highlightedTargets.remove(npc);
}
@Subscribe
public void onGameTick(GameTick tick)
{
@@ -294,15 +313,6 @@ public class SlayerPlugin extends Plugin
removeCounter();
}
}
if (config.highlightTargets())
{
highlightedTargets = buildTargetsToHighlight();
}
else
{
highlightedTargets.clear();
}
}
@Subscribe
@@ -474,6 +484,57 @@ public class SlayerPlugin extends Plugin
infoTimer = Instant.now();
}
private boolean isTarget(NPC npc)
{
if (targetNames.isEmpty())
{
return false;
}
String name = npc.getName();
if (name == null)
{
return false;
}
name = name.toLowerCase();
for (String target : targetNames)
{
if (name.contains(target))
{
NPCComposition composition = npc.getTransformedComposition();
if (composition != null && Arrays.asList(composition.getActions()).contains("Attack"))
{
return true;
}
}
}
return false;
}
private void rebuildTargetNames(Task task)
{
targetNames.clear();
Arrays.stream(task.getTargetNames())
.map(String::toLowerCase)
.forEach(targetNames::add);
targetNames.add(taskName.toLowerCase().replaceAll("s$", ""));
}
private void rebuildTargetList()
{
highlightedTargets.clear();
for (NPC npc : client.getNpcs())
{
if (isTarget(npc))
{
highlightedTargets.add(npc);
}
}
}
private void setTask(String name, int amt)
{
taskName = name;
@@ -482,6 +543,13 @@ public class SlayerPlugin extends Plugin
removeCounter();
addCounter();
infoTimer = Instant.now();
Task task = Task.getTask(name);
if (task != null)
{
rebuildTargetNames(task);
}
rebuildTargetList();
}
private void addCounter()
@@ -517,57 +585,6 @@ public class SlayerPlugin extends Plugin
counter = null;
}
private List<NPC> buildTargetsToHighlight()
{
if (Strings.isNullOrEmpty(taskName))
return Collections.EMPTY_LIST;
List<NPC> npcs = new ArrayList<>();
List<String> highlightedNpcs = new ArrayList<>(Arrays.asList(Task.getTask(taskName).getTargetNames()));
highlightedNpcs.add(taskName.replaceAll("s$", ""));
for (NPC npc : client.getNpcs())
{
NPCComposition composition = getComposition(npc);
if (composition == null || composition.getName() == null)
continue;
String name = npc.getName();
for (String highlight : highlightedNpcs)
{
if (name.toLowerCase().contains(highlight.toLowerCase())
&& Arrays.asList(composition.getActions()).contains("Attack"))
{
npcs.add(npc);
break;
}
}
}
return npcs;
}
/**
* Get npc composition, account for imposters
*
* @param npc
* @return
*/
private static NPCComposition getComposition(NPC npc)
{
if (npc == null)
return null;
NPCComposition composition = npc.getComposition();
if (composition != null && composition.getConfigs() != null)
{
composition = composition.transform();
}
return composition;
}
//Utils
private String capsString(String str)
{

View File

@@ -55,6 +55,11 @@ public class TargetMinimapOverlay extends Overlay
@Override
public Dimension render(Graphics2D graphics)
{
if (!config.highlightTargets())
{
return null;
}
List<NPC> targets = plugin.getHighlightedTargets();
for (NPC target : targets)
{