StopwatchCommand.java

net.minecraft.server.commands.StopwatchCommand

信息

  • 全限定名:net.minecraft.server.commands.StopwatchCommand
  • 类型:public class
  • 包:net.minecraft.server.commands
  • 源码路径:src/main/java/net/minecraft/server/commands/StopwatchCommand.java
  • 起始行号:L18
  • 职责:

    TODO

字段/常量

  • ERROR_ALREADY_EXISTS

    • 类型: DynamicCommandExceptionType
    • 修饰符: private static final
    • 源码定位: L19
    • 说明:

      TODO

  • ERROR_DOES_NOT_EXIST

    • 类型: DynamicCommandExceptionType
    • 修饰符: public static final
    • 源码定位: L22
    • 说明:

      TODO

  • SUGGEST_STOPWATCHES

    • 类型: SuggestionProvider<CommandSourceStack>
    • 修饰符: public static final
    • 源码定位: L25
    • 说明:

      TODO

内部类/嵌套类型

构造器

方法

下面的方法块按源码顺序生成。

public static void register(CommandDispatcher<CommandSourceStack> dispatcher) @ L29

  • 方法名:register
  • 源码定位:L29
  • 返回类型:void
  • 修饰符:public static

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

private static int createStopwatch(CommandSourceStack source, Identifier id) @ L70

  • 方法名:createStopwatch
  • 源码定位:L70
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • id: Identifier

说明:

TODO

private static int queryStopwatch(CommandSourceStack source, Identifier id, double scale) @ L82

  • 方法名:queryStopwatch
  • 源码定位:L82
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • id: Identifier
  • scale: double

说明:

TODO

private static int restartStopwatch(CommandSourceStack source, Identifier id) @ L96

  • 方法名:restartStopwatch
  • 源码定位:L96
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • id: Identifier

说明:

TODO

private static int removeStopwatch(CommandSourceStack source, Identifier id) @ L107

  • 方法名:removeStopwatch
  • 源码定位:L107
  • 返回类型:int
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • id: Identifier

说明:

TODO

代码

public class StopwatchCommand {
    private static final DynamicCommandExceptionType ERROR_ALREADY_EXISTS = new DynamicCommandExceptionType(
        id -> Component.translatableEscape("commands.stopwatch.already_exists", id)
    );
    public static final DynamicCommandExceptionType ERROR_DOES_NOT_EXIST = new DynamicCommandExceptionType(
        id -> Component.translatableEscape("commands.stopwatch.does_not_exist", id)
    );
    public static final SuggestionProvider<CommandSourceStack> SUGGEST_STOPWATCHES = (c, p) -> SharedSuggestionProvider.suggestResource(
        c.getSource().getServer().getStopwatches().ids(), p
    );
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
            Commands.literal("stopwatch")
                .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
                .then(
                    Commands.literal("create")
                        .then(Commands.argument("id", IdentifierArgument.id()).executes(c -> createStopwatch(c.getSource(), IdentifierArgument.getId(c, "id"))))
                )
                .then(
                    Commands.literal("query")
                        .then(
                            Commands.argument("id", IdentifierArgument.id())
                                .suggests(SUGGEST_STOPWATCHES)
                                .then(
                                    Commands.argument("scale", DoubleArgumentType.doubleArg())
                                        .executes(
                                            c -> queryStopwatch(c.getSource(), IdentifierArgument.getId(c, "id"), DoubleArgumentType.getDouble(c, "scale"))
                                        )
                                )
                                .executes(c -> queryStopwatch(c.getSource(), IdentifierArgument.getId(c, "id"), 1.0))
                        )
                )
                .then(
                    Commands.literal("restart")
                        .then(
                            Commands.argument("id", IdentifierArgument.id())
                                .suggests(SUGGEST_STOPWATCHES)
                                .executes(c -> restartStopwatch(c.getSource(), IdentifierArgument.getId(c, "id")))
                        )
                )
                .then(
                    Commands.literal("remove")
                        .then(
                            Commands.argument("id", IdentifierArgument.id())
                                .suggests(SUGGEST_STOPWATCHES)
                                .executes(c -> removeStopwatch(c.getSource(), IdentifierArgument.getId(c, "id")))
                        )
                )
        );
    }
 
    private static int createStopwatch(CommandSourceStack source, Identifier id) throws CommandSyntaxException {
        MinecraftServer server = source.getServer();
        Stopwatches stopwatches = server.getStopwatches();
        Stopwatch now = new Stopwatch(Stopwatches.currentTime());
        if (!stopwatches.add(id, now)) {
            throw ERROR_ALREADY_EXISTS.create(id);
        } else {
            source.sendSuccess(() -> Component.translatable("commands.stopwatch.create.success", Component.translationArg(id)), true);
            return 1;
        }
    }
 
    private static int queryStopwatch(CommandSourceStack source, Identifier id, double scale) throws CommandSyntaxException {
        MinecraftServer server = source.getServer();
        Stopwatches stopwatches = server.getStopwatches();
        Stopwatch stopwatch = stopwatches.get(id);
        if (stopwatch == null) {
            throw ERROR_DOES_NOT_EXIST.create(id);
        } else {
            long currentTime = Stopwatches.currentTime();
            double elapsedSeconds = stopwatch.elapsedSeconds(currentTime);
            source.sendSuccess(() -> Component.translatable("commands.stopwatch.query", Component.translationArg(id), elapsedSeconds), true);
            return (int)(elapsedSeconds * scale);
        }
    }
 
    private static int restartStopwatch(CommandSourceStack source, Identifier id) throws CommandSyntaxException {
        MinecraftServer server = source.getServer();
        Stopwatches stopwatches = server.getStopwatches();
        if (!stopwatches.update(id, stopwatch -> new Stopwatch(Stopwatches.currentTime()))) {
            throw ERROR_DOES_NOT_EXIST.create(id);
        } else {
            source.sendSuccess(() -> Component.translatable("commands.stopwatch.restart.success", Component.translationArg(id)), true);
            return 1;
        }
    }
 
    private static int removeStopwatch(CommandSourceStack source, Identifier id) throws CommandSyntaxException {
        MinecraftServer server = source.getServer();
        Stopwatches stopwatches = server.getStopwatches();
        if (!stopwatches.remove(id)) {
            throw ERROR_DOES_NOT_EXIST.create(id);
        } else {
            source.sendSuccess(() -> Component.translatable("commands.stopwatch.remove.success", Component.translationArg(id)), true);
            return 1;
        }
    }
}

引用的其他类

  • CommandSourceStack

    • 引用位置: 参数/字段
  • Commands

    • 引用位置: 方法调用
    • 关联成员: Commands.argument(), Commands.hasPermission(), Commands.literal()
  • SharedSuggestionProvider

    • 引用位置: 方法调用
    • 关联成员: SharedSuggestionProvider.suggestResource()
  • IdentifierArgument

    • 引用位置: 方法调用
    • 关联成员: IdentifierArgument.getId(), IdentifierArgument.id()
  • Component

    • 引用位置: 方法调用
    • 关联成员: Component.translatable(), Component.translatableEscape(), Component.translationArg()
  • Identifier

    • 引用位置: 参数
  • Stopwatch

    • 引用位置: 构造调用
    • 关联成员: Stopwatch()
  • Stopwatches

    • 引用位置: 方法调用
    • 关联成员: Stopwatches.currentTime()