TitleCommand .java

net.minecraft.server.commands.TitleCommand (class, 171 lines): members: RESPONSE_SHOW_ACTIONBAR, RESPONSE_SHOW_SUBTITLE, RESPONSE_CLEAR_TITLE, packet.

net.minecraft.server.commandsclass171 linesserver7.6 KB

API Surface (1)

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

Fields & Constants (11)

RESPONSE_CLEAR_TITLE
RESPONSE_RESET_TITLE
RESPONSE_SHOW_TITLE
RESPONSE_SHOW_ACTIONBAR
RESPONSE_SHOW_SUBTITLE
RESPONSE_SET_TIMES
packet
source
targets
title
messages

String literals (21)

"commands.title.cleared.single"
"commands.title.cleared.multiple"
"commands.title.reset.single"
"commands.title.reset.multiple"
"commands.title.show.title.single"
"commands.title.show.title.multiple"
"commands.title.show.actionbar.single"
"commands.title.show.actionbar.multiple"
"commands.title.show.subtitle.single"
"commands.title.show.subtitle.multiple"
"commands.title.times.single"
"commands.title.times.multiple"
"targets"
"clear"
"reset"
"subtitle"
"actionbar"
"times"
"fadeIn"
"stay"
"fadeOut"

Full Source

TitleCommand.java171 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.mojang.brigadier.CommandDispatcher;
4
import com.mojang.brigadier.arguments.IntegerArgumentType;
5
import com.mojang.brigadier.exceptions.CommandSyntaxException;
6
import java.util.Collection;
7
import java.util.function.Function;
8
import net.minecraft.commands.CommandBuildContext;
9
import net.minecraft.commands.CommandSourceStack;
10
import net.minecraft.commands.Commands;
11
import net.minecraft.commands.arguments.ComponentArgument;
12
import net.minecraft.commands.arguments.EntityArgument;
13
import net.minecraft.commands.arguments.TimeArgument;
14
import net.minecraft.network.chat.Component;
15
import net.minecraft.network.chat.ComponentUtils;
16
import net.minecraft.network.chat.ResolutionContext;
17
import net.minecraft.network.protocol.Packet;
18
import net.minecraft.network.protocol.game.ClientboundClearTitlesPacket;
19
import net.minecraft.network.protocol.game.ClientboundSetActionBarTextPacket;
20
import net.minecraft.network.protocol.game.ClientboundSetSubtitleTextPacket;
21
import net.minecraft.network.protocol.game.ClientboundSetTitleTextPacket;
22
import net.minecraft.network.protocol.game.ClientboundSetTitlesAnimationPacket;
23
import net.minecraft.server.level.ServerPlayer;
24
25
public class TitleCommand {
26
	private static final CommandResponseTracker.Messages<ServerPlayer> RESPONSE_CLEAR_TITLE = CommandResponseTracker.messages(
27
		(player, var1) -> Component.translatable("commands.title.cleared.single", player.getDisplayName()),
28
		(playerCount, var1) -> Component.translatable("commands.title.cleared.multiple", playerCount)
29
	);
30
	private static final CommandResponseTracker.Messages<ServerPlayer> RESPONSE_RESET_TITLE = CommandResponseTracker.messages(
31
		(player, var1) -> Component.translatable("commands.title.reset.single", player.getDisplayName()),
32
		(playerCount, var1) -> Component.translatable("commands.title.reset.multiple", playerCount)
33
	);
34
	private static final CommandResponseTracker.Messages<ServerPlayer> RESPONSE_SHOW_TITLE = CommandResponseTracker.messages(
35
		(player, var1) -> Component.translatable("commands.title.show.title.single", player.getDisplayName()),
36
		(playerCount, var1) -> Component.translatable("commands.title.show.title.multiple", playerCount)
37
	);
38
	private static final CommandResponseTracker.Messages<ServerPlayer> RESPONSE_SHOW_ACTIONBAR = CommandResponseTracker.messages(
39
		(player, var1) -> Component.translatable("commands.title.show.actionbar.single", player.getDisplayName()),
40
		(playerCount, var1) -> Component.translatable("commands.title.show.actionbar.multiple", playerCount)
41
	);
42
	private static final CommandResponseTracker.Messages<ServerPlayer> RESPONSE_SHOW_SUBTITLE = CommandResponseTracker.messages(
43
		(player, var1) -> Component.translatable("commands.title.show.subtitle.single", player.getDisplayName()),
44
		(playerCount, var1) -> Component.translatable("commands.title.show.subtitle.multiple", playerCount)
45
	);
46
	private static final CommandResponseTracker.Messages<ServerPlayer> RESPONSE_SET_TIMES = CommandResponseTracker.messages(
47
		(player, var1) -> Component.translatable("commands.title.times.single", player.getDisplayName()),
48
		(playerCount, var1) -> Component.translatable("commands.title.times.multiple", playerCount)
49
	);
50
51
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher, final CommandBuildContext context) {
52
		dispatcher.register(
53
			Commands.literal("title")
54
				.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
55
				.then(
56
					Commands.argument("targets", EntityArgument.players())
57
						.then(Commands.literal("clear").executes(c -> clearTitle(c.getSource(), EntityArgument.getPlayers(c, "targets"))))
58
						.then(Commands.literal("reset").executes(c -> resetTitle(c.getSource(), EntityArgument.getPlayers(c, "targets"))))
59
						.then(
60
							Commands.literal("title")
61
								.then(
62
									Commands.argument("title", ComponentArgument.textComponent(context))
63
										.executes(
64
											c -> showTitle(
65
												c.getSource(),
66
												EntityArgument.getPlayers(c, "targets"),
67
												ComponentArgument.getRawComponent(c, "title"),
68
												RESPONSE_SHOW_TITLE,
69
												ClientboundSetTitleTextPacket::new
70
											)
71
										)
72
								)
73
						)
74
						.then(
75
							Commands.literal("subtitle")
76
								.then(
77
									Commands.argument("title", ComponentArgument.textComponent(context))
78
										.executes(
79
											c -> showTitle(
80
												c.getSource(),
81
												EntityArgument.getPlayers(c, "targets"),
82
												ComponentArgument.getRawComponent(c, "title"),
83
												RESPONSE_SHOW_SUBTITLE,
84
												ClientboundSetSubtitleTextPacket::new
85
											)
86
										)
87
								)
88
						)
89
						.then(
90
							Commands.literal("actionbar")
91
								.then(
92
									Commands.argument("title", ComponentArgument.textComponent(context))
93
										.executes(
94
											c -> showTitle(
95
												c.getSource(),
96
												EntityArgument.getPlayers(c, "targets"),
97
												ComponentArgument.getRawComponent(c, "title"),
98
												RESPONSE_SHOW_ACTIONBAR,
99
												ClientboundSetActionBarTextPacket::new
100
											)
101
										)
102
								)
103
						)
104
						.then(
105
							Commands.literal("times")
106
								.then(
107
									Commands.argument("fadeIn", TimeArgument.time())
108
										.then(
109
											Commands.argument("stay", TimeArgument.time())
110
												.then(
111
													Commands.argument("fadeOut", TimeArgument.time())
112
														.executes(
113
															c -> setTimes(
114
																c.getSource(),
115
																EntityArgument.getPlayers(c, "targets"),
116
																IntegerArgumentType.getInteger(c, "fadeIn"),
117
																IntegerArgumentType.getInteger(c, "stay"),
118
																IntegerArgumentType.getInteger(c, "fadeOut")
119
															)
120
														)
121
												)
122
										)
123
								)
124
						)
125
				)
126
		);
127
	}
128
129
	private static int sendPacketToPlayers(
130
		final CommandSourceStack source, final Collection<ServerPlayer> targets, final Packet<?> packet, final CommandResponseTracker.Messages<ServerPlayer> messages
131
	) throws CommandSyntaxException {
132
		CommandResponseTracker<ServerPlayer> tracker = CommandResponseTracker.create();
133
134
		for (ServerPlayer player : targets) {
135
			player.connection.send(packet);
136
			tracker.track(player);
137
		}
138
139
		return tracker.sendFeedback(source, true, messages);
140
	}
141
142
	private static int clearTitle(final CommandSourceStack source, final Collection<ServerPlayer> targets) throws CommandSyntaxException {
143
		return sendPacketToPlayers(source, targets, new ClientboundClearTitlesPacket(false), RESPONSE_CLEAR_TITLE);
144
	}
145
146
	private static int resetTitle(final CommandSourceStack source, final Collection<ServerPlayer> targets) throws CommandSyntaxException {
147
		return sendPacketToPlayers(source, targets, new ClientboundClearTitlesPacket(true), RESPONSE_RESET_TITLE);
148
	}
149
150
	private static int showTitle(
151
		final CommandSourceStack source,
152
		final Collection<ServerPlayer> targets,
153
		final Component title,
154
		final CommandResponseTracker.Messages<ServerPlayer> messages,
155
		final Function<Component, Packet<?>> factory
156
	) throws CommandSyntaxException {
157
		CommandResponseTracker<ServerPlayer> tracker = CommandResponseTracker.create();
158
159
		for (ServerPlayer player : targets) {
160
			player.connection.send(factory.apply(ComponentUtils.resolve(ResolutionContext.builder().withSource(source).withEntityOverride(player).build(), title)));
161
			tracker.track(player);
162
		}
163
164
		return tracker.sendFeedback(source, true, messages);
165
	}
166
167
	private static int setTimes(final CommandSourceStack source, final Collection<ServerPlayer> targets, final int fadeIn, final int stay, final int fadeOut) throws CommandSyntaxException {
168
		return sendPacketToPlayers(source, targets, new ClientboundSetTitlesAnimationPacket(fadeIn, stay, fadeOut), RESPONSE_SET_TIMES);
169
	}
170
}
171

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