| 1 | package net.minecraft.server.level;
|
| 2 |
|
| 3 | import com.google.common.base.Objects;
|
| 4 | import com.google.common.collect.Lists;
|
| 5 | import com.google.common.collect.Sets;
|
| 6 | import java.util.Collection;
|
| 7 | import java.util.Collections;
|
| 8 | import java.util.Set;
|
| 9 | import java.util.UUID;
|
| 10 | import java.util.function.Function;
|
| 11 | import net.minecraft.network.chat.Component;
|
| 12 | import net.minecraft.network.protocol.game.ClientboundBossEventPacket;
|
| 13 | import net.minecraft.world.BossEvent;
|
| 14 |
|
| 15 | public class ServerBossEvent extends BossEvent {
|
| 16 | private final Set<ServerPlayer> players = Sets.newHashSet();
|
| 17 | private final Set<ServerPlayer> unmodifiablePlayers = Collections.unmodifiableSet(this.players);
|
| 18 | private boolean visible = true;
|
| 19 |
|
| 20 | public ServerBossEvent(final UUID id, final Component name, final BossEvent.BossBarColor color, final BossEvent.BossBarOverlay overlay) {
|
| 21 | super(id, name, color, overlay);
|
| 22 | }
|
| 23 |
|
| 24 | @Override
|
| 25 | public void setProgress(final float progress) {
|
| 26 | if (progress != this.progress) {
|
| 27 | super.setProgress(progress);
|
| 28 | this.setDirty();
|
| 29 | this.broadcast(ClientboundBossEventPacket::createUpdateProgressPacket);
|
| 30 | }
|
| 31 | }
|
| 32 |
|
| 33 | @Override
|
| 34 | public void setColor(final BossEvent.BossBarColor color) {
|
| 35 | if (color != this.color) {
|
| 36 | super.setColor(color);
|
| 37 | this.setDirty();
|
| 38 | this.broadcast(ClientboundBossEventPacket::createUpdateStylePacket);
|
| 39 | }
|
| 40 | }
|
| 41 |
|
| 42 | @Override
|
| 43 | public void setOverlay(final BossEvent.BossBarOverlay overlay) {
|
| 44 | if (overlay != this.overlay) {
|
| 45 | super.setOverlay(overlay);
|
| 46 | this.setDirty();
|
| 47 | this.broadcast(ClientboundBossEventPacket::createUpdateStylePacket);
|
| 48 | }
|
| 49 | }
|
| 50 |
|
| 51 | @Override
|
| 52 | public BossEvent setDarkenScreen(final boolean darkenScreen) {
|
| 53 | if (darkenScreen != this.darkenScreen) {
|
| 54 | super.setDarkenScreen(darkenScreen);
|
| 55 | this.setDirty();
|
| 56 | this.broadcast(ClientboundBossEventPacket::createUpdatePropertiesPacket);
|
| 57 | }
|
| 58 |
|
| 59 | return this;
|
| 60 | }
|
| 61 |
|
| 62 | @Override
|
| 63 | public BossEvent setPlayBossMusic(final boolean playBossMusic) {
|
| 64 | if (playBossMusic != this.playBossMusic) {
|
| 65 | super.setPlayBossMusic(playBossMusic);
|
| 66 | this.setDirty();
|
| 67 | this.broadcast(ClientboundBossEventPacket::createUpdatePropertiesPacket);
|
| 68 | }
|
| 69 |
|
| 70 | return this;
|
| 71 | }
|
| 72 |
|
| 73 | @Override
|
| 74 | public BossEvent setCreateWorldFog(final boolean createWorldFog) {
|
| 75 | if (createWorldFog != this.createWorldFog) {
|
| 76 | super.setCreateWorldFog(createWorldFog);
|
| 77 | this.setDirty();
|
| 78 | this.broadcast(ClientboundBossEventPacket::createUpdatePropertiesPacket);
|
| 79 | }
|
| 80 |
|
| 81 | return this;
|
| 82 | }
|
| 83 |
|
| 84 | @Override
|
| 85 | public void setName(final Component name) {
|
| 86 | if (!Objects.equal(name, this.name)) {
|
| 87 | super.setName(name);
|
| 88 | this.setDirty();
|
| 89 | this.broadcast(ClientboundBossEventPacket::createUpdateNamePacket);
|
| 90 | }
|
| 91 | }
|
| 92 |
|
| 93 | private void broadcast(final Function<BossEvent, ClientboundBossEventPacket> factory) {
|
| 94 | if (this.visible) {
|
| 95 | ClientboundBossEventPacket packet = factory.apply(this);
|
| 96 |
|
| 97 | for (ServerPlayer player : this.players) {
|
| 98 | player.connection.send(packet);
|
| 99 | }
|
| 100 | }
|
| 101 | }
|
| 102 |
|
| 103 | public void addPlayer(final ServerPlayer player) {
|
| 104 | if (this.players.add(player) && this.visible) {
|
| 105 | player.connection.send(ClientboundBossEventPacket.createAddPacket(this));
|
| 106 | }
|
| 107 | }
|
| 108 |
|
| 109 | public void removePlayer(final ServerPlayer player) {
|
| 110 | if (this.players.remove(player) && this.visible) {
|
| 111 | player.connection.send(ClientboundBossEventPacket.createRemovePacket(this.getId()));
|
| 112 | }
|
| 113 | }
|
| 114 |
|
| 115 | public void removeAllPlayers() {
|
| 116 | if (!this.players.isEmpty()) {
|
| 117 | for (ServerPlayer player : Lists.newArrayList(this.players)) {
|
| 118 | this.removePlayer(player);
|
| 119 | }
|
| 120 | }
|
| 121 | }
|
| 122 |
|
| 123 | public boolean isVisible() {
|
| 124 | return this.visible;
|
| 125 | }
|
| 126 |
|
| 127 | public void setVisible(final boolean visible) {
|
| 128 | if (visible != this.visible) {
|
| 129 | this.visible = visible;
|
| 130 | this.setDirty();
|
| 131 |
|
| 132 | for (ServerPlayer player : this.players) {
|
| 133 | player.connection.send(visible ? ClientboundBossEventPacket.createAddPacket(this) : ClientboundBossEventPacket.createRemovePacket(this.getId()));
|
| 134 | }
|
| 135 | }
|
| 136 | }
|
| 137 |
|
| 138 | public Collection<ServerPlayer> getPlayers() {
|
| 139 | return this.unmodifiablePlayers;
|
| 140 | }
|
| 141 |
|
| 142 | protected void setDirty() {
|
| 143 | }
|
| 144 | }
|
| 145 |
|