CustomBossEvent .java

net.minecraft.server.bossevents.CustomBossEvent (class): members: dirtyCallback, DEFAULT_MAX, setPlayers, customId, setMax, onPlayerDisconnect, addPlayer.

net.minecraft.server.bosseventsclass223 linesserverextends ServerBossEvent6.1 KB

API Surface (16)

Identifier customId()
void addPlayer(final ServerPlayer player)
void removePlayer(final ServerPlayer player)
void removeAllPlayers()
int value()
int max()
void setValue(final int value)
void setMax(final int max)
Component getDisplayName()
boolean setPlayers(final Collection<ServerPlayer> players)
CustomBossEvent load(final UUID id, final Identifier customId, final CustomBossEvent.Packed packed, final Runnable setDirty)
CustomBossEvent.Packed pack()
void onPlayerConnect(final ServerPlayer player)
void onPlayerDisconnect(final ServerPlayer player)
void setDirty()
record Packed(Component name, boolean visible, int value, int max, BossEvent.BossBarColor color, BossEvent.BossBarOverlay overlay, boolean darkenScreen, boolean playBossMusic, boolean createWorldFog, Set<UUID> players)

Fields & Constants (13)

DEFAULT_MAX
customId
players
value
max
dirtyCallback
name
visible
color
overlay
darkenScreen
playBossMusic
createWorldFog

String literals (7)

"Visible"
"Color"
"Overlay"
"DarkenScreen"
"PlayBossMusic"
"CreateWorldFog"
"Players"

Full Source

CustomBossEvent.java223 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.bossevents;
2
3
import com.google.common.collect.Sets;
4
import com.mojang.serialization.Codec;
5
import com.mojang.serialization.codecs.RecordCodecBuilder;
6
import java.util.Collection;
7
import java.util.Set;
8
import java.util.UUID;
9
import net.minecraft.core.UUIDUtil;
10
import net.minecraft.network.chat.Component;
11
import net.minecraft.network.chat.ComponentSerialization;
12
import net.minecraft.network.chat.ComponentUtils;
13
import net.minecraft.network.chat.HoverEvent;
14
import net.minecraft.resources.Identifier;
15
import net.minecraft.server.level.ServerBossEvent;
16
import net.minecraft.server.level.ServerPlayer;
17
import net.minecraft.util.Mth;
18
import net.minecraft.world.BossEvent;
19
20
public class CustomBossEvent extends ServerBossEvent {
21
	private static final int DEFAULT_MAX = 100;
22
	private final Identifier customId;
23
	private final Set<UUID> players = Sets.newHashSet();
24
	private int value;
25
	private int max = 100;
26
	private final Runnable dirtyCallback;
27
28
	public CustomBossEvent(final UUID id, final Identifier customId, final Component name, final Runnable dirtyCallback) {
29
		super(id, name, BossEvent.BossBarColor.WHITE, BossEvent.BossBarOverlay.PROGRESS);
30
		this.dirtyCallback = dirtyCallback;
31
		this.customId = customId;
32
		this.setProgress(0.0F);
33
	}
34
35
	public Identifier customId() {
36
		return this.customId;
37
	}
38
39
	@Override
40
	public void addPlayer(final ServerPlayer player) {
41
		super.addPlayer(player);
42
		if (this.players.add(player.getUUID())) {
43
			this.setDirty();
44
		}
45
	}
46
47
	@Override
48
	public void removePlayer(final ServerPlayer player) {
49
		super.removePlayer(player);
50
		if (this.players.remove(player.getUUID())) {
51
			this.setDirty();
52
		}
53
	}
54
55
	@Override
56
	public void removeAllPlayers() {
57
		super.removeAllPlayers();
58
		if (!this.players.isEmpty()) {
59
			this.players.clear();
60
			this.setDirty();
61
		}
62
	}
63
64
	public int value() {
65
		return this.value;
66
	}
67
68
	public int max() {
69
		return this.max;
70
	}
71
72
	public void setValue(final int value) {
73
		this.value = value;
74
		this.setProgress(Mth.clamp((float)value / this.max, 0.0F, 1.0F));
75
		this.setDirty();
76
	}
77
78
	public void setMax(final int max) {
79
		this.max = max;
80
		this.setProgress(Mth.clamp((float)this.value / max, 0.0F, 1.0F));
81
		this.setDirty();
82
	}
83
84
	public final Component getDisplayName() {
85
		return ComponentUtils.wrapInSquareBrackets(this.getName())
86
			.withStyle(
87
				s -> s.withColor(this.getColor().getFormatting())
88
					.withHoverEvent(new HoverEvent.ShowText(Component.literal(this.customId().toString())))
89
					.withInsertion(this.customId().toString())
90
			);
91
	}
92
93
	public boolean setPlayers(final Collection<ServerPlayer> players) {
94
		Set<UUID> toRemove = Sets.newHashSet();
95
		Set<ServerPlayer> toAdd = Sets.newHashSet();
96
97
		for (UUID uuid : this.players) {
98
			boolean found = false;
99
100
			for (ServerPlayer player : players) {
101
				if (player.getUUID().equals(uuid)) {
102
					found = true;
103
					break;
104
				}
105
			}
106
107
			if (!found) {
108
				toRemove.add(uuid);
109
			}
110
		}
111
112
		for (ServerPlayer player : players) {
113
			boolean found = false;
114
115
			for (UUID uuid : this.players) {
116
				if (player.getUUID().equals(uuid)) {
117
					found = true;
118
					break;
119
				}
120
			}
121
122
			if (!found) {
123
				toAdd.add(player);
124
			}
125
		}
126
127
		for (UUID uuid : toRemove) {
128
			for (ServerPlayer player : this.getPlayers()) {
129
				if (player.getUUID().equals(uuid)) {
130
					this.removePlayer(player);
131
					break;
132
				}
133
			}
134
135
			this.players.remove(uuid);
136
		}
137
138
		for (ServerPlayer player : toAdd) {
139
			this.addPlayer(player);
140
		}
141
142
		boolean playersChanged = !toRemove.isEmpty() || !toAdd.isEmpty();
143
		if (playersChanged) {
144
			this.setDirty();
145
		}
146
147
		return playersChanged;
148
	}
149
150
	public static CustomBossEvent load(final UUID id, final Identifier customId, final CustomBossEvent.Packed packed, final Runnable setDirty) {
151
		CustomBossEvent event = new CustomBossEvent(id, customId, packed.name, setDirty);
152
		event.setVisible(packed.visible);
153
		event.setValue(packed.value);
154
		event.setMax(packed.max);
155
		event.setColor(packed.color);
156
		event.setOverlay(packed.overlay);
157
		event.setDarkenScreen(packed.darkenScreen);
158
		event.setPlayBossMusic(packed.playBossMusic);
159
		event.setCreateWorldFog(packed.createWorldFog);
160
		event.players.addAll(packed.players);
161
		return event;
162
	}
163
164
	public CustomBossEvent.Packed pack() {
165
		return new CustomBossEvent.Packed(
166
			this.getName(),
167
			this.isVisible(),
168
			this.value(),
169
			this.max(),
170
			this.getColor(),
171
			this.getOverlay(),
172
			this.shouldDarkenScreen(),
173
			this.shouldPlayBossMusic(),
174
			this.shouldCreateWorldFog(),
175
			Set.copyOf(this.players)
176
		);
177
	}
178
179
	public void onPlayerConnect(final ServerPlayer player) {
180
		if (this.players.contains(player.getUUID())) {
181
			this.addPlayer(player);
182
		}
183
	}
184
185
	public void onPlayerDisconnect(final ServerPlayer player) {
186
		super.removePlayer(player);
187
	}
188
189
	@Override
190
	public void setDirty() {
191
		this.dirtyCallback.run();
192
	}
193
194
	public record Packed(
195
		Component name,
196
		boolean visible,
197
		int value,
198
		int max,
199
		BossEvent.BossBarColor color,
200
		BossEvent.BossBarOverlay overlay,
201
		boolean darkenScreen,
202
		boolean playBossMusic,
203
		boolean createWorldFog,
204
		Set<UUID> players
205
	) {
206
		public static final Codec<CustomBossEvent.Packed> CODEC = RecordCodecBuilder.create(
207
			i -> i.group(
208
					ComponentSerialization.CODEC.fieldOf("Name").forGetter(CustomBossEvent.Packed::name),
209
					Codec.BOOL.optionalFieldOf("Visible", false).forGetter(CustomBossEvent.Packed::visible),
210
					Codec.INT.optionalFieldOf("Value", 0).forGetter(CustomBossEvent.Packed::value),
211
					Codec.INT.optionalFieldOf("Max", 100).forGetter(CustomBossEvent.Packed::max),
212
					BossEvent.BossBarColor.CODEC.optionalFieldOf("Color", BossEvent.BossBarColor.WHITE).forGetter(CustomBossEvent.Packed::color),
213
					BossEvent.BossBarOverlay.CODEC.optionalFieldOf("Overlay", BossEvent.BossBarOverlay.PROGRESS).forGetter(CustomBossEvent.Packed::overlay),
214
					Codec.BOOL.optionalFieldOf("DarkenScreen", false).forGetter(CustomBossEvent.Packed::darkenScreen),
215
					Codec.BOOL.optionalFieldOf("PlayBossMusic", false).forGetter(CustomBossEvent.Packed::playBossMusic),
216
					Codec.BOOL.optionalFieldOf("CreateWorldFog", false).forGetter(CustomBossEvent.Packed::createWorldFog),
217
					UUIDUtil.CODEC_SET.optionalFieldOf("Players", Set.of()).forGetter(CustomBossEvent.Packed::players)
218
				)
219
				.apply(i, CustomBossEvent.Packed::new)
220
		);
221
	}
222
}
223

Decompiled Minecraft 26.3 source of CustomBossEvent (net/minecraft/server/bossevents/CustomBossEvent.java) with Mojang official mappings, stored verbatim on the site.