跳到主要内容

调度定义

调度通过 YAML 文件定义,放在插件目录下的 schedule/ 文件夹中。

完整示例

以下是 schedule/broadcast.yml 的真实示例,每 200 tick 周期执行一次广播脚本:

resource-version: 1
id: world.tick.broadcast
type: periodic
delay: 20t
period: 200t
auto-start: true
prototype: false
async: false
max-runs: -1
execute:
route: script
value: schedule.world.tick.broadcast
variables:
triggerSource: periodic

字段说明

字段类型必填默认值说明
id字符串调度唯一标识
type枚举调度类型:DELAY / PERIODIC / CRON / CONDITIONAL
delay时间字符串首次执行前的延迟,如 20t(20 tick)、5s(5 秒)
period时间字符串执行间隔(仅 PERIODIC 类型)
cron字符串Cron 表达式(仅 CRON 类型)
async布尔false是否异步执行
auto-start布尔false加载后是否自动启动
prototype布尔false是否允许多实例并行运行
max-runs整数-1最大执行次数,-1 表示无限制
max-duration-ms整数-1最大运行时长(毫秒),-1 表示无限制
execute字符串/对象路由目标,格式与分发器相同
sender / senders对象/列表发送者选择器,见下方说明
on-start字符串调度启动时执行的脚本 ID
on-stop字符串调度停止时执行的脚本 ID
on-pause字符串调度暂停时执行的脚本 ID
on-resume字符串调度恢复时执行的脚本 ID
variables键值对预注入到执行上下文中的变量

execute 路由目标

与分发器的 execute 格式完全相同,支持简写和完整两种写法:

# 简写 —— 默认路由到脚本
execute: schedule.world.tick.broadcast

# 完整 —— 指定路由类型
execute:
route: script
value: schedule.world.tick.broadcast

Sender 选择器

通过 sender(单个)或 senders(多个)指定调度执行时的目标发送者:

# 单个选择器
sender:
type: WORLD
world: world

# 多个选择器
senders:
- type: ALL_ONLINE
- type: WORLD
world: world_nether
选择器类型参数说明
ALL_ONLINE所有在线玩家
PLAYERvalue(玩家名或 UUID)指定玩家
WORLDworld指定世界中的所有玩家
RADIUSworldoriginradius指定坐标半径范围内的玩家
BOXworldmin-xmin-ymin-zmax-xmax-ymax-z指定立方体区域内的玩家

各类型配置示例

延迟执行(DELAY):

id: one-time-task
type: delay
delay: 100t
execute: schedule.one-time

Cron 定时(CRON):

id: daily-reset
type: cron
cron: "0 0 4 * * ?"
auto-start: true
execute: schedule.daily-reset

条件触发(CONDITIONAL):

id: conditional-check
type: conditional
period: 20t
auto-start: true
execute: schedule.conditional-check

生命周期回调

你可以通过 on-starton-stopon-pauseon-resume 在调度状态变化时执行额外脚本:

id: managed-task
type: periodic
period: 100t
auto-start: true
execute: schedule.main-task
on-start: schedule.callback.started
on-stop: schedule.callback.stopped
on-pause: schedule.callback.paused
on-resume: schedule.callback.resumed