|
24 | 24 | public class EventSubscribers { |
25 | 25 | public static FlipData flipData = null; |
26 | 26 | 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) |
28 | 28 | 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 | | - }; |
40 | 29 | 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 | + } |
41 | 48 |
|
42 | 49 | @Subscribe |
43 | 50 | public void WriteToChat(OnWriteToChatReceive command){ |
@@ -109,21 +116,10 @@ public void onReceiveCommand(ReceiveCommand event){ |
109 | 116 |
|
110 | 117 | @Subscribe |
111 | 118 | public void onCountdownReceive(OnCountdownReceive event){ |
112 | | - countdown = event.CountdownData.getDuration(); |
113 | 119 | countdownData = event.CountdownData; |
| 120 | + // Calculate the target expiry time based on current time + duration |
| 121 | + countdownExpiryTime = System.currentTimeMillis() + (long)(event.CountdownData.getDuration() * 1000); |
114 | 122 | 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); |
127 | 123 | } |
128 | 124 |
|
129 | 125 | @Subscribe |
|
0 commit comments