Class CommandRouter<NCS>

java.lang.Object
io.github.theword.queqiao.core.command.CommandRouter<NCS>
Type Parameters:
NCS - Native Command Source,平台原生命令来源类型

public class CommandRouter<NCS> extends Object
命令路由:路径查找 / dispatch / tab completion

职责:把平台传入的原始参数数组按命令树逐层匹配到目标节点并执行; 以及基于同样的匹配规则给出 tab 补全候选。

不做 parse:参数切分由平台侧的 command API(如 Brigadier)完成, Core 只接收已经切分好的 String[]。因此本类叫 Router 而不是 Dispatcher。

依赖由构造器显式传入,不从静态全局上下文获取。

不依赖平台上下文:权限过滤与回执都已归属 CommandExecutionContext, 因此本类只认识 CommandExecutionContext,不认识任何平台类型。

注册生命周期

 1. 构造 CommandRouter(内部构建 QueQiao 内置命令树)
 2. registration phase:平台可继续 addChild 注册第三方命令
 3. 首次 execute / tabComplete 时自动 freeze(幂等)
 4. dispatch phase:结构不可再修改
 
见 CommandNode.freeze()。
  • Constructor Details

    • CommandRouter

      public CommandRouter(org.slf4j.Logger logger, Config config, WebsocketManager websocketManager, Supplier<ReloadResult> reloadAction)
      构造命令路由
      Parameters:
      logger - 日志实现,不得为 null
      config - 配置运行时状态,不得为 null
      websocketManager - WebSocket 管理器(须在 Runtime.start() 之后获取),不得为 null
      reloadAction - 触发 Runtime 重载的动作,返回重载结果,不得为 null
  • Method Details

    • getRootCommand

      public RootCommand<NCS> getRootCommand()
      获取根命令节点

      平台适配器用它把 Core 命令树映射到平台自己的命令 API; 也可在 dispatch 开始前用它注册第三方命令。

      Returns:
      根命令节点
    • execute

      public int execute(CommandExecutionContext<NCS> context, String[] args)
      执行命令

      首次调用会自动冻结命令树结构(见类级注册生命周期)。

      Parameters:
      context - 命令执行上下文,不得为 null
      args - 已切分的命令参数,不得为 null(无参数传空数组)
      Returns:
      执行信号
      Throws:
      NullPointerException - context 或 args 为 null
    • tabComplete

      public List<String> tabComplete(CommandExecutionContext<NCS> context, String[] args)
      Tab 补全

      首次调用会自动冻结命令树结构(见类级注册生命周期)。

      Parameters:
      context - 命令执行上下文,不得为 null
      args - 已切分的命令参数,不得为 null
      Returns:
      补全候选列表;路径中断时返回空列表
      Throws:
      NullPointerException - context 或 args 为 null