ClientboundSetPlayerTeamPacket .java

net.minecraft.network.protocol.game.ClientboundSetPlayerTeamPacket (class, 176 lines): members: PLAYER_LIST_STREAM_CODEC, createAddOrModifyPacket, method.

net.minecraft.network.protocol.gameclass176 linescommon6.1 KB

Implements

API Surface (9)

ClientboundSetPlayerTeamPacket createAddOrModifyPacket(final PlayerTeam team, final boolean createNew)
ClientboundSetPlayerTeamPacket createRemovePacket(final PlayerTeam team)
ClientboundSetPlayerTeamPacket createPlayerPacket(final PlayerTeam team, final String player, final ClientboundSetPlayerTeamPacket.Action action)
PacketType<ClientboundSetPlayerTeamPacket> type()
void handle(final ClientGamePacketListener listener)
String getName()
Collection<String> getPlayers()
Optional<ClientboundSetPlayerTeamPacket.Parameters> getParameters()
record Parameters(Component displayName, Component playerPrefix, Component playerSuffix, Team.Visibility nameTagVisibility, Team.CollisionRule collisionRule, Optional<TeamColor> color, @PlayerTeam.OptionFlags byte options)

Fields & Constants (17)

PLAYER_LIST_STREAM_CODEC
STREAM_CODEC
METHOD_ADD
METHOD_REMOVE
METHOD_CHANGE
METHOD_JOIN
METHOD_LEAVE
method
name
players
parameters
displayName
playerPrefix
playerSuffix
nameTagVisibility
collisionRule
color

String literals (1)

"Parameters not present, but method is"

Same Package (197)

Full Source

ClientboundSetPlayerTeamPacket.java176 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.network.protocol.game;
2
3
import io.netty.buffer.ByteBuf;
4
import java.util.Collection;
5
import java.util.List;
6
import java.util.Optional;
7
import net.minecraft.network.RegistryFriendlyByteBuf;
8
import net.minecraft.network.chat.Component;
9
import net.minecraft.network.chat.ComponentSerialization;
10
import net.minecraft.network.codec.ByteBufCodecs;
11
import net.minecraft.network.codec.StreamCodec;
12
import net.minecraft.network.protocol.Packet;
13
import net.minecraft.network.protocol.PacketType;
14
import net.minecraft.world.scores.PlayerTeam;
15
import net.minecraft.world.scores.Team;
16
import net.minecraft.world.scores.TeamColor;
17
import org.jspecify.annotations.Nullable;
18
19
public class ClientboundSetPlayerTeamPacket implements Packet<ClientGamePacketListener> {
20
	private static final StreamCodec<ByteBuf, List<String>> PLAYER_LIST_STREAM_CODEC = ByteBufCodecs.STRING_UTF8.apply(ByteBufCodecs.list());
21
	public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundSetPlayerTeamPacket> STREAM_CODEC = Packet.codec(
22
		ClientboundSetPlayerTeamPacket::write, ClientboundSetPlayerTeamPacket::new
23
	);
24
	private static final int METHOD_ADD = 0;
25
	private static final int METHOD_REMOVE = 1;
26
	private static final int METHOD_CHANGE = 2;
27
	private static final int METHOD_JOIN = 3;
28
	private static final int METHOD_LEAVE = 4;
29
	private final int method;
30
	private final String name;
31
	private final List<String> players;
32
	private final Optional<ClientboundSetPlayerTeamPacket.Parameters> parameters;
33
34
	private ClientboundSetPlayerTeamPacket(
35
		final String name, final int method, final Optional<ClientboundSetPlayerTeamPacket.Parameters> parameters, final Collection<String> players
36
	) {
37
		this.name = name;
38
		this.method = method;
39
		this.parameters = parameters;
40
		this.players = List.copyOf(players);
41
	}
42
43
	public static ClientboundSetPlayerTeamPacket createAddOrModifyPacket(final PlayerTeam team, final boolean createNew) {
44
		return new ClientboundSetPlayerTeamPacket(
45
			team.getName(), createNew ? 0 : 2, Optional.of(new ClientboundSetPlayerTeamPacket.Parameters(team)), createNew ? team.getPlayers() : List.of()
46
		);
47
	}
48
49
	public static ClientboundSetPlayerTeamPacket createRemovePacket(final PlayerTeam team) {
50
		return new ClientboundSetPlayerTeamPacket(team.getName(), 1, Optional.empty(), List.of());
51
	}
52
53
	public static ClientboundSetPlayerTeamPacket createPlayerPacket(final PlayerTeam team, final String player, final ClientboundSetPlayerTeamPacket.Action action) {
54
		return new ClientboundSetPlayerTeamPacket(team.getName(), action == ClientboundSetPlayerTeamPacket.Action.ADD ? 3 : 4, Optional.empty(), List.of(player));
55
	}
56
57
	private ClientboundSetPlayerTeamPacket(final RegistryFriendlyByteBuf input) {
58
		this.name = input.readUtf();
59
		this.method = input.readByte();
60
		if (shouldHaveParameters(this.method)) {
61
			this.parameters = Optional.of(ClientboundSetPlayerTeamPacket.Parameters.STREAM_CODEC.decode(input));
62
		} else {
63
			this.parameters = Optional.empty();
64
		}
65
66
		if (shouldHavePlayerList(this.method)) {
67
			this.players = PLAYER_LIST_STREAM_CODEC.decode(input);
68
		} else {
69
			this.players = List.of();
70
		}
71
	}
72
73
	private void write(final RegistryFriendlyByteBuf output) {
74
		output.writeUtf(this.name);
75
		output.writeByte(this.method);
76
		if (shouldHaveParameters(this.method)) {
77
			ClientboundSetPlayerTeamPacket.Parameters.STREAM_CODEC
78
				.encode(output, this.parameters.orElseThrow(() -> new IllegalStateException("Parameters not present, but method is" + this.method)));
79
		}
80
81
		if (shouldHavePlayerList(this.method)) {
82
			PLAYER_LIST_STREAM_CODEC.encode(output, this.players);
83
		}
84
	}
85
86
	private static boolean shouldHavePlayerList(final int method) {
87
		return method == 0 || method == 3 || method == 4;
88
	}
89
90
	private static boolean shouldHaveParameters(final int method) {
91
		return method == 0 || method == 2;
92
	}
93
94
	public ClientboundSetPlayerTeamPacket.@Nullable Action getPlayerAction() {
95
		return switch (this.method) {
96
			case 0, 3 -> ClientboundSetPlayerTeamPacket.Action.ADD;
97
			default -> null;
98
			case 4 -> ClientboundSetPlayerTeamPacket.Action.REMOVE;
99
		};
100
	}
101
102
	public ClientboundSetPlayerTeamPacket.@Nullable Action getTeamAction() {
103
		return switch (this.method) {
104
			case 0 -> ClientboundSetPlayerTeamPacket.Action.ADD;
105
			case 1 -> ClientboundSetPlayerTeamPacket.Action.REMOVE;
106
			default -> null;
107
		};
108
	}
109
110
	@Override
111
	public PacketType<ClientboundSetPlayerTeamPacket> type() {
112
		return GamePacketTypes.CLIENTBOUND_SET_PLAYER_TEAM;
113
	}
114
115
	public void handle(final ClientGamePacketListener listener) {
116
		listener.handleSetPlayerTeamPacket(this);
117
	}
118
119
	public String getName() {
120
		return this.name;
121
	}
122
123
	public Collection<String> getPlayers() {
124
		return this.players;
125
	}
126
127
	public Optional<ClientboundSetPlayerTeamPacket.Parameters> getParameters() {
128
		return this.parameters;
129
	}
130
131
	public enum Action {
132
		ADD,
133
		REMOVE;
134
	}
135
136
	public record Parameters(
137
		Component displayName,
138
		Component playerPrefix,
139
		Component playerSuffix,
140
		Team.Visibility nameTagVisibility,
141
		Team.CollisionRule collisionRule,
142
		Optional<TeamColor> color,
143
		@PlayerTeam.OptionFlags byte options
144
	) {
145
		public static final StreamCodec<RegistryFriendlyByteBuf, ClientboundSetPlayerTeamPacket.Parameters> STREAM_CODEC = StreamCodec.composite(
146
			ComponentSerialization.TRUSTED_STREAM_CODEC,
147
			ClientboundSetPlayerTeamPacket.Parameters::displayName,
148
			ComponentSerialization.TRUSTED_STREAM_CODEC,
149
			ClientboundSetPlayerTeamPacket.Parameters::playerPrefix,
150
			ComponentSerialization.TRUSTED_STREAM_CODEC,
151
			ClientboundSetPlayerTeamPacket.Parameters::playerSuffix,
152
			Team.Visibility.STREAM_CODEC,
153
			ClientboundSetPlayerTeamPacket.Parameters::nameTagVisibility,
154
			Team.CollisionRule.STREAM_CODEC,
155
			ClientboundSetPlayerTeamPacket.Parameters::collisionRule,
156
			ByteBufCodecs.optional(TeamColor.STREAM_CODEC),
157
			ClientboundSetPlayerTeamPacket.Parameters::color,
158
			ByteBufCodecs.BYTE,
159
			ClientboundSetPlayerTeamPacket.Parameters::options,
160
			ClientboundSetPlayerTeamPacket.Parameters::new
161
		);
162
163
		public Parameters(final PlayerTeam team) {
164
			this(
165
				team.getDisplayName(),
166
				team.getPlayerPrefix(),
167
				team.getPlayerSuffix(),
168
				team.getNameTagVisibility(),
169
				team.getCollisionRule(),
170
				team.getColor(),
171
				team.packOptions()
172
			);
173
		}
174
	}
175
}
176

Decompiled Minecraft 26.3 source of ClientboundSetPlayerTeamPacket (net/minecraft/network/protocol/game/ClientboundSetPlayerTeamPacket.java) with Mojang official mappings, stored verbatim on the site.