DREAMFIRE Docs ← Back to site
Loading...
Searching...
No Matches
DreamActionBar.java
Go to the documentation of this file.
1/*
2 * MIT License
3 *
4 * Copyright (c) 2025 Dreamfire Studio
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24package com.dreamfirestudios.dreamcore.DreamActionBar;
25
26import com.dreamfirestudios.dreamcore.DreamJava.DreamClassID;
27import com.dreamfirestudios.dreamcore.DreamCore;
28import org.bukkit.entity.Player;
29
30import java.util.ArrayList;
31import java.util.List;
32
36public class DreamActionBar extends DreamClassID {
37 private final List<DreamActionBarData> barFrames = new ArrayList<>();
38 private final List<Player> viewers = new ArrayList<>();
39 private int currentFrameIndex;
40 private boolean paused;
41
47 public boolean isPlayerViewing(Player player) {
48 if (player == null) throw new IllegalArgumentException("Player cannot be null");
49 return viewers.contains(player);
50 }
51
57 public void addViewer(Player player, boolean multipleActionBars) {
58 if (player == null) throw new IllegalArgumentException("Player cannot be null");
59 if(DreamActionBarAPI.IsPlayerInActionBar(player) && !multipleActionBars) return;
60 if(viewers.contains(player) || new DreamActionBarPlayerAdded(this, player).isCancelled()) return;
61 viewers.add(player);
62 }
63
68 public void removeViewer(Player player) {
69 if (player == null) throw new IllegalArgumentException("Player cannot be null");
70 if(!viewers.contains(player) || new DreamActionBarPlayerRemoved(this, player).isCancelled()) return;
71 viewers.remove(player);
72 }
73
77 public void clearViewers() {
78 viewers.forEach(this::removeViewer);
79 viewers.clear();
80 }
81
87 public void updateFrame(int index, DreamActionBarData dreamActionBarData) {
88 if (dreamActionBarData == null) throw new IllegalArgumentException("DreamActionBarData cannot be null");
89 if (index >= 0 && index < barFrames.size()) barFrames.set(index, dreamActionBarData);
90 }
91
96 public boolean displayNextFrame() {
97 if (barFrames.isEmpty() || viewers.isEmpty()) return true;
98 if(paused) return false;
99 DreamActionBarData dreamActionBarData = barFrames.get(currentFrameIndex);
100 if (dreamActionBarData == null) throw new IllegalArgumentException("DreamActionBarData cannot be null");
101 viewers.parallelStream().forEach(dreamActionBarData::displayActionBar);
102 currentFrameIndex = (currentFrameIndex + 1) % barFrames.size();
103 return false;
104 }
105
109 public void pause() {
110 if (!paused && !new DreamActionBarPaused(this).isCancelled()) {
111 paused = true;
112 }
113 }
114
118 public void play() {
119 if (paused && !new DreamActionBarPlayed(this).isCancelled()) {
120 paused = false;
121 }
122 }
123
127 public void stop() {
128 if(!new DreamActionBarStopped(this).isCancelled()){
129 DreamCore.DreamActionBars.remove(getClassID());
130 clearViewers();
131 }
132 }
133
137 public static class Builder {
138 private final List<DreamActionBarData> barFrames = new ArrayList<>();
139 private final List<Player> viewers = new ArrayList<>();
140
147 public Builder addFrame(DreamActionBarData frame, int repeatFrames) {
148 if (frame == null || repeatFrames <= 0) return this;
149 for (int i = 0; i < repeatFrames; i++) barFrames.add(frame);
150 return this;
151 }
152
158 public Builder addViewer(Player player) {
159 if (player == null) throw new IllegalArgumentException("Player cannot be null");
160 viewers.add(player);
161 return this;
162 }
163
170 public DreamActionBar build(boolean allowMultipleActionBars) {
171 if (barFrames.isEmpty()) throw new IllegalArgumentException("At least one frame must be added.");
172 DreamActionBar actionBar = new DreamActionBar();
173 actionBar.barFrames.addAll(barFrames);
174 actionBar.viewers.addAll(viewers);
175 DreamCore.DreamActionBars.put(actionBar.getClassID(), actionBar);
176 return actionBar;
177 }
178 }
179}
Provides utility methods for querying active DreamActionBar instances.
static boolean IsPlayerInActionBar(Player player)
Checks if a given player is currently viewing any DreamActionBar.
Event fired when a DreamActionBar resumes playing after being paused.
Builder class for creating DreamActionBar instances.
Builder addViewer(Player player)
Adds a viewer to the builder.
Builder addFrame(DreamActionBarData frame, int repeatFrames)
Adds a frame to the builder with optional repetition.
DreamActionBar build(boolean allowMultipleActionBars)
Builds a new DreamActionBar instance.
Represents an action bar instance that cycles through frames and displays them to viewers.
void pause()
Pauses this action bar, preventing further frame updates until resumed.
boolean displayNextFrame()
Displays the next frame in the sequence to all current viewers.
void updateFrame(int index, DreamActionBarData dreamActionBarData)
Updates a specific frame in the action bar sequence.
void addViewer(Player player, boolean multipleActionBars)
Adds a player as a viewer of this action bar.
void removeViewer(Player player)
Removes a player from the list of viewers.
boolean isPlayerViewing(Player player)
Determines whether a player is currently viewing this action bar.
void play()
Resumes this action bar if it was previously paused.
void clearViewers()
Clears all current viewers from this action bar.
void stop()
Stops this action bar, removing it from DreamCore and clearing all viewers.
static final LinkedHashMap< UUID, DreamActionBar > DreamActionBars
record DreamActionBarData(Function< Player, String > messageProvider, Function< Player, DreamMessageSettings > settingsProvider)
Represents a single frame of action bar data, including a message provider and a settings provider fo...