2020-05-02 16:14:47 +00:00
|
|
|
import { task, plugin } from '@ccms/api'
|
|
|
|
import { inject, provideSingleton } from '@ccms/container';
|
2020-01-15 08:43:00 +00:00
|
|
|
|
|
|
|
var Runnable = Java.type('java.lang.Runnable')
|
|
|
|
let TimeUnit = Java.type('java.util.concurrent.TimeUnit')
|
|
|
|
|
2020-02-26 02:15:33 +00:00
|
|
|
@provideSingleton(task.TaskManager)
|
2020-01-15 08:43:00 +00:00
|
|
|
export class BungeeTaskManager 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 BungeeTask(this.pluginInstance, func);
|
|
|
|
}
|
2020-01-17 03:08:51 +00:00
|
|
|
callSyncMethod(func: Function): any {
|
|
|
|
return func();
|
|
|
|
}
|
2020-04-03 03:36:06 +00:00
|
|
|
disable() {
|
|
|
|
this.pluginInstance.getProxy().getScheduler().cancel(this.pluginInstance)
|
|
|
|
}
|
2020-01-15 08:43:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export class BungeeTask extends task.Task {
|
2020-05-30 02:43:33 +00:00
|
|
|
submit(...args: any[]): task.Cancelable {
|
|
|
|
let run = new Runnable({ run: () => this.run(...args) })
|
2020-01-15 08:43:00 +00:00
|
|
|
if (this.isAsync) {
|
|
|
|
return this.plugin.getProxy().getScheduler().runAsync(this.plugin, run)
|
|
|
|
}
|
|
|
|
if (this.interval) {
|
2020-04-01 03:08:15 +00:00
|
|
|
return this.plugin.getProxy().getScheduler().schedule(this.plugin, run, this.laterTime * 50, this.interval * 50, TimeUnit.MILLISECONDS)
|
2020-01-15 08:43:00 +00:00
|
|
|
} else {
|
2020-04-01 03:08:15 +00:00
|
|
|
return this.plugin.getProxy().getScheduler().schedule(this.plugin, run, this.laterTime * 50, TimeUnit.MILLISECONDS)
|
2020-01-15 08:43:00 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|