ScheduleCommand.java

net.minecraft.server.commands.ScheduleCommand

信息

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

    TODO

字段/常量

  • ERROR_SAME_TICK

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

      TODO

  • ERROR_CANT_REMOVE

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

      TODO

  • ERROR_MACRO

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

      TODO

  • SUGGEST_SCHEDULE

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

      TODO

内部类/嵌套类型

构造器

方法

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

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

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

参数:

  • dispatcher: CommandDispatcher

说明:

TODO

private static int schedule(CommandSourceStack source, Pair<Identifier,Either<CommandFunction<CommandSourceStack>,Collection<CommandFunction<CommandSourceStack>>>> callback, int time, boolean replace) @ L93

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

参数:

  • source: CommandSourceStack
  • callback: Pair<Identifier,Either<CommandFunction,Collection<CommandFunction>>>
  • time: int
  • replace: boolean

说明:

TODO

private static int remove(CommandSourceStack source, String id) @ L134

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

参数:

  • source: CommandSourceStack
  • id: String

说明:

TODO

代码

public class ScheduleCommand {
    private static final SimpleCommandExceptionType ERROR_SAME_TICK = new SimpleCommandExceptionType(Component.translatable("commands.schedule.same_tick"));
    private static final DynamicCommandExceptionType ERROR_CANT_REMOVE = new DynamicCommandExceptionType(
        s -> Component.translatableEscape("commands.schedule.cleared.failure", s)
    );
    private static final SimpleCommandExceptionType ERROR_MACRO = new SimpleCommandExceptionType(Component.translatableEscape("commands.schedule.macro"));
    private static final SuggestionProvider<CommandSourceStack> SUGGEST_SCHEDULE = (c, p) -> SharedSuggestionProvider.suggest(
        c.getSource().getServer().getScheduledEvents().getEventsIds(), p
    );
 
    public static void register(CommandDispatcher<CommandSourceStack> dispatcher) {
        dispatcher.register(
            Commands.literal("schedule")
                .requires(Commands.hasPermission(Commands.LEVEL_GAMEMASTERS))
                .then(
                    Commands.literal("function")
                        .then(
                            Commands.argument("function", FunctionArgument.functions())
                                .suggests(FunctionCommand.SUGGEST_FUNCTION)
                                .then(
                                    Commands.argument("time", TimeArgument.time())
                                        .executes(
                                            c -> schedule(
                                                c.getSource(),
                                                FunctionArgument.getFunctionOrTag(c, "function"),
                                                IntegerArgumentType.getInteger(c, "time"),
                                                true
                                            )
                                        )
                                        .then(
                                            Commands.literal("append")
                                                .executes(
                                                    c -> schedule(
                                                        c.getSource(),
                                                        FunctionArgument.getFunctionOrTag(c, "function"),
                                                        IntegerArgumentType.getInteger(c, "time"),
                                                        false
                                                    )
                                                )
                                        )
                                        .then(
                                            Commands.literal("replace")
                                                .executes(
                                                    c -> schedule(
                                                        c.getSource(),
                                                        FunctionArgument.getFunctionOrTag(c, "function"),
                                                        IntegerArgumentType.getInteger(c, "time"),
                                                        true
                                                    )
                                                )
                                        )
                                )
                        )
                )
                .then(
                    Commands.literal("clear")
                        .then(
                            Commands.argument("function", StringArgumentType.greedyString())
                                .suggests(SUGGEST_SCHEDULE)
                                .executes(c -> remove(c.getSource(), StringArgumentType.getString(c, "function")))
                        )
                )
        );
    }
 
    private static int schedule(
        CommandSourceStack source,
        Pair<Identifier, Either<CommandFunction<CommandSourceStack>, Collection<CommandFunction<CommandSourceStack>>>> callback,
        int time,
        boolean replace
    ) throws CommandSyntaxException {
        if (time == 0) {
            throw ERROR_SAME_TICK.create();
        } else {
            long tickTime = source.getLevel().getGameTime() + time;
            Identifier callbackId = callback.getFirst();
            TimerQueue<MinecraftServer> queue = source.getServer().getScheduledEvents();
            Optional<CommandFunction<CommandSourceStack>> function = callback.getSecond().left();
            if (function.isPresent()) {
                if (function.get() instanceof MacroFunction) {
                    throw ERROR_MACRO.create();
                }
 
                String scheduleId = callbackId.toString();
                if (replace) {
                    queue.remove(scheduleId);
                }
 
                queue.schedule(scheduleId, tickTime, new FunctionCallback(callbackId));
                source.sendSuccess(
                    () -> Component.translatable("commands.schedule.created.function", Component.translationArg(callbackId), time, tickTime), true
                );
            } else {
                String scheduleId = "#" + callbackId;
                if (replace) {
                    queue.remove(scheduleId);
                }
 
                queue.schedule(scheduleId, tickTime, new FunctionTagCallback(callbackId));
                source.sendSuccess(() -> Component.translatable("commands.schedule.created.tag", Component.translationArg(callbackId), time, tickTime), true);
            }
 
            return Math.floorMod(tickTime, Integer.MAX_VALUE);
        }
    }
 
    private static int remove(CommandSourceStack source, String id) throws CommandSyntaxException {
        int count = source.getServer().getScheduledEvents().remove(id);
        if (count == 0) {
            throw ERROR_CANT_REMOVE.create(id);
        } else {
            source.sendSuccess(() -> Component.translatable("commands.schedule.cleared.success", count, id), true);
            return count;
        }
    }
}

引用的其他类

  • CommandSourceStack

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

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

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

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

    • 引用位置: 方法调用
    • 关联成员: FunctionArgument.functions(), FunctionArgument.getFunctionOrTag()
  • CommandFunction

    • 引用位置: 参数
  • Component

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

    • 引用位置: 参数
  • FunctionCallback

    • 引用位置: 构造调用
    • 关联成员: FunctionCallback()
  • FunctionTagCallback

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