feat: update plugin & websocket

Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
MiaoWoo 2020-04-24 16:42:36 +08:00
parent 58353cb5b8
commit e68689c560
5 changed files with 31 additions and 25 deletions

View File

@ -2,9 +2,9 @@
/// <reference types="@ms/types/dist/typings/sponge" /> /// <reference types="@ms/types/dist/typings/sponge" />
/// <reference types="@ms/types/dist/typings/bungee" /> /// <reference types="@ms/types/dist/typings/bungee" />
import { server, plugin as pluginApi, channel } from '@ms/api' import { server, plugin as pluginApi, channel, constants } from '@ms/api'
import { inject, optional } from '@ms/container'; import { inject, optional } from '@ms/container';
import { plugin, interfaces, cmd, listener, tab, config } from '@ms/plugin' import { plugin, interfaces, cmd, listener, tab, config, enable } from '@ms/plugin'
import Tellraw from '@ms/common/dist/tellraw' import Tellraw from '@ms/common/dist/tellraw'
const ByteArrayInputStream = Java.type("java.io.ByteArrayInputStream"); const ByteArrayInputStream = Java.type("java.io.ByteArrayInputStream");
@ -12,10 +12,8 @@ const ByteArrayOutputStream = Java.type("java.io.ByteArrayOutputStream");
const StandardCharsets = Java.type("java.nio.charset.StandardCharsets"); const StandardCharsets = Java.type("java.nio.charset.StandardCharsets");
const GZIPInputStream = Java.type("java.util.zip.GZIPInputStream"); const GZIPInputStream = Java.type("java.util.zip.GZIPInputStream");
const GZIPOutputStream = Java.type("java.util.zip.GZIPOutputStream"); const GZIPOutputStream = Java.type("java.util.zip.GZIPOutputStream");
const Consumer = Java.type("java.util.function.Consumer");
const ByteArray = Java.type("byte[]")
const BiConsumer = Java.type('java.util.function.BiConsumer') const BiConsumer = Java.type('java.util.function.BiConsumer')
const ByteArray = Java.type("byte[]")
class MiaoMessage { class MiaoMessage {
// public static final String CHANNEL = "MiaoChat:Default".toLowerCase(); // public static final String CHANNEL = "MiaoChat:Default".toLowerCase();
@ -146,7 +144,7 @@ export class MiaoChat extends interfaces.Plugin {
} }
private compare(prop: string) { private compare(prop: string) {
return function(obj1: { [x: string]: any; }, obj2: { [x: string]: any; }) { return function (obj1: { [x: string]: any; }, obj2: { [x: string]: any; }) {
var val1 = obj1[prop]; var val1 = obj1[prop];
var val2 = obj2[prop]; var val2 = obj2[prop];
if (!isNaN(Number(val1)) && !isNaN(Number(val2))) { if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
@ -184,9 +182,6 @@ export class MiaoChat extends interfaces.Plugin {
} catch (ex) { } catch (ex) {
this.logger.console("§cCan't found me.clip.placeholderapi.PlaceholderAPI variable will not be replaced! Err: " + ex) this.logger.console("§cCan't found me.clip.placeholderapi.PlaceholderAPI variable will not be replaced! Err: " + ex)
} }
this.channelOff = this.Channel?.listen(this, MiaoMessage.CHANNEL, (data) => {
this.sendChatAll(MiaoMessage.decode(data).json)
})
} }
spongeenable() { spongeenable() {
@ -205,6 +200,10 @@ export class MiaoChat extends interfaces.Plugin {
} catch (ex) { } catch (ex) {
this.logger.console("§cCan't found me.rojo8399.placeholderapi.PlaceholderService variable will not be replaced! Err: " + ex) this.logger.console("§cCan't found me.rojo8399.placeholderapi.PlaceholderService variable will not be replaced! Err: " + ex)
} }
}
@enable({ servers: [constants.ServerType.Bukkit, constants.ServerType.Sponge] })
serverEnbale() {
this.channelOff = this.Channel?.listen(this, MiaoMessage.CHANNEL, (data) => { this.channelOff = this.Channel?.listen(this, MiaoMessage.CHANNEL, (data) => {
this.sendChatAll(MiaoMessage.decode(data).json) this.sendChatAll(MiaoMessage.decode(data).json)
}) })
@ -244,7 +243,7 @@ export class MiaoChat extends interfaces.Plugin {
@listener({ servers: ['bukkit'] }) @listener({ servers: ['bukkit'] })
AsyncPlayerChatEvent(event: org.bukkit.event.player.AsyncPlayerChatEvent) { AsyncPlayerChatEvent(event: org.bukkit.event.player.AsyncPlayerChatEvent) {
this.sendChat(event.getPlayer(), event.getMessage(), function() { this.sendChat(event.getPlayer(), event.getMessage(), function () {
event.setCancelled(true); event.setCancelled(true);
}); });
} }
@ -260,7 +259,7 @@ export class MiaoChat extends interfaces.Plugin {
if (plain.startsWith(Tellraw.duplicateChar)) { if (plain.startsWith(Tellraw.duplicateChar)) {
return; return;
} }
this.sendChat(player, plain, function() { this.sendChat(player, plain, function () {
event.setMessageCancelled(true) event.setMessageCancelled(true)
}); });
} }

View File

@ -7,6 +7,12 @@ import { plugin, interfaces, cmd } from '@ms/plugin'
import { inject, ContainerInstance, Container } from '@ms/container' import { inject, ContainerInstance, Container } from '@ms/container'
import io, { Server as SocketIOServer, Socket as SocketIOSocket } from '@ms/websocket' import io, { Server as SocketIOServer, Socket as SocketIOSocket } from '@ms/websocket'
const suffixMap = {
ts: 'typescript',
js: 'javascript',
yml: 'yaml'
}
@plugin({ name: 'MiaoConsole', version: '1.0.0', author: 'MiaoWoo', servers: ['!nukkit'], source: __filename }) @plugin({ name: 'MiaoConsole', version: '1.0.0', author: 'MiaoWoo', servers: ['!nukkit'], source: __filename })
export class MiaoConsole extends interfaces.Plugin { export class MiaoConsole extends interfaces.Plugin {
@inject(ContainerInstance) @inject(ContainerInstance)
@ -84,7 +90,7 @@ export class MiaoConsole extends interfaces.Plugin {
} }
}) })
client.on('edit', (file: string, fn) => { client.on('edit', (file: string, fn) => {
fn && fn(base.read(file), file.split('.', 2)[1]) fn && fn(base.read(file), suffixMap[file.split('.', 2)[1]])
}) })
client.on('disconnect', () => { client.on('disconnect', () => {
this.logger.console(`§6客户端 §b${client.id} §c断开连接...`) this.logger.console(`§6客户端 §b${client.id} §c断开连接...`)
@ -93,16 +99,16 @@ export class MiaoConsole extends interfaces.Plugin {
this.logger.info('Netty Channel Pipeline Inject MiaoDetectHandler Successful!') this.logger.info('Netty Channel Pipeline Inject MiaoDetectHandler Successful!')
} }
private runCode(code: string, namespace, client) { private runCode(code: string, namespace: any, client: any) {
let tfunc = new Function('namespace', 'client', ` let tfunc = new Function('namespace', 'client', `
var reflect = require('@ms/common/dist/reflect'); var reflect = require('@ms/common/dist/reflect');
var tempconcent = ''; var tempconcent = '';
function print(text) { function print(text) {
tempconcent += "\\n" + text tempconcent += text + "\\n"
} }
return eval(${JSON.stringify(code)}) + tempconcent var result = eval(${JSON.stringify(code)});
return tempconcent + result
`) `)
console.log(tfunc)
return this.task.callSyncMethod(() => tfunc.apply(this, [namespace, client])) + '' return this.task.callSyncMethod(() => tfunc.apply(this, [namespace, client])) + ''
} }
} }

View File

@ -3,7 +3,9 @@
"compilerOptions": { "compilerOptions": {
"baseUrl": "src", "baseUrl": "src",
"outDir": "dist", "outDir": "dist",
"skipLibCheck": true "skipLibCheck": true,
"sourceMap": false,
"inlineSourceMap": true
}, },
"exclude": [ "exclude": [
"dist", "dist",

View File

@ -1,10 +1,10 @@
import { EventEmitter } from 'events' import { EventEmitter } from 'events'
import { NettyClient } from './client'
import { ServerOptions } from '../socket-io'
import { ServerEvent, Keys } from './constants' import { ServerEvent, Keys } from './constants'
import { WebSocketDetect } from './websocket_detect' import { WebSocketDetect } from './websocket_detect'
import { WebSocketHandler } from './websocket_handler' import { WebSocketHandler } from './websocket_handler'
import { NettyClient } from './client'
import { ServerOptions } from '../socket-io'
class NettyWebSocketServer extends EventEmitter { class NettyWebSocketServer extends EventEmitter {
private pipeline: any; private pipeline: any;
@ -22,10 +22,9 @@ class NettyWebSocketServer extends EventEmitter {
ctx.fireChannelRead(channel) ctx.fireChannelRead(channel)
}) })
connectEvent.on(ServerEvent.connect, (ctx) => { connectEvent.on(ServerEvent.connect, (ctx) => {
console.log('NettyWebSocketServer ServerEvent.connect', ctx, ctx.channel().id(), ctx.channel().class.name) let nettyClient = new NettyClient(this, ctx.channel())
let nettyClient = new NettyClient(this, ctx.channel()); this.allClients[nettyClient.id] = nettyClient
this.allClients[nettyClient.id] = nettyClient; this.emit(ServerEvent.connect, nettyClient)
this.emit(ServerEvent.connect, nettyClient);
}) })
connectEvent.on(ServerEvent.message, (ctx, msg) => { connectEvent.on(ServerEvent.message, (ctx, msg) => {
this.emit(ServerEvent.message, this.allClients[ctx.channel().id()], msg.text()) this.emit(ServerEvent.message, this.allClients[ctx.channel().id()], msg.text())
@ -46,4 +45,4 @@ export {
NettyWebSocketServer, NettyWebSocketServer,
ServerEvent, ServerEvent,
NettyClient NettyClient
}; }

View File

@ -1,7 +1,7 @@
import { TextWebSocketFrameHandlerAdapter } from '../netty'
import { EventEmitter } from 'events' import { EventEmitter } from 'events'
import { ServerEvent } from './constants' import { ServerEvent } from './constants'
import { ServerOptions } from '../socket-io'; import { ServerOptions } from '../socket-io';
import { TextWebSocketFrameHandlerAdapter } from '../netty'
export class TextWebSocketFrameHandler extends TextWebSocketFrameHandlerAdapter { export class TextWebSocketFrameHandler extends TextWebSocketFrameHandlerAdapter {
private event: EventEmitter; private event: EventEmitter;