TimeCommand.java

net.minecraft.server.commands.TimeCommand

信息

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

    TODO

字段/常量

  • ERROR_NO_DEFAULT_CLOCK

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

      TODO

  • ERROR_NO_TIME_MARKER_FOUND

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

      TODO

  • ERROR_WRONG_TIMELINE_FOR_CLOCK

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

      TODO

  • MAX_CLOCK_RATE

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

      TODO

内部类/嵌套类型

  • net.minecraft.server.commands.TimeCommand.ClockGetter
    • 类型: interface
    • 修饰符: private
    • 源码定位: L214
    • 说明:

      TODO

构造器

方法

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

public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) @ L46

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

参数:

  • dispatcher: CommandDispatcher
  • context: CommandBuildContext

说明:

TODO

private static <A extends ArgumentBuilder<CommandSourceStack,A>> A addClockNodes(CommandBuildContext context, A node, TimeCommand.ClockGetter clockGetter) @ L64

  • 方法名:addClockNodes
  • 源码定位:L64
  • 返回类型:<A extends ArgumentBuilder<CommandSourceStack,A>> A
  • 修饰符:private static

参数:

  • context: CommandBuildContext
  • node: A
  • clockGetter: TimeCommand.ClockGetter

说明:

TODO

private static CompletableFuture<Suggestions> suggestTimeMarkers(CommandSourceStack source, SuggestionsBuilder builder, Holder<WorldClock> clock) @ L116

  • 方法名:suggestTimeMarkers
  • 源码定位:L116
  • 返回类型:CompletableFuture
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • builder: SuggestionsBuilder
  • clock: Holder

说明:

TODO

private static CompletableFuture<Suggestions> suggestTimelines(CommandSourceStack source, SuggestionsBuilder builder, Holder<WorldClock> clock) @ L122

  • 方法名:suggestTimelines
  • 源码定位:L122
  • 返回类型:CompletableFuture
  • 修饰符:private static

参数:

  • source: CommandSourceStack
  • builder: SuggestionsBuilder
  • clock: Holder

说明:

TODO

private static int queryGameTime(CommandSourceStack source) @ L131

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

参数:

  • source: CommandSourceStack

说明:

TODO

private static int queryTime(CommandSourceStack source, Holder<WorldClock> clock) @ L137

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

参数:

  • source: CommandSourceStack
  • clock: Holder

说明:

TODO

private static int queryTimelineTicks(CommandSourceStack source, Holder<WorldClock> clock, Holder<Timeline> timeline) @ L144

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

参数:

  • source: CommandSourceStack
  • clock: Holder
  • timeline: Holder

说明:

TODO

private static int queryTimelineRepetitions(CommandSourceStack source, Holder<WorldClock> clock, Holder<Timeline> timeline) @ L155

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

参数:

  • source: CommandSourceStack
  • clock: Holder
  • timeline: Holder

说明:

TODO

private static int setTotalTicks(CommandSourceStack source, Holder<WorldClock> clock, int totalTicks) @ L166

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

参数:

  • source: CommandSourceStack
  • clock: Holder
  • totalTicks: int

说明:

TODO

private static int addTime(CommandSourceStack source, Holder<WorldClock> clock, int time) @ L173

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

参数:

  • source: CommandSourceStack
  • clock: Holder
  • time: int

说明:

TODO

private static int setTimeToTimeMarker(CommandSourceStack source, Holder<WorldClock> clock, ResourceKey<ClockTimeMarker> timeMarkerId) @ L181

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

参数:

  • source: CommandSourceStack
  • clock: Holder
  • timeMarkerId: ResourceKey

说明:

TODO

private static int setPaused(CommandSourceStack source, Holder<WorldClock> clock, boolean paused) @ L193

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

参数:

  • source: CommandSourceStack
  • clock: Holder
  • paused: boolean

说明:

TODO

private static int setRate(CommandSourceStack source, Holder<WorldClock> clock, float rate) @ L199

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

参数:

  • source: CommandSourceStack
  • clock: Holder
  • rate: float

说明:

TODO

private static int wrapTime(long ticks) @ L205

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

参数:

  • ticks: long

说明:

TODO

private static Holder<WorldClock> getDefaultClock(CommandSourceStack source) @ L209

  • 方法名:getDefaultClock
  • 源码定位:L209
  • 返回类型:Holder
  • 修饰符:private static

参数:

  • source: CommandSourceStack

说明:

TODO

代码

public class TimeCommand {
    private static final DynamicCommandExceptionType ERROR_NO_DEFAULT_CLOCK = new DynamicCommandExceptionType(
        dimension -> Component.translatableEscape("commands.time.no_default_clock", dimension)
    );
    private static final Dynamic2CommandExceptionType ERROR_NO_TIME_MARKER_FOUND = new Dynamic2CommandExceptionType(
        (clock, timeMarker) -> Component.translatableEscape("commands.time.no_time_marker_found", timeMarker, clock)
    );
    private static final Dynamic2CommandExceptionType ERROR_WRONG_TIMELINE_FOR_CLOCK = new Dynamic2CommandExceptionType(
        (clock, timeline) -> Component.translatableEscape("commands.time.wrong_timeline_for_clock", timeline, clock)
    );
    private static final int MAX_CLOCK_RATE = 1000;
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher, CommandBuildContext context) {
        LiteralArgumentBuilder<CommandSourceStack> baseCommand = Commands.literal("time").requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS));
        dispatcher.register(addClockNodes(context, baseCommand, c -> getDefaultClock(c.getSource())));
        dispatcher.register(
            baseCommand.then(Commands.literal("query").then(Commands.literal("gametime").executes(c -> queryGameTime(c.getSource()))))
                .then(
                    Commands.literal("of")
                        .then(
                            addClockNodes(
                                context,
                                Commands.argument("clock", ResourceArgument.resource(context, Registries.WORLD_CLOCK)),
                                c -> ResourceArgument.getClock(c, "clock")
                            )
                        )
                )
        );
    }
 
    private static <A extends ArgumentBuilder<CommandSourceStack, A>> A addClockNodes(CommandBuildContext context, A node, TimeCommand.ClockGetter clockGetter) {
        return node.then(
                Commands.literal("set")
                    .then(
                        Commands.argument("time", TimeArgument.time())
                            .executes(c -> setTotalTicks(c.getSource(), clockGetter.getClock(c), IntegerArgumentType.getInteger(c, "time")))
                    )
                    .then(
                        Commands.argument("timemarker", IdentifierArgument.id())
                            .suggests((c, p) -> suggestTimeMarkers(c.getSource(), p, clockGetter.getClock(c)))
                            .executes(
                                c -> setTimeToTimeMarker(
                                    c.getSource(),
                                    clockGetter.getClock(c),
                                    ResourceKey.create(ClockTimeMarkers.ROOT_ID, IdentifierArgument.getId(c, "timemarker"))
                                )
                            )
                    )
            )
            .then(
                Commands.literal("add")
                    .then(
                        Commands.argument("time", TimeArgument.time(Integer.MIN_VALUE))
                            .executes(c -> addTime(c.getSource(), clockGetter.getClock(c), IntegerArgumentType.getInteger(c, "time")))
                    )
            )
            .then(Commands.literal("pause").executes(c -> setPaused(c.getSource(), clockGetter.getClock(c), true)))
            .then(Commands.literal("resume").executes(c -> setPaused(c.getSource(), clockGetter.getClock(c), false)))
            .then(
                Commands.literal("rate")
                    .then(
                        Commands.argument("rate", FloatArgumentType.floatArg(1.0E-5F, 1000.0F))
                            .executes(c -> setRate(c.getSource(), clockGetter.getClock(c), FloatArgumentType.getFloat(c, "rate")))
                    )
            )
            .then(
                Commands.literal("query")
                    .then(Commands.literal("time").executes(c -> queryTime(c.getSource(), clockGetter.getClock(c))))
                    .then(
                        Commands.argument("timeline", ResourceArgument.resource(context, Registries.TIMELINE))
                            .suggests((c, p) -> suggestTimelines(c.getSource(), p, clockGetter.getClock(c)))
                            .executes(c -> queryTimelineTicks(c.getSource(), clockGetter.getClock(c), ResourceArgument.getTimeline(c, "timeline")))
                            .then(
                                Commands.literal("repetition")
                                    .executes(
                                        c -> queryTimelineRepetitions(c.getSource(), clockGetter.getClock(c), ResourceArgument.getTimeline(c, "timeline"))
                                    )
                            )
                    )
            );
    }
 
    private static CompletableFuture<Suggestions> suggestTimeMarkers(CommandSourceStack source, SuggestionsBuilder builder, Holder<WorldClock> clock) {
        return SharedSuggestionProvider.suggestResource(
            source.getServer().clockManager().commandTimeMarkersForClock(clock).map(ResourceKey::identifier), builder
        );
    }
 
    private static CompletableFuture<Suggestions> suggestTimelines(CommandSourceStack source, SuggestionsBuilder builder, Holder<WorldClock> clock) {
        Stream<ResourceKey<Timeline>> timelines = source.registryAccess()
            .lookupOrThrow(Registries.TIMELINE)
            .listElements()
            .filter(timeline -> timeline.value().clock().equals(clock))
            .map(Holder.Reference::key);
        return SharedSuggestionProvider.suggestResource(timelines.map(ResourceKey::identifier), builder);
    }
 
    private static int queryGameTime(CommandSourceStack source) {
        long gameTime = source.getLevel().getGameTime();
        source.sendSuccess(() -> Component.translatable("commands.time.query.gametime", gameTime), false);
        return wrapTime(gameTime);
    }
 
    private static int queryTime(CommandSourceStack source, Holder<WorldClock> clock) {
        ServerClockManager clockManager = source.getServer().clockManager();
        long totalTicks = clockManager.getTotalTicks(clock);
        source.sendSuccess(() -> Component.translatable("commands.time.query.absolute", clock.getRegisteredName(), totalTicks), false);
        return wrapTime(totalTicks);
    }
 
    private static int queryTimelineTicks(CommandSourceStack source, Holder<WorldClock> clock, Holder<Timeline> timeline) throws CommandSyntaxException {
        if (!clock.equals(timeline.value().clock())) {
            throw ERROR_WRONG_TIMELINE_FOR_CLOCK.create(clock.getRegisteredName(), timeline.getRegisteredName());
        } else {
            ServerClockManager clockManager = source.getServer().clockManager();
            long currentTicks = timeline.value().getCurrentTicks(clockManager);
            source.sendSuccess(() -> Component.translatable("commands.time.query.timeline", timeline.getRegisteredName(), currentTicks), false);
            return wrapTime(currentTicks);
        }
    }
 
    private static int queryTimelineRepetitions(CommandSourceStack source, Holder<WorldClock> clock, Holder<Timeline> timeline) throws CommandSyntaxException {
        if (!clock.equals(timeline.value().clock())) {
            throw ERROR_WRONG_TIMELINE_FOR_CLOCK.create(clock.getRegisteredName(), timeline.getRegisteredName());
        } else {
            ServerClockManager clockManager = source.getServer().clockManager();
            long repetitions = timeline.value().getPeriodCount(clockManager);
            source.sendSuccess(() -> Component.translatable("commands.time.query.timeline.repetitions", timeline.getRegisteredName(), repetitions), false);
            return wrapTime(repetitions);
        }
    }
 
    private static int setTotalTicks(CommandSourceStack source, Holder<WorldClock> clock, int totalTicks) {
        ServerClockManager clockManager = source.getServer().clockManager();
        clockManager.setTotalTicks(clock, totalTicks);
        source.sendSuccess(() -> Component.translatable("commands.time.set.absolute", clock.getRegisteredName(), totalTicks), true);
        return totalTicks;
    }
 
    private static int addTime(CommandSourceStack source, Holder<WorldClock> clock, int time) {
        ServerClockManager clockManager = source.getServer().clockManager();
        clockManager.addTicks(clock, time);
        long totalTicks = clockManager.getTotalTicks(clock);
        source.sendSuccess(() -> Component.translatable("commands.time.set.absolute", clock.getRegisteredName(), totalTicks), true);
        return wrapTime(totalTicks);
    }
 
    private static int setTimeToTimeMarker(CommandSourceStack source, Holder<WorldClock> clock, ResourceKey<ClockTimeMarker> timeMarkerId) throws CommandSyntaxException {
        ServerClockManager clockManager = source.getServer().clockManager();
        if (!clockManager.moveToTimeMarker(clock, timeMarkerId)) {
            throw ERROR_NO_TIME_MARKER_FOUND.create(clock.getRegisteredName(), timeMarkerId);
        } else {
            source.sendSuccess(
                () -> Component.translatable("commands.time.set.time_marker", clock.getRegisteredName(), timeMarkerId.identifier().toString()), true
            );
            return wrapTime(clockManager.getTotalTicks(clock));
        }
    }
 
    private static int setPaused(CommandSourceStack source, Holder<WorldClock> clock, boolean paused) {
        source.getServer().clockManager().setPaused(clock, paused);
        source.sendSuccess(() -> Component.translatable(paused ? "commands.time.pause" : "commands.time.resume", clock.getRegisteredName()), true);
        return 1;
    }
 
    private static int setRate(CommandSourceStack source, Holder<WorldClock> clock, float rate) {
        source.getServer().clockManager().setRate(clock, rate);
        source.sendSuccess(() -> Component.translatable("commands.time.rate", clock.getRegisteredName(), rate), true);
        return 1;
    }
 
    private static int wrapTime(long ticks) {
        return Math.toIntExact(ticks % 2147483647L);
    }
 
    private static Holder<WorldClock> getDefaultClock(CommandSourceStack source) throws CommandSyntaxException {
        Holder<DimensionType> dimensionType = source.getLevel().dimensionTypeRegistration();
        return dimensionType.value().defaultClock().orElseThrow(() -> ERROR_NO_DEFAULT_CLOCK.create(dimensionType.getRegisteredName()));
    }
 
    private interface ClockGetter {
        Holder<WorldClock> getClock(CommandContext<CommandSourceStack> context) throws CommandSyntaxException;
    }
}

引用的其他类

  • CommandBuildContext

    • 引用位置: 参数
  • CommandSourceStack

    • 引用位置: 参数/返回值
  • Commands

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

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

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

    • 引用位置: 方法调用
    • 关联成员: ResourceArgument.getClock(), ResourceArgument.getTimeline(), ResourceArgument.resource()
  • TimeArgument

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

    • 引用位置: 参数/返回值
  • Component

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

    • 引用位置: 参数/方法调用
    • 关联成员: ResourceKey.create()
  • ClockTimeMarker

    • 引用位置: 参数
  • WorldClock

    • 引用位置: 参数/返回值
  • Timeline

    • 引用位置: 参数