Skip to content
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

Add boss bar animation when it appears #300

Merged
merged 9 commits into from
Jul 8, 2024
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 final 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 @@ -157,13 +159,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 +183,23 @@ 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;
if (!ANIMATE_PROGRESS) {
progressOld = progressNew;
}
BossBar bossBar = BossBar.bossBar(name, (float) progress, color, overlay);

BossBar bossBar = BossBar.bossBar(name, progressOld, color, overlay);

plugin.getAudiences().player(player).showBossBar(bossBar);
if (ANIMATE_PROGRESS) {
// Boss bar progress is updated later, so the player sees the animation going from progressOld to progressNew
plugin.getScheduler().scheduleSync(() -> bossBar.progress(progressNew), 2 * 50, TimeUnit.MILLISECONDS);
}
// Add to maps
if (mode.equals("single")) {
singleBossBars.put(player.getUniqueId(), bossBar);
Expand All @@ -197,19 +209,21 @@ 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) {
bossBar.progress(progress);
}
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);

if (ANIMATE_PROGRESS) {
// Boss bar progress is updated later, so the player sees the animation going from previous progress to new
plugin.getScheduler().scheduleSync(() -> bossBar.progress(progress), 2 * 50, TimeUnit.MILLISECONDS);
}
}

private String getBossBarText(Player player, Skill skill, double currentXp, long levelXp, double xpGained, int level, boolean maxed, double income, Locale locale) {
Expand Down Expand Up @@ -264,8 +278,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
Loading