refactor: extract proxy & fix bungee chaneel
Signed-off-by: MiaoWoo <admin@yumc.pw>
This commit is contained in:
parent
7a1dd3e7b3
commit
51fb5aece3
@ -1,4 +1,4 @@
|
||||
import { injectable, inject, provideSingleton } from "@ccms/container"
|
||||
import { injectable } from "@ccms/container"
|
||||
|
||||
export namespace channel {
|
||||
/**
|
||||
@ -63,156 +63,4 @@ export namespace channel {
|
||||
*/
|
||||
abstract unregister(channel: string, listener?: any): void
|
||||
}
|
||||
export namespace proxy {
|
||||
const CHANNEL_NAME = "BungeeCord"
|
||||
const ByteArrayOutputStream = Java.type('java.io.ByteArrayOutputStream')
|
||||
const DataOutputStream = Java.type('java.io.DataOutputStream')
|
||||
const ByteStreams = Java.type('com.google.common.io.ByteStreams')
|
||||
namespace bungeecord {
|
||||
export class SubChannelBuilder {
|
||||
private channel: Channel
|
||||
private player: any
|
||||
private params: string[]
|
||||
constructor(channel: Channel, player: any) {
|
||||
this.channel = channel
|
||||
this.player = player
|
||||
this.params = []
|
||||
}
|
||||
connect(server: string) {
|
||||
this.params.push("Connect")
|
||||
this.params.push(server)
|
||||
}
|
||||
connectOther(player: string, server: string) {
|
||||
this.params.push("ConnectOther")
|
||||
this.params.push(player)
|
||||
this.params.push(server)
|
||||
}
|
||||
ip() {
|
||||
this.params.push("IP")
|
||||
}
|
||||
ipOther(player: string) {
|
||||
this.params.push("IPOther")
|
||||
this.params.push(player)
|
||||
}
|
||||
playerCount(server: string | "ALL") {
|
||||
this.params.push("PlayerCount")
|
||||
this.params.push(server)
|
||||
}
|
||||
/**
|
||||
* Get a list of players connected on a certain server, or on ALL the servers.
|
||||
* @param server count server
|
||||
* Response:
|
||||
* String server = in.readUTF(); // The name of the server you got the player list of, as given in args.
|
||||
* String[] playerList = in.readUTF().split(", ");
|
||||
*/
|
||||
playerList(server: string | "ALL") {
|
||||
this.params.push("PlayerList")
|
||||
this.params.push(server)
|
||||
}
|
||||
/**
|
||||
* Get a list of server name strings, as defined in BungeeCord's config.yml
|
||||
* Response:
|
||||
* String[] serverList = in.readUTF().split(", ");
|
||||
*/
|
||||
getServers() {
|
||||
this.params.push("GetServers")
|
||||
return this.finalSend()
|
||||
}
|
||||
/**
|
||||
* Get this server's name, as defined in BungeeCord's config.yml
|
||||
*/
|
||||
getServer() {
|
||||
this.params.push("GetServer")
|
||||
return this.finalSend()
|
||||
}
|
||||
broadcast(message: string) {
|
||||
this.message("ALL", message)
|
||||
return this.finalSend()
|
||||
}
|
||||
/**
|
||||
* Send a message (as in, a chat message) to the specified player.
|
||||
* @param player who reciver message
|
||||
* @param message message content
|
||||
*/
|
||||
message(player: string | "ALL", message: string) {
|
||||
this.params.push("Message")
|
||||
this.params.push(player)
|
||||
this.params.push(message)
|
||||
return this.finalSend()
|
||||
}
|
||||
/**
|
||||
* Send a raw message (as in, a chat message) to the specified player. The advantage of this method over Message is that you can include click events and hover events.
|
||||
* @param player who reciver message
|
||||
* @param message message content
|
||||
*/
|
||||
messageRaw(player: string | "ALL", json: string) {
|
||||
this.params.push("MessageRaw")
|
||||
this.params.push(player)
|
||||
this.params.push(json)
|
||||
return this.finalSend()
|
||||
}
|
||||
forwardAll(channel: string, data: any) {
|
||||
this.forward("ALL", channel, data)
|
||||
}
|
||||
/**
|
||||
* Send a custom plugin message to said server. This is one of the most useful channels ever.
|
||||
* Remember, the sending and receiving server(s) need to have a player online.
|
||||
* @param server reciver
|
||||
* @param channel channelName
|
||||
* @param data data
|
||||
*/
|
||||
forward(server: string | "ALL", channel: string, data: any) {
|
||||
this.params.push("Forward")
|
||||
this.params.push(server)
|
||||
this.params.push(channel)
|
||||
this.params.push(typeof data === "string" ? data : JSON.stringify(data))
|
||||
return this.finalSend()
|
||||
}
|
||||
/**
|
||||
* Send a custom plugin message to said server. This is one of the most useful channels ever.
|
||||
* Remember, the sending and receiving server(s) need to have a player online.
|
||||
* @param server reciver
|
||||
* @param channel channelName
|
||||
* @param data data
|
||||
*/
|
||||
forwardToPlayer(server: string | "ALL", channel: string, data: any) {
|
||||
this.params.push("Forward")
|
||||
this.params.push(server)
|
||||
this.params.push(channel)
|
||||
this.params.push(typeof data === "string" ? data : JSON.stringify(data))
|
||||
return this.finalSend()
|
||||
}
|
||||
generic(...args: string[]) {
|
||||
args && this.params.concat(...args)
|
||||
return this.finalSend()
|
||||
}
|
||||
private send(...middlewares: ((out: java.io.DataOutputStream) => void)[]) {
|
||||
let byteArray = new ByteArrayOutputStream()
|
||||
let out = new DataOutputStream(byteArray)
|
||||
this.params.forEach(utf => out.writeUTF(utf))
|
||||
for (let middleware of middlewares) {
|
||||
middleware(out)
|
||||
}
|
||||
this.channel.send(this.player, CHANNEL_NAME, byteArray.toByteArray())
|
||||
}
|
||||
private finalSend() {
|
||||
return {
|
||||
send: this.send.bind(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@provideSingleton(BungeeCord)
|
||||
export class BungeeCord {
|
||||
@inject(Channel)
|
||||
private channel: Channel
|
||||
/**
|
||||
* 获得代理
|
||||
* @param player 玩家
|
||||
*/
|
||||
for(player: any): bungeecord.SubChannelBuilder {
|
||||
return new bungeecord.SubChannelBuilder(this.channel, player)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -3,6 +3,7 @@ import "@ccms/nashorn"
|
||||
export * from './chat'
|
||||
export * from './task'
|
||||
export * from './event'
|
||||
export * from './proxy'
|
||||
export * from './console'
|
||||
export * from './channel'
|
||||
export * from './command'
|
||||
|
160
packages/api/src/proxy.ts
Normal file
160
packages/api/src/proxy.ts
Normal file
@ -0,0 +1,160 @@
|
||||
import { provideSingleton, Autowired } from '@ccms/container'
|
||||
import { channel } from './channel'
|
||||
|
||||
export namespace proxy {
|
||||
const ByteArrayOutputStream = Java.type('java.io.ByteArrayOutputStream')
|
||||
const DataOutputStream = Java.type('java.io.DataOutputStream')
|
||||
namespace bungeecord {
|
||||
const CHANNEL_NAME = "BungeeCord"
|
||||
export class SubChannelBuilder {
|
||||
private channel: channel.Channel
|
||||
private player: any
|
||||
private params: string[]
|
||||
constructor(channel: channel.Channel, player: any) {
|
||||
this.channel = channel
|
||||
this.player = player
|
||||
this.params = []
|
||||
}
|
||||
connect(server: string) {
|
||||
this.params.push("Connect")
|
||||
this.params.push(server)
|
||||
return this.finalSend()
|
||||
}
|
||||
connectOther(player: string, server: string) {
|
||||
this.params.push("ConnectOther")
|
||||
this.params.push(player)
|
||||
this.params.push(server)
|
||||
return this.finalSend()
|
||||
}
|
||||
ip() {
|
||||
this.params.push("IP")
|
||||
return this.finalSend()
|
||||
}
|
||||
ipOther(player: string) {
|
||||
this.params.push("IPOther")
|
||||
this.params.push(player)
|
||||
return this.finalSend()
|
||||
}
|
||||
playerCount(server: string | "ALL") {
|
||||
this.params.push("PlayerCount")
|
||||
this.params.push(server)
|
||||
return this.finalSend()
|
||||
}
|
||||
/**
|
||||
* Get a list of players connected on a certain server, or on ALL the servers.
|
||||
* @param server count server
|
||||
* Response:
|
||||
* String server = in.readUTF(); // The name of the server you got the player list of, as given in args.
|
||||
* String[] playerList = in.readUTF().split(", ");
|
||||
*/
|
||||
playerList(server: string | "ALL") {
|
||||
this.params.push("PlayerList")
|
||||
this.params.push(server)
|
||||
return this.finalSend()
|
||||
}
|
||||
/**
|
||||
* Get a list of server name strings, as defined in BungeeCord's config.yml
|
||||
* Response:
|
||||
* String[] serverList = in.readUTF().split(", ");
|
||||
*/
|
||||
getServers() {
|
||||
this.params.push("GetServers")
|
||||
return this.finalSend()
|
||||
}
|
||||
/**
|
||||
* Get this server's name, as defined in BungeeCord's config.yml
|
||||
*/
|
||||
getServer() {
|
||||
this.params.push("GetServer")
|
||||
return this.finalSend()
|
||||
}
|
||||
broadcast(message: string) {
|
||||
this.message("ALL", message)
|
||||
return this.finalSend()
|
||||
}
|
||||
/**
|
||||
* Send a message (as in, a chat message) to the specified player.
|
||||
* @param player who reciver message
|
||||
* @param message message content
|
||||
*/
|
||||
message(player: string | "ALL", message: string) {
|
||||
this.params.push("Message")
|
||||
this.params.push(player)
|
||||
this.params.push(message)
|
||||
return this.finalSend()
|
||||
}
|
||||
/**
|
||||
* Send a raw message (as in, a chat message) to the specified player. The advantage of this method over Message is that you can include click events and hover events.
|
||||
* @param player who reciver message
|
||||
* @param message message content
|
||||
*/
|
||||
messageRaw(player: string | "ALL", json: string) {
|
||||
this.params.push("MessageRaw")
|
||||
this.params.push(player)
|
||||
this.params.push(json)
|
||||
return this.finalSend()
|
||||
}
|
||||
forwardAll(channel: string, data: any) {
|
||||
return this.forward("ALL", channel, data)
|
||||
}
|
||||
/**
|
||||
* Send a custom plugin message to said server. This is one of the most useful channels ever.
|
||||
* Remember, the sending and receiving server(s) need to have a player online.
|
||||
* @param server reciver
|
||||
* @param channel channelName
|
||||
* @param data data
|
||||
*/
|
||||
forward(server: string | "ALL", channel: string, data: any) {
|
||||
this.params.push("Forward")
|
||||
this.params.push(server)
|
||||
this.params.push(channel)
|
||||
this.params.push(typeof data === "string" ? data : JSON.stringify(data))
|
||||
return this.finalSend()
|
||||
}
|
||||
/**
|
||||
* Send a custom plugin message to said server. This is one of the most useful channels ever.
|
||||
* Remember, the sending and receiving server(s) need to have a player online.
|
||||
* @param server reciver
|
||||
* @param channel channelName
|
||||
* @param data data
|
||||
*/
|
||||
forwardToPlayer(server: string | "ALL", channel: string, data: any) {
|
||||
this.params.push("Forward")
|
||||
this.params.push(server)
|
||||
this.params.push(channel)
|
||||
this.params.push(typeof data === "string" ? data : JSON.stringify(data))
|
||||
return this.finalSend()
|
||||
}
|
||||
generic(...args: string[]) {
|
||||
args && this.params.concat(...args)
|
||||
return this.finalSend()
|
||||
}
|
||||
private send(...middlewares: ((out: java.io.DataOutputStream) => void)[]) {
|
||||
let byteArray = new ByteArrayOutputStream()
|
||||
let out = new DataOutputStream(byteArray)
|
||||
this.params.forEach(utf => out.writeUTF(utf))
|
||||
for (let middleware of middlewares) {
|
||||
middleware(out)
|
||||
}
|
||||
return this.channel.send(this.player, CHANNEL_NAME, byteArray.toByteArray())
|
||||
}
|
||||
private finalSend() {
|
||||
return {
|
||||
send: this.send.bind(this)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@provideSingleton(BungeeCord)
|
||||
export class BungeeCord {
|
||||
@Autowired()
|
||||
private channel: channel.Channel
|
||||
/**
|
||||
* 获得代理
|
||||
* @param player 玩家
|
||||
*/
|
||||
for(player: any): bungeecord.SubChannelBuilder {
|
||||
return new bungeecord.SubChannelBuilder(this.channel, player)
|
||||
}
|
||||
}
|
||||
}
|
@ -6,20 +6,20 @@ const Bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy()
|
||||
@provideSingleton(channel.Channel)
|
||||
export class BungeeChannel extends channel.Channel {
|
||||
@inject(event.Event)
|
||||
private eventManager: event.Event;
|
||||
private eventManager: event.Event
|
||||
|
||||
send(player: any, channel: string, data: any) {
|
||||
throw new Error("Method not implemented.");
|
||||
throw new Error("Method not implemented.")
|
||||
}
|
||||
register(channel: string, listener: channel.ChannelListener) {
|
||||
Bungee.registerChannel(channel);
|
||||
Bungee.registerChannel(channel)
|
||||
// console.console('§6[§eWARN§6] §eMiaoScript channel in BungeeCord only register. you need self hanler PluginMessageEvent!')
|
||||
return this.eventManager.listen({ description: { name: channel } }, "PluginMessageEvent", (event: net.md_5.bungee.api.event.PluginMessageEvent) => {
|
||||
listener(event.getData(), event)
|
||||
})
|
||||
}
|
||||
unregister(channel: string, listener: any) {
|
||||
Bungee.unregisterChannel(channel);
|
||||
listener.off();
|
||||
unregister(channel: string, off: any) {
|
||||
Bungee.unregisterChannel(channel)
|
||||
off()
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user