feat: support get root Logger

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-05-15 15:48:38 +08:00
parent 54af491399
commit 873964575b
4 changed files with 49 additions and 6 deletions

View File

@ -31,6 +31,7 @@ export namespace server {
getPluginsFolder(): string;
getNativePluginManager(): NativePluginManager;
getNettyPipeline(): any;
getRootLogger(): any;
sendJson(sender: string | any, json: object | string): void;
}
}

View File

@ -10,10 +10,11 @@ let Bukkit = org.bukkit.Bukkit;
export class BukkitServer implements server.Server {
private pluginsFolder: string;
private pipeline: any;
private rootLogger: any;
constructor() {
this.pluginsFolder = Bukkit.getUpdateFolderFile().getParentFile().getCanonicalPath();
this.reflectPipeline()
this.reflect()
}
getPlayer(name: string) {
@ -49,6 +50,9 @@ export class BukkitServer implements server.Server {
getNettyPipeline() {
return this.pipeline;
}
getRootLogger() {
return this.rootLogger;
}
sendJson(sender: string | any, json: object | string): void {
if (typeof sender === "string") {
sender = this.getPlayer(sender)
@ -59,14 +63,20 @@ export class BukkitServer implements server.Server {
}
}
private reflectPipeline() {
private reflect() {
let consoleServer = reflect.on(Bukkit.getServer()).get('console').get()
this.reflectPipeline(consoleServer)
this.reflectRootLogger(consoleServer)
}
private reflectPipeline(consoleServer: any) {
let connection: any;
let promise: any;
for (const method of constants.Reflect.Method.getServerConnection) {
try {
connection = reflect.on(consoleServer).call(method).get()
if (connection.class.name.indexOf('ServerConnection') !== -1) { break; }
if (connection.class.name.indexOf('ServerConnection') !== -1
|| connection.class.name.indexOf('NetworkSystem') !== -1) { break; }
connection = undefined;
} catch (error) { }
}
@ -81,4 +91,12 @@ export class BukkitServer implements server.Server {
if (!promise) { console.error("Can't found listeningChannels!"); return }
this.pipeline = reflect.on(promise).get('channel').get().pipeline()
}
private reflectRootLogger(consoleServer: any) {
try {
this.rootLogger = reflect.on(consoleServer).get('LOGGER').get().parent
} catch (error) {
console.error("Can't found rootLogger!")
}
}
}

View File

@ -9,6 +9,7 @@ let Bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy();
export class BungeeServer implements server.Server {
private pluginsFolder: string;
private pipeline: any;
private rootLogger: any;
@inject(task.TaskManager)
private task: task.TaskManager
@ -35,6 +36,11 @@ export class BungeeServer implements server.Server {
}
}
}).later(10).timer(20).submit()
try {
this.rootLogger = Bungee.getLogger()
} catch (error) {
console.error("Can't found rootLogger!")
}
}
getPlayer(name: string) {
@ -70,6 +76,9 @@ export class BungeeServer implements server.Server {
getNettyPipeline() {
return this.pipeline;
}
getRootLogger() {
return this.rootLogger;
}
sendJson(sender: string | any, json: string): void {
throw new Error("Method not implemented.");
}

View File

@ -11,10 +11,11 @@ const File = Java.type("java.io.File");
export class SpongeServer implements server.Server {
private pluginsFolder: string;
private pipeline: any;
private rootLogger: any;
constructor() {
this.pluginsFolder = new File(base.getInstance().getClass().getProtectionDomain().getCodeSource().getLocation().getPath()).getParentFile().getCanonicalPath()
this.reflectPipeline()
this.reflect()
}
getPlayer(name: string) {
@ -50,20 +51,27 @@ export class SpongeServer implements server.Server {
getNettyPipeline() {
return this.pipeline;
}
getRootLogger() {
return this.rootLogger;
}
sendJson(sender: string | any, json: string): void {
if (typeof sender === "string") {
sender = this.getPlayer(sender)
}
sender.sendMessage(TextSerializers.JSON.deserialize(json))
}
private reflectPipeline() {
private reflect() {
let consoleServer = reflect.on(Sponge.getServer()).get()
this.reflectPipeline(consoleServer)
this.reflectRootLogger(consoleServer)
}
private reflectPipeline(consoleServer: any) {
let connection: any;
let promise: any;
for (const method of constants.Reflect.Method.getServerConnection) {
try {
connection = reflect.on(consoleServer).call(method).get()
if (connection.class.name.indexOf('ServerConnection') !== -1) { break; }
if (connection.class.name.indexOf('NetworkSystem') !== -1) { break; }
connection = undefined;
} catch (error) { }
}
@ -78,4 +86,11 @@ export class SpongeServer implements server.Server {
if (!promise) { console.error("Can't found listeningChannels!"); return }
this.pipeline = reflect.on(promise).get('channel').get().pipeline()
}
private reflectRootLogger(consoleServer: any) {
try {
this.rootLogger = reflect.on(consoleServer).get('LOGGER').get().parent
} catch (error) {
console.error("Can't found rootLogger!")
}
}
}