-
-
Notifications
You must be signed in to change notification settings - Fork 3
fix: Improve progress calculation and reduce update interval in BossB… #110
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -42,11 +42,14 @@ 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 = duration.toMillis(); | ||||||||||||||
| double remainingMillis = Math.max(0, remaining.toMillis()); | ||||||||||||||
| float progress = (float) (remainingMillis / totalMillis); | ||||||||||||||
| progress = Math.max(0.0f, Math.min(1.0f, progress)); | ||||||||||||||
|
||||||||||||||
| float progress = (float) (remainingMillis / totalMillis); | |
| progress = Math.max(0.0f, Math.min(1.0f, progress)); | |
| double totalMillis = duration.toMillis(); | |
| double remainingMillis = Math.max(0, remaining.toMillis()); | |
| float progress = (float) (1.0 - (remainingMillis / totalMillis)); // Invert the progress calculation | |
| progress = Math.max(0.0f, Math.min(1.0f, progress)); |
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Reducing the scheduler interval to 100ms can lead to increased CPU usage, especially if there are many boss bars being updated simultaneously. While this provides a smoother visual update, it's important to consider the performance implications. Make sure this value is configurable or that performance is carefully monitored.
Consider making the update interval configurable to allow administrators to tune performance.
long updateInterval = 100L; // Make this configurable
this.scheduler.schedule(() -> updateProgress(expiration, duration, bossBar, viewer), updateInterval, TimeUnit.MILLISECONDS);
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The current progress calculation might lead to unexpected behavior due to integer division. Specifically,
remainingMillis / totalMilliscould result in0ifremainingMillisis less thantotalMillisbut still greater than0, causing the boss bar to appear empty prematurely. To fix this, ensure that the division is performed with double precision before casting to float.Consider swapping the order of the cast and the division to ensure floating point division is performed.