feat: optimize code

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-01-17 17:47:38 +08:00
parent 9984787202
commit da72a0dac6
2 changed files with 18 additions and 23 deletions

View File

@ -35,10 +35,6 @@ export class MiaoScriptPackageManager extends interfaces.Plugin {
}).async().submit(); }).async().submit();
} }
enable() {
}
@cmd() @cmd()
mpm(sender: any, command: string, args: string[]) { mpm(sender: any, command: string, args: string[]) {
this.taskManager.create(() => this.main(sender, command, args)).async().submit(); this.taskManager.create(() => this.main(sender, command, args)).async().submit();

View File

@ -4,22 +4,25 @@
import { plugin as pluginApi, server, task } from '@ms/api' import { plugin as pluginApi, server, task } from '@ms/api'
import { plugin, interfaces, cmd } from '@ms/plugin' import { plugin, interfaces, cmd } from '@ms/plugin'
import { DefaultContainer as container, inject, postConstruct } from '@ms/container' import { inject, Container, ContainerInstance, postConstruct } from '@ms/container'
import * as reflect from '@ms/common/dist/reflect' import * as reflect from '@ms/common/dist/reflect'
let clients: any[] = [] let clients: any[] = []
let SPLIT_LINE = '\\M\\W\\S|T|S|S/L/T/' let SPLIT_LINE = '\\M\\W\\S|T|S|S/L/T/'
let MessageHandle = Symbol.for('MiaoConsole:MessageHandle')
const refList: Array<{ server: string, future: string }> = [ const refList: Array<{ server: string, future: string }> = [
{ server: 'an', future: 'g' },//spigot 1.12.2 { server: 'an', future: 'g' },//spigot 1.12.2
{ server: 'getServerConnection', future: 'f' },//spigot 1.14.4 { server: 'getServerConnection', future: 'f' },//after spigot 1.14.4
{ server: 'func_147137_ag', future: 'field_151274_e' }//catserver 1.12.2 { server: 'func_147137_ag', future: 'field_151274_e' }//catserver 1.12.2
] ]
const Callable = Java.type('java.util.concurrent.Callable')
const Runnable = Java.type('java.lang.Runnable')
@plugin({ name: 'MiaoConsole', version: '1.0.0', author: 'MiaoWoo', source: __filename }) @plugin({ name: 'MiaoConsole', version: '1.0.0', author: 'MiaoWoo', source: __filename })
export class MiaoConsole extends interfaces.Plugin { export class MiaoConsole extends interfaces.Plugin {
public static GlobalContainer: Container
public static GlobalLogger: Console
@inject(ContainerInstance)
private container: Container
@inject(pluginApi.PluginManager) @inject(pluginApi.PluginManager)
private PluginManager: pluginApi.PluginManager private PluginManager: pluginApi.PluginManager
@inject(server.ServerType) @inject(server.ServerType)
@ -28,8 +31,6 @@ export class MiaoConsole extends interfaces.Plugin {
private Server: server.Server private Server: server.Server
@inject(task.TaskManager) @inject(task.TaskManager)
private Task: task.TaskManager private Task: task.TaskManager
@inject(pluginApi.PluginInstance)
private pluginInstance: any
private pipeline: any private pipeline: any
@ -43,13 +44,18 @@ export class MiaoConsole extends interfaces.Plugin {
} }
} }
enable() {
MiaoConsole.GlobalLogger = this.logger
MiaoConsole.GlobalContainer = this.container
}
disable() { disable() {
if (this.pipeline) { if (this.pipeline) {
if (this.pipeline.names().contains('miao_detect')) { if (this.pipeline.names().contains('miao_detect')) {
this.pipeline.remove('miao_detect') this.pipeline.remove('miao_detect')
} }
clients.forEach(c => c.close()) clients.forEach(c => c.close())
container.unbind('onmessage') this.container.unbind(MessageHandle)
} }
} }
@ -93,7 +99,7 @@ export class MiaoConsole extends interfaces.Plugin {
injectMiaoDetect() { injectMiaoDetect() {
this.pipeline.addFirst('miao_detect', new MiaoDetectHandler()) this.pipeline.addFirst('miao_detect', new MiaoDetectHandler())
container.bind('onmessage').toFunction(this.onmessage.bind(this)) this.container.bind(MessageHandle).toFunction(this.onmessage.bind(this))
this.logger.info('Netty Channel Pipeline Inject MiaoDetectHandler Successful!') this.logger.info('Netty Channel Pipeline Inject MiaoDetectHandler Successful!')
} }
@ -114,14 +120,7 @@ export class MiaoConsole extends interfaces.Plugin {
} }
execCode(ctx: any, code: string) { execCode(ctx: any, code: string) {
switch (this.ServerType) { return this.Task.callSyncMethod(() => eval(code)) + '';
case "bukkit":
return org.bukkit.Bukkit.getScheduler().callSyncMethod(this.pluginInstance, new Callable({ call: () => eval(code) })).get() || '无返回结果'
case "sponge":
return org.spongepowered.api.Sponge.getScheduler().createSyncExecutor(this.pluginInstance).schedule(new Runnable({ run: () => eval(code) }), 0, {})
case "bungee":
return eval(code)
}
} }
execDetect(ctx: any, cmd: string) { execDetect(ctx: any, cmd: string) {
@ -203,14 +202,14 @@ const TextWebSocketFrameHandler = Java.extend(SimpleChannelInboundHandler, {
userEventTriggered: (ctx: any, evt: any) => { userEventTriggered: (ctx: any, evt: any) => {
if (evt == 'HANDSHAKE_COMPLETE') { if (evt == 'HANDSHAKE_COMPLETE') {
clients.push(ctx.channel()) clients.push(ctx.channel())
console.console(`§6[§cMS§6][§bWebSocket§6]§r new client §b${ctx.channel().id()} §aconnected...`) MiaoConsole.GlobalLogger.console(`new client §b${ctx.channel().id()} §aconnected...`)
} }
}, },
acceptInboundMessage: (msg: any) => { acceptInboundMessage: (msg: any) => {
return TextWebSocketFrameMatcher.match(msg) return TextWebSocketFrameMatcher.match(msg)
}, },
channelRead0: (ctx: any, msg: any) => { channelRead0: (ctx: any, msg: any) => {
container.get<any>('onmessage')(ctx, msg) MiaoConsole.GlobalContainer.get<any>(MessageHandle)(ctx, msg)
} }
}) })
const WebSocketHandler = Java.extend(ChannelInboundHandlerAdapter, { const WebSocketHandler = Java.extend(ChannelInboundHandlerAdapter, {