2020-06-23 07:51:55 +00:00
|
|
|
/// <reference types="@javatypes/bungee-api" />
|
|
|
|
/// <reference types="@javatypes/spigot-api" />
|
|
|
|
/// <reference types="@javatypes/sponge-api" />
|
2020-01-16 09:27:24 +00:00
|
|
|
|
2020-09-24 05:51:18 +00:00
|
|
|
import { server, channel, constants, chat } from '@ccms/api'
|
|
|
|
import { optional, Autowired } from '@ccms/container'
|
2020-05-02 16:14:47 +00:00
|
|
|
import { plugin, interfaces, cmd, listener, tab, config, enable } from '@ccms/plugin'
|
|
|
|
import Tellraw from '@ccms/common/dist/tellraw'
|
2020-01-16 09:27:24 +00:00
|
|
|
|
2020-09-22 10:43:02 +00:00
|
|
|
const ByteArrayInputStream = Java.type("java.io.ByteArrayInputStream")
|
|
|
|
const ByteArrayOutputStream = Java.type("java.io.ByteArrayOutputStream")
|
|
|
|
const StandardCharsets = Java.type("java.nio.charset.StandardCharsets")
|
|
|
|
const GZIPInputStream = Java.type("java.util.zip.GZIPInputStream")
|
|
|
|
const GZIPOutputStream = Java.type("java.util.zip.GZIPOutputStream")
|
2020-02-13 20:03:25 +00:00
|
|
|
const BiConsumer = Java.type('java.util.function.BiConsumer')
|
2020-04-24 08:42:36 +00:00
|
|
|
const ByteArray = Java.type("byte[]")
|
2020-02-13 20:03:25 +00:00
|
|
|
|
2020-01-17 09:48:23 +00:00
|
|
|
class MiaoMessage {
|
|
|
|
// public static final String CHANNEL = "MiaoChat:Default".toLowerCase();
|
|
|
|
public static CHANNEL: string = "MiaoChat:Default".toLowerCase()
|
|
|
|
// private static final int MAX_MESSAGE_LENGTH = 32000;
|
|
|
|
private static MAX_MESSAGE_LENGTH = 32000;
|
|
|
|
|
|
|
|
private static copy(input, output) {
|
2020-09-22 10:43:02 +00:00
|
|
|
let buffer = new ByteArray(1024)
|
|
|
|
let n: number
|
2020-01-17 09:48:23 +00:00
|
|
|
while ((n = input.read(buffer)) != -1) {
|
2020-09-22 10:43:02 +00:00
|
|
|
output.write(buffer, 0, n)
|
2020-01-17 09:48:23 +00:00
|
|
|
}
|
2020-09-22 10:43:02 +00:00
|
|
|
input.close()
|
|
|
|
output.close()
|
2020-01-17 09:48:23 +00:00
|
|
|
}
|
|
|
|
public static encode(input: any): any {
|
2020-09-22 10:43:02 +00:00
|
|
|
return new MiaoMessage(input).encode()
|
2020-01-17 09:48:23 +00:00
|
|
|
}
|
|
|
|
public static decode(input: any): MiaoMessage {
|
2020-09-22 10:43:02 +00:00
|
|
|
let baos = new ByteArrayOutputStream()
|
|
|
|
MiaoMessage.copy(new GZIPInputStream(new ByteArrayInputStream(input)), baos)
|
|
|
|
return new MiaoMessage(baos.toString(StandardCharsets.UTF_8.name()))
|
2020-01-17 09:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// private String json;
|
|
|
|
constructor(public json: any) { }
|
|
|
|
|
|
|
|
public encode(): any {
|
2020-09-22 10:43:02 +00:00
|
|
|
let baos = new ByteArrayOutputStream()
|
|
|
|
MiaoMessage.copy(new ByteArrayInputStream(this.json.getBytes(StandardCharsets.UTF_8)), new GZIPOutputStream(baos))
|
|
|
|
if (baos.size() > MiaoMessage.MAX_MESSAGE_LENGTH) { return null }
|
|
|
|
return baos.toByteArray()
|
2020-01-17 09:48:23 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-16 09:27:24 +00:00
|
|
|
@plugin({ name: 'MiaoChat', version: '1.0.0', author: 'MiaoWoo', source: __filename })
|
|
|
|
export class MiaoChat extends interfaces.Plugin {
|
2020-09-24 05:51:18 +00:00
|
|
|
@Autowired()
|
2020-01-16 09:27:24 +00:00
|
|
|
private Server: server.Server
|
2020-09-24 05:51:18 +00:00
|
|
|
@optional()
|
|
|
|
@Autowired()
|
2020-09-22 10:43:02 +00:00
|
|
|
private chat: chat.Chat
|
2020-09-24 05:51:18 +00:00
|
|
|
@Autowired()
|
|
|
|
private Channel: channel.Channel
|
2020-02-27 10:14:17 +00:00
|
|
|
|
2020-09-22 10:43:02 +00:00
|
|
|
private channelOff: { off: () => void }
|
2020-01-16 09:27:24 +00:00
|
|
|
|
2020-02-24 08:25:38 +00:00
|
|
|
@config()
|
2020-01-16 09:27:24 +00:00
|
|
|
private config = {
|
2020-02-24 08:25:38 +00:00
|
|
|
Version: "1.0.0",
|
2020-01-16 09:27:24 +00:00
|
|
|
BungeeCord: true,
|
|
|
|
Server: "生存服",
|
|
|
|
ChatFormats: {
|
|
|
|
default: {
|
|
|
|
index: 50,
|
|
|
|
permission: "MiaoChat.default",
|
|
|
|
range: 0,
|
|
|
|
format: "[world][player]: ",
|
|
|
|
item: true,
|
|
|
|
itemformat: "&6[&b%s&6]&r"
|
|
|
|
},
|
|
|
|
admin: {
|
|
|
|
index: 49,
|
|
|
|
permission: "MiaoChat.admin",
|
|
|
|
format: "[admin][world][player][help]: ",
|
|
|
|
range: 0,
|
|
|
|
item: true,
|
|
|
|
itemformat: "&6[&b%s&6]&r"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
StyleFormats: {
|
|
|
|
world: {
|
|
|
|
text: "&6[&a%player_world%&6]",
|
|
|
|
hover: [
|
|
|
|
"&6当前所在位置:",
|
|
|
|
"&6世界: &d%player_world%",
|
|
|
|
"&6坐标: &aX:%player_x% Y: %player_y% Z: %player_z%",
|
|
|
|
"",
|
|
|
|
"&c点击即可TP我!"
|
|
|
|
],
|
|
|
|
click: {
|
|
|
|
type: "COMMAND",
|
|
|
|
command: "/tpa %player_name%"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
player: {
|
|
|
|
text: "&b%player_name%",
|
|
|
|
hover: [
|
|
|
|
"&6玩家名称: &b%player_name%",
|
|
|
|
"&6玩家等级: &a%player_level%",
|
|
|
|
"&6玩家血量: &c%player_health%",
|
|
|
|
"&6玩家饥饿: &d%player_food_level%",
|
|
|
|
"&6游戏模式: &4%player_gamemode%",
|
|
|
|
"",
|
|
|
|
"&c点击与我聊天"
|
|
|
|
],
|
|
|
|
click: {
|
|
|
|
type: "SUGGEST",
|
|
|
|
command: "/tell %player_name%"
|
|
|
|
}
|
|
|
|
},
|
|
|
|
admin: {
|
|
|
|
text: "&6[&c管理员&6]"
|
|
|
|
},
|
|
|
|
help: {
|
|
|
|
text: "&4[求助]",
|
|
|
|
hover: [
|
|
|
|
"点击求助OP"
|
|
|
|
],
|
|
|
|
click: {
|
|
|
|
type: "COMMAND",
|
|
|
|
command: "管理员@%player_name% 我需要你的帮助!"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-22 10:43:02 +00:00
|
|
|
private chatFormats: any[]
|
|
|
|
private styleFormats: any
|
2020-01-16 09:27:24 +00:00
|
|
|
// 用于匹配 '[xx]' 聊天格式
|
|
|
|
private FORMAT_PATTERN = /[\[]([^\[\]]+)[\]]/ig;
|
|
|
|
|
2020-09-22 10:43:02 +00:00
|
|
|
private PlaceholderAPI: { setPlaceholders: (player: any, str: string) => string }
|
2020-01-16 09:27:24 +00:00
|
|
|
|
|
|
|
load() {
|
2020-09-22 10:43:02 +00:00
|
|
|
this.chatFormats = Object.values(this.config.ChatFormats)
|
|
|
|
this.chatFormats.sort(this.compare('index'))
|
|
|
|
this.initFormat(this.chatFormats)
|
|
|
|
this.styleFormats = this.config.StyleFormats
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private compare(prop: string) {
|
2020-09-22 10:43:02 +00:00
|
|
|
return function (obj1: { [x: string]: any }, obj2: { [x: string]: any }) {
|
|
|
|
var val1 = obj1[prop]
|
|
|
|
var val2 = obj2[prop]
|
2020-01-16 09:27:24 +00:00
|
|
|
if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
|
2020-09-22 10:43:02 +00:00
|
|
|
val1 = Number(val1)
|
|
|
|
val2 = Number(val2)
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
|
|
|
if (val1 < val2) {
|
2020-09-22 10:43:02 +00:00
|
|
|
return -1
|
2020-01-16 09:27:24 +00:00
|
|
|
} else if (val1 > val2) {
|
2020-09-22 10:43:02 +00:00
|
|
|
return 1
|
2020-01-16 09:27:24 +00:00
|
|
|
} else {
|
2020-09-22 10:43:02 +00:00
|
|
|
return 0
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enable() {
|
2020-01-17 09:48:23 +00:00
|
|
|
this.PlaceholderAPI = {
|
|
|
|
setPlaceholders: (player: any, string: string) => {
|
2020-09-22 10:43:02 +00:00
|
|
|
return string
|
2020-01-17 09:48:23 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-24 05:51:18 +00:00
|
|
|
if (!this.chat) {
|
|
|
|
this.logger.console('§4消息管理器注入失败 请检查当前服务器是否兼容...')
|
|
|
|
this.AsyncPlayerChatEvent['off']()
|
|
|
|
this.MessageChannelEvent$Chat['off']()
|
|
|
|
}
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
2020-01-17 09:48:23 +00:00
|
|
|
|
2020-01-16 09:27:24 +00:00
|
|
|
disable() {
|
2020-02-27 10:14:17 +00:00
|
|
|
this.channelOff?.off()
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bukkitenable() {
|
|
|
|
// 尝试加载 Bukkit 的 PlaceholderAPI
|
|
|
|
try {
|
|
|
|
//@ts-ignore
|
2020-09-22 10:43:02 +00:00
|
|
|
this.PlaceholderAPI = base.getClass("me.clip.placeholderapi.PlaceholderAPI").static
|
2020-01-17 09:48:23 +00:00
|
|
|
this.logger.log('[PAPI] Found Bukkit PlaceholderAPI Hooking...')
|
2020-01-16 09:27:24 +00:00
|
|
|
} catch (ex) {
|
2020-01-17 09:48:23 +00:00
|
|
|
this.logger.console("§cCan't found me.clip.placeholderapi.PlaceholderAPI variable will not be replaced! Err: " + ex)
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spongeenable() {
|
|
|
|
// 尝试加载 Sponge 的 PlaceholderAPI
|
|
|
|
try {
|
2020-09-22 10:43:02 +00:00
|
|
|
var spongePapi = this.Server.getService('me.rojo8399.placeholderapi.PlaceholderService')
|
|
|
|
var s = org.spongepowered.api.text.serializer.TextSerializers.formattingCode('§')
|
2020-01-16 09:27:24 +00:00
|
|
|
if (spongePapi) {
|
|
|
|
this.PlaceholderAPI = {
|
|
|
|
setPlaceholders: (player: any, string: string) => {
|
2020-09-22 10:43:02 +00:00
|
|
|
return s.serialize(spongePapi.replacePlaceholders(string, player, player))
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
2020-09-22 10:43:02 +00:00
|
|
|
}
|
2020-01-17 09:48:23 +00:00
|
|
|
this.logger.log('[PAPI] Found Sponge PlaceholderAPI Hooking...')
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
|
|
|
} catch (ex) {
|
2020-01-17 09:48:23 +00:00
|
|
|
this.logger.console("§cCan't found me.rojo8399.placeholderapi.PlaceholderService variable will not be replaced! Err: " + ex)
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
2020-04-24 08:42:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@enable({ servers: [constants.ServerType.Bukkit, constants.ServerType.Sponge] })
|
|
|
|
serverEnbale() {
|
2020-02-27 10:14:17 +00:00
|
|
|
this.channelOff = this.Channel?.listen(this, MiaoMessage.CHANNEL, (data) => {
|
|
|
|
this.sendChatAll(MiaoMessage.decode(data).json)
|
|
|
|
})
|
2020-01-17 09:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bungeeenable() {
|
2020-02-27 10:14:17 +00:00
|
|
|
this.channelOff = this.Channel?.listen(this, MiaoMessage.CHANNEL, (data, event: net.md_5.bungee.api.event.PluginMessageEvent) => {
|
|
|
|
let bungee: net.md_5.bungee.api.ProxyServer = base.getInstance().getProxy()
|
|
|
|
if (event.getTag() == MiaoMessage.CHANNEL) {
|
2020-09-22 10:43:02 +00:00
|
|
|
let origin = event.getSender().getAddress()
|
2020-02-27 10:14:17 +00:00
|
|
|
bungee.getServers().forEach(new BiConsumer({
|
|
|
|
accept: (s, server) => {
|
|
|
|
if (server.getAddress() != origin && server.getPlayers().size() > 0) {
|
|
|
|
server.sendData(event.getTag(), event.getData())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
}
|
|
|
|
})
|
2020-01-17 09:48:23 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 05:51:18 +00:00
|
|
|
@cmd({ servers: [constants.ServerType.Bungee] })
|
2020-01-17 09:48:23 +00:00
|
|
|
mct(sender: any, command: string, args: string[]) {
|
2020-09-22 10:43:02 +00:00
|
|
|
this.logger.log(sender, command, args)
|
2020-01-17 09:48:23 +00:00
|
|
|
sender.sendMessage(JSON.stringify({ command, ...args }))
|
|
|
|
}
|
|
|
|
|
2020-09-24 05:51:18 +00:00
|
|
|
@cmd({ servers: [`!${constants.ServerType.Bungee}`] })
|
2020-01-16 09:27:24 +00:00
|
|
|
mchat(sender: any, command: string, args: string[]) {
|
2020-09-22 10:43:02 +00:00
|
|
|
this.logger.log(sender, command, args)
|
2020-01-16 09:27:24 +00:00
|
|
|
sender.sendMessage(JSON.stringify({ command, ...args }))
|
|
|
|
}
|
|
|
|
|
|
|
|
@tab()
|
|
|
|
tabmchat(_sender: any, _command: string, _args: string[]) {
|
|
|
|
}
|
|
|
|
|
2020-09-24 05:51:18 +00:00
|
|
|
@listener({ servers: [constants.ServerType.Bukkit] })
|
2020-01-16 09:27:24 +00:00
|
|
|
AsyncPlayerChatEvent(event: org.bukkit.event.player.AsyncPlayerChatEvent) {
|
2020-09-22 10:43:02 +00:00
|
|
|
this.sendChat(event.getPlayer(), event.getMessage(), () => event.setCancelled(true))
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
|
|
|
|
2020-09-24 05:51:18 +00:00
|
|
|
@listener({ servers: [constants.ServerType.Sponge] })
|
2020-01-16 09:27:24 +00:00
|
|
|
MessageChannelEvent$Chat(event: org.spongepowered.api.event.message.MessageChannelEvent.Chat) {
|
|
|
|
//@ts-ignore
|
2020-09-22 10:43:02 +00:00
|
|
|
var player = event.getCause().first(org.spongepowered.api.entity.living.player.Player.class).orElse(null)
|
2020-01-16 09:27:24 +00:00
|
|
|
if (player == null) {
|
2020-09-22 10:43:02 +00:00
|
|
|
return
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
2020-09-22 10:43:02 +00:00
|
|
|
var plain = event.getRawMessage().toPlain()
|
2020-01-16 09:27:24 +00:00
|
|
|
if (plain.startsWith(Tellraw.duplicateChar)) {
|
2020-09-22 10:43:02 +00:00
|
|
|
return
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
2020-09-22 10:43:02 +00:00
|
|
|
this.sendChat(player, plain, () => event.setMessageCancelled(true))
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
initFormat(chatFormats: any[]) {
|
|
|
|
chatFormats.forEach(chatFormat => {
|
2020-09-22 10:43:02 +00:00
|
|
|
var chat_format_str = chatFormat.format
|
|
|
|
var temp = []
|
|
|
|
var r: any[]
|
2020-01-16 09:27:24 +00:00
|
|
|
while (r = this.FORMAT_PATTERN.exec(chat_format_str)) {
|
2020-09-22 10:43:02 +00:00
|
|
|
temp.push(r[1])
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
2020-09-22 10:43:02 +00:00
|
|
|
var format_list = []
|
2020-01-17 09:48:23 +00:00
|
|
|
temp.forEach(t => {
|
2020-09-22 10:43:02 +00:00
|
|
|
var arr = chat_format_str.split('[' + t + ']', 2)
|
2020-01-16 09:27:24 +00:00
|
|
|
if (arr[0]) {
|
2020-09-22 10:43:02 +00:00
|
|
|
format_list.push(arr[0])
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
2020-09-22 10:43:02 +00:00
|
|
|
format_list.push(t)
|
|
|
|
chat_format_str = arr[1]
|
|
|
|
})
|
2020-01-16 09:27:24 +00:00
|
|
|
if (chat_format_str) {
|
2020-09-22 10:43:02 +00:00
|
|
|
format_list.push(chat_format_str)
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
2020-09-22 10:43:02 +00:00
|
|
|
chatFormat.format_list = format_list
|
2020-01-16 09:27:24 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-09-22 10:43:02 +00:00
|
|
|
sendChat(player: any, plain: string, callback: { (): void }) {
|
|
|
|
var chat_format = this.getChatFormat(player)
|
2020-01-16 09:27:24 +00:00
|
|
|
if (!chat_format) {
|
2020-09-22 10:43:02 +00:00
|
|
|
console.debug('未获得用户', player.getName(), '的 ChatRule 跳过执行...')
|
|
|
|
return
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
2020-09-22 10:43:02 +00:00
|
|
|
callback()
|
|
|
|
var tr = Tellraw.create()
|
2020-01-16 09:27:24 +00:00
|
|
|
chat_format.format_list.forEach((format) => {
|
2020-09-22 10:43:02 +00:00
|
|
|
var style = this.styleFormats[format]
|
2020-01-16 09:27:24 +00:00
|
|
|
if (style) {
|
2020-09-22 10:43:02 +00:00
|
|
|
tr.then(this.replace(player, style.text.replace(/&(\w)/g, '§$1')))
|
2020-01-16 09:27:24 +00:00
|
|
|
if (style.hover) {
|
2020-09-22 10:43:02 +00:00
|
|
|
tr.tip(this.replace(player, style.hover.join('\n')))
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
|
|
|
if (style.click && style.click.type && style.click.command) {
|
2020-09-22 10:43:02 +00:00
|
|
|
var command = this.replace(player, style.click.command)
|
2020-01-16 09:27:24 +00:00
|
|
|
switch (style.click.type.toUpperCase()) {
|
|
|
|
case "COMMAND":
|
2020-09-22 10:43:02 +00:00
|
|
|
tr.command(command)
|
|
|
|
break
|
2020-01-16 09:27:24 +00:00
|
|
|
case "OPENURL":
|
2020-09-22 10:43:02 +00:00
|
|
|
tr.link(command)
|
|
|
|
break
|
2020-01-16 09:27:24 +00:00
|
|
|
case "SUGGEST":
|
2020-09-22 10:43:02 +00:00
|
|
|
tr.suggest(command)
|
|
|
|
break
|
2020-01-16 09:27:24 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2020-09-22 10:43:02 +00:00
|
|
|
tr.then(this.replace(player, format))
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
2020-09-22 10:43:02 +00:00
|
|
|
})
|
2020-01-16 09:27:24 +00:00
|
|
|
let json = tr.then(this.replace(player, plain)).json()
|
2020-01-17 09:48:23 +00:00
|
|
|
this.sendChatAll(json)
|
2020-02-27 10:14:17 +00:00
|
|
|
this.Channel?.send(player, MiaoMessage.CHANNEL, MiaoMessage.encode(json))
|
2020-01-17 09:48:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
sendChatAll(json: string) {
|
2020-09-22 10:43:02 +00:00
|
|
|
this.Server.getOnlinePlayers().forEach(player => this.chat.sendJson(player, json))
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
getChatFormat(player: any) {
|
2020-09-22 10:43:02 +00:00
|
|
|
for (let format of this.chatFormats) {
|
2020-01-16 09:27:24 +00:00
|
|
|
if (player.hasPermission(format.permission)) {
|
2020-09-22 10:43:02 +00:00
|
|
|
return format
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
|
|
|
}
|
2020-09-22 10:43:02 +00:00
|
|
|
return null
|
2020-01-16 09:27:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
replace(player: any, target: string) {
|
|
|
|
return this.PlaceholderAPI.setPlaceholders(player, target)
|
|
|
|
}
|
|
|
|
}
|