Skip to content

Commit 10a78ce

Browse files
committed
fix timer rendering
1 parent 510bdb3 commit 10a78ce

File tree

2 files changed

+22
-26
lines changed

2 files changed

+22
-26
lines changed

src/client/java/com/coflnet/CoflModClient.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -346,7 +346,7 @@ public void onInitializeClient() {
346346
RenderUtils.drawStringWithShadow(
347347
drawContext,
348348
EventSubscribers.countdownData.getPrefix()
349-
+ getStringFromDouble(EventSubscribers.countdown, EventSubscribers.countdownData.getMaxPrecision()),
349+
+ getStringFromDouble(EventSubscribers.getCountdown(), EventSubscribers.countdownData.getMaxPrecision()),
350350
x,
351351
y,
352352
0xFFFFFFFF, EventSubscribers.countdownData.getScale());

src/client/java/com/coflnet/EventSubscribers.java

Lines changed: 21 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,27 @@
2424
public class EventSubscribers {
2525
public static FlipData flipData = null;
2626
public static Countdown countdownData = null;
27-
public static float countdown = 0.0f;
27+
public static long countdownExpiryTime = 0L; // Target time when countdown expires (in milliseconds)
2828
public static boolean showCountdown = false;
29-
public static Timer timer = new Timer();
30-
public static TimerTask task = new TimerTask() {
31-
@Override
32-
public void run() {
33-
countdown -= 0.1f;
34-
if(countdown < 0.0) {
35-
showCountdown = false;
36-
timer.cancel();
37-
}
38-
}
39-
};
4029
public static List<Position> positions = null;
30+
31+
/**
32+
* Gets the current remaining countdown time in seconds.
33+
* Calculates based on the difference between the target expiry time and current time.
34+
*/
35+
public static float getCountdown() {
36+
if (!showCountdown || countdownExpiryTime == 0L) {
37+
return 0.0f;
38+
}
39+
40+
long remainingMs = countdownExpiryTime - System.currentTimeMillis();
41+
if (remainingMs <= 0) {
42+
showCountdown = false;
43+
return 0.0f;
44+
}
45+
46+
return remainingMs / 1000.0f;
47+
}
4148

4249
@Subscribe
4350
public void WriteToChat(OnWriteToChatReceive command){
@@ -109,21 +116,10 @@ public void onReceiveCommand(ReceiveCommand event){
109116

110117
@Subscribe
111118
public void onCountdownReceive(OnCountdownReceive event){
112-
countdown = event.CountdownData.getDuration();
113119
countdownData = event.CountdownData;
120+
// Calculate the target expiry time based on current time + duration
121+
countdownExpiryTime = System.currentTimeMillis() + (long)(event.CountdownData.getDuration() * 1000);
114122
showCountdown = true;
115-
116-
timer = new Timer();
117-
timer.scheduleAtFixedRate(new TimerTask() {
118-
@Override
119-
public void run() {
120-
countdown -= 0.1f;
121-
if(countdown < 0.0) {
122-
showCountdown = false;
123-
timer.cancel();
124-
}
125-
}
126-
}, 1, 100);
127123
}
128124

129125
@Subscribe

0 commit comments

Comments
 (0)