NPC Highlighter

AdvancedOverlay

Highlights specific NPCs on the screen. Demonstrates NPC iteration, filtering, and overlay rendering with shapes.

18 min read

What It Does

Highlights specific NPCs on the screen. Demonstrates NPC iteration, filtering, and overlay rendering with shapes.

Source Code

java
1package com.example;
2
3import net.runelite.api.Client;
4import net.runelite.api.NPC;
5import net.runelite.api.events.NpcSpawned;
6import net.runelite.api.events.NpcDespawned;
7import net.runelite.client.eventbus.Subscribe;
8import net.runelite.client.plugins.Plugin;
9import net.runelite.client.plugins.PluginDescriptor;
10import net.runelite.client.ui.overlay.Overlay;
11import net.runelite.client.ui.overlay.OverlayManager;
12import net.runelite.client.ui.overlay.OverlayLayer;
13import javax.inject.Inject;
14import java.awt.*;
15import java.util.HashSet;
16import java.util.Set;
17
18@PluginDescriptor(
19    name = "NPC Highlighter",
20    description = "Highlights NPCs on screen"
21)
22public class NPCHighlighterPlugin extends Plugin {
23    @Inject
24    private Client client;
25
26    @Inject
27    private OverlayManager overlayManager;
28
29    // Track interesting NPCs using events (much better performance)
30    private final Set<NPC> highlightedNPCs = new HashSet<>();
31
32    @Subscribe
33    public void onNpcSpawned(NpcSpawned event) {
34        NPC npc = event.getNpc();
35        if (npc.getName() != null && npc.getName().contains("Goblin")) {
36            highlightedNPCs.add(npc);
37        }
38    }
39
40    @Subscribe
41    public void onNpcDespawned(NpcDespawned event) {
42        highlightedNPCs.remove(event.getNpc());
43    }
44
45    private final Overlay overlay = new Overlay() {
46        @Override
47        public Dimension render(Graphics2D graphics) {
48            // Only render tracked NPCs (much better performance)
49            for (NPC npc : highlightedNPCs) {
50                // Use Perspective to get NPC's model center point
51                net.runelite.api.coords.LocalPoint localPoint = 
52                    net.runelite.api.coords.LocalPoint.fromWorld(client, npc.getWorldLocation());
53                if (localPoint != null) {
54                    Point screenPoint = net.runelite.api.Perspective
55                        .localToCanvas(client, localPoint, client.getPlane());
56                    if (screenPoint != null) {
57                        // Draw highlight
58                        graphics.setColor(new Color(255, 0, 0, 100));
59                        graphics.fillOval(screenPoint.x - 20, screenPoint.y - 20, 40, 40);
60                        graphics.setColor(Color.RED);
61                        graphics.drawOval(screenPoint.x - 20, screenPoint.y - 20, 40, 40);
62                    }
63                }
64            }
65            return null;
66        }
67    };
68
69    {
70        overlay.setLayer(OverlayLayer.ABOVE_SCENE);
71    }
72
73    @Override
74    protected void startUp() {
75        overlayManager.add(overlay);
76    }
77
78    @Override
79    protected void shutDown() {
80        overlayManager.remove(overlay);
81    }
82}

Code Annotations

Line 30

Iterate through all NPCs in the game

Line 31

Filter NPCs by name or other criteria

Line 34

Get the NPC's position on screen

Line 36

Draw a highlight shape around the NPC

API Classes Used

ClientNPC
NpcSpawned
NpcDespawned
Overlay

Key Concepts

  • Iterate through all NPCs in the game
  • Filter NPCs by name or other criteria
  • Get the NPC's position on screen
  • Draw a highlight shape around the NPC

Next Steps

  • Learn about NPC API
  • Explore overlay rendering techniques
  • Add configurable NPC names