2020-01-16 09:27:24 +00:00
|
|
|
/// <reference types="@ms/types/dist/typings/bukkit" />
|
|
|
|
/// <reference types="@ms/types/dist/typings/sponge" />
|
2020-02-23 16:13:19 +00:00
|
|
|
/// <reference types="@ms/types/dist/typings/bungee" />
|
2020-01-16 09:27:24 +00:00
|
|
|
|
2020-04-24 08:42:36 +00:00
|
|
|
import { server, plugin as pluginApi, channel, constants } from '@ms/api'
|
2020-02-27 10:14:17 +00:00
|
|
|
import { inject, optional } from '@ms/container';
|
2020-04-24 08:42:36 +00:00
|
|
|
import { plugin, interfaces, cmd, listener, tab, config, enable } from '@ms/plugin'
|
2020-01-16 09:27:24 +00:00
|
|
|
import Tellraw from '@ms/common/dist/tellraw'
|
|
|
|
|
2020-01-17 09:48:23 +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) {
|
|
|
|
let buffer = new ByteArray(1024);
|
|
|
|
let n: number;
|
|
|
|
while ((n = input.read(buffer)) != -1) {
|
|
|
|
output.write(buffer, 0, n);
|
|
|
|
}
|
|
|
|
input.close();
|
|
|
|
output.close();
|
|
|
|
}
|
|
|
|
public static encode(input: any): any {
|
|
|
|
return new MiaoMessage(input).encode();
|
|
|
|
}
|
|
|
|
public static decode(input: any): MiaoMessage {
|
|
|
|
let baos = new ByteArrayOutputStream();
|
|
|
|
MiaoMessage.copy(new GZIPInputStream(new ByteArrayInputStream(input)), baos);
|
|
|
|
return new MiaoMessage(baos.toString(StandardCharsets.UTF_8.name()));
|
|
|
|
}
|
|
|
|
|
|
|
|
// private String json;
|
|
|
|
constructor(public json: any) { }
|
|
|
|
|
|
|
|
public encode(): any {
|
|
|
|
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-16 09:27:24 +00:00
|
|
|
@plugin({ name: 'MiaoChat', version: '1.0.0', author: 'MiaoWoo', source: __filename })
|
|
|
|
export class MiaoChat extends interfaces.Plugin {
|
|
|
|
@inject(server.Server)
|
|
|
|
private Server: server.Server
|
2020-02-24 10:36:32 +00:00
|
|
|
@inject(channel.Channel)
|
2020-02-27 10:14:17 +00:00
|
|
|
@optional() private Channel: channel.Channel
|
|
|
|
|
|
|
|
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% 我需要你的帮助!"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private chatFormats: any[];
|
|
|
|
private styleFormats: any;
|
|
|
|
// 用于匹配 '[xx]' 聊天格式
|
|
|
|
private FORMAT_PATTERN = /[\[]([^\[\]]+)[\]]/ig;
|
|
|
|
|
|
|
|
private PlaceholderAPI: { setPlaceholders: (player: any, str: string) => string };
|
|
|
|
|
|
|
|
load() {
|
|
|
|
this.chatFormats = Object.values(this.config.ChatFormats);
|
|
|
|
this.chatFormats.sort(this.compare('index'));
|
|
|
|
this.initFormat(this.chatFormats);
|
|
|
|
this.styleFormats = this.config.StyleFormats;
|
|
|
|
}
|
|
|
|
|
|
|
|
private compare(prop: string) {
|
2020-04-24 08:42:36 +00:00
|
|
|
return function (obj1: { [x: string]: any; }, obj2: { [x: string]: any; }) {
|
2020-01-16 09:27:24 +00:00
|
|
|
var val1 = obj1[prop];
|
|
|
|
var val2 = obj2[prop];
|
|
|
|
if (!isNaN(Number(val1)) && !isNaN(Number(val2))) {
|
|
|
|
val1 = Number(val1);
|
|
|
|
val2 = Number(val2);
|
|
|
|
}
|
|
|
|
if (val1 < val2) {
|
|
|
|
return -1;
|
|
|
|
} else if (val1 > val2) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
enable() {
|
2020-01-17 09:48:23 +00:00
|
|
|
this.PlaceholderAPI = {
|
|
|
|
setPlaceholders: (player: any, string: string) => {
|
|
|
|
return string;
|
|
|
|
}
|
|
|
|
}
|
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
|
|
|
|
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 {
|
|
|
|
var spongePapi = this.Server.getService('me.rojo8399.placeholderapi.PlaceholderService');
|
|
|
|
var s = org.spongepowered.api.text.serializer.TextSerializers.formattingCode('§');
|
|
|
|
if (spongePapi) {
|
|
|
|
this.PlaceholderAPI = {
|
|
|
|
setPlaceholders: (player: any, string: string) => {
|
|
|
|
return s.serialize(spongePapi.replacePlaceholders(string, player, player));
|
|
|
|
}
|
|
|
|
};
|
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) {
|
|
|
|
let origin = event.getSender().getAddress();
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
@cmd({ servers: ["bungee"] })
|
|
|
|
mct(sender: any, command: string, args: string[]) {
|
|
|
|
this.logger.log(sender, command, args);
|
|
|
|
sender.sendMessage(JSON.stringify({ command, ...args }))
|
|
|
|
}
|
|
|
|
|
|
|
|
@cmd({ servers: ["!bungee"] })
|
2020-01-16 09:27:24 +00:00
|
|
|
mchat(sender: any, command: string, args: string[]) {
|
|
|
|
this.logger.log(sender, command, args);
|
|
|
|
sender.sendMessage(JSON.stringify({ command, ...args }))
|
|
|
|
}
|
|
|
|
|
|
|
|
@tab()
|
|
|
|
tabmchat(_sender: any, _command: string, _args: string[]) {
|
|
|
|
}
|
|
|
|
|
2020-01-17 09:48:23 +00:00
|
|
|
@listener({ servers: ['bukkit'] })
|
2020-01-16 09:27:24 +00:00
|
|
|
AsyncPlayerChatEvent(event: org.bukkit.event.player.AsyncPlayerChatEvent) {
|
2020-04-24 08:42:36 +00:00
|
|
|
this.sendChat(event.getPlayer(), event.getMessage(), function () {
|
2020-01-16 09:27:24 +00:00
|
|
|
event.setCancelled(true);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-01-17 09:48:23 +00:00
|
|
|
@listener({ servers: ['sponge'] })
|
2020-01-16 09:27:24 +00:00
|
|
|
MessageChannelEvent$Chat(event: org.spongepowered.api.event.message.MessageChannelEvent.Chat) {
|
|
|
|
//@ts-ignore
|
|
|
|
var player = event.getCause().first(org.spongepowered.api.entity.living.player.Player.class).orElse(null);
|
|
|
|
if (player == null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
var plain = event.getRawMessage().toPlain();
|
|
|
|
if (plain.startsWith(Tellraw.duplicateChar)) {
|
|
|
|
return;
|
|
|
|
}
|
2020-04-24 08:42:36 +00:00
|
|
|
this.sendChat(player, plain, function () {
|
2020-01-16 09:27:24 +00:00
|
|
|
event.setMessageCancelled(true)
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
initFormat(chatFormats: any[]) {
|
|
|
|
chatFormats.forEach(chatFormat => {
|
|
|
|
var chat_format_str = chatFormat.format;
|
|
|
|
var temp = [];
|
|
|
|
var r: any[];
|
|
|
|
while (r = this.FORMAT_PATTERN.exec(chat_format_str)) {
|
|
|
|
temp.push(r[1]);
|
|
|
|
}
|
|
|
|
var format_list = [];
|
2020-01-17 09:48:23 +00:00
|
|
|
temp.forEach(t => {
|
2020-01-16 09:27:24 +00:00
|
|
|
var arr = chat_format_str.split('[' + t + ']', 2);
|
|
|
|
if (arr[0]) {
|
|
|
|
format_list.push(arr[0]);
|
|
|
|
}
|
|
|
|
format_list.push(t);
|
|
|
|
chat_format_str = arr[1];
|
|
|
|
});
|
|
|
|
if (chat_format_str) {
|
|
|
|
format_list.push(chat_format_str);
|
|
|
|
}
|
|
|
|
chatFormat.format_list = format_list;
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
sendChat(player: any, plain: string, callback: { (): void; }) {
|
|
|
|
var chat_format = this.getChatFormat(player);
|
|
|
|
if (!chat_format) {
|
|
|
|
console.debug('未获得用户', player.getName(), '的 ChatRule 跳过执行...');
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
callback();
|
|
|
|
var tr = Tellraw.create();
|
|
|
|
chat_format.format_list.forEach((format) => {
|
|
|
|
var style = this.styleFormats[format];
|
|
|
|
if (style) {
|
2020-02-27 10:14:17 +00:00
|
|
|
tr.then(this.replace(player, style.text.replace(/&(\w)/g, '§$1')));
|
2020-01-16 09:27:24 +00:00
|
|
|
if (style.hover) {
|
|
|
|
tr.tip(this.replace(player, style.hover.join('\n')));
|
|
|
|
}
|
|
|
|
if (style.click && style.click.type && style.click.command) {
|
|
|
|
var command = this.replace(player, style.click.command);
|
|
|
|
switch (style.click.type.toUpperCase()) {
|
|
|
|
case "COMMAND":
|
|
|
|
tr.command(command);
|
|
|
|
break;
|
|
|
|
case "OPENURL":
|
|
|
|
tr.link(command);
|
|
|
|
break;
|
|
|
|
case "SUGGEST":
|
|
|
|
tr.suggest(command);
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
tr.then(this.replace(player, format));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
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-01-16 09:27:24 +00:00
|
|
|
this.Server.getOnlinePlayers().forEach(player => this.Server.sendJson(player, json))
|
|
|
|
}
|
|
|
|
|
|
|
|
getChatFormat(player: any) {
|
|
|
|
for (var i in this.chatFormats) {
|
|
|
|
var format = this.chatFormats[i];
|
|
|
|
if (player.hasPermission(format.permission)) {
|
|
|
|
return format;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
replace(player: any, target: string) {
|
|
|
|
return this.PlaceholderAPI.setPlaceholders(player, target)
|
|
|
|
}
|
|
|
|
}
|