TeamCommand .java

net.minecraft.server.commands.TeamCommand (class, 403 lines): members: ERROR_TEAM_ALREADY_FRIENDLYINVISIBLES_DISABLED, ERROR_TEAM_COLLISION_UNCHANGED.

net.minecraft.server.commandsclass403 linesserver18.6 KB

API Surface (1)

void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context)

Fields & Constants (13)

ERROR_TEAM_ALREADY_EXISTS
ERROR_TEAM_ALREADY_EMPTY
ERROR_TEAM_ALREADY_NAME
ERROR_TEAM_ALREADY_COLOR
ERROR_TEAM_ALREADY_FRIENDLYFIRE_ENABLED
ERROR_TEAM_ALREADY_FRIENDLYFIRE_DISABLED
ERROR_TEAM_ALREADY_FRIENDLYINVISIBLES_ENABLED
ERROR_TEAM_ALREADY_FRIENDLYINVISIBLES_DISABLED
ERROR_TEAM_NAMETAG_VISIBLITY_UNCHANGED
ERROR_TEAM_DEATH_MESSAGE_VISIBLITY_UNCHANGED
ERROR_TEAM_COLLISION_UNCHANGED
RESPONSE_LEAVE
RESPONSE_JOIN

String literals (54)

"commands.team.add.duplicate"
"commands.team.empty.unchanged"
"commands.team.option.name.unchanged"
"commands.team.option.color.unchanged"
"commands.team.option.friendlyfire.alreadyEnabled"
"commands.team.option.nametagVisibility.unchanged"
"commands.team.option.collisionRule.unchanged"
"commands.team.leave.success.single"
"commands.team.leave.success.multiple"
"commands.team.join.success.single"
"commands.team.join.success.multiple"
"team"
"list"
"displayName"
"remove"
"empty"
"join"
"members"
"leave"
"modify"
"color"
"reset"
"friendlyFire"
"allowed"

Full Source

TeamCommand.java403 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.google.common.collect.Lists;
4
import com.mojang.brigadier.CommandDispatcher;
5
import com.mojang.brigadier.arguments.BoolArgumentType;
6
import com.mojang.brigadier.arguments.StringArgumentType;
7
import com.mojang.brigadier.exceptions.CommandSyntaxException;
8
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
9
import java.util.Collection;
10
import java.util.Collections;
11
import java.util.Optional;
12
import net.minecraft.commands.CommandBuildContext;
13
import net.minecraft.commands.CommandSourceStack;
14
import net.minecraft.commands.Commands;
15
import net.minecraft.commands.arguments.ComponentArgument;
16
import net.minecraft.commands.arguments.ScoreHolderArgument;
17
import net.minecraft.commands.arguments.TeamArgument;
18
import net.minecraft.commands.arguments.TeamColorArgument;
19
import net.minecraft.network.chat.Component;
20
import net.minecraft.network.chat.ComponentUtils;
21
import net.minecraft.world.scores.PlayerTeam;
22
import net.minecraft.world.scores.ScoreHolder;
23
import net.minecraft.world.scores.Scoreboard;
24
import net.minecraft.world.scores.Team;
25
import net.minecraft.world.scores.TeamColor;
26
27
public class TeamCommand {
28
	private static final SimpleCommandExceptionType ERROR_TEAM_ALREADY_EXISTS = new SimpleCommandExceptionType(
29
		Component.translatable("commands.team.add.duplicate")
30
	);
31
	private static final SimpleCommandExceptionType ERROR_TEAM_ALREADY_EMPTY = new SimpleCommandExceptionType(
32
		Component.translatable("commands.team.empty.unchanged")
33
	);
34
	private static final SimpleCommandExceptionType ERROR_TEAM_ALREADY_NAME = new SimpleCommandExceptionType(
35
		Component.translatable("commands.team.option.name.unchanged")
36
	);
37
	private static final SimpleCommandExceptionType ERROR_TEAM_ALREADY_COLOR = new SimpleCommandExceptionType(
38
		Component.translatable("commands.team.option.color.unchanged")
39
	);
40
	private static final SimpleCommandExceptionType ERROR_TEAM_ALREADY_FRIENDLYFIRE_ENABLED = new SimpleCommandExceptionType(
41
		Component.translatable("commands.team.option.friendlyfire.alreadyEnabled")
42
	);
43
	private static final SimpleCommandExceptionType ERROR_TEAM_ALREADY_FRIENDLYFIRE_DISABLED = new SimpleCommandExceptionType(
44
		Component.translatable("commands.team.option.friendlyfire.alreadyDisabled")
45
	);
46
	private static final SimpleCommandExceptionType ERROR_TEAM_ALREADY_FRIENDLYINVISIBLES_ENABLED = new SimpleCommandExceptionType(
47
		Component.translatable("commands.team.option.seeFriendlyInvisibles.alreadyEnabled")
48
	);
49
	private static final SimpleCommandExceptionType ERROR_TEAM_ALREADY_FRIENDLYINVISIBLES_DISABLED = new SimpleCommandExceptionType(
50
		Component.translatable("commands.team.option.seeFriendlyInvisibles.alreadyDisabled")
51
	);
52
	private static final SimpleCommandExceptionType ERROR_TEAM_NAMETAG_VISIBLITY_UNCHANGED = new SimpleCommandExceptionType(
53
		Component.translatable("commands.team.option.nametagVisibility.unchanged")
54
	);
55
	private static final SimpleCommandExceptionType ERROR_TEAM_DEATH_MESSAGE_VISIBLITY_UNCHANGED = new SimpleCommandExceptionType(
56
		Component.translatable("commands.team.option.deathMessageVisibility.unchanged")
57
	);
58
	private static final SimpleCommandExceptionType ERROR_TEAM_COLLISION_UNCHANGED = new SimpleCommandExceptionType(
59
		Component.translatable("commands.team.option.collisionRule.unchanged")
60
	);
61
	private static final CommandResponseTracker.Messages<ScoreHolder> RESPONSE_LEAVE = CommandResponseTracker.messages(
62
		(holder, var1) -> Component.translatable("commands.team.leave.success.single", holder.getFeedbackDisplayName()),
63
		(holderCount, var1) -> Component.translatable("commands.team.leave.success.multiple", holderCount)
64
	);
65
	private static final CommandResponseTracker.MessagesWithArg<ScoreHolder, PlayerTeam> RESPONSE_JOIN = CommandResponseTracker.messages(
66
		(holder, var1, team) -> Component.translatable("commands.team.join.success.single", holder.getFeedbackDisplayName(), team.getFormattedDisplayName()),
67
		(holderCount, var1, team) -> Component.translatable("commands.team.join.success.multiple", holderCount, team.getFormattedDisplayName())
68
	);
69
70
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
71
		dispatcher.register(
72
			Commands.literal("team")
73
				.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
74
				.then(
75
					Commands.literal("list")
76
						.executes(c -> listTeams(c.getSource()))
77
						.then(Commands.argument("team", TeamArgument.team()).executes(c -> listMembers(c.getSource(), TeamArgument.getTeam(c, "team"))))
78
				)
79
				.then(
80
					Commands.literal("add")
81
						.then(
82
							Commands.argument("team", StringArgumentType.word())
83
								.executes(c -> createTeam(c.getSource(), StringArgumentType.getString(c, "team")))
84
								.then(
85
									Commands.argument("displayName", ComponentArgument.textComponent(context))
86
										.executes(c -> createTeam(c.getSource(), StringArgumentType.getString(c, "team"), ComponentArgument.getResolvedComponent(c, "displayName")))
87
								)
88
						)
89
				)
90
				.then(
91
					Commands.literal("remove").then(Commands.argument("team", TeamArgument.team()).executes(c -> deleteTeam(c.getSource(), TeamArgument.getTeam(c, "team"))))
92
				)
93
				.then(
94
					Commands.literal("empty").then(Commands.argument("team", TeamArgument.team()).executes(c -> emptyTeam(c.getSource(), TeamArgument.getTeam(c, "team"))))
95
				)
96
				.then(
97
					Commands.literal("join")
98
						.then(
99
							Commands.argument("team", TeamArgument.team())
100
								.executes(c -> joinTeam(c.getSource(), TeamArgument.getTeam(c, "team"), Collections.singleton(c.getSource().getEntityOrException())))
101
								.then(
102
									Commands.argument("members", ScoreHolderArgument.scoreHolders())
103
										.suggests(ScoreHolderArgument.SUGGEST_SCORE_HOLDERS)
104
										.executes(c -> joinTeam(c.getSource(), TeamArgument.getTeam(c, "team"), ScoreHolderArgument.getNamesWithDefaultWildcard(c, "members")))
105
								)
106
						)
107
				)
108
				.then(
109
					Commands.literal("leave")
110
						.then(
111
							Commands.argument("members", ScoreHolderArgument.scoreHolders())
112
								.suggests(ScoreHolderArgument.SUGGEST_SCORE_HOLDERS)
113
								.executes(c -> leaveTeam(c.getSource(), ScoreHolderArgument.getNamesWithDefaultWildcard(c, "members")))
114
						)
115
				)
116
				.then(
117
					Commands.literal("modify")
118
						.then(
119
							Commands.argument("team", TeamArgument.team())
120
								.then(
121
									Commands.literal("displayName")
122
										.then(
123
											Commands.argument("displayName", ComponentArgument.textComponent(context))
124
												.executes(c -> setDisplayName(c.getSource(), TeamArgument.getTeam(c, "team"), ComponentArgument.getResolvedComponent(c, "displayName")))
125
										)
126
								)
127
								.then(
128
									Commands.literal("color")
129
										.then(Commands.literal("reset").executes(c -> clearColor(c.getSource(), TeamArgument.getTeam(c, "team"))))
130
										.then(
131
											Commands.argument("value", TeamColorArgument.teamColor())
132
												.executes(c -> setColor(c.getSource(), TeamArgument.getTeam(c, "team"), TeamColorArgument.getTeamColor(c, "value")))
133
										)
134
								)
135
								.then(
136
									Commands.literal("friendlyFire")
137
										.then(
138
											Commands.argument("allowed", BoolArgumentType.bool())
139
												.executes(c -> setFriendlyFire(c.getSource(), TeamArgument.getTeam(c, "team"), BoolArgumentType.getBool(c, "allowed")))
140
										)
141
								)
142
								.then(
143
									Commands.literal("seeFriendlyInvisibles")
144
										.then(
145
											Commands.argument("allowed", BoolArgumentType.bool())
146
												.executes(c -> setFriendlySight(c.getSource(), TeamArgument.getTeam(c, "team"), BoolArgumentType.getBool(c, "allowed")))
147
										)
148
								)
149
								.then(
150
									Commands.literal("nametagVisibility")
151
										.then(Commands.literal("never").executes(c -> setNameTagVisibility(c.getSource(), TeamArgument.getTeam(c, "team"), Team.Visibility.NEVER)))
152
										.then(
153
											Commands.literal("hideForOtherTeams")
154
												.executes(c -> setNameTagVisibility(c.getSource(), TeamArgument.getTeam(c, "team"), Team.Visibility.HIDE_FOR_OTHER_TEAMS))
155
										)
156
										.then(
157
											Commands.literal("hideForOwnTeam")
158
												.executes(c -> setNameTagVisibility(c.getSource(), TeamArgument.getTeam(c, "team"), Team.Visibility.HIDE_FOR_OWN_TEAM))
159
										)
160
										.then(Commands.literal("always").executes(c -> setNameTagVisibility(c.getSource(), TeamArgument.getTeam(c, "team"), Team.Visibility.ALWAYS)))
161
								)
162
								.then(
163
									Commands.literal("deathMessageVisibility")
164
										.then(Commands.literal("never").executes(c -> setDeathMessageVisibility(c.getSource(), TeamArgument.getTeam(c, "team"), Team.Visibility.NEVER)))
165
										.then(
166
											Commands.literal("hideForOtherTeams")
167
												.executes(c -> setDeathMessageVisibility(c.getSource(), TeamArgument.getTeam(c, "team"), Team.Visibility.HIDE_FOR_OTHER_TEAMS))
168
										)
169
										.then(
170
											Commands.literal("hideForOwnTeam")
171
												.executes(c -> setDeathMessageVisibility(c.getSource(), TeamArgument.getTeam(c, "team"), Team.Visibility.HIDE_FOR_OWN_TEAM))
172
										)
173
										.then(Commands.literal("always").executes(c -> setDeathMessageVisibility(c.getSource(), TeamArgument.getTeam(c, "team"), Team.Visibility.ALWAYS)))
174
								)
175
								.then(
176
									Commands.literal("collisionRule")
177
										.then(Commands.literal("never").executes(c -> setCollision(c.getSource(), TeamArgument.getTeam(c, "team"), Team.CollisionRule.NEVER)))
178
										.then(Commands.literal("pushOwnTeam").executes(c -> setCollision(c.getSource(), TeamArgument.getTeam(c, "team"), Team.CollisionRule.PUSH_OWN_TEAM)))
179
										.then(
180
											Commands.literal("pushOtherTeams").executes(c -> setCollision(c.getSource(), TeamArgument.getTeam(c, "team"), Team.CollisionRule.PUSH_OTHER_TEAMS))
181
										)
182
										.then(Commands.literal("always").executes(c -> setCollision(c.getSource(), TeamArgument.getTeam(c, "team"), Team.CollisionRule.ALWAYS)))
183
								)
184
								.then(
185
									Commands.literal("prefix")
186
										.then(
187
											Commands.argument("prefix", ComponentArgument.textComponent(context))
188
												.executes(c -> setPrefix(c.getSource(), TeamArgument.getTeam(c, "team"), ComponentArgument.getResolvedComponent(c, "prefix")))
189
										)
190
								)
191
								.then(
192
									Commands.literal("suffix")
193
										.then(
194
											Commands.argument("suffix", ComponentArgument.textComponent(context))
195
												.executes(c -> setSuffix(c.getSource(), TeamArgument.getTeam(c, "team"), ComponentArgument.getResolvedComponent(c, "suffix")))
196
										)
197
								)
198
						)
199
				)
200
		);
201
	}
202
203
	private static int leaveTeam(final CommandSourceStack source, final Collection<ScoreHolder> members) throws CommandSyntaxException {
204
		CommandResponseTracker<ScoreHolder> tracker = CommandResponseTracker.create();
205
		Scoreboard scoreboard = source.getServer().getScoreboard();
206
207
		for (ScoreHolder member : members) {
208
			tracker.track(member, scoreboard.removePlayerFromTeam(member.getScoreboardName()));
209
		}
210
211
		return tracker.sendFeedback(source, true, RESPONSE_LEAVE);
212
	}
213
214
	private static int joinTeam(final CommandSourceStack source, final PlayerTeam team, final Collection<ScoreHolder> members) throws CommandSyntaxException {
215
		CommandResponseTracker<ScoreHolder> tracker = CommandResponseTracker.create();
216
		Scoreboard scoreboard = source.getServer().getScoreboard();
217
218
		for (ScoreHolder member : members) {
219
			tracker.track(member, scoreboard.addPlayerToTeam(member.getScoreboardName(), team));
220
		}
221
222
		return tracker.sendFeedback(source, true, RESPONSE_JOIN, team);
223
	}
224
225
	private static int setNameTagVisibility(final CommandSourceStack source, final PlayerTeam team, final Team.Visibility visibility) throws CommandSyntaxException {
226
		if (team.getNameTagVisibility() == visibility) {
227
			throw ERROR_TEAM_NAMETAG_VISIBLITY_UNCHANGED.create();
228
		}
229
230
		team.setNameTagVisibility(visibility);
231
		source.sendSuccess(
232
			() -> Component.translatable("commands.team.option.nametagVisibility.success", team.getFormattedDisplayName(), visibility.getDisplayName()), true
233
		);
234
		return 0;
235
	}
236
237
	private static int setDeathMessageVisibility(final CommandSourceStack source, final PlayerTeam team, final Team.Visibility visibility) throws CommandSyntaxException {
238
		if (team.getDeathMessageVisibility() == visibility) {
239
			throw ERROR_TEAM_DEATH_MESSAGE_VISIBLITY_UNCHANGED.create();
240
		}
241
242
		team.setDeathMessageVisibility(visibility);
243
		source.sendSuccess(
244
			() -> Component.translatable("commands.team.option.deathMessageVisibility.success", team.getFormattedDisplayName(), visibility.getDisplayName()), true
245
		);
246
		return 0;
247
	}
248
249
	private static int setCollision(final CommandSourceStack source, final PlayerTeam team, final Team.CollisionRule collision) throws CommandSyntaxException {
250
		if (team.getCollisionRule() == collision) {
251
			throw ERROR_TEAM_COLLISION_UNCHANGED.create();
252
		}
253
254
		team.setCollisionRule(collision);
255
		source.sendSuccess(
256
			() -> Component.translatable("commands.team.option.collisionRule.success", team.getFormattedDisplayName(), collision.getDisplayName()), true
257
		);
258
		return 0;
259
	}
260
261
	private static int setFriendlySight(final CommandSourceStack source, final PlayerTeam team, final boolean allowed) throws CommandSyntaxException {
262
		if (team.canSeeFriendlyInvisibles() == allowed) {
263
			if (allowed) {
264
				throw ERROR_TEAM_ALREADY_FRIENDLYINVISIBLES_ENABLED.create();
265
			} else {
266
				throw ERROR_TEAM_ALREADY_FRIENDLYINVISIBLES_DISABLED.create();
267
			}
268
		} else {
269
			team.setSeeFriendlyInvisibles(allowed);
270
			source.sendSuccess(
271
				() -> Component.translatable("commands.team.option.seeFriendlyInvisibles." + (allowed ? "enabled" : "disabled"), team.getFormattedDisplayName()), true
272
			);
273
			return 0;
274
		}
275
	}
276
277
	private static int setFriendlyFire(final CommandSourceStack source, final PlayerTeam team, final boolean allowed) throws CommandSyntaxException {
278
		if (team.isAllowFriendlyFire() == allowed) {
279
			if (allowed) {
280
				throw ERROR_TEAM_ALREADY_FRIENDLYFIRE_ENABLED.create();
281
			} else {
282
				throw ERROR_TEAM_ALREADY_FRIENDLYFIRE_DISABLED.create();
283
			}
284
		} else {
285
			team.setAllowFriendlyFire(allowed);
286
			source.sendSuccess(
287
				() -> Component.translatable("commands.team.option.friendlyfire." + (allowed ? "enabled" : "disabled"), team.getFormattedDisplayName()), true
288
			);
289
			return 0;
290
		}
291
	}
292
293
	private static int setDisplayName(final CommandSourceStack source, final PlayerTeam team, final Component displayName) throws CommandSyntaxException {
294
		if (team.getDisplayName().equals(displayName)) {
295
			throw ERROR_TEAM_ALREADY_NAME.create();
296
		}
297
298
		team.setDisplayName(displayName);
299
		source.sendSuccess(() -> Component.translatable("commands.team.option.name.success", team.getFormattedDisplayName()), true);
300
		return 0;
301
	}
302
303
	private static int setColor(final CommandSourceStack source, final PlayerTeam team, final TeamColor color) throws CommandSyntaxException {
304
		Optional<TeamColor> currentColor = team.getColor();
305
		if (currentColor.isPresent() && currentColor.get().equals(color)) {
306
			throw ERROR_TEAM_ALREADY_COLOR.create();
307
		}
308
309
		team.setColor(Optional.of(color));
310
		source.sendSuccess(() -> Component.translatable("commands.team.option.color.success", team.getFormattedDisplayName(), color.getSerializedName()), true);
311
		return 0;
312
	}
313
314
	private static int clearColor(final CommandSourceStack source, final PlayerTeam team) throws CommandSyntaxException {
315
		Optional<TeamColor> currentColor = team.getColor();
316
		if (currentColor.isEmpty()) {
317
			throw ERROR_TEAM_ALREADY_COLOR.create();
318
		}
319
320
		team.setColor(Optional.empty());
321
		source.sendSuccess(() -> Component.translatable("commands.team.option.color.clear.success", team.getFormattedDisplayName()), true);
322
		return 0;
323
	}
324
325
	private static int emptyTeam(final CommandSourceStack source, final PlayerTeam team) throws CommandSyntaxException {
326
		Scoreboard scoreboard = source.getServer().getScoreboard();
327
		Collection<String> members = Lists.newArrayList(team.getPlayers());
328
		if (members.isEmpty()) {
329
			throw ERROR_TEAM_ALREADY_EMPTY.create();
330
		}
331
332
		for (String member : members) {
333
			scoreboard.removePlayerFromTeam(member, team);
334
		}
335
336
		source.sendSuccess(() -> Component.translatable("commands.team.empty.success", members.size(), team.getFormattedDisplayName()), true);
337
		return members.size();
338
	}
339
340
	private static int deleteTeam(final CommandSourceStack source, final PlayerTeam team) {
341
		Scoreboard scoreboard = source.getServer().getScoreboard();
342
		scoreboard.removePlayerTeam(team);
343
		source.sendSuccess(() -> Component.translatable("commands.team.remove.success", team.getFormattedDisplayName()), true);
344
		return scoreboard.getPlayerTeams().size();
345
	}
346
347
	private static int createTeam(final CommandSourceStack source, final String name) throws CommandSyntaxException {
348
		return createTeam(source, name, Component.literal(name));
349
	}
350
351
	private static int createTeam(final CommandSourceStack source, final String name, final Component displayName) throws CommandSyntaxException {
352
		Scoreboard scoreboard = source.getServer().getScoreboard();
353
		if (scoreboard.getPlayerTeam(name) != null) {
354
			throw ERROR_TEAM_ALREADY_EXISTS.create();
355
		}
356
357
		PlayerTeam team = scoreboard.addPlayerTeam(name);
358
		team.setDisplayName(displayName);
359
		source.sendSuccess(() -> Component.translatable("commands.team.add.success", team.getFormattedDisplayName()), true);
360
		return scoreboard.getPlayerTeams().size();
361
	}
362
363
	private static int listMembers(final CommandSourceStack source, final PlayerTeam team) {
364
		Collection<String> members = team.getPlayers();
365
		if (members.isEmpty()) {
366
			source.sendSuccess(() -> Component.translatable("commands.team.list.members.empty", team.getFormattedDisplayName()), false);
367
		} else {
368
			source.sendSuccess(
369
				() -> Component.translatable("commands.team.list.members.success", team.getFormattedDisplayName(), members.size(), ComponentUtils.formatList(members)),
370
				false
371
			);
372
		}
373
374
		return members.size();
375
	}
376
377
	private static int listTeams(final CommandSourceStack source) {
378
		Collection<PlayerTeam> teams = source.getServer().getScoreboard().getPlayerTeams();
379
		if (teams.isEmpty()) {
380
			source.sendSuccess(() -> Component.translatable("commands.team.list.teams.empty"), false);
381
		} else {
382
			source.sendSuccess(
383
				() -> Component.translatable("commands.team.list.teams.success", teams.size(), ComponentUtils.formatList(teams, PlayerTeam::getFormattedDisplayName)),
384
				false
385
			);
386
		}
387
388
		return teams.size();
389
	}
390
391
	private static int setPrefix(final CommandSourceStack source, final PlayerTeam team, final Component prefix) {
392
		team.setPlayerPrefix(prefix);
393
		source.sendSuccess(() -> Component.translatable("commands.team.option.prefix.success", prefix), false);
394
		return 1;
395
	}
396
397
	private static int setSuffix(final CommandSourceStack source, final PlayerTeam team, final Component suffix) {
398
		team.setPlayerSuffix(suffix);
399
		source.sendSuccess(() -> Component.translatable("commands.team.option.suffix.success", suffix), false);
400
		return 1;
401
	}
402
}
403

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