24package com.dreamfirestudios.dreamcore.DreamBossBar;
26import com.dreamfirestudios.dreamcore.DreamCore;
27import com.dreamfirestudios.dreamcore.DreamJava.DreamClassID;
29import org.bukkit.Bukkit;
30import org.bukkit.boss.BarFlag;
31import org.bukkit.boss.BossBar;
32import org.bukkit.entity.Player;
54 private List<DreamBossBarData> frames;
55 private BarFlag[] barFlags;
56 @Getter
private BossBar bossBar;
57 private int currentFrameIndex;
58 @Getter
private boolean bossBarPaused =
true;
59 private final List<UUID> viewers =
new ArrayList<>();
68 if (player ==
null)
throw new IllegalArgumentException(
"Player cannot be null.");
69 return viewers.contains(player.getUniqueId());
76 if (frames ==
null || frames.isEmpty())
return;
77 currentFrameIndex = 0;
78 var first = frames.get(0);
79 if (bossBar ==
null) {
80 bossBar = Bukkit.createBossBar(first.safeTitle(
null), first.barColor(), first.barStyle(), barFlags);
82 bossBar.setTitle(first.safeTitle(
null));
83 bossBar.setColor(first.barColor());
84 bossBar.setStyle(first.barStyle());
85 bossBar.setProgress(first.clampedProgress());
92 if (players ==
null)
return;
93 for (var p : players) {
104 if (player ==
null)
throw new IllegalArgumentException(
"Player cannot be null.");
105 var
id = player.getUniqueId();
106 if (viewers.contains(
id))
return;
108 ensureBossBarInitialized();
109 bossBar.addPlayer(player);
119 if (player ==
null)
throw new IllegalArgumentException(
"Player cannot be null.");
120 var
id = player.getUniqueId();
121 if (!viewers.contains(
id))
return;
123 if (bossBar !=
null) bossBar.removePlayer(player);
131 if (viewers.isEmpty())
return;
133 List<UUID> copy =
new ArrayList<>(viewers);
134 for (var uuid : copy){
135 var player = Bukkit.getPlayer(uuid);
144 if (frames ==
null || frames.isEmpty() || viewers.isEmpty() || bossBarPaused)
return;
145 ensureBossBarInitialized();
147 var frame = frames.get(currentFrameIndex);
148 for (var uuid : viewers){
149 var player = Bukkit.getPlayer(uuid);
150 if (player ==
null)
continue;
151 frame.DisplayBarData(bossBar, player);
155 currentFrameIndex = (currentFrameIndex + 1) % frames.size();
161 bossBarPaused =
true;
169 bossBarPaused =
false;
179 bossBarPaused =
true;
188 private void ensureBossBarInitialized() {
189 if (bossBar !=
null)
return;
190 if (frames ==
null || frames.isEmpty()) {
191 bossBar = Bukkit.createBossBar(
"", org.bukkit.boss.BarColor.WHITE, org.bukkit.boss.BarStyle.SOLID, barFlags);
194 var first = frames.get(0);
195 bossBar = Bukkit.createBossBar(first.safeTitle(
null), first.barColor(), first.barStyle(), barFlags);
196 bossBar.setProgress(first.clampedProgress());
203 private final List<DreamBossBarData> frames =
new ArrayList<>();
204 private final List<Player> players =
new ArrayList<>();
214 if (barData ==
null)
throw new IllegalArgumentException(
"Bar data cannot be null.");
215 if (numberOfFrames <= 0)
throw new IllegalArgumentException(
"Number of frames must be > 0.");
216 for (
int i = 0; i < numberOfFrames; i++) frames.add(barData);
224 if (toAdd ==
null)
return this;
225 for (var p : toAdd)
if (p !=
null) players.add(p);
240 if (frames.isEmpty())
throw new IllegalArgumentException(
"Boss bar frames must not be empty.");
242 dbb.frames =
new ArrayList<>(frames);
243 dbb.barFlags = (barFlags ==
null ?
new BarFlag[0] : barFlags.clone());
245 players.forEach(dbb::addPlayer);
Fired after a DreamBossBar applies a frame to its viewers.
Fired when a DreamBossBar is paused.
Fired before a player is added to a DreamBossBar.
Fired when a player is removed from a DreamBossBar.
Fired when a DreamBossBar is resumed (played).
Fired when a DreamBossBar is stopped.
Builder for DreamBossBar.
Builder players(Player... toAdd)
Adds players to be attached at build time.
Builder dreamfireBossBarData(DreamBossBarData barData, int numberOfFrames)
Adds a frame repeated numberOfFrames times.
DreamBossBar build(BarFlag... barFlags)
Builds (or reuses) a DreamBossBar and registers it with the core.
Animated boss bar with viewer management and frame sequencing.
void displayNextFrame()
Advances to the next frame and updates the bar for all viewers.
void addPlayer(Player... players)
Adds one or more players to the boss bar.
void pause()
Pauses the animation (no frame progression).
void addPlayer(Player player)
Adds a single player to the boss bar.
void play()
Resumes the animation (frame progression continues).
boolean isPlayer(Player player)
Checks if the player currently has this boss bar.
void removePlayer(Player player)
Removes a player from the boss bar.
void removeAllPlayers()
Removes all players from the boss bar.
void resetBossBar()
Resets the bar back to frame 0 and reinitializes the underlying BossBar.
DreamBossBar stop()
Stops the boss bar, removes viewers, and unregisters from the core registry.
static final LinkedHashMap< UUID, DreamBossBar > DreamBossBars
record DreamBossBarData(BarColor barColor, BarStyle barStyle, double barProgress, Function< Player, String > messageProvider)
Immutable data for a single boss bar frame.