Skip to content

Commit dca14b8

Browse files
committed
Made the info bar actually work.
Refactored time loop logic to enhance clarity and reliability, particularly for ticks and time-of-day tracking. Updated commands to reflect changes, such as allowing loop type modifications and enabling/disabling loop info visibility. Added and exposed a method to retrieve boss bar percentage for better modularity.
1 parent f0b12d1 commit dca14b8

3 files changed

Lines changed: 53 additions & 19 deletions

File tree

src/main/java/com/vltno/timeloop/Commands.java

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandR
7171
.then(CommandManager.argument("loopType", new LoopTypesArgumentType())
7272
.executes(context -> {
7373
LoopTypes newLoopType = LoopTypesArgumentType.getLoopType(context, "loopType");
74+
mod.loopType = newLoopType;
75+
mod.config.loopType = newLoopType;
76+
mod.config.save();
77+
7478
context.getSource().sendMessage(Text.literal("Looping type is set to: " + newLoopType.asString()));
7579
LOGGER.info("Loop type set to {}", newLoopType.asString());
7680
return 1;
@@ -135,10 +139,10 @@ public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandR
135139

136140
// TOGGLES
137141
.then(CommandManager.literal("toggles")
138-
.then(CommandManager.literal("loopTimeOfDay")
142+
.then(CommandManager.literal("trackTimeOfDay")
139143
.requires(source -> source.hasPermissionLevel(2))
140144
.executes(context -> {
141-
context.getSource().sendMessage(Text.literal("Looping time of day is set to: " + mod.trackTimeOfDay));
145+
context.getSource().sendMessage(Text.literal("Tracking time of day is set to: " + mod.trackTimeOfDay));
142146
return 1;
143147
})
144148
.then(CommandManager.argument("value", BoolArgumentType.bool())
@@ -147,7 +151,7 @@ public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandR
147151
mod.trackTimeOfDay = newTrackTimeOfDay;
148152
mod.config.trackTimeOfDay = newTrackTimeOfDay;
149153
mod.config.save();
150-
154+
151155
context.getSource().sendMessage(Text.literal("Tracking time of day is set to: " + newTrackTimeOfDay));
152156
LOGGER.info("Tracking time of day set to {}", newTrackTimeOfDay);
153157
return 1;
@@ -185,6 +189,8 @@ public void register(CommandDispatcher<ServerCommandSource> dispatcher, CommandR
185189
mod.showLoopInfo = newShowLoopInfo;
186190
mod.config.showLoopInfo = newShowLoopInfo;
187191
mod.config.save();
192+
193+
mod.loopBossBar.visible(newShowLoopInfo);
188194

189195
context.getSource().sendMessage(Text.literal("Showing loop info is set to: " + newShowLoopInfo));
190196
LOGGER.info("Show loop info set to {}", newShowLoopInfo);

src/main/java/com/vltno/timeloop/LoopBossBar.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ public void setBossBarName(String bossBarName) {
2828
public void setBossBarPercentage(int whole, int part) {
2929
bossBar.setPercent(1.0f - ((float) part / whole));
3030
}
31+
32+
public float getBossBarPercentage() {
33+
return bossBar.getPercent();
34+
}
3135

3236
public void addPlayer(ServerPlayerEntity player) {
3337
LOGGER.info("Adding player: {}", player.getName().getString());

src/main/java/com/vltno/timeloop/TimeLoop.java

Lines changed: 40 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,11 @@
2323
public class TimeLoop implements ModInitializer {
2424
public static final Logger LOOP_LOGGER = LoggerFactory.getLogger("TimeLoop");
2525
private Commands commands;
26-
private LoopBossBar loopBossBar;
2726
private static MinecraftServer server;
2827
private ServerWorld serverWorld;
2928

29+
public LoopBossBar loopBossBar;
30+
3031
// These fields will be initialized from the configuration file.
3132
public int loopIteration;
3233
public int loopTicks;
@@ -69,9 +70,8 @@ public void onInitialize() {
6970
loopBossBar = new LoopBossBar();
7071

7172
EntitySleepEvents.STOP_SLEEPING.register((entity, sleepingPos) -> {
72-
LOOP_LOGGER.info("Loop type: " + loopType);
7373
if (entity.isPlayer() && (loopType == LoopTypes.SLEEP) ) {
74-
LOOP_LOGGER.info("PLAYER SLEPT, LOOPING");
74+
LOOP_LOGGER.info("Player slept, looping.");
7575
runLoopIteration();
7676
}
7777
});
@@ -98,9 +98,9 @@ public void onInitialize() {
9898
TimeLoop.server = server;
9999
this.serverWorld = server.getOverworld();
100100

101-
// loop bossbar info
101+
// Loop boss bar info
102102
String loopInfo = (loopType == LoopTypes.TICKS ? "Ticks Left: " + loopTicks : loopType == LoopTypes.TIME_OF_DAY ? "Time left: " + (timeOfDay - timeSetting) : "");
103-
loopBossBar.visible(showLoopInfo);
103+
loopBossBar.visible(false);
104104
loopBossBar.setBossBarName(loopInfo);
105105

106106
// set mocap settings
@@ -163,19 +163,43 @@ public void onInitialize() {
163163

164164
ServerTickEvents.END_SERVER_TICK.register(server -> {
165165
if (loopType == LoopTypes.SLEEP || loopType == LoopTypes.DEATH) { return; }
166-
if (loopType == LoopTypes.TIME_OF_DAY) { timeOfDay = serverWorld.getTimeOfDay(); };
166+
167167
if (isLooping) {
168-
tickCounter++;
169-
ticksLeft = loopTicks - tickCounter;
170-
loopBossBar.visible(showLoopInfo);
171-
if (showLoopInfo && (loopType == LoopTypes.TICKS || loopType == LoopTypes.TIME_OF_DAY)) {
172-
loopBossBar.setBossBarName(loopType == LoopTypes.TICKS ? "Ticks Left: " + ticksLeft : loopType == LoopTypes.TIME_OF_DAY ? "Time left: " + (timeOfDay - timeSetting) : "");
173-
loopBossBar.setBossBarPercentage(loopTicks, tickCounter);
168+
if (loopType == LoopTypes.TIME_OF_DAY) {
169+
LOOP_LOGGER.info("TimeOfDay: {}", serverWorld.getTimeOfDay());
170+
timeOfDay = serverWorld.getTimeOfDay();
171+
172+
long timeLeft = (timeOfDay > timeSetting) ? Math.abs(serverWorld.getTimeOfDay() - (2 * timeSetting)) : Math.abs(timeOfDay - timeSetting);
173+
174+
LOOP_LOGGER.info("Time Left: {}", timeLeft);
175+
176+
LOOP_LOGGER.info("Time setting: {}", timeSetting);
177+
178+
LOOP_LOGGER.info("Time setting - timeleft: {}", timeSetting - timeLeft);
179+
180+
if (showLoopInfo) {
181+
loopBossBar.setBossBarName("Time Left: " + timeLeft);
182+
loopBossBar.setBossBarPercentage((int)timeSetting, (int)(timeSetting - timeLeft));
183+
}
184+
185+
if (timeSetting - timeLeft == timeSetting) {
186+
runLoopIteration();
187+
}
174188
}
175-
if (tickCounter >= loopTicks || ( timeOfDay == timeSetting && (loopType == LoopTypes.TIME_OF_DAY)) ) {
176-
tickCounter = 0; // Reset counter
177-
ticksLeft = loopTicks; // Reset
178-
runLoopIteration();
189+
190+
else if (loopType == LoopTypes.TICKS) {
191+
tickCounter++;
192+
ticksLeft = loopTicks - tickCounter;
193+
if (showLoopInfo) {
194+
loopBossBar.setBossBarName("Ticks Left: " + ticksLeft);
195+
loopBossBar.setBossBarPercentage(loopTicks, tickCounter);
196+
}
197+
198+
if (tickCounter >= loopTicks) {
199+
tickCounter = 0; // Reset counter
200+
ticksLeft = loopTicks; // Reset
201+
runLoopIteration();
202+
}
179203
}
180204
}
181205
});

0 commit comments

Comments
 (0)