11package com .vltno .timeloop ;
22
3+ import com .google .gson .JsonArray ;
4+ import com .google .gson .JsonObject ;
5+ import com .google .gson .JsonParser ;
36import net .fabricmc .api .ModInitializer ;
47import net .fabricmc .fabric .api .command .v2 .ArgumentTypeRegistry ;
58import net .fabricmc .fabric .api .command .v2 .CommandRegistrationCallback ;
1720import net .minecraft .util .WorldSavePath ;
1821import org .slf4j .Logger ;
1922import org .slf4j .LoggerFactory ;
23+
24+ import java .io .IOException ;
25+ import java .nio .file .Files ;
2026import java .nio .file .Path ;
2127import java .util .ArrayList ;
2228import java .util .List ;
@@ -43,6 +49,7 @@ public class TimeLoop implements ModInitializer {
4349 private List <String > recordingPlayers ; // Add this field
4450
4551 public boolean showLoopInfo ;
52+ public boolean displayTimeInTicks ;
4653 public boolean trackItems ;
4754 public LoopTypes loopType ;
4855
@@ -93,6 +100,7 @@ public void onInitialize() {
93100 sceneName = config .sceneName ;
94101
95102 showLoopInfo = config .showLoopInfo ;
103+ displayTimeInTicks = config .displayTimeInTicks ;
96104 trackItems = config .trackItems ;
97105 loopType = config .loopType ;
98106
@@ -181,11 +189,7 @@ public void onInitialize() {
181189 long time = serverWorld .getTimeOfDay ();
182190 long timeLeft = (time > timeSetting ) ? Math .abs (serverWorld .getTimeOfDay () - (2 * timeSetting )) : Math .abs (startTimeOfDay - timeSetting );
183191
184- if (showLoopInfo && isLooping ) {
185- loopBossBar .setBossBarName ("Time Left: " + timeLeft );
186- loopBossBar .setBossBarPercentage ((int )timeSetting , (int )(timeSetting - timeLeft ));
187- }
188-
192+ updateProgressBar ((int )timeSetting , (int )(timeSetting - timeLeft ));
189193 if (timeSetting - timeLeft == timeSetting ) {
190194 runLoopIteration ();
191195 }
@@ -194,11 +198,8 @@ public void onInitialize() {
194198 else if (loopType == LoopTypes .TICKS ) {
195199 tickCounter ++;
196200 ticksLeft = loopLengthTicks - tickCounter ;
197- if (showLoopInfo && isLooping ) {
198- loopBossBar .setBossBarName ("Ticks Left: " + ticksLeft );
199- loopBossBar .setBossBarPercentage (loopLengthTicks , tickCounter );
200- }
201201
202+ updateProgressBar (loopLengthTicks , ticksLeft );
202203 if (tickCounter >= loopLengthTicks ) {
203204 tickCounter = 0 ; // Reset counter
204205 ticksLeft = loopLengthTicks ; // Reset
@@ -291,6 +292,15 @@ public void stopLoop() {
291292 }
292293 }
293294
295+ public void updateProgressBar (int time , int timeLeft ) {
296+ if (showLoopInfo && isLooping ) {
297+ if (displayTimeInTicks ) {loopBossBar .setBossBarName ("Time Left: " + timeLeft );}
298+ else {loopBossBar .setBossBarName ("Time Left: " + convertTicksToTime (timeLeft ));}
299+
300+ loopBossBar .setBossBarPercentage (time , timeLeft );
301+ }
302+ }
303+
294304 /**
295305 * Executes a minecraft chat command.
296306 */
@@ -343,11 +353,11 @@ private void removeOldSceneEntries() {
343353 if (sceneFile .toFile ().exists ()) {
344354 try {
345355 // Load the scene data from the file
346- String jsonContent = new String (java . nio . file . Files .readAllBytes (sceneFile ));
356+ String jsonContent = new String (Files .readAllBytes (sceneFile ));
347357
348358 // Parse the content
349- com . google . gson . JsonObject jsonObject = com . google . gson . JsonParser .parseString (jsonContent ).getAsJsonObject ();
350- com . google . gson . JsonArray subScenes = jsonObject .getAsJsonArray ("subscenes" );
359+ JsonObject jsonObject = JsonParser .parseString (jsonContent ).getAsJsonObject ();
360+ JsonArray subScenes = jsonObject .getAsJsonArray ("subscenes" );
351361
352362 // Check if we have more scenes than maxLoops
353363 if (subScenes .size () > maxLoops ) {
@@ -362,10 +372,10 @@ private void removeOldSceneEntries() {
362372 jsonObject .add ("subScenes" , subScenes );
363373
364374 // Write the updated JSON back to the file
365- java . nio . file . Files .write (sceneFile , jsonObject .toString ().getBytes ());
375+ Files .write (sceneFile , jsonObject .toString ().getBytes ());
366376 LOOP_LOGGER .info ("Removed old scene entries to maintain maxLoops: {}" , maxLoops );
367377 }
368- } catch (java . io . IOException e ) {
378+ } catch (IOException e ) {
369379 LOOP_LOGGER .error ("Failed to read or write scene file: {}" , sceneFile , e );
370380 }
371381 } else {
@@ -388,6 +398,19 @@ public void updateEntitiesToTrack(boolean items) {
388398 executeCommand (String .format ("mocap settings playback record_entities %s" , entitiesToTrack ));
389399 executeCommand (String .format ("mocap settings playback play_entities %s" , entitiesToTrack ));
390400 }
401+
402+ /**
403+ * Converts time in ticks to HH:MM:SS
404+ *
405+ * @param ticksLeft A int value.
406+ */
407+ public String convertTicksToTime (int ticksLeft ) {
408+ int timeLeft = ticksLeft / 20 ;
409+ int hours = timeLeft / 3600 ;
410+ int minutes = (timeLeft % 3600 ) / 60 ;
411+ int seconds = timeLeft % 60 ;
412+ return String .format ("%02d:%02d:%02d" , hours , minutes , seconds );
413+ }
391414}
392415
393416// use this in the future
0 commit comments