2020-05-02 16:14:47 +00:00
|
|
|
import { task, plugin } from '@ccms/api'
|
|
|
|
import { inject, provideSingleton } from '@ccms/container';
|
2019-09-21 07:04:25 +00:00
|
|
|
|
2020-01-17 03:08:51 +00:00
|
|
|
const Bukkit = Java.type('org.bukkit.Bukkit');
|
|
|
|
const BukkitRunnable = Java.type('org.bukkit.scheduler.BukkitRunnable');
|
|
|
|
const Callable = Java.type('java.util.concurrent.Callable')
|
2019-09-21 07:04:25 +00:00
|
|
|
|
2020-02-26 02:15:33 +00:00
|
|
|
@provideSingleton(task.TaskManager)
|
2019-09-21 07:04:25 +00:00
|
|
|
export class BukkitTaskManager implements task.TaskManager {
|
|
|
|
@inject(plugin.PluginInstance)
|
|
|
|
private pluginInstance: any;
|
|
|
|
|
|
|
|
create(func: Function): task.Task {
|
|
|
|
if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); };
|
|
|
|
return new BukkitTask(this.pluginInstance, func);
|
|
|
|
}
|
2020-01-17 03:08:51 +00:00
|
|
|
callSyncMethod(func: Function): any {
|
|
|
|
return Bukkit.getScheduler().callSyncMethod(this.pluginInstance, new Callable({ call: () => func() })).get()
|
|
|
|
}
|
2020-04-03 03:36:06 +00:00
|
|
|
disable() {
|
|
|
|
Bukkit.getScheduler().cancelTasks(this.pluginInstance);
|
|
|
|
}
|
2019-09-21 07:04:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class BukkitTask extends task.Task {
|
|
|
|
submit(): task.Cancelable {
|
|
|
|
let run = new BukkitRunnable({
|
|
|
|
run: () => this.run()
|
|
|
|
})
|
|
|
|
let funcName = `runTask${this.interval ? 'Timer' : 'Later'}${this.isAsync ? 'Asynchronously' : ''}`
|
|
|
|
if (this.interval) {
|
|
|
|
return run[funcName](this.plugin, this.laterTime, this.interval)
|
|
|
|
} else {
|
2019-09-22 10:01:50 +00:00
|
|
|
return run[funcName](this.plugin, this.laterTime)
|
2019-09-21 07:04:25 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|