Skip to content

Commit ea7b3b1

Browse files
committed
refactor: merge boss bar implementation into api
1 parent 1693ee9 commit ea7b3b1

File tree

5 files changed

+82
-114
lines changed

5 files changed

+82
-114
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ Unless otherwise specified, any version comparison below is the comparison of th
1212

1313
<small>[Compare with 0.12.0](https://github.com/AllayMC/Allay/compare/0.12.0...HEAD)</small>
1414

15+
### Changed
16+
17+
- (API) Merged `org.allaymc.server.bossbar.AllayBossBar` into the concrete `org.allaymc.api.bossbar.BossBar` class, so boss bars no longer use separate API interface and server implementation types.
18+
1519
# 0.12.0 (API 0.27.0) - 2026/3/29
1620

1721
<small>[Compare with 0.11.1](https://github.com/AllayMC/Allay/compare/0.11.1...0.12.0)</small>

api/src/main/java/org/allaymc/api/AllayAPI.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import lombok.NoArgsConstructor;
55
import lombok.experimental.StandardException;
66
import lombok.extern.slf4j.Slf4j;
7-
import org.allaymc.api.bossbar.BossBar;
87
import org.allaymc.api.command.selector.EntitySelectorAPI;
98
import org.allaymc.api.command.tree.CommandNodeFactory;
109
import org.allaymc.api.command.tree.CommandTree;
@@ -172,7 +171,6 @@ private void registerDefaultAPIRequirements() {
172171
requireImpl(CommandNodeFactory.class, CommandNodeFactory.FACTORY::set);
173172

174173
// Misc
175-
requireImpl(BossBar.Factory.class, BossBar.FACTORY::set);
176174
requireImpl(NBTIO.class, NBTIO.API::set);
177175
requireImpl(FakeContainerFactory.class, FakeContainerFactory.FACTORY::set);
178176
}
Lines changed: 77 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,76 @@
11
package org.allaymc.api.bossbar;
22

3-
import org.allaymc.api.AllayAPI;
3+
import com.google.common.base.Preconditions;
44
import org.jetbrains.annotations.Range;
55
import org.jetbrains.annotations.UnmodifiableView;
66

77
import 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
}

server/src/main/java/org/allaymc/server/Allay.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.allaymc.api.AllayAPI;
88
import org.allaymc.api.block.type.BlockType;
99
import org.allaymc.api.blockentity.type.BlockEntityType;
10-
import org.allaymc.api.bossbar.BossBar;
1110
import org.allaymc.api.command.selector.EntitySelectorAPI;
1211
import org.allaymc.api.command.tree.CommandNodeFactory;
1312
import org.allaymc.api.command.tree.CommandTree;
@@ -28,8 +27,6 @@
2827
import org.allaymc.api.utils.identifier.Identifier;
2928
import org.allaymc.api.world.biome.BiomeType;
3029
import org.allaymc.api.world.feature.WorldFeature;
31-
import org.allaymc.server.bossbar.AllayBossBar;
32-
import org.allaymc.server.world.biome.CustomBiomeIdAllocator;
3330
import org.allaymc.server.command.selector.AllayEntitySelectorAPI;
3431
import org.allaymc.server.command.tree.AllayCommandNodeFactory;
3532
import org.allaymc.server.command.tree.AllayCommandTree;
@@ -50,6 +47,7 @@
5047
import org.allaymc.server.utils.AllayNBTIO;
5148
import org.allaymc.server.utils.DynamicURLClassLoader;
5249
import org.allaymc.server.utils.GitProperties;
50+
import org.allaymc.server.world.biome.CustomBiomeIdAllocator;
5351
import org.apache.logging.log4j.core.async.AsyncLoggerContextSelector;
5452
import org.jetbrains.annotations.VisibleForTesting;
5553

@@ -163,7 +161,6 @@ private static void initAllayAPI() {
163161
api.bind(CommandNodeFactory.class, AllayCommandNodeFactory::new);
164162

165163
// Misc
166-
api.bind(BossBar.Factory.class, () -> AllayBossBar::new);
167164
api.bind(NBTIO.class, AllayNBTIO::new);
168165
api.bind(FakeContainerFactory.class, AllayFakeContainerFactory::new);
169166

server/src/main/java/org/allaymc/server/bossbar/AllayBossBar.java

Lines changed: 0 additions & 87 deletions
This file was deleted.

0 commit comments

Comments
 (0)