From 363d0c164a4b58a6361600257e6de502197c108f Mon Sep 17 00:00:00 2001 From: MiaoWoo Date: Fri, 17 Jan 2020 11:08:51 +0800 Subject: [PATCH] feat: add callSyncMethod Signed-off-by: MiaoWoo --- packages/api/src/task.ts | 1 + packages/bukkit/src/task.ts | 7 ++++++- packages/bungee/src/task.ts | 3 +++ packages/sponge/src/task.ts | 10 ++++++++-- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/packages/api/src/task.ts b/packages/api/src/task.ts index 71259ce0..16862508 100644 --- a/packages/api/src/task.ts +++ b/packages/api/src/task.ts @@ -4,6 +4,7 @@ export namespace task { export const TaskManager = Symbol('TaskManager') export interface TaskManager { create(func: Function): task.Task; + callSyncMethod(func: Function): any; } /** * 任务抽象 diff --git a/packages/bukkit/src/task.ts b/packages/bukkit/src/task.ts index 375bae9b..69ed492c 100644 --- a/packages/bukkit/src/task.ts +++ b/packages/bukkit/src/task.ts @@ -1,7 +1,9 @@ import { task, plugin } from '@ms/api' 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() 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 !'); }; 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 { diff --git a/packages/bungee/src/task.ts b/packages/bungee/src/task.ts index b65a3828..9d537cca 100644 --- a/packages/bungee/src/task.ts +++ b/packages/bungee/src/task.ts @@ -13,6 +13,9 @@ export class BungeeTaskManager implements task.TaskManager { if (Object.prototype.toString.call(func) !== "[object Function]") { throw TypeError('第一个参数 Task 必须为 function !'); }; return new BungeeTask(this.pluginInstance, func); } + callSyncMethod(func: Function): any { + return func(); + } } export class BungeeTask extends task.Task { diff --git a/packages/sponge/src/task.ts b/packages/sponge/src/task.ts index 421d96a1..85772031 100644 --- a/packages/sponge/src/task.ts +++ b/packages/sponge/src/task.ts @@ -1,8 +1,11 @@ import { task, plugin } from '@ms/api' import { injectable, inject } from '@ms/container'; -var Consumer = Java.type('java.util.function.Consumer'); -var Task = Java.type("org.spongepowered.api.scheduler.Task"); +const Sponge = Java.type("org.spongepowered.api.Sponge"); +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() 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 !'); }; 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 {