feat: complete event module fix tab completer
This commit is contained in:
parent
bee7e19fc3
commit
d06321d9b5
@ -4,7 +4,7 @@ import { command, plugin } from '@ms/api'
|
||||
import * as reflect from '@ms/common/dist/reflect'
|
||||
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 TabCompleter = Java.type('org.bukkit.command.TabCompleter');
|
||||
let PluginCommand = Java.type('org.bukkit.command.PluginCommand');
|
||||
@ -18,37 +18,7 @@ export class BukkitCommand extends command.Command {
|
||||
|
||||
@postConstruct()
|
||||
init() {
|
||||
this.commandMap = reflect.on(Bukkit.pluginManager).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 删除插件命令
|
||||
}
|
||||
}
|
||||
this.commandMap = reflect.on(Bukkit.getPluginManager()).get('commandMap').get();
|
||||
}
|
||||
|
||||
create(jsp, command) {
|
||||
@ -77,16 +47,27 @@ export class BukkitCommand extends command.Command {
|
||||
// 必须指定需要实现的接口类型 否则MOD服会报错
|
||||
// noinspection JSUnusedGlobalSymbols
|
||||
c.setTabCompleter(new TabCompleter({
|
||||
onTabComplete: function(sender, _, command, args) {
|
||||
onTabComplete: (sender: any, _, command: string, args: string[]) => {
|
||||
try {
|
||||
var token = args[args.length - 1];
|
||||
var complete = tab(sender, command, Java.from(args)) || [];
|
||||
return Arrays.asList(complete.copyPartialMatches(token, []));
|
||||
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
|
||||
}
|
||||
}
|
@ -1,21 +1,20 @@
|
||||
import { MiaoScriptConsole } from '@ms/api'
|
||||
|
||||
let Bukkit = Java.type("org.bukkit.Bukkit");
|
||||
let CommandSender = Java.type("org.bukkit.command.CommandSender");
|
||||
let Bukkit = org.bukkit.Bukkit;
|
||||
|
||||
export class BukkitConsole extends MiaoScriptConsole {
|
||||
sender(sender, ...args) {
|
||||
if (!(sender instanceof CommandSender)) {
|
||||
if (!(sender instanceof org.bukkit.command.CommandSender)) {
|
||||
this.error("第一个参数未实现 org.bukkit.command.CommandSender 无法发送消息!")
|
||||
return;
|
||||
}
|
||||
if (args[0].toString() === "[object Array]") {
|
||||
if (''.toString.call(args[0]) === "[object Array]") {
|
||||
args[0].forEach(line => sender.sendMessage(this.prefix + line))
|
||||
} else {
|
||||
sender.sendMessage(this.prefix + args.join(' '));
|
||||
}
|
||||
}
|
||||
console(...args): void {
|
||||
this.sender(Bukkit.consoleSender, args.join(' '));
|
||||
this.sender(Bukkit.getConsoleSender(), args.join(' '));
|
||||
}
|
||||
}
|
||||
|
45
packages/bukkit/src/event.ts
Normal file
45
packages/bukkit/src/event.ts
Normal 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);
|
||||
}
|
||||
}
|
@ -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 { BukkitConsole } from './console'
|
||||
import { BukkitEvent } from './event';
|
||||
import { BukkitConsole } from './console';
|
||||
import { BukkitCommand } from './command';
|
||||
|
||||
let BukkitServerType = 'bukkit';
|
||||
@ -11,4 +14,7 @@ container.bind(server.Console).toConstantValue(BukkitConsole);
|
||||
container.bind(server.ServerType).toConstantValue(BukkitServerType);
|
||||
container.bind(plugin.PluginInstance).toConstantValue(Bukkit.pluginManager.getPlugin('MiaoScript'));
|
||||
|
||||
container.bind(event.Event).to(BukkitEvent).inSingletonScope();
|
||||
container.bind(command.Command).to(BukkitCommand).inSingletonScope();
|
||||
|
||||
console.debug(`Detect Sponge Compatible set ServerType to ${BukkitServerType} ...`)
|
||||
|
Loading…
Reference in New Issue
Block a user