FetchProfileCommand .java

net.minecraft.server.commands.FetchProfileCommand (class, 149 lines): members: printForAvatar, NO_PROFILE, register. strings: "fetchprofile", "entity".

net.minecraft.server.commandsclass149 linesserver6.5 KB

API Surface (2)

void register(final CommandDispatcher<CommandSourceStack> dispatcher)
void printForAvatar(final CommandSourceStack source, final Avatar avatar)

Fields & Constants (1)

NO_PROFILE

String literals (15)

"commands.fetchprofile.no_profile"
"fetchprofile"
"entity"
"commands.fetchprofile.copy_component"
"commands.fetchprofile.give_item"
"give @s minecraft:player_head[profile="
"commands.fetchprofile.summon_mannequin"
"summon minecraft:mannequin ~ ~ ~ {profile:"
"commands.fetchprofile.copy_text"
"commands.fetchprofile.failed_to_serialize"
"commands.fetchprofile.name.success"
"commands.fetchprofile.name.failure"
"commands.fetchprofile.id.success"
"commands.fetchprofile.id.failure"
"commands.fetchprofile.entity.success"

Full Source

FetchProfileCommand.java149 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.mojang.authlib.GameProfile;
4
import com.mojang.brigadier.CommandDispatcher;
5
import com.mojang.brigadier.arguments.StringArgumentType;
6
import com.mojang.brigadier.exceptions.CommandSyntaxException;
7
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
8
import java.util.List;
9
import java.util.Optional;
10
import java.util.UUID;
11
import net.minecraft.ChatFormatting;
12
import net.minecraft.commands.CommandSourceStack;
13
import net.minecraft.commands.Commands;
14
import net.minecraft.commands.arguments.EntityArgument;
15
import net.minecraft.commands.arguments.UuidArgument;
16
import net.minecraft.nbt.NbtOps;
17
import net.minecraft.network.chat.ClickEvent;
18
import net.minecraft.network.chat.CommonComponents;
19
import net.minecraft.network.chat.Component;
20
import net.minecraft.network.chat.ComponentSerialization;
21
import net.minecraft.network.chat.ComponentUtils;
22
import net.minecraft.network.chat.MutableComponent;
23
import net.minecraft.network.chat.contents.objects.PlayerSprite;
24
import net.minecraft.server.MinecraftServer;
25
import net.minecraft.server.players.ProfileResolver;
26
import net.minecraft.util.Util;
27
import net.minecraft.world.entity.Avatar;
28
import net.minecraft.world.entity.Entity;
29
import net.minecraft.world.item.component.ResolvableProfile;
30
31
public class FetchProfileCommand {
32
	private static final DynamicCommandExceptionType NO_PROFILE = new DynamicCommandExceptionType(
33
		id -> Component.translatableEscape("commands.fetchprofile.no_profile", id)
34
	);
35
36
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
37
		dispatcher.register(
38
			Commands.literal("fetchprofile")
39
				.requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
40
				.then(
41
					Commands.literal("name")
42
						.then(Commands.argument("name", StringArgumentType.greedyString()).executes(c -> resolveName(c.getSource(), StringArgumentType.getString(c, "name"))))
43
				)
44
				.then(Commands.literal("id").then(Commands.argument("id", UuidArgument.uuid()).executes(c -> resolveId(c.getSource(), UuidArgument.getUuid(c, "id")))))
45
				.then(
46
					Commands.literal("entity")
47
						.then(Commands.argument("entity", EntityArgument.entity()).executes(c -> printForEntity(c.getSource(), EntityArgument.getEntity(c, "entity"))))
48
				)
49
		);
50
	}
51
52
	private static void reportResolvedProfile(final CommandSourceStack sender, final GameProfile gameProfile, final String messageId, final Component argument) {
53
		reportResolvedProfile(sender, messageId, argument, ResolvableProfile.createResolved(gameProfile));
54
	}
55
56
	private static void reportResolvedProfile(final CommandSourceStack sender, final String messageId, final Component argument, final ResolvableProfile profile) {
57
		ResolvableProfile.CODEC
58
			.encodeStart(NbtOps.INSTANCE, profile)
59
			.ifSuccess(
60
				encodedProfile -> {
61
					String encodedProfileAsString = encodedProfile.toString();
62
					MutableComponent headComponent = Component.object(new PlayerSprite(profile, true));
63
					ComponentSerialization.CODEC
64
						.encodeStart(NbtOps.INSTANCE, headComponent)
65
						.ifSuccess(
66
							encodedComponent -> {
67
								String encodedComponentAsString = encodedComponent.toString();
68
								sender.sendSuccess(
69
									() -> {
70
										Component clickable = ComponentUtils.formatList(
71
											List.of(
72
												Component.translatable("commands.fetchprofile.copy_component")
73
													.withStyle(s -> s.withClickEvent(new ClickEvent.CopyToClipboard(encodedProfileAsString))),
74
												Component.translatable("commands.fetchprofile.give_item")
75
													.withStyle(s -> s.withClickEvent(new ClickEvent.RunCommand("give @s minecraft:player_head[profile=" + encodedProfileAsString + "]"))),
76
												Component.translatable("commands.fetchprofile.summon_mannequin")
77
													.withStyle(s -> s.withClickEvent(new ClickEvent.RunCommand("summon minecraft:mannequin ~ ~ ~ {profile:" + encodedProfileAsString + "}"))),
78
												Component.translatable("commands.fetchprofile.copy_text", headComponent.withStyle(ChatFormatting.WHITE))
79
													.withStyle(s -> s.withClickEvent(new ClickEvent.CopyToClipboard(encodedComponentAsString)))
80
											),
81
											CommonComponents.SPACE,
82
											c -> ComponentUtils.wrapInSquareBrackets(c.withStyle(ChatFormatting.GREEN))
83
										);
84
										return Component.translatable(messageId, argument, clickable);
85
									},
86
									false
87
								);
88
							}
89
						)
90
						.ifError(
91
							componentEncodingError -> sender.sendFailure(Component.translatable("commands.fetchprofile.failed_to_serialize", componentEncodingError.message()))
92
						);
93
				}
94
			)
95
			.ifError(error -> sender.sendFailure(Component.translatable("commands.fetchprofile.failed_to_serialize", error.message())));
96
	}
97
98
	private static int resolveName(final CommandSourceStack source, final String name) {
99
		MinecraftServer server = source.getServer();
100
		ProfileResolver resolver = server.services().profileResolver();
101
		Util.nonCriticalIoPool()
102
			.execute(
103
				() -> {
104
					Component nameComponent = Component.literal(name);
105
					Optional<GameProfile> result = resolver.fetchByName(name);
106
					server.execute(
107
						() -> result.ifPresentOrElse(
108
							profile -> reportResolvedProfile(source, profile, "commands.fetchprofile.name.success", nameComponent),
109
							() -> source.sendFailure(Component.translatable("commands.fetchprofile.name.failure", nameComponent))
110
						)
111
					);
112
				}
113
			);
114
		return 1;
115
	}
116
117
	private static int resolveId(final CommandSourceStack source, final UUID id) {
118
		MinecraftServer server = source.getServer();
119
		ProfileResolver resolver = server.services().profileResolver();
120
		Util.nonCriticalIoPool()
121
			.execute(
122
				() -> {
123
					Component idComponent = Component.translationArg(id);
124
					Optional<GameProfile> result = resolver.fetchById(id);
125
					server.execute(
126
						() -> result.ifPresentOrElse(
127
							profile -> reportResolvedProfile(source, profile, "commands.fetchprofile.id.success", idComponent),
128
							() -> source.sendFailure(Component.translatable("commands.fetchprofile.id.failure", idComponent))
129
						)
130
					);
131
				}
132
			);
133
		return 1;
134
	}
135
136
	private static int printForEntity(final CommandSourceStack source, final Entity entity) throws CommandSyntaxException {
137
		if (entity instanceof Avatar avatar) {
138
			printForAvatar(source, avatar);
139
			return 1;
140
		} else {
141
			throw NO_PROFILE.create(entity.getDisplayName());
142
		}
143
	}
144
145
	public static void printForAvatar(final CommandSourceStack source, final Avatar avatar) {
146
		reportResolvedProfile(source, "commands.fetchprofile.entity.success", avatar.getDisplayName(), avatar.getProfile());
147
	}
148
}
149

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