File size: 3,115 Bytes
d46f4a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package net.minecraft.server.commands;

import com.mojang.brigadier.CommandDispatcher;
import com.mojang.brigadier.context.CommandContext;
import com.mojang.brigadier.exceptions.CommandSyntaxException;
import com.mojang.brigadier.exceptions.DynamicCommandExceptionType;
import com.mojang.brigadier.exceptions.SimpleCommandExceptionType;
import java.nio.file.Path;
import java.nio.file.Paths;
import net.minecraft.ChatFormatting;
import net.minecraft.SharedConstants;
import net.minecraft.commands.CommandSourceStack;
import net.minecraft.commands.Commands;
import net.minecraft.network.chat.ClickEvent;
import net.minecraft.network.chat.Component;
import net.minecraft.network.chat.HoverEvent;
import net.minecraft.network.chat.Style;
import net.minecraft.util.profiling.jfr.Environment;
import net.minecraft.util.profiling.jfr.JvmProfiler;

public class JfrCommand {
    private static final SimpleCommandExceptionType START_FAILED = new SimpleCommandExceptionType(Component.translatable("commands.jfr.start.failed"));
    private static final DynamicCommandExceptionType DUMP_FAILED = new DynamicCommandExceptionType(
        p_308759_ -> Component.translatableEscape("commands.jfr.dump.failed", p_308759_)
    );

    private JfrCommand() {
    }

    public static void register(CommandDispatcher<CommandSourceStack> p_183646_) {
        p_183646_.register(
            Commands.literal("jfr")
                .requires(p_183661_ -> p_183661_.hasPermission(4))
                .then(Commands.literal("start").executes(p_183657_ -> startJfr(p_183657_.getSource())))
                .then(Commands.literal("stop").executes(p_183648_ -> stopJfr(p_183648_.getSource())))
        );
    }

    private static int startJfr(CommandSourceStack p_183650_) throws CommandSyntaxException {
        Environment environment = Environment.from(p_183650_.getServer());
        if (!JvmProfiler.INSTANCE.start(environment)) {
            throw START_FAILED.create();
        } else {
            p_183650_.sendSuccess(() -> Component.translatable("commands.jfr.started"), false);
            return 1;
        }
    }

    private static int stopJfr(CommandSourceStack p_183659_) throws CommandSyntaxException {
        try {
            Path path = Paths.get(".").relativize(JvmProfiler.INSTANCE.stop().normalize());
            Path path1 = p_183659_.getServer().isPublished() && !SharedConstants.IS_RUNNING_IN_IDE ? path : path.toAbsolutePath();
            Component component = Component.literal(path.toString())
                .withStyle(ChatFormatting.UNDERLINE)
                .withStyle(
                    p_183655_ -> p_183655_.withClickEvent(new ClickEvent(ClickEvent.Action.COPY_TO_CLIPBOARD, path1.toString()))
                            .withHoverEvent(new HoverEvent(HoverEvent.Action.SHOW_TEXT, Component.translatable("chat.copy.click")))
                );
            p_183659_.sendSuccess(() -> Component.translatable("commands.jfr.stopped", component), false);
            return 1;
        } catch (Throwable throwable) {
            throw DUMP_FAILED.create(throwable.getMessage());
        }
    }
}