-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathGauntletTimer.java
More file actions
232 lines (193 loc) · 9.13 KB
/
GauntletTimer.java
File metadata and controls
232 lines (193 loc) · 9.13 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
/*
* Copyright (c) 2018, Seth <http://github.com/sethtroll>
* 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.gauntlet;
import net.runelite.api.ChatMessageType;
import net.runelite.api.Client;
import net.runelite.api.Player;
import net.runelite.client.ui.overlay.Overlay;
import net.runelite.client.ui.overlay.OverlayMenuEntry;
import net.runelite.client.ui.overlay.OverlayPosition;
import net.runelite.client.ui.overlay.OverlayPriority;
import net.runelite.client.ui.overlay.components.LineComponent;
import net.runelite.client.ui.overlay.components.PanelComponent;
import net.runelite.client.ui.overlay.components.TitleComponent;
import javax.inject.Inject;
import java.awt.*;
import static net.runelite.api.MenuAction.RUNELITE_OVERLAY_CONFIG;
import static net.runelite.client.ui.overlay.OverlayManager.OPTION_CONFIGURE;
class GauntletTimer extends Overlay {
private final Client client;
private final GauntletPlugin plugin;
private final GauntletConfig config;
private final PanelComponent panelComponent = new PanelComponent();
public enum RaidState {
UNKNOWN, IN_RAID, IN_BOSS;
}
public long timeRaidStart = -1L;
public long timeBossEnter = -1L;
public RaidState currentState = RaidState.UNKNOWN;
/**
* Resets the timer.
*/
public void resetStates() {
timeRaidStart = -1L;
timeBossEnter = -1L;
currentState = RaidState.UNKNOWN;
}
/**
* This is called when the player resets the plugin mid-raid. We do not want to confuse the timer.
* <p>
* TODO: Originally, this function will disable the timer if the plugin is started mid raid.
* Unfortunately, VARBITS can't be checked unless you're on the client thread.
* I've no idea how to access RL's task handler.
* Good luck to you. If you restart plugin mid raid, oh well. Your timer's going to be inaccurate.
*/
public void initStates() {
timeRaidStart = -1L;
timeBossEnter = -1L;
if (GauntletUtils.inRaid(client)) {
currentState = RaidState.IN_RAID;
if (GauntletUtils.inBoss(client)) {
currentState = RaidState.IN_BOSS;
}
} else
currentState = RaidState.UNKNOWN;
}
/**
* Converts the different between two epoch times into minutes:seconds format.
*
* @param epochA long
* @param epochB long
* @return String
*/
private String calculateElapsedTime(long epochA, long epochB) {
long max = Math.max(epochA, epochB);
long min = Math.min(epochA, epochB);
long elapsedEpoch = max - min;
long seconds = elapsedEpoch / 1000L;
long minutes = seconds / 60L;
seconds = seconds % 60;
if (seconds == 0) {
return minutes + ":00";
}
if (seconds < 10) {
return minutes + ":0" + seconds;
}
return minutes + ":" + seconds;
}
/**
* Called when varbit changes. See if the the raid state has changed.
*/
public void checkStates(boolean checkVarps) {
final Player p = client.getLocalPlayer();
if (p == null || !plugin.completeStartup)
return;
if (checkVarps) {
if (currentState == RaidState.UNKNOWN) {
if (GauntletUtils.inRaid(client) && p.getHealthRatio() != 0) { // Player has started a new raid.
if (!GauntletUtils.inBoss(client)) {
currentState = RaidState.IN_RAID;
timeRaidStart = System.currentTimeMillis();
} else {
currentState = RaidState.IN_RAID;
timeRaidStart = timeBossEnter = System.currentTimeMillis();
}
}
} else if (currentState == RaidState.IN_RAID) {
if (GauntletUtils.inRaid(client)) {
if (GauntletUtils.inBoss(client)) { // Player has begun the boss fight.
printPrepTime();
currentState = RaidState.IN_BOSS;
timeBossEnter = System.currentTimeMillis();
}
} else { // Player has died or left the raid.
printPrepTime();
resetStates();
}
} else if (currentState == RaidState.IN_BOSS) {
if (!GauntletUtils.inBoss(client) || !GauntletUtils.inRaid(client)) { // Player has killed the boss.
resetStates();
}
}
} else {
if (currentState == RaidState.IN_BOSS) {
if (p.getHealthRatio() == 0) { // Boss has killed the player.
printBossTime();
resetStates();
}
}
}
}
private void printPrepTime() {
if (!config.displayTimerChat() || timeRaidStart == -1L)
return;
String elapsedTime = calculateElapsedTime(System.currentTimeMillis(), timeRaidStart);
this.client.addChatMessage(ChatMessageType.GAMEMESSAGE, "", "Preparation time: <col=ff0000>" + elapsedTime + "<col=000000>.", null);
}
private void printBossTime() {
if (!config.displayTimerChat() || timeRaidStart == -1L || timeBossEnter == -1L)
return;
String elapsedBossTime = calculateElapsedTime(System.currentTimeMillis(), timeBossEnter);
String elapsedPrepTime = calculateElapsedTime(timeRaidStart, timeBossEnter);
String elapsedTotalTime = calculateElapsedTime(System.currentTimeMillis(), timeRaidStart);
this.client.addChatMessage(ChatMessageType.GAMEMESSAGE, "", "Challenge duration: <col=ff0000>" + elapsedTotalTime + "<col=000000>.", null);
this.client.addChatMessage(ChatMessageType.GAMEMESSAGE, "", "Preparation time: <col=ff0000>" + elapsedPrepTime + "<col=000000>. Player death time: <col=ff0000>" + elapsedBossTime + "<col=000000>.", null);
}
@Inject
public GauntletTimer(Client client, GauntletPlugin plugin, GauntletConfig config) {
super(plugin);
setPosition(OverlayPosition.ABOVE_CHATBOX_RIGHT);
setPriority(OverlayPriority.HIGH);
this.client = client;
this.plugin = plugin;
this.config = config;
getMenuEntries().add(new OverlayMenuEntry(RUNELITE_OVERLAY_CONFIG, OPTION_CONFIGURE, "Gauntlet Timer Overlay"));
}
@Override
public Dimension render(Graphics2D graphics) {
if (currentState == RaidState.UNKNOWN) {
return null;
}
panelComponent.getChildren().clear();
panelComponent.getChildren().add(TitleComponent.builder().text("Gauntlet Timer").color(Color.WHITE).build());
if (timeRaidStart == -1L) { // User restarted the plugin mid raid. Timer is inaccurate.
panelComponent.getChildren().add(LineComponent.builder().left("Inactive").right("0:00").build());
} else {
String elapsedPrepTime, elapsedBossTime, elapsedTotalTime;
elapsedTotalTime = calculateElapsedTime(System.currentTimeMillis(), timeRaidStart);
if (currentState == RaidState.IN_RAID) {
elapsedPrepTime = calculateElapsedTime(timeRaidStart, System.currentTimeMillis());
elapsedBossTime = "0:00";
} else {
elapsedPrepTime = calculateElapsedTime(timeRaidStart, timeBossEnter);
elapsedBossTime = calculateElapsedTime(System.currentTimeMillis(), timeBossEnter);
}
panelComponent.getChildren().add(LineComponent.builder().left("Preparation").right(elapsedPrepTime).build());
panelComponent.getChildren().add(LineComponent.builder().left("Boss Fight").right(elapsedBossTime).build());
panelComponent.getChildren().add(LineComponent.builder().left("Total Time").right(elapsedTotalTime).build());
}
return panelComponent.render(graphics);
}
}