Files
boybook 760c2dd9ad 2.6
2025-12-01 20:59:16 +08:00

68 lines
1.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
front:
hard: 入门
time: 10分钟
---
# 调度器
## 传统写法
用Bukkit写调度器一般是通过下述代码完成
```java
Bukkit.getScheduler().runTask(BukkitPlugin.javaPlugin){
// TODO
}
new BukkitRunnable(){
@Override
public void run(){
// TODO
}
}
```
但是Taboolib创建调度器则非常方便代码也是非常简约
```kotlin
submit(period = 10, async = true, delay = 20) {
// TODO
}
```
这样就创建了一个 每10Tick 运行一次 异步的 第一次运行延迟20Tick的调度器
## 方法详解
```kotlin
fun submit(
now: Boolean = false, // 是否立即执行
async: Boolean = false, // 是否异步执行
delay: Long = 0, // 延迟执行时间
period: Long = 0, // 重复执行时间
comment: String? = null, // 注释(无用)
executor: PlatformExecutor.PlatformTask.() -> Unit, // 调度器具体行为
): PlatformExecutor.PlatformTask
```
> 如果now为true时这个task不会重复执行。
## 变体 - 简单的异步调度器
```kotlin
submitAsync{
// TODO
}
```
## 变体 - 通过注解的方式注册调度器
```kotlin
@Schedule(period = 20, async = true)
fun tick() {
Bukkit.getOnlinePlayers().forEach {
it.sendMessage("Hello super bee")
}
}
```