feat: compatible brower behavior

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-05-30 10:43:33 +08:00
parent 503ca79dab
commit 684a033525
10 changed files with 94 additions and 96 deletions

View File

@ -1,5 +1,6 @@
import i18n from '@ccms/i18n' import i18n from '@ccms/i18n'
import { injectable } from "@ccms/container"; import { injectable } from "@ccms/container"
import { plugin } from './interfaces'
export namespace command { export namespace command {
@injectable() @injectable()
@ -10,16 +11,17 @@ export namespace command {
* @param name * @param name
* @param exec * @param exec
*/ */
on(plugin: any, name: string, exec: { cmd: Function, tab?: Function }) { on(plugin: plugin.Plugin, name: string, exec: { cmd: Function, tab?: Function }) {
var cmd = this.create(plugin, name); var cmd = this.create(plugin, name)
if (!cmd) { throw Error("") }
console.debug(i18n.translate("ms.api.command.register", { plugin: plugin.description.name, name, cmd })) console.debug(i18n.translate("ms.api.command.register", { plugin: plugin.description.name, name, cmd }))
if (exec.cmd && typeof exec.cmd === "function") { if (exec.cmd && typeof exec.cmd === "function") {
this.onCommand(plugin, cmd, exec.cmd); this.onCommand(plugin, cmd, exec.cmd)
} else { } else {
throw Error(i18n.translate("ms.api.command.register.input.error", { exec: exec.cmd })) throw Error(i18n.translate("ms.api.command.register.input.error", { exec: exec.cmd }))
} }
if (exec.tab && typeof exec.tab === "function") { if (exec.tab && typeof exec.tab === "function") {
this.onTabComplete(plugin, cmd, exec.tab); this.onTabComplete(plugin, cmd, exec.tab)
} }
} }
/** /**
@ -27,40 +29,40 @@ export namespace command {
* @param plugin * @param plugin
* @param name * @param name
*/ */
off(plugin: any, name: string) { off(plugin: plugin.Plugin, name: string) {
console.debug(i18n.translate("ms.api.command.unregister", { plugin: plugin.description.name, name })) console.debug(i18n.translate("ms.api.command.unregister", { plugin: plugin.description.name, name }))
this.remove(plugin, name); this.remove(plugin, name)
} }
protected abstract create(plugin: any, command: string); protected abstract create(plugin: plugin.Plugin, command: string): any
protected abstract remove(plugin: any, command: string); protected abstract remove(plugin: plugin.Plugin, command: string): void
protected abstract onCommand(plugin: any, command: any, executor: Function); protected abstract onCommand(plugin: plugin.Plugin, command: any, executor: Function)
protected abstract onTabComplete(plugin: any, command: any, tabCompleter: Function); protected abstract onTabComplete(plugin: plugin.Plugin, command: any, tabCompleter: Function)
protected setExecutor(plugin: any, command: any, executor: Function) { protected setExecutor(plugin: plugin.Plugin, command: any, executor: Function) {
return (sender: any, _: any, command: string, args: string[]) => { return (sender: any, _: any, command: string, args: string[]) => {
try { try {
return executor(sender, command, Java.from(args)); return executor(sender, command, Java.from(args))
} catch (ex) { } catch (ex) {
console.i18n("ms.api.command.execute.error", { player: sender.name, plugin: plugin.description.name, command, args: Java.from(args).join(' '), ex }) console.i18n("ms.api.command.execute.error", { player: sender.name, plugin: plugin.description.name, command, args: Java.from(args).join(' '), ex })
console.ex(ex); console.ex(ex)
if (sender.name != 'CONSOLE') { if (sender.name != 'CONSOLE') {
console.sender(sender, [i18n.translate("ms.api.command.execute.error", { player: sender.name, plugin: plugin.description.name, command, args: Java.from(args).join(' '), ex }), ...console.stack(ex)]) console.sender(sender, [i18n.translate("ms.api.command.execute.error", { player: sender.name, plugin: plugin.description.name, command, args: Java.from(args).join(' '), ex }), ...console.stack(ex)])
} }
return true; return true
} }
} }
} }
protected setTabCompleter(plugin: any, command: any, tabCompleter: Function) { protected setTabCompleter(plugin: plugin.Plugin, command: any, tabCompleter: Function) {
return (sender: any, _: any, command: string, args: string[]) => { return (sender: any, _: any, command: string, args: string[]) => {
try { try {
var token = args[args.length - 1]; var token = args[args.length - 1]
var complete = tabCompleter(sender, command, Java.from(args)) || []; var complete = tabCompleter(sender, command, Java.from(args)) || []
return this.copyPartialMatches(complete, token); return this.copyPartialMatches(complete, token)
} catch (ex) { } catch (ex) {
console.i18n("ms.api.command.tab.completer.error", { sender: sender.name, plugin: plugin.description.name, command, args: Java.from(args).join(' '), ex }) console.i18n("ms.api.command.tab.completer.error", { sender: sender.name, plugin: plugin.description.name, command, args: Java.from(args).join(' '), ex })
console.ex(ex); console.ex(ex)
console.sender(sender, [i18n.translate("ms.api.command.tab.completer.error", { sender: sender.name, plugin: plugin.description.name, command, args: Java.from(args).join(' '), ex }), ...console.stack(ex)]); console.sender(sender, [i18n.translate("ms.api.command.tab.completer.error", { sender: sender.name, plugin: plugin.description.name, command, args: Java.from(args).join(' '), ex }), ...console.stack(ex)])
return []; return []
} }
} }
} }
@ -70,7 +72,7 @@ export namespace command {
if (typeof e === "string" && e.toLowerCase().startsWith(token.toLowerCase())) { if (typeof e === "string" && e.toLowerCase().startsWith(token.toLowerCase())) {
array.push(e) array.push(e)
} }
}); })
return array return array
} }
} }

View File

@ -58,9 +58,9 @@ export namespace task {
return this; return this;
} }
protected run(): void { protected run(...args: any[]): void {
try { try {
this.func(); this.func(...args);
} catch (ex) { } catch (ex) {
console.console('§4插件执行任务时发生错误', ex) console.console('§4插件执行任务时发生错误', ex)
console.ex(ex); console.ex(ex);
@ -69,8 +69,9 @@ export namespace task {
/** /**
* *
* @param args
*/ */
abstract submit(): Cancelable; abstract submit(...args: any[]): Cancelable;
} }
/** /**
* *

View File

@ -23,10 +23,8 @@ export class BukkitTaskManager implements task.TaskManager {
} }
export class BukkitTask extends task.Task { export class BukkitTask extends task.Task {
submit(): task.Cancelable { submit(...args: any[]): task.Cancelable {
let run = new BukkitRunnable({ let run = new BukkitRunnable({ run: () => this.run(...args) })
run: () => this.run()
})
let funcName = `runTask${this.interval ? 'Timer' : 'Later'}${this.isAsync ? 'Asynchronously' : ''}` let funcName = `runTask${this.interval ? 'Timer' : 'Later'}${this.isAsync ? 'Asynchronously' : ''}`
if (this.interval) { if (this.interval) {
return run[funcName](this.plugin, this.laterTime, this.interval) return run[funcName](this.plugin, this.laterTime, this.interval)

View File

@ -22,8 +22,8 @@ export class BungeeTaskManager implements task.TaskManager {
} }
export class BungeeTask extends task.Task { export class BungeeTask extends task.Task {
submit(): task.Cancelable { submit(...args: any[]): task.Cancelable {
let run = new Runnable({ run: () => this.run() }) let run = new Runnable({ run: () => this.run(...args) })
if (this.isAsync) { if (this.isAsync) {
return this.plugin.getProxy().getScheduler().runAsync(this.plugin, run) return this.plugin.getProxy().getScheduler().runAsync(this.plugin, run)
} }

View File

@ -32,11 +32,11 @@ class MiaoScriptCore {
} }
loadTaskFunction() { loadTaskFunction() {
global.setGlobal('setTimeout', (func: Function, tick: number, async: boolean = false) => { global.setGlobal('setTimeout', (func: Function, tick: number, ...args: any[]) => {
this.taskManager.create(func).later(tick).async(async).submit() this.taskManager.create(func).later(tick).submit(...args)
}, { writable: false, configurable: false }) }, { writable: false, configurable: false })
global.setGlobal('setInterval', (func: Function, tick: number, async: boolean = false) => { global.setGlobal('setInterval', (func: Function, tick: number, ...args: any[]) => {
this.taskManager.create(func).timer(tick).async(async).submit() this.taskManager.create(func).timer(tick).submit(...args)
}, { writable: false, configurable: false }) }, { writable: false, configurable: false })
} }

View File

@ -19,9 +19,9 @@ function convertJson2TypeDefiend(infile: string, outDir: string) {
let isInterface = classModifier.includes('interface') let isInterface = classModifier.includes('interface')
let safeInterface = []; let safeInterface = [];
for (const ifs of obj.interfaces) { for (const ifs of obj.interfaces) {
if (!ifs.qualifiedName.startsWith('java')) { // if (!ifs.qualifiedName.startsWith('java')) {
safeInterface.push(ifs) safeInterface.push(ifs)
} // }
} }
if (isInterface) { if (isInterface) {
if (safeInterface.length > 0) { if (safeInterface.length > 0) {

View File

@ -16,6 +16,7 @@ declare global {
function from(javaObj: any): any[]; function from(javaObj: any): any[];
function to(array: any[]): any; function to(array: any[]): any;
function extend(...parentTypes: any[]); function extend(...parentTypes: any[]);
function synchronized(func: () => void, lock: any);
//@ts-ignore //@ts-ignore
// function super(type: any); // function super(type: any);
} }

View File

@ -21,10 +21,8 @@ export class NukkitTaskManager implements task.TaskManager {
} }
export class NukkitTask extends task.Task { export class NukkitTask extends task.Task {
submit(): task.Cancelable { submit(...args: any[]): task.Cancelable {
let run = new NukkitRunnable({ let run = new NukkitRunnable({ run: () => this.run(...args) })
run: () => this.run()
})
let funcName = `runTask${this.interval ? 'Timer' : 'Later'}${this.isAsync ? 'Asynchronously' : ''}` let funcName = `runTask${this.interval ? 'Timer' : 'Later'}${this.isAsync ? 'Asynchronously' : ''}`
if (this.interval) { if (this.interval) {
run[funcName](this.plugin, this.laterTime, this.interval); run[funcName](this.plugin, this.laterTime, this.interval);

View File

@ -1,80 +1,80 @@
import { command, plugin } from "@ccms/api"; import { command, plugin } from "@ccms/api"
import { inject, provideSingleton } from "@ccms/container"; import { inject, provideSingleton } from "@ccms/container"
let Sponge = Java.type('org.spongepowered.api.Sponge'); let Sponge = Java.type('org.spongepowered.api.Sponge')
let Text = Java.type('org.spongepowered.api.text.Text'); let Text = Java.type('org.spongepowered.api.text.Text')
var CommandCallable = Java.type('org.spongepowered.api.command.CommandCallable'); var CommandCallable = Java.type('org.spongepowered.api.command.CommandCallable')
var CommandResult = Java.type('org.spongepowered.api.command.CommandResult'); var CommandResult = Java.type('org.spongepowered.api.command.CommandResult')
var Optional = Java.type('java.util.Optional'); var Optional = Java.type('java.util.Optional')
@provideSingleton(command.Command) @provideSingleton(command.Command)
export class SpongeCommand extends command.Command { export class SpongeCommand extends command.Command {
@inject(plugin.PluginInstance) @inject(plugin.PluginInstance)
private pluginInstance: any private pluginInstance: any
private commandMapping: any[] = []; private commandMapping: any[] = []
create(plugin: any, command: string) { create(plugin: any, command: string) {
let commandKey = this.getCommandKey(plugin, command); let commandKey = this.getCommandKey(plugin, command)
let commandCallable = new SimpleCommandCallable(command); let commandCallable = new SimpleCommandCallable(command)
this.commandMapping[commandKey] = Sponge.getCommandManager().register(this.pluginInstance, commandCallable.callable, command, commandKey).orElse(null); this.commandMapping[commandKey] = Sponge.getCommandManager().register(this.pluginInstance, commandCallable.callable, command, commandKey).orElse(null)
return commandCallable; return commandCallable
} }
remove(plugin: any, command: string) { remove(plugin: any, command: string) {
var commandKey = this.getCommandKey(plugin, command); var commandKey = this.getCommandKey(plugin, command)
if (this.commandMapping[commandKey]) { if (this.commandMapping[commandKey]) {
Sponge.getCommandManager().removeMapping(this.commandMapping[commandKey]); Sponge.getCommandManager().removeMapping(this.commandMapping[commandKey])
delete this.commandMapping[commandKey]; delete this.commandMapping[commandKey]
} }
} }
onCommand(plugin: any, command: any, executor: Function) { onCommand(plugin: plugin.Plugin, command: any, executor: Function) {
command.setExecutor(super.setExecutor(plugin, command, executor)); command.setExecutor(super.setExecutor(plugin, command, executor))
} }
onTabComplete(plugin: any, command: any, tabCompleter: Function) { onTabComplete(plugin: plugin.Plugin, command: any, tabCompleter: Function) {
command.setTabCompleter(super.setTabCompleter(plugin, command, tabCompleter)); command.setTabCompleter(super.setTabCompleter(plugin, command, tabCompleter))
} }
private getCommandKey(plugin: any, command: string) { private getCommandKey(plugin: any, command: string) {
return plugin.description.name.toLowerCase() + ":" + command; return plugin.description.name.toLowerCase() + ":" + command
} }
} }
class SimpleCommandCallable { class SimpleCommandCallable {
public callable: any; public callable: any
private name: string; private name: string
private executor: Function; private executor: Function
private tabCompleter: Function; private tabCompleter: Function
constructor(command: string, description: string = '暂无描述!') { constructor(command: string, description: string = '暂无描述!') {
this.name = command; this.name = command
this.callable = new CommandCallable({ this.callable = new CommandCallable({
//CommandResult process(CommandSource source, String arguments) throws CommandException; //CommandResult process(CommandSource source, String arguments) throws CommandException
process: (sender: any, args) => { process: (sender: any, args) => {
return this.executor(sender, '', command, Java.to(args.split(" ").filter(e => e))) ? CommandResult.success() : CommandResult.empty(); return this.executor(sender, '', command, Java.to(args.split(" ").filter(e => e))) ? CommandResult.success() : CommandResult.empty()
}, },
//List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException; //List<String> getSuggestions(CommandSource source, String arguments, @Nullable Location<World> targetPosition) throws CommandException
getSuggestions: (sender: any, args, target) => { getSuggestions: (sender: any, args, target) => {
return this.tabCompleter(sender, '', command, Java.to(args.split(" ").filter(e => e))); return this.tabCompleter(sender, '', command, Java.to(args.split(" ").filter(e => e)))
}, },
//boolean testPermission(CommandSource source); //boolean testPermission(CommandSource source)
testPermission: () => { testPermission: () => {
return true; return true
}, },
//Optional<Text> getShortDescription(CommandSource source); //Optional<Text> getShortDescription(CommandSource source)
getShortDescription: () => { getShortDescription: () => {
return Optional.of(Text.of(description)); return Optional.of(Text.of(description))
}, },
//Optional<Text> getHelp(CommandSource source); //Optional<Text> getHelp(CommandSource source)
getHelp: () => { getHelp: () => {
return Optional.of(Text.of("")); return Optional.of(Text.of(""))
}, },
//Text getUsage(CommandSource source); //Text getUsage(CommandSource source)
getUsage: () => { getUsage: () => {
return Text.of(''); return Text.of('')
} }
}) })
} }
setExecutor = (executor: Function) => this.executor = executor; setExecutor = (executor: Function) => this.executor = executor
setTabCompleter = (tabCompleter: Function) => this.tabCompleter = tabCompleter; setTabCompleter = (tabCompleter: Function) => this.tabCompleter = tabCompleter
toString = () => `Sponge SimpleCommandCallable(${this.name})` toString = () => `Sponge SimpleCommandCallable(${this.name})`
} }

View File

@ -1,17 +1,17 @@
import { task, plugin } from '@ccms/api' import { task, plugin } from '@ccms/api'
import { inject, provideSingleton, postConstruct } from '@ccms/container'; import { inject, provideSingleton, postConstruct } from '@ccms/container'
const Sponge = Java.type("org.spongepowered.api.Sponge"); const Sponge = Java.type("org.spongepowered.api.Sponge")
const 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 Consumer = Java.type('java.util.function.Consumer')
const Callable = Java.type('java.util.concurrent.Callable'); const Callable = Java.type('java.util.concurrent.Callable')
const TimeUnit = Java.type('java.util.concurrent.TimeUnit'); const TimeUnit = Java.type('java.util.concurrent.TimeUnit')
@provideSingleton(task.TaskManager) @provideSingleton(task.TaskManager)
export class SpongeTaskManager implements task.TaskManager { export class SpongeTaskManager implements task.TaskManager {
@inject(plugin.PluginInstance) @inject(plugin.PluginInstance)
private pluginInstance: any; private pluginInstance: any
private syncExecutor: any; private syncExecutor: any
@postConstruct() @postConstruct()
initialize() { initialize() {
@ -19,8 +19,8 @@ export class SpongeTaskManager implements task.TaskManager {
} }
create(func: Function): task.Task { create(func: Function): task.Task {
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 { callSyncMethod(func: Function): any {
// @ts-ignore // @ts-ignore
@ -32,10 +32,8 @@ export class SpongeTaskManager implements task.TaskManager {
} }
export class SpongeTask extends task.Task { export class SpongeTask extends task.Task {
submit(): task.Cancelable { submit(...args: any[]): task.Cancelable {
let run = Task.builder().execute(new Consumer({ let run = Task.builder().execute(new Consumer({ accept: () => this.run(...args) }))
accept: () => this.run()
}));
if (this.isAsync) { run.async() } if (this.isAsync) { run.async() }
if (this.laterTime) { run.delayTicks(this.laterTime) } if (this.laterTime) { run.delayTicks(this.laterTime) }
if (this.interval) { run.intervalTicks(this.interval) } if (this.interval) { run.intervalTicks(this.interval) }