11package org .allaymc .api .bossbar ;
22
3- import org . allaymc . api . AllayAPI ;
3+ import com . google . common . base . Preconditions ;
44import org .jetbrains .annotations .Range ;
55import org .jetbrains .annotations .UnmodifiableView ;
66
77import java .util .Collection ;
8+ import java .util .Collections ;
9+ import java .util .HashSet ;
10+ import java .util .Set ;
811
912/**
1013 * Represents a boss bar, a visual element often used in games to display progress, health, or status.
1114 * It supports customization options such as color, style, title, progress, and visibility behavior.
1215 *
1316 * @author daoge_cmd
1417 */
15- public interface BossBar {
18+ public class BossBar {
1619
17- AllayAPI .APIInstanceHolder <Factory > FACTORY = AllayAPI .APIInstanceHolder .create ();
20+ protected Set <BossBarViewer > viewers ;
21+ protected String title ;
22+ protected float progress ;
23+ protected BossBarColor color ;
24+ protected BossBarStyle style ;
25+ protected boolean darkenSky ;
26+
27+ /**
28+ * Create a new boss bar.
29+ */
30+ public BossBar () {
31+ this .viewers = new HashSet <>();
32+ this .title = "" ;
33+ this .progress = 1.0f ;
34+ this .color = BossBarColor .PINK ;
35+ this .style = BossBarStyle .SOLID ;
36+ this .darkenSky = false ;
37+ }
1838
1939 /**
2040 * Create a new boss bar.
2141 *
2242 * @return the boss bar
2343 */
24- static BossBar create () {
25- return FACTORY . get (). create ();
44+ public static BossBar create () {
45+ return new BossBar ();
2646 }
2747
2848 /**
2949 * Add a viewer to the boss bar.
3050 *
3151 * @param viewer the viewer to add
3252 */
33- void addViewer (BossBarViewer viewer );
53+ public void addViewer (BossBarViewer viewer ) {
54+ if (viewers .add (viewer )) {
55+ viewer .viewBossBar (this );
56+ }
57+ }
3458
3559 /**
3660 * Remove a viewer from the boss bar.
3761 *
3862 * @param viewer the viewer to remove
3963 */
40- void removeViewer (BossBarViewer viewer );
64+ public void removeViewer (BossBarViewer viewer ) {
65+ if (viewers .remove (viewer )) {
66+ viewer .clearBossBar ();
67+ }
68+ }
4169
4270 /**
4371 * Remove all viewers from the boss bar.
4472 */
45- default void removeAllViewers () {
73+ public void removeAllViewers () {
4674 getViewers ().forEach (this ::removeViewer );
4775 }
4876
@@ -52,80 +80,108 @@ default void removeAllViewers() {
5280 * @return the viewers of the boss bar
5381 */
5482 @ UnmodifiableView
55- Collection <BossBarViewer > getViewers ();
83+ public Collection <BossBarViewer > getViewers () {
84+ return Collections .unmodifiableSet (viewers );
85+ }
5686
5787 /**
5888 * Get the color of the boss bar.
5989 *
6090 * @return the color of the boss bar
6191 */
62- BossBarColor getColor ();
92+ public BossBarColor getColor () {
93+ return color ;
94+ }
6395
6496 /**
6597 * Set the color of the boss bar.
6698 *
6799 * @param color the color to set
68100 */
69- void setColor (BossBarColor color );
101+ public void setColor (BossBarColor color ) {
102+ this .color = color ;
103+ update ();
104+ }
70105
71106 /**
72107 * Get the style of the boss bar.
73108 *
74109 * @return the style of the boss bar
75110 */
76- BossBarStyle getStyle ();
111+ public BossBarStyle getStyle () {
112+ return style ;
113+ }
77114
78115 /**
79116 * Set the style of the boss bar.
80117 *
81118 * @param style the style to set
82119 */
83- void setStyle (BossBarStyle style );
120+ public void setStyle (BossBarStyle style ) {
121+ this .style = style ;
122+ update ();
123+ }
84124
85125 /**
86126 * Check if the boss bar will darken the sky
87127 *
88128 * @return {@code true} if the boss bar will darken the sky, otherwise {@code false}.
89129 */
90- boolean isDarkenSky ();
130+ public boolean isDarkenSky () {
131+ return darkenSky ;
132+ }
91133
92134 /**
93135 * Set if the boss bar will darken the sky.
94136 *
95137 * @param darkenSky {@code true} if the boss bar will darken the sky, otherwise {@code false}
96138 */
97- void setDarkenSky (boolean darkenSky );
139+ public void setDarkenSky (boolean darkenSky ) {
140+ this .darkenSky = darkenSky ;
141+ update ();
142+ }
98143
99144 /**
100145 * Get the progress of the boss bar.
101146 *
102147 * @return the progress of the boss bar, between 0 and 1
103148 */
104149 @ Range (from = 0 , to = 1 )
105- float getProgress ();
150+ public float getProgress () {
151+ return progress ;
152+ }
106153
107154 /**
108155 * Set the progress of the boss bar.
109156 *
110157 * @param progress the progress to set, between 0 and 1
111158 */
112- void setProgress (@ Range (from = 0 , to = 1 ) float progress );
159+ public void setProgress (@ Range (from = 0 , to = 1 ) float progress ) {
160+ Preconditions .checkArgument (progress >= 0 && progress <= 1 , "Progress must be between 0 and 1" );
161+ this .progress = progress ;
162+ update ();
163+ }
113164
114165 /**
115166 * Get the title of the boss bar.
116167 *
117168 * @return the title of the boss bar
118169 */
119- String getTitle ();
170+ public String getTitle () {
171+ return title ;
172+ }
120173
121174 /**
122175 * Set the title of the boss bar.
123176 *
124177 * @param name the title to set
125178 */
126- void setTitle (String name );
179+ public void setTitle (String name ) {
180+ this .title = name ;
181+ update ();
182+ }
127183
128- interface Factory {
129- BossBar create ( );
184+ protected void update () {
185+ viewers . forEach ( viewer -> viewer . viewBossBar ( this ) );
130186 }
131187}
0 commit comments