-
Notifications
You must be signed in to change notification settings - Fork 1
/
VolcanicMineTrackerPlugin.java
99 lines (76 loc) · 3.12 KB
/
VolcanicMineTrackerPlugin.java
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
package fking.work.vmtracker;
import net.runelite.api.Client;
import net.runelite.api.events.ScriptPostFired;
import net.runelite.api.widgets.Widget;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDescriptor;
import javax.inject.Inject;
@PluginDescriptor(name = "Volcanic Mine Stability Tracker", description = "Tracks the mine stability changes and displays them on the hud.")
public class VolcanicMineTrackerPlugin extends Plugin {
private static final int VARBIT_STABILITY = 5938;
private static final int VARBIT_GAME_STATE = 5941;
private static final int PROC_VOLCANIC_MINE_SET_OTHERINFO = 2022;
private static final int HUD_COMPONENT = 611;
private static final int HUD_STABILITY_COMPONENT = 13;
private static final int GAME_STATE_IN_LOBBY = 1;
private static final int GAME_STATE_IN_GAME = 2;
private static final int STARTING_STABILITY = 50;
private static final int STABILITY_CHANGE_HISTORY_SIZE = 2;
private final int[] stabilityChangeHistory = new int[STABILITY_CHANGE_HISTORY_SIZE];
@Inject
private Client client;
private int changeHistorySize = 0;
private int lastMineStability;
@Subscribe
public void onScriptPostFired(ScriptPostFired event) {
if (event.getScriptId() != PROC_VOLCANIC_MINE_SET_OTHERINFO) {
return;
}
int gameState = client.getVarbitValue(VARBIT_GAME_STATE);
if (gameState == GAME_STATE_IN_LOBBY) {
lastMineStability = STARTING_STABILITY;
changeHistorySize = 0;
}
int stability = client.getVarbitValue(VARBIT_STABILITY);
int delta = stability - lastMineStability;
lastMineStability = stability;
if (delta != 0) {
pushStabilityChangeHistory(delta);
}
updateHudText();
}
private void updateHudText() {
if (changeHistorySize <= 0) {
return;
}
Widget widget = client.getWidget(HUD_COMPONENT, HUD_STABILITY_COMPONENT);
if (widget != null) {
String text = widget.getText();
text += " (" + buildStabilityHistoryText() + ")";
widget.setText(text);
}
}
private String buildStabilityHistoryText() {
StringBuilder builder = new StringBuilder();
for (int i = 0; i < changeHistorySize; i++) {
int delta = stabilityChangeHistory[i];
if (delta >= 0) {
builder.append("<col=00ff00>").append(delta).append("</col>");
} else {
builder.append("<col=ff0000>").append(delta).append("</col>");
}
if (i != changeHistorySize - 1) {
builder.append(", ");
}
}
return builder.toString();
}
private void pushStabilityChangeHistory(int delta) {
System.arraycopy(stabilityChangeHistory, 0, stabilityChangeHistory, 1, stabilityChangeHistory.length - 1);
stabilityChangeHistory[0] = delta;
if (changeHistorySize < STABILITY_CHANGE_HISTORY_SIZE) {
changeHistorySize++;
}
}
}