Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,31 @@ public class BossBarService {

private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(8);

// Configurable update interval in milliseconds (used for scheduling progress updates)
private final long updateIntervalMillis;

// Default constructor: reads system property 'multification.bossbar.update-interval-ms' or falls back to 100ms
public BossBarService() {
this(getUpdateIntervalFromSystemProperty());
}

// Constructor allowing explicit configuration (useful for tests or programmatic configuration)
public BossBarService(long updateIntervalMillis) {
this.updateIntervalMillis = Math.max(1L, updateIntervalMillis);
}

private static long getUpdateIntervalFromSystemProperty() {
final String key = "multification.bossbar.update-interval-ms";
final long fallback = 100L;
String value = System.getProperty(key);
if (value == null) return fallback;
try {
return Long.parseLong(value);
} catch (NumberFormatException ex) {
return fallback;
}
}

void sendBossBar(
ComponentSerializer<Component, Component, String> serializer,
BossBarContent content,
Expand Down Expand Up @@ -42,11 +67,21 @@ private void updateProgress(Instant expiration, Duration duration, BossBar bossB
}

Duration remaining = Duration.between(Instant.now(), expiration);
float progress = 1 - (float) remaining.getSeconds() / duration.getSeconds();
double totalMillis = Math.max(0.0, duration.toMillis());
double remainingMillis = Math.max(0.0, remaining.toMillis());

float progress;
if (totalMillis <= 0.0) {
progress = 0.0f;
} else {
double fraction = remainingMillis / totalMillis;
progress = (float) fraction;
progress = Math.max(0.0f, Math.min(1.0f, progress));
}

bossBar.progress(progress);

this.scheduler.schedule(() -> updateProgress(expiration, duration, bossBar, viewer), 500L, TimeUnit.MILLISECONDS);
this.scheduler.schedule(() -> updateProgress(expiration, duration, bossBar, viewer), this.updateIntervalMillis, TimeUnit.MILLISECONDS);
}

}