feat: add command bean and event bean
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
05bf312076
commit
eb6a2a32db
@ -23,7 +23,7 @@
|
|||||||
},
|
},
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@ms/common": "^0.0.0",
|
"@ms/common": "^0.0.0",
|
||||||
"inversify": "^5.0.1"
|
"@ms/container": "^0.0.0"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"reflect-metadata": "^0.1.13",
|
"reflect-metadata": "^0.1.13",
|
||||||
|
@ -1,7 +1,9 @@
|
|||||||
import { PluginInfo } from './typings/plugin';
|
import { injectable } from "@ms/container";
|
||||||
|
|
||||||
export namespace command {
|
export namespace command {
|
||||||
|
@injectable()
|
||||||
export abstract class Command {
|
export abstract class Command {
|
||||||
on(plugin: PluginInfo, name: string, exec: { cmd: Function, tab?: Function }) {
|
on(plugin: any, name: string, exec: { cmd: Function, tab?: Function }) {
|
||||||
var cmd = this.create(plugin, { name: name });
|
var cmd = this.create(plugin, { name: name });
|
||||||
console.debug(`插件 ${plugin.description.name} 创建命令 ${name}(${cmd})...`)
|
console.debug(`插件 ${plugin.description.name} 创建命令 ${name}(${cmd})...`)
|
||||||
if (exec.cmd && typeof exec.cmd === "function") {
|
if (exec.cmd && typeof exec.cmd === "function") {
|
||||||
|
@ -6,11 +6,12 @@ export class MiaoScriptConsole implements Console {
|
|||||||
Console: NodeJS.ConsoleConstructor;
|
Console: NodeJS.ConsoleConstructor;
|
||||||
|
|
||||||
private _name: string = '';
|
private _name: string = '';
|
||||||
private logger: any;
|
|
||||||
|
|
||||||
|
protected logger: any;
|
||||||
protected prefix: string = '§6[§bMiaoScript§6]§r ';
|
protected prefix: string = '§6[§bMiaoScript§6]§r ';
|
||||||
|
|
||||||
constructor() {
|
constructor(name?: string) {
|
||||||
|
this.name = name;
|
||||||
this.logger = global.logger;
|
this.logger = global.logger;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -65,7 +66,8 @@ export class MiaoScriptConsole implements Console {
|
|||||||
stack.forEach(function(trace) {
|
stack.forEach(function(trace) {
|
||||||
if (trace.className.startsWith('<')) {
|
if (trace.className.startsWith('<')) {
|
||||||
var fileName = trace.fileName
|
var fileName = trace.fileName
|
||||||
fileName = fileName.indexOf('runtime') > -1 ? fileName.split('runtime')[1] : fileName;
|
if (fileName.startsWith(root)) { fileName = fileName.split(root)[1] }
|
||||||
|
if (fileName.startsWith('/runtime')) { fileName = fileName.split('/runtime')[1] }
|
||||||
cache.push(` §e->§c ${fileName} => §4${trace.methodName}:${trace.lineNumber}`)
|
cache.push(` §e->§c ${fileName} => §4${trace.methodName}:${trace.lineNumber}`)
|
||||||
} else {
|
} else {
|
||||||
var className = trace.className;
|
var className = trace.className;
|
||||||
|
@ -4,151 +4,147 @@
|
|||||||
*/
|
*/
|
||||||
import '@ms/core'
|
import '@ms/core'
|
||||||
import '@ms/nashorn'
|
import '@ms/nashorn'
|
||||||
import { injectable } from 'inversify'
|
import { injectable } from '@ms/container'
|
||||||
|
|
||||||
const Thread = Java.type("java.lang.Thread");
|
const Thread = Java.type("java.lang.Thread");
|
||||||
|
|
||||||
@injectable()
|
export namespace event {
|
||||||
abstract class EventService {
|
@injectable()
|
||||||
private plugin;
|
export abstract class Event {
|
||||||
private mapEvent = [];
|
private mapEvent = [];
|
||||||
private listenerMap = [];
|
private listenerMap = [];
|
||||||
private baseEventDir = '';
|
private baseEventDir = '';
|
||||||
constructor(baseEventDir: string) {
|
|
||||||
this.baseEventDir = baseEventDir;
|
|
||||||
|
|
||||||
}
|
constructor(baseEventDir: string) {
|
||||||
/**
|
this.baseEventDir = baseEventDir;
|
||||||
* 扫描包 org.bukkit.event 下的所有事件
|
|
||||||
* 映射简写名称 org.bukkit.event.player.PlayerLoginEvent => playerloginevent
|
|
||||||
*/
|
|
||||||
mapEventName() {
|
|
||||||
if (this.baseEventDir === "") {
|
|
||||||
throw new Error("事件基础包名为空 无法进行事件映射!");
|
|
||||||
}
|
}
|
||||||
var count = 0;
|
|
||||||
var dirs = Thread.currentThread().getContextClassLoader().getResources(this.baseEventDir);
|
/**
|
||||||
while (dirs.hasMoreElements()) {
|
* 扫描包 org.bukkit.event 下的所有事件
|
||||||
var url = dirs.nextElement();
|
* 映射简写名称 org.bukkit.event.player.PlayerLoginEvent => playerloginevent
|
||||||
var protocol = url.protocol;
|
*/
|
||||||
if (protocol === "jar") {
|
mapEventName() {
|
||||||
// noinspection JSUnresolvedVariable
|
if (this.baseEventDir === "") {
|
||||||
var jar = url.openConnection().jarFile;
|
throw new Error("事件基础包名为空 无法进行事件映射!");
|
||||||
var entries = jar.entries();
|
}
|
||||||
while (entries.hasMoreElements()) {
|
var count = 0;
|
||||||
var entry = entries.nextElement();
|
var dirs = Thread.currentThread().getContextClassLoader().getResources(this.baseEventDir);
|
||||||
var name = entry.name;
|
while (dirs.hasMoreElements()) {
|
||||||
// 以 org/bukkit/event 开头 并且以 .class 结尾
|
var url = dirs.nextElement();
|
||||||
if (name.startsWith(this.baseEventDir) && name.endsWith(".class")) {
|
var protocol = url.protocol;
|
||||||
var i = name.replaceAll('/', '.');
|
if (protocol === "jar") {
|
||||||
try {
|
// noinspection JSUnresolvedVariable
|
||||||
var clz = base.getClass(i.substring(0, i.length - 6));
|
var jar = url.openConnection().jarFile;
|
||||||
// 继承于 org.bukkit.event.Event 访问符为Public
|
var entries = jar.entries();
|
||||||
if (this.isValidEvent(clz)) {
|
while (entries.hasMoreElements()) {
|
||||||
var simpleName = this.class2Name(clz).toLowerCase();
|
var entry = entries.nextElement();
|
||||||
console.debug(`Mapping Event [${clz.canonicalName}] => ${simpleName}`);
|
var name = entry.name;
|
||||||
this.mapEvent[simpleName] = clz;
|
// 以 org/bukkit/event 开头 并且以 .class 结尾
|
||||||
count++;
|
if (name.startsWith(this.baseEventDir) && name.endsWith(".class")) {
|
||||||
|
var i = name.replaceAll('/', '.');
|
||||||
|
try {
|
||||||
|
var clz = base.getClass(i.substring(0, i.length - 6));
|
||||||
|
// 继承于 org.bukkit.event.Event 访问符为Public
|
||||||
|
if (this.isValidEvent(clz)) {
|
||||||
|
var simpleName = this.class2Name(clz).toLowerCase();
|
||||||
|
console.debug(`Mapping Event [${clz.canonicalName}] => ${simpleName}`);
|
||||||
|
this.mapEvent[simpleName] = clz;
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
} catch (ex) {
|
||||||
|
//ignore already loaded class
|
||||||
}
|
}
|
||||||
} catch (ex) {
|
|
||||||
//ignore already loaded class
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
return count;
|
||||||
return count;
|
};
|
||||||
};
|
|
||||||
|
|
||||||
class2Name(clazz) {
|
class2Name(clazz) {
|
||||||
return clazz.simpleName;
|
return clazz.simpleName;
|
||||||
};
|
};
|
||||||
|
|
||||||
name2Class(name, event) {
|
name2Class(name, event) {
|
||||||
var eventCls = this.mapEvent[event.toLowerCase()] || this.mapEvent[event.toLowerCase() + 'event'];
|
var eventCls = this.mapEvent[event.toLowerCase()] || this.mapEvent[event.toLowerCase() + 'event'];
|
||||||
if (!eventCls) {
|
if (!eventCls) {
|
||||||
try {
|
try {
|
||||||
eventCls = base.getClass(eventCls);
|
eventCls = base.getClass(eventCls);
|
||||||
this.mapEvent[event] = eventCls;
|
this.mapEvent[event] = eventCls;
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
console.console(`§6插件 §b${name} §6注册事件 §c${event} §6失败 §4事件未找到!`);
|
console.console(`§6插件 §b${name} §6注册事件 §c${event} §6失败 §4事件未找到!`);
|
||||||
console.ex(new Error(`插件 ${name} 注册事件 ${event} 失败 事件未找到!`));
|
console.ex(new Error(`插件 ${name} 注册事件 ${event} 失败 事件未找到!`));
|
||||||
return;
|
return;
|
||||||
}
|
|
||||||
}
|
|
||||||
return eventCls;
|
|
||||||
};
|
|
||||||
|
|
||||||
execute(name, exec, eventCls) {
|
|
||||||
return (...args) => {
|
|
||||||
try {
|
|
||||||
var time = new Date().getTime()
|
|
||||||
exec(args[args.length - 1]);
|
|
||||||
var cost = new Date().getTime() - time;
|
|
||||||
if (cost > 20) {
|
|
||||||
console.console(`§c注意! §6插件 §b${name} §6处理 §d${this.class2Name(eventCls)} §6事件 §c耗时 §4${cost}ms !`)
|
|
||||||
}
|
}
|
||||||
} catch (ex) {
|
|
||||||
console.console(`§6插件 §b${name} §6处理 §d${this.class2Name(eventCls)} §6事件时发生异常 §4${ex}`);
|
|
||||||
console.ex(ex);
|
|
||||||
}
|
}
|
||||||
|
return eventCls;
|
||||||
};
|
};
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
execute(name, exec, eventCls) {
|
||||||
* 添加事件监听
|
return (...args) => {
|
||||||
* @param jsp
|
try {
|
||||||
* @param event
|
var time = new Date().getTime()
|
||||||
* @param exec {function}
|
exec(args[args.length - 1]);
|
||||||
* @param priority [LOWEST,LOW,NORMAL,HIGH,HIGHEST,MONITOR]
|
var cost = new Date().getTime() - time;
|
||||||
* @param ignoreCancel
|
if (cost > 20) {
|
||||||
*/
|
console.console(`§c注意! §6插件 §b${name} §6处理 §d${this.class2Name(eventCls)} §6事件 §c耗时 §4${cost}ms !`)
|
||||||
listen(jsp, event, exec, priority, ignoreCancel) {
|
}
|
||||||
if (!jsp || !jsp.description || !jsp.description.name) throw new TypeError('插件名称为空 请检查传入参数!');
|
} catch (ex) {
|
||||||
var name = jsp.description.name;
|
console.console(`§6插件 §b${name} §6处理 §d${this.class2Name(eventCls)} §6事件时发生异常 §4${ex}`);
|
||||||
var eventCls = this.name2Class(name, event);
|
console.ex(ex);
|
||||||
if (!eventCls) { return; }
|
}
|
||||||
if (typeof priority === 'boolean') {
|
};
|
||||||
ignoreCancel = priority;
|
};
|
||||||
priority = 'NORMAL';
|
|
||||||
|
/**
|
||||||
|
* 添加事件监听
|
||||||
|
* @param jsp
|
||||||
|
* @param event
|
||||||
|
* @param exec {function}
|
||||||
|
* @param priority [LOWEST,LOW,NORMAL,HIGH,HIGHEST,MONITOR]
|
||||||
|
* @param ignoreCancel
|
||||||
|
*/
|
||||||
|
listen(jsp, event, exec, priority, ignoreCancel) {
|
||||||
|
if (!jsp || !jsp.description || !jsp.description.name) throw new TypeError('插件名称为空 请检查传入参数!');
|
||||||
|
var name = jsp.description.name;
|
||||||
|
var eventCls = this.name2Class(name, event);
|
||||||
|
if (!eventCls) { return; }
|
||||||
|
if (typeof priority === 'boolean') {
|
||||||
|
ignoreCancel = priority;
|
||||||
|
priority = 'NORMAL';
|
||||||
|
}
|
||||||
|
priority = priority || 'NORMAL';
|
||||||
|
ignoreCancel = ignoreCancel || false;
|
||||||
|
// noinspection JSUnusedGlobalSymbols
|
||||||
|
var listener = this.register(eventCls, this.execute(name, exec, eventCls), priority, ignoreCancel);
|
||||||
|
var listenerMap = this.listenerMap;
|
||||||
|
// 添加到缓存 用于关闭插件的时候关闭事件
|
||||||
|
if (!listenerMap[name]) listenerMap[name] = [];
|
||||||
|
var offExec = () => {
|
||||||
|
this.unregister(eventCls, listener);
|
||||||
|
console.debug(`插件 ${name} 注销事件 ${this.class2Name(eventCls)}`);
|
||||||
|
};
|
||||||
|
var off = {
|
||||||
|
event: eventCls,
|
||||||
|
listener: listener,
|
||||||
|
off: offExec
|
||||||
|
};
|
||||||
|
listenerMap[name].push(off);
|
||||||
|
// noinspection JSUnresolvedVariable
|
||||||
|
console.debug(`插件 ${name} 注册事件 ${this.class2Name(eventCls)} => ${exec.name || '匿名方法'}`);
|
||||||
|
return off;
|
||||||
}
|
}
|
||||||
priority = priority || 'NORMAL';
|
|
||||||
ignoreCancel = ignoreCancel || false;
|
|
||||||
// noinspection JSUnusedGlobalSymbols
|
|
||||||
var listener = this.register(eventCls, this.execute(name, exec, eventCls), priority, ignoreCancel);
|
|
||||||
var listenerMap = this.listenerMap;
|
|
||||||
// 添加到缓存 用于关闭插件的时候关闭事件
|
|
||||||
if (!listenerMap[name]) listenerMap[name] = [];
|
|
||||||
var offExec = () => {
|
|
||||||
this.unregister(eventCls, listener);
|
|
||||||
console.debug(`插件 ${name} 注销事件 ${this.class2Name(eventCls)}`);
|
|
||||||
};
|
|
||||||
var off = {
|
|
||||||
event: eventCls,
|
|
||||||
listener: listener,
|
|
||||||
off: offExec
|
|
||||||
};
|
|
||||||
listenerMap[name].push(off);
|
|
||||||
// noinspection JSUnresolvedVariable
|
|
||||||
console.debug(`插件 ${name} 注册事件 ${this.class2Name(eventCls)} => ${exec.name || '匿名方法'}`);
|
|
||||||
return off;
|
|
||||||
}
|
|
||||||
|
|
||||||
abstract isValidEvent(clazz: any): boolean;
|
disable(plugin: any) {
|
||||||
abstract register(eventCls: any, exec: Function, priority, ignoreCancel);
|
var eventCache = this.listenerMap[plugin.description.name];
|
||||||
abstract unregister(event, listener);
|
if (eventCache) {
|
||||||
|
eventCache.forEach(t => t.off());
|
||||||
|
delete this.listenerMap[plugin.description.name];
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
abstract isValidEvent(clazz: any): boolean;
|
||||||
|
abstract register(eventCls: any, exec: Function, priority: any, ignoreCancel: boolean): any;
|
||||||
|
abstract unregister(event: any, listener: any): void;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
var EventHandler = Object.assign(new EventHandler(), require('event'));
|
|
||||||
// 映射事件名称
|
|
||||||
console.info(`bukkit 事件映射完毕 共计 ${EventHandler.mapEventName().toFixed(0)} 个事件!`);
|
|
||||||
module.exports = {
|
|
||||||
on: EventHandler.listen.bind(EventHandler),
|
|
||||||
disable: function(jsp) {
|
|
||||||
var eventCache = EventHandler.listenerMap[jsp.description.name];
|
|
||||||
if (eventCache) {
|
|
||||||
eventCache.forEach(t => t.off());
|
|
||||||
delete EventHandler.listenerMap[jsp.description.name];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
@ -1,5 +1,6 @@
|
|||||||
import './typings/global'
|
import './typings/global'
|
||||||
|
|
||||||
|
export * from './event'
|
||||||
|
export * from './console'
|
||||||
export * from './command'
|
export * from './command'
|
||||||
export * from './interfaces'
|
export * from './interfaces'
|
||||||
export * from './console'
|
|
||||||
|
Loading…
Reference in New Issue
Block a user