| 1 | package net.minecraft.network.protocol.game;
|
| 2 |
|
| 3 | import java.util.UUID;
|
| 4 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 5 | import net.minecraft.network.chat.Component;
|
| 6 | import net.minecraft.network.chat.ComponentSerialization;
|
| 7 | import net.minecraft.network.codec.StreamCodec;
|
| 8 | import net.minecraft.network.codec.StreamDecoder;
|
| 9 | import net.minecraft.network.protocol.Packet;
|
| 10 | import net.minecraft.network.protocol.PacketType;
|
| 11 | import net.minecraft.world.BossEvent;
|
| 12 |
|
| 13 | public class ClientboundBossEventPacket implements Packet<ClientGamePacketListener> {
|
| 14 | public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundBossEventPacket> STREAM_CODEC = Packet.codec(
|
| 15 | ClientboundBossEventPacket::write, ClientboundBossEventPacket::new
|
| 16 | );
|
| 17 | private static final int FLAG_DARKEN = 1;
|
| 18 | private static final int FLAG_MUSIC = 2;
|
| 19 | private static final int FLAG_FOG = 4;
|
| 20 | private final UUID id;
|
| 21 | private final ClientboundBossEventPacket.Operation operation;
|
| 22 | private static final ClientboundBossEventPacket.Operation REMOVE_OPERATION = new ClientboundBossEventPacket.Operation() {
|
| 23 | @Override
|
| 24 | public ClientboundBossEventPacket.OperationType getType() {
|
| 25 | return ClientboundBossEventPacket.OperationType.REMOVE;
|
| 26 | }
|
| 27 |
|
| 28 | @Override
|
| 29 | public void dispatch(final UUID id, final ClientboundBossEventPacket.Handler handler) {
|
| 30 | handler.remove(id);
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public void write(final RegistryFriendlyByteBuf output) {
|
| 35 | }
|
| 36 | };
|
| 37 |
|
| 38 | private ClientboundBossEventPacket(final UUID id, final ClientboundBossEventPacket.Operation operation) {
|
| 39 | this.id = id;
|
| 40 | this.operation = operation;
|
| 41 | }
|
| 42 |
|
| 43 | private ClientboundBossEventPacket(final RegistryFriendlyByteBuf input) {
|
| 44 | this.id = input.readUUID();
|
| 45 | ClientboundBossEventPacket.OperationType type = input.readEnum(ClientboundBossEventPacket.OperationType.class);
|
| 46 | this.operation = type.reader.decode(input);
|
| 47 | }
|
| 48 |
|
| 49 | public static ClientboundBossEventPacket createAddPacket(final BossEvent event) {
|
| 50 | return new ClientboundBossEventPacket(event.getId(), new ClientboundBossEventPacket.AddOperation(event));
|
| 51 | }
|
| 52 |
|
| 53 | public static ClientboundBossEventPacket createRemovePacket(final UUID id) {
|
| 54 | return new ClientboundBossEventPacket(id, REMOVE_OPERATION);
|
| 55 | }
|
| 56 |
|
| 57 | public static ClientboundBossEventPacket createUpdateProgressPacket(final BossEvent event) {
|
| 58 | return new ClientboundBossEventPacket(event.getId(), new ClientboundBossEventPacket.UpdateProgressOperation(event.getProgress()));
|
| 59 | }
|
| 60 |
|
| 61 | public static ClientboundBossEventPacket createUpdateNamePacket(final BossEvent event) {
|
| 62 | return new ClientboundBossEventPacket(event.getId(), new ClientboundBossEventPacket.UpdateNameOperation(event.getName()));
|
| 63 | }
|
| 64 |
|
| 65 | public static ClientboundBossEventPacket createUpdateStylePacket(final BossEvent event) {
|
| 66 | return new ClientboundBossEventPacket(event.getId(), new ClientboundBossEventPacket.UpdateStyleOperation(event.getColor(), event.getOverlay()));
|
| 67 | }
|
| 68 |
|
| 69 | public static ClientboundBossEventPacket createUpdatePropertiesPacket(final BossEvent event) {
|
| 70 | return new ClientboundBossEventPacket(
|
| 71 | event.getId(),
|
| 72 | new ClientboundBossEventPacket.UpdatePropertiesOperation(event.shouldDarkenScreen(), event.shouldPlayBossMusic(), event.shouldCreateWorldFog())
|
| 73 | );
|
| 74 | }
|
| 75 |
|
| 76 | private void write(final RegistryFriendlyByteBuf output) {
|
| 77 | output.writeUUID(this.id);
|
| 78 | output.writeEnum(this.operation.getType());
|
| 79 | this.operation.write(output);
|
| 80 | }
|
| 81 |
|
| 82 | private static int encodeProperties(final boolean darkenScreen, final boolean playMusic, final boolean createWorldFog) {
|
| 83 | int properties = 0;
|
| 84 | if (darkenScreen) {
|
| 85 | properties |= 1;
|
| 86 | }
|
| 87 |
|
| 88 | if (playMusic) {
|
| 89 | properties |= 2;
|
| 90 | }
|
| 91 |
|
| 92 | if (createWorldFog) {
|
| 93 | properties |= 4;
|
| 94 | }
|
| 95 |
|
| 96 | return properties;
|
| 97 | }
|
| 98 |
|
| 99 | @Override
|
| 100 | public PacketType<ClientboundBossEventPacket> type() {
|
| 101 | return GamePacketTypes.CLIENTBOUND_BOSS_EVENT;
|
| 102 | }
|
| 103 |
|
| 104 | public void handle(final ClientGamePacketListener listener) {
|
| 105 | listener.handleBossUpdate(this);
|
| 106 | }
|
| 107 |
|
| 108 | public void dispatch(final ClientboundBossEventPacket.Handler handler) {
|
| 109 | this.operation.dispatch(this.id, handler);
|
| 110 | }
|
| 111 |
|
| 112 | private static class AddOperation implements ClientboundBossEventPacket.Operation {
|
| 113 | private final Component name;
|
| 114 | private final float progress;
|
| 115 | private final BossEvent.BossBarColor color;
|
| 116 | private final BossEvent.BossBarOverlay overlay;
|
| 117 | private final boolean darkenScreen;
|
| 118 | private final boolean playMusic;
|
| 119 | private final boolean createWorldFog;
|
| 120 |
|
| 121 | private AddOperation(final BossEvent event) {
|
| 122 | this.name = event.getName();
|
| 123 | this.progress = event.getProgress();
|
| 124 | this.color = event.getColor();
|
| 125 | this.overlay = event.getOverlay();
|
| 126 | this.darkenScreen = event.shouldDarkenScreen();
|
| 127 | this.playMusic = event.shouldPlayBossMusic();
|
| 128 | this.createWorldFog = event.shouldCreateWorldFog();
|
| 129 | }
|
| 130 |
|
| 131 | private AddOperation(final RegistryFriendlyByteBuf input) {
|
| 132 | this.name = ComponentSerialization.TRUSTED_STREAM_CODEC.decode(input);
|
| 133 | this.progress = input.readFloat();
|
| 134 | this.color = input.readEnum(BossEvent.BossBarColor.class);
|
| 135 | this.overlay = input.readEnum(BossEvent.BossBarOverlay.class);
|
| 136 | int flags = input.readUnsignedByte();
|
| 137 | this.darkenScreen = (flags & 1) > 0;
|
| 138 | this.playMusic = (flags & 2) > 0;
|
| 139 | this.createWorldFog = (flags & 4) > 0;
|
| 140 | }
|
| 141 |
|
| 142 | @Override
|
| 143 | public ClientboundBossEventPacket.OperationType getType() {
|
| 144 | return ClientboundBossEventPacket.OperationType.ADD;
|
| 145 | }
|
| 146 |
|
| 147 | @Override
|
| 148 | public void dispatch(final UUID id, final ClientboundBossEventPacket.Handler handler) {
|
| 149 | handler.add(id, this.name, this.progress, this.color, this.overlay, this.darkenScreen, this.playMusic, this.createWorldFog);
|
| 150 | }
|
| 151 |
|
| 152 | @Override
|
| 153 | public void write(final RegistryFriendlyByteBuf output) {
|
| 154 | ComponentSerialization.TRUSTED_STREAM_CODEC.encode(output, this.name);
|
| 155 | output.writeFloat(this.progress);
|
| 156 | output.writeEnum(this.color);
|
| 157 | output.writeEnum(this.overlay);
|
| 158 | output.writeByte(ClientboundBossEventPacket.encodeProperties(this.darkenScreen, this.playMusic, this.createWorldFog));
|
| 159 | }
|
| 160 | }
|
| 161 |
|
| 162 | public interface Handler {
|
| 163 | default void add(
|
| 164 | final UUID id,
|
| 165 | final Component name,
|
| 166 | final float progress,
|
| 167 | final BossEvent.BossBarColor color,
|
| 168 | final BossEvent.BossBarOverlay overlay,
|
| 169 | final boolean darkenScreen,
|
| 170 | final boolean playMusic,
|
| 171 | final boolean createWorldFog
|
| 172 | ) {
|
| 173 | }
|
| 174 |
|
| 175 | default void remove(final UUID id) {
|
| 176 | }
|
| 177 |
|
| 178 | default void updateProgress(final UUID id, final float progress) {
|
| 179 | }
|
| 180 |
|
| 181 | default void updateName(final UUID id, final Component name) {
|
| 182 | }
|
| 183 |
|
| 184 | default void updateStyle(final UUID id, final BossEvent.BossBarColor color, final BossEvent.BossBarOverlay overlay) {
|
| 185 | }
|
| 186 |
|
| 187 | default void updateProperties(final UUID id, final boolean darkenScreen, final boolean playMusic, final boolean createWorldFog) {
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | private interface Operation {
|
| 192 | ClientboundBossEventPacket.OperationType getType();
|
| 193 |
|
| 194 | void dispatch(UUID id, ClientboundBossEventPacket.Handler handler);
|
| 195 |
|
| 196 | void write(RegistryFriendlyByteBuf output);
|
| 197 | }
|
| 198 |
|
| 199 | private enum OperationType {
|
| 200 | ADD(ClientboundBossEventPacket.AddOperation::new),
|
| 201 | REMOVE(input -> ClientboundBossEventPacket.REMOVE_OPERATION),
|
| 202 | UPDATE_PROGRESS(ClientboundBossEventPacket.UpdateProgressOperation::new),
|
| 203 | UPDATE_NAME(ClientboundBossEventPacket.UpdateNameOperation::new),
|
| 204 | UPDATE_STYLE(ClientboundBossEventPacket.UpdateStyleOperation::new),
|
| 205 | UPDATE_PROPERTIES(ClientboundBossEventPacket.UpdatePropertiesOperation::new);
|
| 206 |
|
| 207 | private final StreamDecoder<RegistryFriendlyByteBuf, ClientboundBossEventPacket.Operation> reader;
|
| 208 |
|
| 209 | OperationType(final StreamDecoder<RegistryFriendlyByteBuf, ClientboundBossEventPacket.Operation> reader) {
|
| 210 | this.reader = reader;
|
| 211 | }
|
| 212 | }
|
| 213 |
|
| 214 | private record UpdateNameOperation(Component name) implements ClientboundBossEventPacket.Operation {
|
| 215 | private UpdateNameOperation(final RegistryFriendlyByteBuf input) {
|
| 216 | this(ComponentSerialization.TRUSTED_STREAM_CODEC.decode(input));
|
| 217 | }
|
| 218 |
|
| 219 | @Override
|
| 220 | public ClientboundBossEventPacket.OperationType getType() {
|
| 221 | return ClientboundBossEventPacket.OperationType.UPDATE_NAME;
|
| 222 | }
|
| 223 |
|
| 224 | @Override
|
| 225 | public void dispatch(final UUID id, final ClientboundBossEventPacket.Handler handler) {
|
| 226 | handler.updateName(id, this.name);
|
| 227 | }
|
| 228 |
|
| 229 | @Override
|
| 230 | public void write(final RegistryFriendlyByteBuf output) {
|
| 231 | ComponentSerialization.TRUSTED_STREAM_CODEC.encode(output, this.name);
|
| 232 | }
|
| 233 | }
|
| 234 |
|
| 235 | private record UpdateProgressOperation(float progress) implements ClientboundBossEventPacket.Operation {
|
| 236 | private UpdateProgressOperation(final RegistryFriendlyByteBuf input) {
|
| 237 | this(input.readFloat());
|
| 238 | }
|
| 239 |
|
| 240 | @Override
|
| 241 | public ClientboundBossEventPacket.OperationType getType() {
|
| 242 | return ClientboundBossEventPacket.OperationType.UPDATE_PROGRESS;
|
| 243 | }
|
| 244 |
|
| 245 | @Override
|
| 246 | public void dispatch(final UUID id, final ClientboundBossEventPacket.Handler handler) {
|
| 247 | handler.updateProgress(id, this.progress);
|
| 248 | }
|
| 249 |
|
| 250 | @Override
|
| 251 | public void write(final RegistryFriendlyByteBuf output) {
|
| 252 | output.writeFloat(this.progress);
|
| 253 | }
|
| 254 | }
|
| 255 |
|
| 256 | private static class UpdatePropertiesOperation implements ClientboundBossEventPacket.Operation {
|
| 257 | private final boolean darkenScreen;
|
| 258 | private final boolean playMusic;
|
| 259 | private final boolean createWorldFog;
|
| 260 |
|
| 261 | private UpdatePropertiesOperation(final boolean darkenScreen, final boolean playMusic, final boolean createWorldFog) {
|
| 262 | this.darkenScreen = darkenScreen;
|
| 263 | this.playMusic = playMusic;
|
| 264 | this.createWorldFog = createWorldFog;
|
| 265 | }
|
| 266 |
|
| 267 | private UpdatePropertiesOperation(final RegistryFriendlyByteBuf input) {
|
| 268 | int flags = input.readUnsignedByte();
|
| 269 | this.darkenScreen = (flags & 1) > 0;
|
| 270 | this.playMusic = (flags & 2) > 0;
|
| 271 | this.createWorldFog = (flags & 4) > 0;
|
| 272 | }
|
| 273 |
|
| 274 | @Override
|
| 275 | public ClientboundBossEventPacket.OperationType getType() {
|
| 276 | return ClientboundBossEventPacket.OperationType.UPDATE_PROPERTIES;
|
| 277 | }
|
| 278 |
|
| 279 | @Override
|
| 280 | public void dispatch(final UUID id, final ClientboundBossEventPacket.Handler handler) {
|
| 281 | handler.updateProperties(id, this.darkenScreen, this.playMusic, this.createWorldFog);
|
| 282 | }
|
| 283 |
|
| 284 | @Override
|
| 285 | public void write(final RegistryFriendlyByteBuf output) {
|
| 286 | output.writeByte(ClientboundBossEventPacket.encodeProperties(this.darkenScreen, this.playMusic, this.createWorldFog));
|
| 287 | }
|
| 288 | }
|
| 289 |
|
| 290 | private static class UpdateStyleOperation implements ClientboundBossEventPacket.Operation {
|
| 291 | private final BossEvent.BossBarColor color;
|
| 292 | private final BossEvent.BossBarOverlay overlay;
|
| 293 |
|
| 294 | private UpdateStyleOperation(final BossEvent.BossBarColor color, final BossEvent.BossBarOverlay overlay) {
|
| 295 | this.color = color;
|
| 296 | this.overlay = overlay;
|
| 297 | }
|
| 298 |
|
| 299 | private UpdateStyleOperation(final RegistryFriendlyByteBuf input) {
|
| 300 | this.color = input.readEnum(BossEvent.BossBarColor.class);
|
| 301 | this.overlay = input.readEnum(BossEvent.BossBarOverlay.class);
|
| 302 | }
|
| 303 |
|
| 304 | @Override
|
| 305 | public ClientboundBossEventPacket.OperationType getType() {
|
| 306 | return ClientboundBossEventPacket.OperationType.UPDATE_STYLE;
|
| 307 | }
|
| 308 |
|
| 309 | @Override
|
| 310 | public void dispatch(final UUID id, final ClientboundBossEventPacket.Handler handler) {
|
| 311 | handler.updateStyle(id, this.color, this.overlay);
|
| 312 | }
|
| 313 |
|
| 314 | @Override
|
| 315 | public void write(final RegistryFriendlyByteBuf output) {
|
| 316 | output.writeEnum(this.color);
|
| 317 | output.writeEnum(this.overlay);
|
| 318 | }
|
| 319 | }
|
| 320 | }
|
| 321 |
|