-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSpawnDumperPlugin.java
More file actions
283 lines (241 loc) · 8.6 KB
/
SpawnDumperPlugin.java
File metadata and controls
283 lines (241 loc) · 8.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package net.runelite.client.plugins.spawndumper;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import java.awt.image.BufferedImage;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.inject.Inject;
import javax.swing.JButton;
import lombok.AccessLevel;
import lombok.Getter;
import lombok.extern.slf4j.Slf4j;
import net.runelite.api.Client;
import net.runelite.api.MenuAction;
import static net.runelite.api.MenuAction.MENU_ACTION_DEPRIORITIZE_OFFSET;
import net.runelite.api.MenuEntry;
import net.runelite.api.NPC;
import net.runelite.api.NPCComposition;
import net.runelite.api.events.GameTick;
import net.runelite.api.events.MenuEntryAdded;
import net.runelite.api.events.MenuOptionClicked;
import net.runelite.api.events.NpcSpawned;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.plugins.devtools.DevToolsPlugin;
import net.runelite.client.ui.ClientToolbar;
import net.runelite.client.ui.NavigationButton;
import net.runelite.client.ui.overlay.OverlayManager;
import net.runelite.client.util.ColorUtil;
import net.runelite.client.util.ImageUtil;
import net.runelite.client.util.Text;
@PluginDescriptor(
name = "Spawn Dumper"
)
@Slf4j
public class SpawnDumperPlugin extends Plugin
{
// Option added to NPC menu
private static final String TAG = "Spawn Info";
private static final String UNTAG = "Remove Spawn Info";
@Inject
private Client client;
@Inject
private ClientToolbar clientToolbar;
@Inject
private OverlayManager overlayManager;
@Inject
private SpawnDumperOverlay spawnDumperOverlay;
@Inject
private SpawnDumperNPCOverlay spawnDumperNPCOverlay;
@Getter
private SpawnDumperButton enabled;
@Getter
private JButton saveSpawns;
@Getter
private JButton loadSpawns;
@Getter
private JButton clearSpawns;
private NavigationButton navButton;
/**
* A map of {@link NPC#getIndex()} to a {@link NPCSpawn}.
*/
@Getter(AccessLevel.PACKAGE)
private Map<Integer, NPCSpawn> spawns = new HashMap<>();
/**
* Resets every tick, used to show the amount of updates in a single tick.
*/
@Getter(AccessLevel.PACKAGE)
private int updatedThisTick = 0;
/**
* The npc that is set to be highlighted and show information for.
*/
@Getter(AccessLevel.PACKAGE)
private int selectedNPCIndex = -1;
@Override
protected void startUp() throws Exception
{
enabled = new SpawnDumperButton("Enabled");
saveSpawns = new JButton("Save Spawns");
saveSpawns.addActionListener(e ->
{
try
{
Path spawnsPath = Paths.get("spawns.json");
Files.write(spawnsPath, new Gson().toJson(spawns.values()).getBytes());
int points = spawns.values().stream().mapToInt(value -> value.getPoints().size()).sum();
System.out.println("Saved " + spawns.size() + " spawns with " + points + " points to " + spawnsPath + ".");
}
catch (IOException ex)
{
ex.printStackTrace();
}
});
loadSpawns = new JButton("Load Spawns");
loadSpawns.addActionListener(e ->
{
try
{
List<NPCSpawn> tempSpawns = new Gson().fromJson(new InputStreamReader(new FileInputStream("spawns.json")), new TypeToken<List<NPCSpawn>>() {}.getType());
int points = 0;
spawns.clear();
for (NPCSpawn spawn : tempSpawns)
{
points += spawn.getPoints().size();
spawns.put(spawn.getIndex(), spawn);
}
System.out.println("Loaded " + spawns.size() + " spawns with " + points + " points.");
}
catch (FileNotFoundException ex)
{
ex.printStackTrace();
}
});
clearSpawns = new JButton("Clear Spawns");
clearSpawns.addActionListener(e -> spawns.clear());
final SpawnDumperPanel panel = injector.getInstance(SpawnDumperPanel.class);
final BufferedImage icon = ImageUtil.getResourceStreamFromClass(DevToolsPlugin.class, "devtools_icon.png");
overlayManager.add(spawnDumperOverlay);
overlayManager.add(spawnDumperNPCOverlay);
navButton = NavigationButton.builder()
.tooltip("Spawn Dumper")
.icon(icon)
.priority(1)
.panel(panel)
.build();
clientToolbar.addNavigation(navButton);
}
@Override
protected void shutDown() throws Exception
{
overlayManager.remove(spawnDumperOverlay);
overlayManager.remove(spawnDumperNPCOverlay);
clientToolbar.removeNavigation(navButton);
}
@Subscribe
public void onMenuEntryAdded(MenuEntryAdded event)
{
int type = event.getType();
if (type >= MENU_ACTION_DEPRIORITIZE_OFFSET)
{
type -= MENU_ACTION_DEPRIORITIZE_OFFSET;
}
if (type == MenuAction.EXAMINE_NPC.getId())
{
// Add tag option
MenuEntry[] menuEntries = client.getMenuEntries();
menuEntries = Arrays.copyOf(menuEntries, menuEntries.length + 1);
final MenuEntry tagEntry = menuEntries[menuEntries.length - 1] = new MenuEntry();
tagEntry.setOption(event.getIdentifier() == selectedNPCIndex ? UNTAG : TAG);
tagEntry.setTarget(event.getTarget());
tagEntry.setParam0(event.getActionParam0());
tagEntry.setParam1(event.getActionParam1());
tagEntry.setIdentifier(event.getIdentifier());
tagEntry.setType(MenuAction.RUNELITE.getId());
client.setMenuEntries(menuEntries);
}
}
@Subscribe
public void onMenuOptionClicked(MenuOptionClicked click)
{
if (click.getMenuAction() != MenuAction.RUNELITE ||
!(click.getMenuOption().equals(TAG) || click.getMenuOption().equals(UNTAG)))
{
return;
}
final int id = click.getId();
if (selectedNPCIndex == id)
{
selectedNPCIndex = -1;
}
else
{
selectedNPCIndex = id;
}
click.consume();
}
@Subscribe
public void onNpcSpawned(NpcSpawned event)
{
// check if the dumper is active
if (!enabled.isActive())
{
return;
}
NPC npc = event.getNpc();
NPCComposition def = client.getNpcDefinition(event.getNpc().getId());
NPCSpawn existSpawn = spawns.get(npc.getIndex());
if (existSpawn != null && existSpawn.getNpc() == npc.getId())
{
// spawn exists and npc ids are the same
return;
}
NPCSpawn spawn = new NPCSpawn(npc.getId(), npc.getIndex());
spawn.setOrientation(npc.getOrientation());
spawn.getPoints().add(npc.getWorldLocation());
spawns.put(npc.getIndex(), spawn);
if (existSpawn != null)
{
log.debug("Replaced " + existSpawn.getNpc() + " with " + spawn.getNpc() + " due to same index but different ids.");
}
else
{
log.debug("Added new NPC to spawns: index=" + npc.getIndex() + ", id=" + npc.getId() + ", name=" + def.getName() + "");
}
}
@Subscribe
public void onGameTick(GameTick event)
{
updatedThisTick = 0;
// check if the dumper is active
if (!enabled.isActive())
{
return;
}
for (NPC npc : client.getNpcs())
{
NPCSpawn spawn = spawns.get(npc.getIndex());
if (spawn == null)
{
continue;
}
if (spawn.getOrientation() != -1 && npc.getOrientation() != spawn.getOrientation())
{
spawn.setOrientation(-1);
}
if (spawn.getPoints().add(npc.getWorldLocation()))
{
updatedThisTick++;
}
}
}
}