feat: supoort mod server scan event
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
032f4d98ae
commit
b939c7c891
@ -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,28 +28,22 @@ 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();
|
|
||||||
var protocol = url.protocol;
|
|
||||||
if (protocol === "jar") {
|
|
||||||
// noinspection JSUnresolvedVariable
|
|
||||||
var jar = url.openConnection().jarFile;
|
|
||||||
var entries = jar.entries();
|
|
||||||
while (entries.hasMoreElements()) {
|
while (entries.hasMoreElements()) {
|
||||||
var entry = entries.nextElement();
|
let entry = entries.nextElement();
|
||||||
var name = entry.name;
|
let name = entry.name;
|
||||||
// 以 org/bukkit/event 开头 并且以 .class 结尾
|
// 以 org/bukkit/event 开头 并且以 .class 结尾
|
||||||
if (name.startsWith(this.baseEventDir) && name.endsWith(".class")) {
|
if (name.startsWith(this.baseEventDir) && name.endsWith(".class")) {
|
||||||
var i = name.replaceAll('/', '.');
|
let qualifiedName = name.replaceAll('/', '.');
|
||||||
try {
|
try {
|
||||||
var clz = base.getClass(i.substring(0, i.length - 6));
|
let clazz = base.getClass(qualifiedName.substring(0, qualifiedName.length - 6));
|
||||||
// 继承于 org.bukkit.event.Event 访问符为Public
|
// 继承于 org.bukkit.event.Event 访问符为Public
|
||||||
if (this.isValidEvent(clz)) {
|
if (this.isValidEvent(clazz)) {
|
||||||
var simpleName = this.class2Name(clz).toLowerCase();
|
let simpleName = this.class2Name(clazz).toLowerCase();
|
||||||
console.debug(`Mapping Event [${clz.canonicalName}] => ${simpleName}`);
|
console.debug(`Mapping Event [${clazz.canonicalName}] => ${simpleName}`);
|
||||||
this.mapEvent[simpleName] = clz;
|
this.mapEvent[simpleName] = clazz;
|
||||||
count++;
|
count++;
|
||||||
}
|
}
|
||||||
} catch (ex) {
|
} catch (ex) {
|
||||||
@ -56,14 +51,21 @@ export namespace event {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
}
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
};
|
}
|
||||||
};
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 添加事件监听
|
* 添加事件监听
|
||||||
|
@ -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,
|
||||||
|
@ -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": []
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
Reference in New Issue
Block a user