JfrCommand .java

net.minecraft.server.commands.JfrCommand (class, 64 lines): members: START_FAILED, DUMP_FAILED, register. strings: "commands.jfr.start.failed", "stop".

net.minecraft.server.commandsclass64 linesserver2.6 KB

API Surface (1)

void register(final CommandDispatcher<CommandSourceStack> dispatcher)

Fields & Constants (2)

START_FAILED
DUMP_FAILED

String literals (7)

"commands.jfr.start.failed"
"commands.jfr.dump.failed"
"start"
"stop"
"commands.jfr.started"
"chat.copy.click"
"commands.jfr.stopped"

Full Source

JfrCommand.java64 lines · Minecraft 26.3 · verbatim
1
package net.minecraft.server.commands;
2
3
import com.mojang.brigadier.CommandDispatcher;
4
import com.mojang.brigadier.exceptions.CommandSyntaxException;
5
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
6
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
7
import java.nio.file.Path;
8
import java.nio.file.Paths;
9
import net.minecraft.ChatFormatting;
10
import net.minecraft.SharedConstants;
11
import net.minecraft.commands.CommandSourceStack;
12
import net.minecraft.commands.Commands;
13
import net.minecraft.network.chat.ClickEvent;
14
import net.minecraft.network.chat.Component;
15
import net.minecraft.network.chat.HoverEvent;
16
import net.minecraft.util.profiling.jfr.Environment;
17
import net.minecraft.util.profiling.jfr.JvmProfiler;
18
19
public class JfrCommand {
20
	private static final SimpleCommandExceptionType START_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.jfr.start.failed"));
21
	private static final DynamicCommandExceptionType DUMP_FAILED = new DynamicCommandExceptionType(
22
		message -> Component.translatableEscape("commands.jfr.dump.failed", message)
23
	);
24
25
	private JfrCommand() {
26
	}
27
28
	public static void register(final CommandDispatcher<CommandSourceStack> dispatcher) {
29
		dispatcher.register(
30
			Commands.literal("jfr")
31
				.requires(Commands.hasPermission(Commands.LEVEL_OWNERS))
32
				.then(Commands.literal("start").executes(c -> startJfr(c.getSource())))
33
				.then(Commands.literal("stop").executes(c -> stopJfr(c.getSource())))
34
		);
35
	}
36
37
	private static int startJfr(final CommandSourceStack source) throws CommandSyntaxException {
38
		Environment env = Environment.from(source.getServer());
39
		if (!JvmProfiler.INSTANCE.start(env)) {
40
			throw START_FAILED.create();
41
		}
42
43
		source.sendSuccess(() -> Component.translatable("commands.jfr.started"), false);
44
		return 1;
45
	}
46
47
	private static int stopJfr(final CommandSourceStack source) throws CommandSyntaxException {
48
		try {
49
			Path savedRecording = Paths.get(".").relativize(JvmProfiler.INSTANCE.stop().normalize());
50
			Path clipboardPath = source.getServer().isPublished() && !SharedConstants.IS_RUNNING_IN_IDE ? savedRecording : savedRecording.toAbsolutePath();
51
			Component fileText = Component.literal(savedRecording.toString())
52
				.withStyle(ChatFormatting.UNDERLINE)
53
				.withStyle(
54
					style -> style.withClickEvent(new ClickEvent.CopyToClipboard(clipboardPath.toString()))
55
						.withHoverEvent(new HoverEvent.ShowText(Component.translatable("chat.copy.click")))
56
				);
57
			source.sendSuccess(() -> Component.translatable("commands.jfr.stopped", fileText), false);
58
			return 1;
59
		} catch (Throwable t) {
60
			throw DUMP_FAILED.create(t.getMessage());
61
		}
62
	}
63
}
64

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