feat: add callSyncMethod

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-01-17 11:08:51 +08:00
parent a0304183c4
commit 2063a2484d
4 changed files with 18 additions and 3 deletions

View File

@ -4,6 +4,7 @@ export namespace task {
export const TaskManager = Symbol('TaskManager') export const TaskManager = Symbol('TaskManager')
export interface TaskManager { export interface TaskManager {
create(func: Function): task.Task; create(func: Function): task.Task;
callSyncMethod(func: Function): any;
} }
/** /**
* *

View File

@ -1,7 +1,9 @@
import { task, plugin } from '@ms/api' import { task, plugin } from '@ms/api'
import { injectable, inject } from '@ms/container'; import { injectable, inject } from '@ms/container';
let BukkitRunnable = Java.type('org.bukkit.scheduler.BukkitRunnable'); const Bukkit = Java.type('org.bukkit.Bukkit');
const BukkitRunnable = Java.type('org.bukkit.scheduler.BukkitRunnable');
const Callable = Java.type('java.util.concurrent.Callable')
@injectable() @injectable()
export class BukkitTaskManager implements task.TaskManager { export class BukkitTaskManager implements task.TaskManager {
@ -12,6 +14,9 @@ export class BukkitTaskManager implements task.TaskManager {
if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); }; if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); };
return new BukkitTask(this.pluginInstance, func); return new BukkitTask(this.pluginInstance, func);
} }
callSyncMethod(func: Function): any {
return Bukkit.getScheduler().callSyncMethod(this.pluginInstance, new Callable({ call: () => func() })).get()
}
} }
export class BukkitTask extends task.Task { export class BukkitTask extends task.Task {

View File

@ -13,6 +13,9 @@ export class BungeeTaskManager implements task.TaskManager {
if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); }; if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); };
return new BungeeTask(this.pluginInstance, func); return new BungeeTask(this.pluginInstance, func);
} }
callSyncMethod(func: Function): any {
return func();
}
} }
export class BungeeTask extends task.Task { export class BungeeTask extends task.Task {

View File

@ -1,8 +1,11 @@
import { task, plugin } from '@ms/api' import { task, plugin } from '@ms/api'
import { injectable, inject } from '@ms/container'; import { injectable, inject } from '@ms/container';
var Consumer = Java.type('java.util.function.Consumer'); const Sponge = Java.type("org.spongepowered.api.Sponge");
var Task = Java.type("org.spongepowered.api.scheduler.Task"); const Task = Java.type("org.spongepowered.api.scheduler.Task");
const Consumer = Java.type('java.util.function.Consumer');
const Callable = Java.type('java.util.concurrent.Callable');
const TimeUnit = Java.type('java.util.concurrent.TimeUnit');
@injectable() @injectable()
export class SpongeTaskManager implements task.TaskManager { export class SpongeTaskManager implements task.TaskManager {
@ -13,6 +16,9 @@ export class SpongeTaskManager implements task.TaskManager {
if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); }; if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); };
return new SpongeTask(this.pluginInstance, func); return new SpongeTask(this.pluginInstance, func);
} }
callSyncMethod(func: Function): any {
return Sponge.getScheduler().createSyncExecutor(this.pluginInstance).schedule(new Callable({ call: () => func() }), 0, TimeUnit.NANOSECONDS).get()
}
} }
export class SpongeTask extends task.Task { export class SpongeTask extends task.Task {