feat: supoort mod server scan event

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2019-09-27 09:49:45 +08:00
parent 032f4d98ae
commit b939c7c891
3 changed files with 49 additions and 42 deletions

View File

@ -6,14 +6,15 @@ import '@ms/core'
import '@ms/nashorn' import '@ms/nashorn'
import { injectable } from '@ms/container' import { injectable } from '@ms/container'
const Thread = Java.type("java.lang.Thread"); const Thread = Java.type('java.lang.Thread');
export namespace event { export namespace event {
@injectable() @injectable()
export abstract class Event { export abstract class Event {
private mapEvent = []; private mapEvent = [];
private listenerMap = []; private listenerMap = [];
private baseEventDir = '';
protected baseEventDir = '';
constructor(baseEventDir: string) { constructor(baseEventDir: string) {
this.baseEventDir = baseEventDir; this.baseEventDir = baseEventDir;
@ -27,43 +28,44 @@ export namespace event {
if (this.baseEventDir === "") { if (this.baseEventDir === "") {
throw new Error("事件基础包名为空 无法进行事件映射!"); throw new Error("事件基础包名为空 无法进行事件映射!");
} }
var count = 0; let count = 0;
var dirs = Thread.currentThread().getContextClassLoader().getResources(this.baseEventDir); let jar = this.getJarFile(this.baseEventDir);
while (dirs.hasMoreElements()) { let entries = jar.entries();
var url = dirs.nextElement(); while (entries.hasMoreElements()) {
var protocol = url.protocol; let entry = entries.nextElement();
if (protocol === "jar") { let name = entry.name;
// noinspection JSUnresolvedVariable // 以 org/bukkit/event 开头 并且以 .class 结尾
var jar = url.openConnection().jarFile; if (name.startsWith(this.baseEventDir) && name.endsWith(".class")) {
var entries = jar.entries(); let qualifiedName = name.replaceAll('/', '.');
while (entries.hasMoreElements()) { try {
var entry = entries.nextElement(); let clazz = base.getClass(qualifiedName.substring(0, qualifiedName.length - 6));
var name = entry.name; // 继承于 org.bukkit.event.Event 访问符为Public
// 以 org/bukkit/event 开头 并且以 .class 结尾 if (this.isValidEvent(clazz)) {
if (name.startsWith(this.baseEventDir) && name.endsWith(".class")) { let simpleName = this.class2Name(clazz).toLowerCase();
var i = name.replaceAll('/', '.'); console.debug(`Mapping Event [${clazz.canonicalName}] => ${simpleName}`);
try { this.mapEvent[simpleName] = clazz;
var clz = base.getClass(i.substring(0, i.length - 6)); count++;
// 继承于 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;
}; }
getJarFile(resource: string) {
let dirs = Thread.currentThread().getContextClassLoader().getResources(resource);
if (dirs.hasMoreElements()) {
let url = dirs.nextElement();
if (url.protocol === "jar") { return url.openConnection().jarFile; }
}
throw new Error(`Can't Mapping Event Because not found Resources ${resource}!`)
}
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'];
@ -78,7 +80,7 @@ export namespace event {
} }
} }
return eventCls; return eventCls;
}; }
execute(name, exec, eventCls) { execute(name, exec, eventCls) {
return (...args) => { return (...args) => {
@ -93,8 +95,8 @@ export namespace event {
console.console(`§6插件 §b${name} §6处理 §d${this.class2Name(eventCls)} §6事件时发生异常 §4${ex}`); console.console(`§6插件 §b${name} §6处理 §d${this.class2Name(eventCls)} §6事件时发生异常 §4${ex}`);
console.ex(ex); console.ex(ex);
} }
}; }
}; }
/** /**
* *

View File

@ -2,12 +2,12 @@ import { event, server, plugin } from '@ms/api'
import { injectable, inject } from '@ms/container'; import { injectable, inject } from '@ms/container';
import * as reflect from '@ms/common/dist/reflect' import * as reflect from '@ms/common/dist/reflect'
let Bukkit = Java.type("org.bukkit.Bukkit"); const Bukkit = Java.type("org.bukkit.Bukkit");
let Event = Java.type("org.bukkit.event.Event"); const Event = Java.type("org.bukkit.event.Event");
let Modifier = Java.type("java.lang.reflect.Modifier"); const Modifier = Java.type("java.lang.reflect.Modifier");
let Listener = Java.type("org.bukkit.event.Listener"); const Listener = Java.type("org.bukkit.event.Listener");
let EventPriority = Java.type("org.bukkit.event.EventPriority"); const EventPriority = Java.type("org.bukkit.event.EventPriority");
let EventExecutor = Java.type("org.bukkit.plugin.EventExecutor"); const EventExecutor = Java.type("org.bukkit.plugin.EventExecutor");
@injectable() @injectable()
export class BukkitEvent extends event.Event { export class BukkitEvent extends event.Event {
@ -18,6 +18,9 @@ export class BukkitEvent extends event.Event {
super('org/bukkit/event'); super('org/bukkit/event');
} }
getJarFile(resource: string) {
return super.getJarFile('org/bukkit/Bukkit.class')
}
isValidEvent(clazz: any): boolean { isValidEvent(clazz: any): boolean {
// 继承于 org.bukkit.event.Event // 继承于 org.bukkit.event.Event
return Event.class.isAssignableFrom(clazz) && return Event.class.isAssignableFrom(clazz) &&
@ -27,7 +30,7 @@ export class BukkitEvent extends event.Event {
!Modifier.isAbstract(clazz.getModifiers()); !Modifier.isAbstract(clazz.getModifiers());
} }
register(eventCls: any, exec: Function, priority: any, ignoreCancel: boolean) { register(eventCls: any, exec: Function, priority: any, ignoreCancel: boolean) {
var listener = new Listener({}); let listener = new Listener({});
Bukkit.pluginManager.registerEvent( Bukkit.pluginManager.registerEvent(
eventCls, eventCls,
listener, listener,

View File

@ -4,13 +4,15 @@
"outDir": "dist", "outDir": "dist",
"target": "es5", "target": "es5",
"module": "commonjs", "module": "commonjs",
"strict": true,
"strictNullChecks": false,
"sourceMap": false, "sourceMap": false,
"declaration": true, "declaration": true,
"noImplicitAny": false, "noImplicitAny": false,
"downlevelIteration": true, "downlevelIteration": true,
"allowUnreachableCode": true, "allowUnreachableCode": true,
"experimentalDecorators": true,
"emitDecoratorMetadata": true, "emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [] "lib": []
} }
} }