feat: complete event module fix tab completer

This commit is contained in:
MiaoWoo 2019-09-19 18:58:01 +08:00
parent bee7e19fc3
commit d06321d9b5
4 changed files with 72 additions and 41 deletions

View File

@ -4,7 +4,7 @@ import { command, plugin } from '@ms/api'
import * as reflect from '@ms/common/dist/reflect' import * as reflect from '@ms/common/dist/reflect'
import { injectable, postConstruct, inject } from '@ms/container' import { injectable, postConstruct, inject } from '@ms/container'
let Bukkit = Java.type("org.bukkit.Bukkit"); let Bukkit = org.bukkit.Bukkit;
let Arrays = Java.type('java.util.Arrays'); 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');
@ -18,37 +18,7 @@ export class BukkitCommand extends command.Command {
@postConstruct() @postConstruct()
init() { init() {
this.commandMap = reflect.on(Bukkit.pluginManager).get('commandMap').get(); this.commandMap = reflect.on(Bukkit.getPluginManager()).get('commandMap').get();
}
enable(jsp) {
var commands = jsp.description.commands;
if (commands) {
var pluginCommands = [];
for (var name in commands) {
var command = commands[name];
if (typeof command !== 'object') continue;
command.name = name;
var newCmd = this.create(jsp, command);
if (command.description) newCmd.setDescription(command.description);
if (command.usage) newCmd.setUsage(command.usage);
if (command.aliases) newCmd.setAliases(Arrays.asList(command.aliases));
if (command.permission) newCmd.setPermission(command.permission);
if (command['permission-message']) newCmd.setPermissionMessage(command['permission-message']);
pluginCommands.push(newCmd);
console.debug(`插件 ${jsp.description.name} 注册命令 ${name} ...`);
}
this.commandMap.registerAll(jsp.description.name, Arrays.asList(pluginCommands));
}
}
disable(jsp) {
var commands = jsp.description.commands;
if (commands) {
for (var name in commands) {
//TODO 删除插件命令
}
}
} }
create(jsp, command) { create(jsp, command) {
@ -77,16 +47,27 @@ export class BukkitCommand extends command.Command {
// 必须指定需要实现的接口类型 否则MOD服会报错 // 必须指定需要实现的接口类型 否则MOD服会报错
// noinspection JSUnusedGlobalSymbols // noinspection JSUnusedGlobalSymbols
c.setTabCompleter(new TabCompleter({ c.setTabCompleter(new TabCompleter({
onTabComplete: function(sender, _, command, args) { onTabComplete: (sender: any, _, command: string, args: string[]) => {
try { try {
var token = args[args.length - 1]; var token = args[args.length - 1];
var complete = tab(sender, command, Java.from(args)) || []; var complete = tab(sender, command, Java.from(args)) || [];
return Arrays.asList(complete.copyPartialMatches(token, [])); return this.copyPartialMatches(complete, token);
} catch (ex) { } catch (ex) {
console.console(`§6玩家 §a${sender.name} §6执行 §b${jsp.description.name} §6插件 §d${command} ${Java.from(args).join(' ')} §6补全时发生异常 §4${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); 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

@ -1,21 +1,20 @@
import { MiaoScriptConsole } from '@ms/api' import { MiaoScriptConsole } from '@ms/api'
let Bukkit = Java.type("org.bukkit.Bukkit"); let Bukkit = org.bukkit.Bukkit;
let CommandSender = Java.type("org.bukkit.command.CommandSender");
export class BukkitConsole extends MiaoScriptConsole { export class BukkitConsole extends MiaoScriptConsole {
sender(sender, ...args) { sender(sender, ...args) {
if (!(sender instanceof CommandSender)) { if (!(sender instanceof org.bukkit.command.CommandSender)) {
this.error("第一个参数未实现 org.bukkit.command.CommandSender 无法发送消息!") this.error("第一个参数未实现 org.bukkit.command.CommandSender 无法发送消息!")
return; return;
} }
if (args[0].toString() === "[object Array]") { if (''.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(' '));
} }
} }
console(...args): void { console(...args): void {
this.sender(Bukkit.consoleSender, args.join(' ')); this.sender(Bukkit.getConsoleSender(), args.join(' '));
} }
} }

View File

@ -0,0 +1,45 @@
import { event, server, plugin } from '@ms/api'
import { injectable, inject } from '@ms/container';
import * as reflect from '@ms/common/dist/reflect'
let Bukkit = Java.type("org.bukkit.Bukkit");
let Event = Java.type("org.bukkit.event.Event");
let Modifier = Java.type("java.lang.reflect.Modifier");
let Listener = Java.type("org.bukkit.event.Listener");
let EventPriority = Java.type("org.bukkit.event.EventPriority");
let EventExecutor = Java.type("org.bukkit.plugin.EventExecutor");
@injectable()
export class BukkitEvent extends event.Event {
@inject(plugin.PluginInstance)
private pluginInstance: any
constructor() {
super('org/bukkit/event');
}
isValidEvent(clazz: any): boolean {
// 继承于 org.bukkit.event.Event
return Event.class.isAssignableFrom(clazz) &&
// 访问符为Public
Modifier.isPublic(clazz.getModifiers()) &&
// 不是抽象类
!Modifier.isAbstract(clazz.getModifiers());
}
register(eventCls: any, exec: Function, priority: any, ignoreCancel: boolean) {
var listener = new Listener({});
Bukkit.pluginManager.registerEvent(
eventCls,
listener,
EventPriority[priority],
new EventExecutor({
execute: exec
}),
this.pluginInstance,
ignoreCancel);
return listener;
}
unregister(event: any, listener: any): void {
reflect.on(event).call('getHandlerList').get().unregister(listener);
}
}

View File

@ -1,7 +1,10 @@
import { server, plugin, command } from '@ms/api' import './typings'
import { server, plugin, command, event } from '@ms/api'
import { DefaultContainer as container } from '@ms/container' import { DefaultContainer as container } from '@ms/container'
import { BukkitConsole } from './console' import { BukkitEvent } from './event';
import { BukkitConsole } from './console';
import { BukkitCommand } from './command'; import { BukkitCommand } from './command';
let BukkitServerType = 'bukkit'; let BukkitServerType = 'bukkit';
@ -11,4 +14,7 @@ container.bind(server.Console).toConstantValue(BukkitConsole);
container.bind(server.ServerType).toConstantValue(BukkitServerType); container.bind(server.ServerType).toConstantValue(BukkitServerType);
container.bind(plugin.PluginInstance).toConstantValue(Bukkit.pluginManager.getPlugin('MiaoScript')); container.bind(plugin.PluginInstance).toConstantValue(Bukkit.pluginManager.getPlugin('MiaoScript'));
container.bind(event.Event).to(BukkitEvent).inSingletonScope();
container.bind(command.Command).to(BukkitCommand).inSingletonScope(); container.bind(command.Command).to(BukkitCommand).inSingletonScope();
console.debug(`Detect Sponge Compatible set ServerType to ${BukkitServerType} ...`)