TickCommand.java

net.minecraft.server.commands.TickCommand

信息

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

    TODO

字段/常量

  • MAX_TICKRATE

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

      TODO

  • DEFAULT_TICKRATE

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

      TODO

内部类/嵌套类型

构造器

方法

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

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

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

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

private static String nanosToMilisString(long nanos) @ L57

  • 方法名:nanosToMilisString
  • 源码定位:L57
  • 返回类型:String
  • 修饰符:private static

参数:

  • nanos: long

说明:

TODO

private static int setTickingRate(CommandSourceStack source, float rate) @ L61

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

参数:

  • source: CommandSourceStack
  • rate: float

说明:

TODO

private static int tickQuery(CommandSourceStack source) @ L69

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

参数:

  • source: CommandSourceStack

说明:

TODO

private static int sprint(CommandSourceStack source, int time) @ L99

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

参数:

  • source: CommandSourceStack
  • time: int

说明:

TODO

private static int setFreeze(CommandSourceStack source, boolean freeze) @ L109

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

参数:

  • source: CommandSourceStack
  • freeze: boolean

说明:

TODO

private static int step(CommandSourceStack source, int advance) @ L131

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

参数:

  • source: CommandSourceStack
  • advance: int

说明:

TODO

private static int stopStepping(CommandSourceStack source) @ L143

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

参数:

  • source: CommandSourceStack

说明:

TODO

private static int stopSprinting(CommandSourceStack source) @ L155

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

参数:

  • source: CommandSourceStack

说明:

TODO

代码

public class TickCommand {
    private static final float MAX_TICKRATE = 10000.0F;
    private static final String DEFAULT_TICKRATE = String.valueOf(20);
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
            Commands.literal("tick")
                .requires(Commands.hasPermission(Commands.LEVEL_ADMINS))
                .then(Commands.literal("query").executes(c -> tickQuery(c.getSource())))
                .then(
                    Commands.literal("rate")
                        .then(
                            Commands.argument("rate", FloatArgumentType.floatArg(1.0F, 10000.0F))
                                .suggests((c, b) -> SharedSuggestionProvider.suggest(new String[]{DEFAULT_TICKRATE}, b))
                                .executes(c -> setTickingRate(c.getSource(), FloatArgumentType.getFloat(c, "rate")))
                        )
                )
                .then(
                    Commands.literal("step")
                        .executes(c -> step(c.getSource(), 1))
                        .then(Commands.literal("stop").executes(c -> stopStepping(c.getSource())))
                        .then(
                            Commands.argument("time", TimeArgument.time(1))
                                .suggests((c, b) -> SharedSuggestionProvider.suggest(new String[]{"1t", "1s"}, b))
                                .executes(c -> step(c.getSource(), IntegerArgumentType.getInteger(c, "time")))
                        )
                )
                .then(
                    Commands.literal("sprint")
                        .then(Commands.literal("stop").executes(c -> stopSprinting(c.getSource())))
                        .then(
                            Commands.argument("time", TimeArgument.time(1))
                                .suggests((c, b) -> SharedSuggestionProvider.suggest(new String[]{"60s", "1d", "3d"}, b))
                                .executes(c -> sprint(c.getSource(), IntegerArgumentType.getInteger(c, "time")))
                        )
                )
                .then(Commands.literal("unfreeze").executes(c -> setFreeze(c.getSource(), false)))
                .then(Commands.literal("freeze").executes(c -> setFreeze(c.getSource(), true)))
        );
    }
 
    private static String nanosToMilisString(long nanos) {
        return String.format(Locale.ROOT, "%.1f", (float)nanos / (float)TimeUtil.NANOSECONDS_PER_MILLISECOND);
    }
 
    private static int setTickingRate(CommandSourceStack source, float rate) {
        ServerTickRateManager manager = source.getServer().tickRateManager();
        manager.setTickRate(rate);
        String tickRateString = String.format(Locale.ROOT, "%.1f", rate);
        source.sendSuccess(() -> Component.translatable("commands.tick.rate.success", tickRateString), true);
        return (int)rate;
    }
 
    private static int tickQuery(CommandSourceStack source) {
        ServerTickRateManager manager = source.getServer().tickRateManager();
        String busyTime = nanosToMilisString(source.getServer().getAverageTickTimeNanos());
        float tickRate = manager.tickrate();
        String tickRateString = String.format(Locale.ROOT, "%.1f", tickRate);
        if (manager.isSprinting()) {
            source.sendSuccess(() -> Component.translatable("commands.tick.status.sprinting"), false);
            source.sendSuccess(() -> Component.translatable("commands.tick.query.rate.sprinting", tickRateString, busyTime), false);
        } else {
            if (manager.isFrozen()) {
                source.sendSuccess(() -> Component.translatable("commands.tick.status.frozen"), false);
            } else if (manager.nanosecondsPerTick() < source.getServer().getAverageTickTimeNanos()) {
                source.sendSuccess(() -> Component.translatable("commands.tick.status.lagging"), false);
            } else {
                source.sendSuccess(() -> Component.translatable("commands.tick.status.running"), false);
            }
 
            String milliSecondsPerTickTarget = nanosToMilisString(manager.nanosecondsPerTick());
            source.sendSuccess(() -> Component.translatable("commands.tick.query.rate.running", tickRateString, busyTime, milliSecondsPerTickTarget), false);
        }
 
        long[] samples = Arrays.copyOf(source.getServer().getTickTimesNanos(), source.getServer().getTickTimesNanos().length);
        Arrays.sort(samples);
        String p50 = nanosToMilisString(samples[samples.length / 2]);
        String p95 = nanosToMilisString(samples[(int)(samples.length * 0.95)]);
        String p99 = nanosToMilisString(samples[(int)(samples.length * 0.99)]);
        source.sendSuccess(() -> Component.translatable("commands.tick.query.percentiles", p50, p95, p99, samples.length), false);
        return (int)tickRate;
    }
 
    private static int sprint(CommandSourceStack source, int time) {
        boolean interrupted = source.getServer().tickRateManager().requestGameToSprint(time);
        if (interrupted) {
            source.sendSuccess(() -> Component.translatable("commands.tick.sprint.stop.success"), true);
        }
 
        source.sendSuccess(() -> Component.translatable("commands.tick.status.sprinting"), true);
        return 1;
    }
 
    private static int setFreeze(CommandSourceStack source, boolean freeze) {
        ServerTickRateManager manager = source.getServer().tickRateManager();
        if (freeze) {
            if (manager.isSprinting()) {
                manager.stopSprinting();
            }
 
            if (manager.isSteppingForward()) {
                manager.stopStepping();
            }
        }
 
        manager.setFrozen(freeze);
        if (freeze) {
            source.sendSuccess(() -> Component.translatable("commands.tick.status.frozen"), true);
        } else {
            source.sendSuccess(() -> Component.translatable("commands.tick.status.running"), true);
        }
 
        return freeze ? 1 : 0;
    }
 
    private static int step(CommandSourceStack source, int advance) {
        ServerTickRateManager manager = source.getServer().tickRateManager();
        boolean success = manager.stepGameIfPaused(advance);
        if (success) {
            source.sendSuccess(() -> Component.translatable("commands.tick.step.success", advance), true);
        } else {
            source.sendFailure(Component.translatable("commands.tick.step.fail"));
        }
 
        return 1;
    }
 
    private static int stopStepping(CommandSourceStack source) {
        ServerTickRateManager manager = source.getServer().tickRateManager();
        boolean success = manager.stopStepping();
        if (success) {
            source.sendSuccess(() -> Component.translatable("commands.tick.step.stop.success"), true);
            return 1;
        } else {
            source.sendFailure(Component.translatable("commands.tick.step.stop.fail"));
            return 0;
        }
    }
 
    private static int stopSprinting(CommandSourceStack source) {
        ServerTickRateManager manager = source.getServer().tickRateManager();
        boolean success = manager.stopSprinting();
        if (success) {
            source.sendSuccess(() -> Component.translatable("commands.tick.sprint.stop.success"), true);
            return 1;
        } else {
            source.sendFailure(Component.translatable("commands.tick.sprint.stop.fail"));
            return 0;
        }
    }
}

引用的其他类

  • CommandSourceStack

    • 引用位置: 参数
  • Commands

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

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

    • 引用位置: 方法调用
    • 关联成员: TimeArgument.time()
  • Component

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