feat: add task and optimize command

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-09-21 15:04:25 +08:00
parent 13e9f0a5af
commit bf5628a646
4 changed files with 48 additions and 46 deletions

View File

@ -5,7 +5,6 @@ import * as reflect from '@ms/common/dist/reflect'
import { injectable, postConstruct, inject } from '@ms/container' import { injectable, postConstruct, inject } from '@ms/container'
let Bukkit = org.bukkit.Bukkit; let Bukkit = org.bukkit.Bukkit;
let Arrays = Java.type('java.util.Arrays');
let TabCompleter = Java.type('org.bukkit.command.TabCompleter'); let TabCompleter = Java.type('org.bukkit.command.TabCompleter');
let PluginCommand = Java.type('org.bukkit.command.PluginCommand'); let PluginCommand = Java.type('org.bukkit.command.PluginCommand');
let CommandExecutor = Java.type('org.bukkit.command.CommandExecutor'); let CommandExecutor = Java.type('org.bukkit.command.CommandExecutor');
@ -21,53 +20,23 @@ export class BukkitCommand extends command.Command {
this.commandMap = reflect.on(Bukkit.getPluginManager()).get('commandMap').get(); this.commandMap = reflect.on(Bukkit.getPluginManager()).get('commandMap').get();
} }
create(jsp, command) { create(plugin: any, command: string) {
var cmd = this.commandMap.getCommand(command.name) var cmd = this.commandMap.getCommand(command)
if (cmd && cmd instanceof PluginCommand) { return cmd }; if (cmd && cmd instanceof PluginCommand) { return cmd };
cmd = reflect.on(PluginCommand).create(command.name, this.pluginInstance).get(); cmd = reflect.on(PluginCommand).create(command, this.pluginInstance).get();
this.commandMap.register(jsp.description.name, cmd); this.commandMap.register(plugin.description.name, cmd);
return cmd; return cmd;
} }
onCommand(plugin: any, command: any, executor: Function) {
onCommand(jsp, c, cmd) {
// 必须指定需要实现的接口类型 否则MOD服会报错 // 必须指定需要实现的接口类型 否则MOD服会报错
c.setExecutor(new CommandExecutor({ command.setExecutor(new CommandExecutor({
onCommand: function(sender, _, command, args) { onCommand: super.setExecutor(plugin, command, executor)
try {
return cmd(sender, command, Java.from(args));
} catch (ex) {
console.console(`§6玩家 §a${sender.name} §6执行 §b${jsp.description.name} §6插件 §d${command} ${Java.from(args).join(' ')} §6命令时发生异常 §4${ex}`);
console.ex(ex);
}
}
})); }));
} }
onTabComplete(plugin: any, command: any, tabCompleter: Function) {
onTabComplete(jsp, c, tab) {
// 必须指定需要实现的接口类型 否则MOD服会报错 // 必须指定需要实现的接口类型 否则MOD服会报错
// noinspection JSUnusedGlobalSymbols command.setTabCompleter(new TabCompleter({
c.setTabCompleter(new TabCompleter({ onTabComplete: super.setTabCompleter(plugin, command, tabCompleter)
onTabComplete: (sender: any, _, command: string, args: string[]) => {
try {
var token = args[args.length - 1];
var complete = tab(sender, command, Java.from(args)) || [];
return this.copyPartialMatches(complete, token);
} catch (ex) {
console.console(`§6玩家 §a${sender.name} §6执行 §b${jsp.description.name} §6插件 §d${command} ${Java.from(args).join(' ')} §6补全时发生异常 §4${ex}`);
console.ex(ex);
return [];
}
}
})); }));
} }
}
copyPartialMatches(complete: string[], token: string, array: string[] = []): string[] {
if (!token) { return complete }
complete.forEach(function(e) {
if (typeof e === "string" && e.toLowerCase().startsWith(token.toLowerCase())) {
array.push(e)
}
});
return array
}
}

View File

@ -8,7 +8,7 @@ export class BukkitConsole extends MiaoScriptConsole {
this.error("第一个参数未实现 org.bukkit.command.CommandSender 无法发送消息!") this.error("第一个参数未实现 org.bukkit.command.CommandSender 无法发送消息!")
return; return;
} }
if (''.toString.call(args[0]) === "[object Array]") { if (Object.prototype.toString.call(args[0]) === "[object Array]") {
args[0].forEach(line => sender.sendMessage(this.prefix + line)) args[0].forEach(line => sender.sendMessage(this.prefix + line))
} else { } else {
sender.sendMessage(this.prefix + args.join(' ')); sender.sendMessage(this.prefix + args.join(' '));

View File

@ -1,11 +1,12 @@
import './typings' import './typings'
import { server, plugin, command, event } from '@ms/api' import { server, plugin, command, event, task } from '@ms/api'
import { DefaultContainer as container } from '@ms/container' import { DefaultContainer as container } from '@ms/container'
import { BukkitEvent } from './event';
import { BukkitConsole } from './console'; import { BukkitConsole } from './console';
import { BukkitEvent } from './event';
import { BukkitCommand } from './command'; import { BukkitCommand } from './command';
import { BukkitTaskManager } from './task';
let BukkitServerType = 'bukkit'; let BukkitServerType = 'bukkit';
let Bukkit = Java.type("org.bukkit.Bukkit"); let Bukkit = Java.type("org.bukkit.Bukkit");
@ -16,5 +17,6 @@ container.bind(plugin.PluginInstance).toConstantValue(Bukkit.pluginManager.getPl
container.bind(event.Event).to(BukkitEvent).inSingletonScope(); container.bind(event.Event).to(BukkitEvent).inSingletonScope();
container.bind(command.Command).to(BukkitCommand).inSingletonScope(); container.bind(command.Command).to(BukkitCommand).inSingletonScope();
container.bind(task.TaskManager).to(BukkitTaskManager).inSingletonScope();
console.debug(`Detect Sponge Compatible set ServerType to ${BukkitServerType} ...`) console.debug(`Detect Bukkit Compatible set ServerType to ${BukkitServerType} ...`)

View File

@ -0,0 +1,31 @@
import { task, plugin } from '@ms/api'
import { injectable, inject } from '@ms/container';
let BukkitRunnable = Java.type('org.bukkit.scheduler.BukkitRunnable');
@injectable()
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);
}
}
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 if (this.laterTime) {
return run[funcName](this.plugin, this.laterTime)
} else {
return run[funcName](this.plugin)
}
}
}