| 1 | package net.minecraft.network.protocol.game;
|
| 2 |
|
| 3 | import com.google.common.base.MoreObjects;
|
| 4 | import com.mojang.authlib.GameProfile;
|
| 5 | import com.mojang.authlib.properties.PropertyMap;
|
| 6 | import io.netty.buffer.ByteBuf;
|
| 7 | import java.util.Collection;
|
| 8 | import java.util.EnumSet;
|
| 9 | import java.util.List;
|
| 10 | import java.util.Objects;
|
| 11 | import java.util.UUID;
|
| 12 | import java.util.function.IntFunction;
|
| 13 | import net.minecraft.Optionull;
|
| 14 | import net.minecraft.network.FriendlyByteBuf;
|
| 15 | import net.minecraft.network.RegistryFriendlyByteBuf;
|
| 16 | import net.minecraft.network.chat.Component;
|
| 17 | import net.minecraft.network.chat.ComponentSerialization;
|
| 18 | import net.minecraft.network.chat.RemoteChatSession;
|
| 19 | import net.minecraft.network.codec.ByteBufCodecs;
|
| 20 | import net.minecraft.network.codec.StreamCodec;
|
| 21 | import net.minecraft.network.protocol.Packet;
|
| 22 | import net.minecraft.network.protocol.PacketType;
|
| 23 | import net.minecraft.server.level.ServerPlayer;
|
| 24 | import net.minecraft.util.ByIdMap;
|
| 25 | import net.minecraft.world.entity.player.PlayerModelPart;
|
| 26 | import net.minecraft.world.level.GameType;
|
| 27 | import org.jspecify.annotations.Nullable;
|
| 28 |
|
| 29 | public class ClientboundPlayerInfoUpdatePacket implements Packet<ClientGamePacketListener> {
|
| 30 | public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundPlayerInfoUpdatePacket> STREAM_CODEC = Packet.codec(
|
| 31 | ClientboundPlayerInfoUpdatePacket::write, ClientboundPlayerInfoUpdatePacket::new
|
| 32 | );
|
| 33 | private final EnumSet<ClientboundPlayerInfoUpdatePacket.Action> actions;
|
| 34 | private final List<ClientboundPlayerInfoUpdatePacket.Entry> entries;
|
| 35 |
|
| 36 | public ClientboundPlayerInfoUpdatePacket(final EnumSet<ClientboundPlayerInfoUpdatePacket.Action> actions, final Collection<ServerPlayer> players) {
|
| 37 | this.actions = actions;
|
| 38 | this.entries = players.stream().map(ClientboundPlayerInfoUpdatePacket.Entry::new).toList();
|
| 39 | }
|
| 40 |
|
| 41 | public ClientboundPlayerInfoUpdatePacket(final ClientboundPlayerInfoUpdatePacket.Action action, final ServerPlayer player) {
|
| 42 | this.actions = EnumSet.of(action);
|
| 43 | this.entries = List.of(new ClientboundPlayerInfoUpdatePacket.Entry(player));
|
| 44 | }
|
| 45 |
|
| 46 | public static ClientboundPlayerInfoUpdatePacket createPlayerInitializing(final Collection<ServerPlayer> players) {
|
| 47 | EnumSet<ClientboundPlayerInfoUpdatePacket.Action> actions = EnumSet.of(
|
| 48 | ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER,
|
| 49 | ClientboundPlayerInfoUpdatePacket.Action.INITIALIZE_CHAT,
|
| 50 | ClientboundPlayerInfoUpdatePacket.Action.UPDATE_GAME_MODE,
|
| 51 | ClientboundPlayerInfoUpdatePacket.Action.UPDATE_LISTED,
|
| 52 | ClientboundPlayerInfoUpdatePacket.Action.UPDATE_LATENCY,
|
| 53 | ClientboundPlayerInfoUpdatePacket.Action.UPDATE_DISPLAY_NAME,
|
| 54 | ClientboundPlayerInfoUpdatePacket.Action.UPDATE_HAT,
|
| 55 | ClientboundPlayerInfoUpdatePacket.Action.UPDATE_LIST_ORDER
|
| 56 | );
|
| 57 | return new ClientboundPlayerInfoUpdatePacket(actions, players);
|
| 58 | }
|
| 59 |
|
| 60 | private static StreamCodec<RegistryFriendlyByteBuf, ClientboundPlayerInfoUpdatePacket.Entry> entryCodec(
|
| 61 | final EnumSet<ClientboundPlayerInfoUpdatePacket.Action> actions
|
| 62 | ) {
|
| 63 | return StreamCodec.of((output, entry) -> {
|
| 64 | output.writeUUID(entry.profileId());
|
| 65 |
|
| 66 | for (ClientboundPlayerInfoUpdatePacket.Action action : actions) {
|
| 67 | action.writer.write(output, entry);
|
| 68 | }
|
| 69 | }, input -> {
|
| 70 | ClientboundPlayerInfoUpdatePacket.EntryBuilder builder = new ClientboundPlayerInfoUpdatePacket.EntryBuilder(input.readUUID());
|
| 71 |
|
| 72 | for (ClientboundPlayerInfoUpdatePacket.Action action : actions) {
|
| 73 | action.reader.read(builder, input);
|
| 74 | }
|
| 75 |
|
| 76 | return builder.build();
|
| 77 | });
|
| 78 | }
|
| 79 |
|
| 80 | private ClientboundPlayerInfoUpdatePacket(final RegistryFriendlyByteBuf input) {
|
| 81 | this.actions = input.readEnumSet(ClientboundPlayerInfoUpdatePacket.Action.class);
|
| 82 | this.entries = entryCodec(this.actions).apply(ByteBufCodecs.list()).decode(input);
|
| 83 | }
|
| 84 |
|
| 85 | private void write(final RegistryFriendlyByteBuf output) {
|
| 86 | output.writeEnumSet(this.actions, ClientboundPlayerInfoUpdatePacket.Action.class);
|
| 87 | entryCodec(this.actions).apply(ByteBufCodecs.list()).encode(output, this.entries);
|
| 88 | }
|
| 89 |
|
| 90 | @Override
|
| 91 | public PacketType<ClientboundPlayerInfoUpdatePacket> type() {
|
| 92 | return GamePacketTypes.CLIENTBOUND_PLAYER_INFO_UPDATE;
|
| 93 | }
|
| 94 |
|
| 95 | public void handle(final ClientGamePacketListener listener) {
|
| 96 | listener.handlePlayerInfoUpdate(this);
|
| 97 | }
|
| 98 |
|
| 99 | public EnumSet<ClientboundPlayerInfoUpdatePacket.Action> actions() {
|
| 100 | return this.actions;
|
| 101 | }
|
| 102 |
|
| 103 | public List<ClientboundPlayerInfoUpdatePacket.Entry> entries() {
|
| 104 | return this.entries;
|
| 105 | }
|
| 106 |
|
| 107 | public List<ClientboundPlayerInfoUpdatePacket.Entry> newEntries() {
|
| 108 | return this.actions.contains(ClientboundPlayerInfoUpdatePacket.Action.ADD_PLAYER) ? this.entries : List.of();
|
| 109 | }
|
| 110 |
|
| 111 | @Override
|
| 112 | public String toString() {
|
| 113 | return MoreObjects.toStringHelper(this).add("actions", this.actions).add("entries", this.entries).toString();
|
| 114 | }
|
| 115 |
|
| 116 | public enum Action {
|
| 117 | ADD_PLAYER(0, (entry, input) -> {
|
| 118 | String name = ByteBufCodecs.PLAYER_NAME.decode(input);
|
| 119 | PropertyMap properties = ByteBufCodecs.GAME_PROFILE_PROPERTIES.decode(input);
|
| 120 | entry.profile = new GameProfile(entry.profileId, name, properties);
|
| 121 | }, (output, entry) -> {
|
| 122 | GameProfile profile = Objects.requireNonNull(entry.profile());
|
| 123 | ByteBufCodecs.PLAYER_NAME.encode(output, profile.name());
|
| 124 | ByteBufCodecs.GAME_PROFILE_PROPERTIES.encode(output, profile.properties());
|
| 125 | }),
|
| 126 | INITIALIZE_CHAT(
|
| 127 | 1,
|
| 128 | (entry, input) -> entry.chatSession = input.readNullable(RemoteChatSession.Data.STREAM_CODEC),
|
| 129 | (output, entry) -> output.writeNullable(entry.chatSession, RemoteChatSession.Data.STREAM_CODEC)
|
| 130 | ),
|
| 131 | UPDATE_GAME_MODE(
|
| 132 | 2, (entry, input) -> entry.gameMode = GameType.STREAM_CODEC.decode(input), (output, entry) -> GameType.STREAM_CODEC.encode(output, entry.gameMode)
|
| 133 | ),
|
| 134 | UPDATE_LISTED(3, (entry, input) -> entry.listed = input.readBoolean(), (output, entry) -> output.writeBoolean(entry.listed())),
|
| 135 | UPDATE_LATENCY(4, (entry, input) -> entry.latency = input.readVarInt(), (output, entry) -> output.writeVarInt(entry.latency())),
|
| 136 | UPDATE_DISPLAY_NAME(
|
| 137 | 5,
|
| 138 | (entry, input) -> entry.displayName = FriendlyByteBuf.readNullable(input, ComponentSerialization.TRUSTED_STREAM_CODEC),
|
| 139 | (output, entry) -> FriendlyByteBuf.writeNullable(output, entry.displayName(), ComponentSerialization.TRUSTED_STREAM_CODEC)
|
| 140 | ),
|
| 141 | UPDATE_LIST_ORDER(6, (entry, input) -> entry.listOrder = input.readVarInt(), (output, entry) -> output.writeVarInt(entry.listOrder)),
|
| 142 | UPDATE_HAT(7, (entry, input) -> entry.showHat = input.readBoolean(), (output, entry) -> output.writeBoolean(entry.showHat));
|
| 143 |
|
| 144 | private final int id;
|
| 145 | private final ClientboundPlayerInfoUpdatePacket.Action.Reader reader;
|
| 146 | private final ClientboundPlayerInfoUpdatePacket.Action.Writer writer;
|
| 147 | private static final IntFunction<ClientboundPlayerInfoUpdatePacket.Action> BY_ID = ByIdMap.continuous(a -> a.id, values(), ByIdMap.OutOfBoundsStrategy.ZERO);
|
| 148 | public static final StreamCodec<ByteBuf, ClientboundPlayerInfoUpdatePacket.Action> STREAM_CODEC = ByteBufCodecs.idMapper(BY_ID, a -> a.id);
|
| 149 |
|
| 150 | Action(final int id, final ClientboundPlayerInfoUpdatePacket.Action.Reader reader, final ClientboundPlayerInfoUpdatePacket.Action.Writer writer) {
|
| 151 | this.id = id;
|
| 152 | this.reader = reader;
|
| 153 | this.writer = writer;
|
| 154 | }
|
| 155 |
|
| 156 | public interface Reader {
|
| 157 | void read(ClientboundPlayerInfoUpdatePacket.EntryBuilder entry, RegistryFriendlyByteBuf input);
|
| 158 | }
|
| 159 |
|
| 160 | public interface Writer {
|
| 161 | void write(RegistryFriendlyByteBuf output, ClientboundPlayerInfoUpdatePacket.Entry entry);
|
| 162 | }
|
| 163 | }
|
| 164 |
|
| 165 | public record Entry(
|
| 166 | UUID profileId,
|
| 167 | @Nullable GameProfile profile,
|
| 168 | boolean listed,
|
| 169 | int latency,
|
| 170 | GameType gameMode,
|
| 171 | @Nullable Component displayName,
|
| 172 | boolean showHat,
|
| 173 | int listOrder,
|
| 174 | RemoteChatSession.@Nullable Data chatSession
|
| 175 | ) {
|
| 176 | private Entry(final ServerPlayer player) {
|
| 177 | this(
|
| 178 | player.getUUID(),
|
| 179 | player.getGameProfile(),
|
| 180 | true,
|
| 181 | player.connection.latency(),
|
| 182 | player.gameMode(),
|
| 183 | player.getTabListDisplayName(),
|
| 184 | player.isModelPartShown(PlayerModelPart.HAT),
|
| 185 | player.getTabListOrder(),
|
| 186 | Optionull.map(player.getChatSession(), RemoteChatSession::asData)
|
| 187 | );
|
| 188 | }
|
| 189 | }
|
| 190 |
|
| 191 | private static class EntryBuilder {
|
| 192 | private final UUID profileId;
|
| 193 | private @Nullable GameProfile profile;
|
| 194 | private boolean listed;
|
| 195 | private int latency;
|
| 196 | private GameType gameMode = GameType.DEFAULT_MODE;
|
| 197 | private @Nullable Component displayName;
|
| 198 | private boolean showHat;
|
| 199 | private int listOrder;
|
| 200 | private RemoteChatSession.@Nullable Data chatSession;
|
| 201 |
|
| 202 | private EntryBuilder(final UUID profileId) {
|
| 203 | this.profileId = profileId;
|
| 204 | }
|
| 205 |
|
| 206 | private ClientboundPlayerInfoUpdatePacket.Entry build() {
|
| 207 | return new ClientboundPlayerInfoUpdatePacket.Entry(
|
| 208 | this.profileId, this.profile, this.listed, this.latency, this.gameMode, this.displayName, this.showHat, this.listOrder, this.chatSession
|
| 209 | );
|
| 210 | }
|
| 211 | }
|
| 212 | }
|
| 213 |
|