Skip to content

Commit

Permalink
Add boss bar animation using delay when updating progress (#300)
Browse files Browse the repository at this point in the history
- This can be toggled with the boss_bar.animate_progress option in the main config
  • Loading branch information
IamMusavaRibica authored Jul 8, 2024
1 parent 4a5fc15 commit dd473ea
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public class BossBarManager implements Listener {
private NumberFormat moneyFormat;
private final AuraSkills plugin;
private final TextFormatter tf = new TextFormatter();
private boolean ANIMATE_PROGRESS;

public BossBarManager(AuraSkills plugin) {
this.bossBars = new HashMap<>();
Expand All @@ -56,6 +57,7 @@ public BossBarManager(AuraSkills plugin) {
this.checkCurrentActions = new HashMap<>();
this.singleCheckCurrentActions = new HashMap<>();
loadNumberFormats();
this.ANIMATE_PROGRESS = plugin.configBoolean(Option.BOSS_BAR_ANIMATE_PROGRESS);
}

public NumberFormat getXpFormat() {
Expand Down Expand Up @@ -89,6 +91,7 @@ public void loadOptions() {
stayTime = plugin.configInt(Option.BOSS_BAR_STAY_TIME);
colors = new HashMap<>();
overlays = new HashMap<>();
ANIMATE_PROGRESS = plugin.configBoolean(Option.BOSS_BAR_ANIMATE_PROGRESS);
for (String entry : plugin.configStringList(Option.BOSS_BAR_FORMAT)) {
String[] splitEntry = entry.split(" ");
Skill skill;
Expand Down Expand Up @@ -157,13 +160,20 @@ public void sendBossBar(Player player, Skill skill, double currentXp, double lev
bossBar = bossBars.get(playerId).get(skill);
}
String text = getBossBarText(player, skill, currentXp, (long) levelXp, xpGained, level, maxed, income, plugin.getLocale(player));
// Calculate xp progress
float progressNew = (float) (currentXp / levelXp);
if (progressNew > 1 || progressNew < 0) {
progressNew = 1.0f;
}
// If player does not have a boss bar in that skill
if (bossBar == null) {
bossBar = handleNewBossBar(player, skill, currentXp, levelXp, text);
// Calculate progress before gaining xp, for boss bar animation
float progressOld = (float) (Math.max(currentXp - xpGained, 0) / levelXp);
bossBar = handleNewBossBar(player, skill, progressOld > 1 ? 1.0f : progressOld, progressNew, text);
}
// Use existing one
else {
handleExistingBossBar(bossBar, player, skill, currentXp, levelXp, text);
handleExistingBossBar(bossBar, player, skill, progressNew, text);
}
// Increment current action
if (mode.equals("single")) {
Expand All @@ -174,20 +184,20 @@ public void sendBossBar(Player player, Skill skill, double currentXp, double lev
scheduleHide(playerId, skill, bossBar); // Schedule tasks to hide the boss bar
}

private BossBar handleNewBossBar(Player player, Skill skill, double currentXp, double levelXp, String text) {
private BossBar handleNewBossBar(Player player, Skill skill, float progressOld, float progressNew, String text) {
BossBar.Color color = getColor(skill);
BossBar.Overlay overlay = getOverlay(skill);

Component name = tf.toComponent(text);

// Calculate xp progress
double progress = currentXp / levelXp;
if (progress > 1 || progress < 0) {
progress = 1.0;
BossBar bossBar = BossBar.bossBar(name, progressOld, color, overlay);
if (!ANIMATE_PROGRESS) { // If the config option is disabled, immediately show new progress
bossBar.progress(progressNew);
} else { // Update the progress later to display its animation from progressOld to progressNew
plugin.getScheduler().scheduleSync(() -> bossBar.progress(progressNew), 2 * 50, TimeUnit.MILLISECONDS);
}
BossBar bossBar = BossBar.bossBar(name, (float) progress, color, overlay);

plugin.getAudiences().player(player).showBossBar(bossBar);

// Add to maps
if (mode.equals("single")) {
singleBossBars.put(player.getUniqueId(), bossBar);
Expand All @@ -197,16 +207,15 @@ private BossBar handleNewBossBar(Player player, Skill skill, double currentXp, d
return bossBar;
}

private void handleExistingBossBar(BossBar bossBar, Player player, Skill skill, double currentXp, double levelXp, String text) {
private void handleExistingBossBar(BossBar bossBar, Player player, Skill skill, float progress, String text) {
Component name = tf.toComponent(text);

bossBar.name(name); // Update the boss bar to the new text value
// Calculate xp progress
double progress = currentXp / levelXp;
if (progress > 1 || progress < 0) {
progress = 1.0;
if (!ANIMATE_PROGRESS) { // Update boss bar progress immediately
bossBar.progress(progress);
} else { // Update progress later, so the player sees the animation from previous progress (from reused boss bar) to new
plugin.getScheduler().scheduleSync(() -> bossBar.progress(progress), 2 * 50, TimeUnit.MILLISECONDS);
}
bossBar.progress((float) progress);
bossBar.name(name); // Update the boss bar to the new text value
bossBar.color(getColor(skill));

plugin.getAudiences().player(player).showBossBar(bossBar);
Expand Down Expand Up @@ -264,8 +273,7 @@ public void incrementAction(Player player, Skill skill) {
// Increment current action
if (mode.equals("single")) {
singleCheckCurrentActions.put(playerId, singleCheckCurrentActions.get(playerId) + 1);
}
else {
} else {
Integer currentAction = checkCurrentActions.get(playerId).get(skill);
if (currentAction != null) {
checkCurrentActions.get(playerId).put(skill, currentAction + 1);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ public enum Option {
BOSS_BAR_XP_FORMAT("boss_bar.xp_format", OptionType.STRING),
BOSS_BAR_PERCENT_FORMAT("boss_bar.percent_format", OptionType.STRING),
BOSS_BAR_MONEY_FORMAT("boss_bar.money_format", OptionType.STRING),
BOSS_BAR_ANIMATE_PROGRESS("boss_bar.animate_progress", OptionType.BOOLEAN),
// Jobs options
JOBS_ENABLED("jobs.enabled", OptionType.BOOLEAN),
JOBS_SELECTION_REQUIRE_SELECTION("jobs.selection.require_selection", OptionType.BOOLEAN),
Expand Down
1 change: 1 addition & 0 deletions common/src/main/resources/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ boss_bar:
xp_format: '#.#'
percent_format: '#.##'
money_format: '0.00'
animate_progress: true
jobs:
enabled: false
selection:
Expand Down

0 comments on commit dd473ea

Please sign in to comment.