/
MiningPlugin.java
345 lines (309 loc) · 10.1 KB
/
MiningPlugin.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
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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
/*
* Copyright (c) 2019, Adam <Adam@sigterm.info>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package net.runelite.client.plugins.mining;
import com.google.inject.Provides;
import java.time.Duration;
import java.time.Instant;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import javax.annotation.Nullable;
import javax.inject.Inject;
import lombok.AccessLevel;
import lombok.Getter;
import net.runelite.api.AnimationID;
import net.runelite.api.ChatMessageType;
import net.runelite.api.Client;
import net.runelite.api.GameObject;
import net.runelite.api.GameState;
import net.runelite.api.MenuAction;
import static net.runelite.api.ObjectID.DEPLETED_VEIN_26665;
import static net.runelite.api.ObjectID.DEPLETED_VEIN_26666;
import static net.runelite.api.ObjectID.DEPLETED_VEIN_26667;
import static net.runelite.api.ObjectID.DEPLETED_VEIN_26668;
import static net.runelite.api.ObjectID.EMPTY_WALL;
import static net.runelite.api.ObjectID.ORE_VEIN_26661;
import static net.runelite.api.ObjectID.ORE_VEIN_26662;
import static net.runelite.api.ObjectID.ORE_VEIN_26663;
import static net.runelite.api.ObjectID.ORE_VEIN_26664;
import net.runelite.api.Player;
import net.runelite.api.WallObject;
import net.runelite.api.coords.WorldPoint;
import net.runelite.api.events.AnimationChanged;
import net.runelite.api.events.ChatMessage;
import net.runelite.api.events.GameObjectDespawned;
import net.runelite.api.events.GameObjectSpawned;
import net.runelite.api.events.GameStateChanged;
import net.runelite.api.events.GameTick;
import net.runelite.api.events.WallObjectSpawned;
import net.runelite.client.config.ConfigManager;
import net.runelite.client.eventbus.Subscribe;
import net.runelite.client.events.OverlayMenuClicked;
import net.runelite.client.plugins.Plugin;
import net.runelite.client.plugins.PluginDependency;
import net.runelite.client.plugins.PluginDescriptor;
import net.runelite.client.plugins.xptracker.XpTrackerPlugin;
import net.runelite.client.ui.overlay.OverlayManager;
import net.runelite.client.ui.overlay.OverlayMenuEntry;
@PluginDescriptor(
name = "Mining",
description = "Show mining statistics and ore respawn timers",
tags = {"overlay", "skilling", "timers"},
enabledByDefault = false
)
@PluginDependency(XpTrackerPlugin.class)
public class MiningPlugin extends Plugin
{
private static final int ROCK_DISTANCE = 14;
private static final Pattern MINING_PATERN = Pattern.compile(
"You " +
"(?:manage to|just)" +
" (?:mined?|quarry) " +
"(?:some|an?) " +
"(?:copper|tin|clay|iron|silver|coal|gold|mithril|adamantite|runeite|amethyst|sandstone|granite|Opal|piece of Jade|Red Topaz|Emerald|Sapphire|Ruby|Diamond)" +
"(?:\\.|!)");
@Inject
private Client client;
@Inject
private OverlayManager overlayManager;
@Inject
private MiningOverlay overlay;
@Inject
private MiningRocksOverlay rocksOverlay;
@Inject
private MiningConfig config;
@Getter
@Nullable
private MiningSession session;
@Getter(AccessLevel.PACKAGE)
private final List<RockRespawn> respawns = new ArrayList<>();
private boolean recentlyLoggedIn;
@Getter
@Nullable
private Pickaxe pickaxe;
@Provides
MiningConfig getConfig(ConfigManager configManager)
{
return configManager.getConfig(MiningConfig.class);
}
@Override
protected void startUp()
{
overlayManager.add(overlay);
overlayManager.add(rocksOverlay);
}
@Override
protected void shutDown() throws Exception
{
session = null;
pickaxe = null;
overlayManager.remove(overlay);
overlayManager.remove(rocksOverlay);
respawns.clear();
}
@Subscribe
public void onOverlayMenuClicked(OverlayMenuClicked overlayMenuClicked)
{
OverlayMenuEntry overlayMenuEntry = overlayMenuClicked.getEntry();
if (overlayMenuEntry.getMenuAction() == MenuAction.RUNELITE_OVERLAY
&& overlayMenuClicked.getEntry().getOption().equals(MiningOverlay.MINING_RESET)
&& overlayMenuClicked.getOverlay() == overlay)
{
session = null;
}
}
@Subscribe
public void onGameStateChanged(GameStateChanged event)
{
switch (event.getGameState())
{
case HOPPING:
respawns.clear();
break;
case LOGGED_IN:
// After login rocks that are depleted will be changed,
// so wait for the next game tick before watching for
// rocks to deplete
recentlyLoggedIn = true;
break;
}
}
@Subscribe
public void onAnimationChanged(final AnimationChanged event)
{
Player local = client.getLocalPlayer();
if (event.getActor() != local)
{
return;
}
int animId = local.getAnimation();
if (animId == AnimationID.DENSE_ESSENCE_CHIPPING)
{
// Can't use chat messages to start mining session on Dense Essence as they don't have a chat message when mined,
// so we track the session here instead.
if (session == null)
{
session = new MiningSession();
}
session.setLastMined();
}
else
{
Pickaxe pickaxe = Pickaxe.fromAnimation(animId);
if (pickaxe != null)
{
this.pickaxe = pickaxe;
}
}
}
@Subscribe
public void onGameTick(GameTick gameTick)
{
respawns.removeIf(RockRespawn::isExpired);
recentlyLoggedIn = false;
if (session == null || session.getLastMined() == null)
{
return;
}
if (pickaxe != null && pickaxe.matchesMiningAnimation(client.getLocalPlayer()))
{
session.setLastMined();
return;
}
Duration statTimeout = Duration.ofMinutes(config.statTimeout());
Duration sinceMined = Duration.between(session.getLastMined(), Instant.now());
if (sinceMined.compareTo(statTimeout) >= 0)
{
resetSession();
}
}
public void resetSession()
{
session = null;
pickaxe = null;
}
@Subscribe
public void onGameObjectDespawned(GameObjectDespawned event)
{
if (client.getGameState() != GameState.LOGGED_IN || recentlyLoggedIn)
{
return;
}
final GameObject object = event.getGameObject();
final int region = client.getLocalPlayer().getWorldLocation().getRegionID();
Rock rock = Rock.getRock(object.getId());
if (rock != null)
{
if (rock == Rock.DAEYALT_ESSENCE)
{
final WorldPoint point = object.getWorldLocation();
respawns.removeIf(rockRespawn -> rockRespawn.getWorldPoint().equals(point));
}
else
{
RockRespawn rockRespawn = new RockRespawn(rock, object.getWorldLocation(), Instant.now(), (int) rock.getRespawnTime(region).toMillis(), rock.getZOffset());
respawns.add(rockRespawn);
}
}
}
@Subscribe
public void onGameObjectSpawned(GameObjectSpawned event)
{
if (client.getGameState() != GameState.LOGGED_IN || recentlyLoggedIn)
{
return;
}
GameObject object = event.getGameObject();
Rock rock = Rock.getRock(object.getId());
// Inverse timer to track daeyalt essence active duration
if (rock == Rock.DAEYALT_ESSENCE)
{
final int region = client.getLocalPlayer().getWorldLocation().getRegionID();
RockRespawn rockRespawn = new RockRespawn(rock, object.getWorldLocation(), Instant.now(), (int) rock.getRespawnTime(region).toMillis(), rock.getZOffset());
respawns.add(rockRespawn);
}
// If the Lovakite ore respawns before the timer is up, remove it
else if (rock == Rock.LOVAKITE)
{
final WorldPoint point = object.getWorldLocation();
respawns.removeIf(rockRespawn -> rockRespawn.getWorldPoint().equals(point));
}
}
@Subscribe
public void onWallObjectSpawned(WallObjectSpawned event)
{
if (client.getGameState() != GameState.LOGGED_IN)
{
return;
}
final WallObject object = event.getWallObject();
final int region = client.getLocalPlayer().getWorldLocation().getRegionID();
switch (object.getId())
{
case EMPTY_WALL:
{
Rock rock = Rock.AMETHYST;
RockRespawn rockRespawn = new RockRespawn(rock, object.getWorldLocation(), Instant.now(), (int) rock.getRespawnTime(region).toMillis(), rock.getZOffset());
respawns.add(rockRespawn);
break;
}
case DEPLETED_VEIN_26665: // Depleted motherlode vein
case DEPLETED_VEIN_26666: // Depleted motherlode vein
case DEPLETED_VEIN_26667: // Depleted motherlode vein
case DEPLETED_VEIN_26668: // Depleted motherlode vein
{
Rock rock = Rock.ORE_VEIN;
RockRespawn rockRespawn = new RockRespawn(rock, object.getWorldLocation(), Instant.now(), (int) rock.getRespawnTime(region).toMillis(), rock.getZOffset());
respawns.add(rockRespawn);
break;
}
case ORE_VEIN_26661: // Motherlode vein
case ORE_VEIN_26662: // Motherlode vein
case ORE_VEIN_26663: // Motherlode vein
case ORE_VEIN_26664: // Motherlode vein
{
// If the vein respawns before the timer is up, remove it
final WorldPoint point = object.getWorldLocation();
respawns.removeIf(rockRespawn -> rockRespawn.getWorldPoint().equals(point));
break;
}
}
}
@Subscribe
public void onChatMessage(ChatMessage event)
{
if (event.getType() == ChatMessageType.SPAM || event.getType() == ChatMessageType.GAMEMESSAGE)
{
if (MINING_PATERN.matcher(event.getMessage()).matches())
{
if (session == null)
{
session = new MiningSession();
}
session.setLastMined();
}
}
}
}