From 33fb251aae682a6423caa6e688daac884422aab7 Mon Sep 17 00:00:00 2001 From: Krakenied Date: Thu, 22 Aug 2024 13:02:28 +0200 Subject: Add some new boss bar options --- .../quests/bukkit/hook/bossbar/BossBar_Bukkit.java | 45 ++++++++++++++++++++++ .../src/main/resources/resources/bukkit/config.yml | 4 ++ 2 files changed, 49 insertions(+) (limited to 'bukkit') diff --git a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/bossbar/BossBar_Bukkit.java b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/bossbar/BossBar_Bukkit.java index 7645cfe6..6c5bc7ee 100644 --- a/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/bossbar/BossBar_Bukkit.java +++ b/bukkit/src/main/java/com/leonardobishop/quests/bukkit/hook/bossbar/BossBar_Bukkit.java @@ -16,6 +16,7 @@ import java.util.Collections; import java.util.HashMap; import java.util.Iterator; import java.util.Map; +import java.util.Set; import java.util.concurrent.TimeUnit; import java.util.logging.Level; @@ -25,6 +26,8 @@ public class BossBar_Bukkit implements QuestsBossBar { private final RemovalListener removalListener; private final Map barColorMap; private final Map barStyleMap; + private final int limit; + private final boolean replaceOnLimit; // use cache because of its concurrency and automatic player on quit removal private final Cache> playerQuestBarCache = CacheBuilder.newBuilder().weakKeys().build(); @@ -39,6 +42,12 @@ public class BossBar_Bukkit implements QuestsBossBar { // Load bossbar style config this.barStyleMap = loadConfig(BarStyle.class, "style", BarStyle.SOLID); + // Set boss bar amount limit + this.limit = plugin.getConfig().getInt("options.bossbar.limit", -1); + + // Set whether boss bars should be replaced + this.replaceOnLimit = plugin.getConfig().getBoolean("options.bossbar.replace-on-limit", true); + //noinspection CodeBlock2Expr (for readability) plugin.getScheduler().runTaskTimerAsynchronously(() -> { playerQuestBarCache.asMap() @@ -64,6 +73,42 @@ public class BossBar_Bukkit implements QuestsBossBar { .build(); }); + limitCheck: + if (this.limit >= 0 && questBarCache.size() >= this.limit && !questBarCache.asMap().containsKey(questId)) { + if (!this.replaceOnLimit) { + return; + } + + final Set> cacheEntries = questBarCache.asMap().entrySet(); + Map.Entry toRemove = null; + double minProgress = -1.0d; + + // search for boss bar with the lowest progress + for (final Map.Entry cacheEntry : cacheEntries) { + final double bossBarProgress = cacheEntry.getValue().getProgress(); + + if (bossBarProgress > minProgress) { + toRemove = cacheEntry; + minProgress = bossBarProgress; + } + } + + // no boss bars found + if (toRemove == null) { + break limitCheck; + } + + // we don't want to replace higher progress boss bar with the requested if it has lower progress + final double toRemoveProgress = toRemove.getValue().getProgress(); + if (toRemoveProgress > progress) { + return; + } + + // Remove it from the cache and schedule removal from player's boss bars + questBarCache.invalidate(toRemove.getKey()); + this.plugin.getScheduler().runTask(toRemove.getValue()::removeAll); + } + BarColor color = getBest(barColorMap, progress); BarStyle style = getBest(barStyleMap, progress); diff --git a/bukkit/src/main/resources/resources/bukkit/config.yml b/bukkit/src/main/resources/resources/bukkit/config.yml index e22b0566..a7e46c18 100644 --- a/bukkit/src/main/resources/resources/bukkit/config.yml +++ b/bukkit/src/main/resources/resources/bukkit/config.yml @@ -51,6 +51,10 @@ options: # See https://hub.spigotmc.org/javadocs/spigot/org/bukkit/boss/BarStyle.html style: '0.0': SOLID # for 0.0 and higher progress values (progress is always between 0.0 and 1.0) + # Max amount of active task progress boss bars at once + limit: -1 + # Whether new boss bar should be added and make another (the least progress one - if exists) disappear + replace-on-limit: true # Enable/disable ActionBar actionbar: progress: false -- cgit v1.2.3-70-g09d2