feat: 跨服聊天压缩数据 增加可传输内容大小 修复1.7.10 不兼容问题

Signed-off-by: MiaoWoo <admin@yumc.pw>
master
MiaoWoo 2019-05-31 12:05:14 +08:00
parent ab58ee3bc6
commit f22113584d
6 changed files with 71 additions and 45 deletions

View File

@ -3,7 +3,7 @@
<modelVersion>4.0.0</modelVersion>
<artifactId>MiaoChat</artifactId>
<version>1.8.8</version>
<version>1.8.9</version>
<parent>
<groupId>pw.yumc</groupId>
@ -13,8 +13,10 @@
</parent>
<properties>
<update.description>§a正式版本 §bv1.8.7 §a兼容 Spigot 1.13.2</update.description>
<update.description>§a正式版本 §bv1.8.9 §a兼容 Spigot 1.13.2</update.description>
<update.changes>
§619-05-31 §a增强: 跨服聊天压缩数据 增加可传输内容大小;
§619-05-29 §c修复: 1.7.10不兼容的问题;
§619-02-23 §c修复: BungeeCord 兼容性问题;
</update.changes>
<update.changelog>

View File

@ -15,7 +15,6 @@ import org.bukkit.plugin.messaging.PluginMessageListener;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.PlaceholderHook;
import pw.yumc.MiaoChat.config.ChatConfig;
@ -74,8 +73,8 @@ public class MiaoChat extends JavaPlugin implements Executor, PluginMessageListe
Bukkit.getPluginManager().registerEvents(this, this);
Bukkit.getMessenger().registerIncomingPluginChannel(this, MiaoMessage.CHANNEL, this);
Bukkit.getMessenger().registerOutgoingPluginChannel(this, MiaoMessage.CHANNEL);
Bukkit.getMessenger().registerIncomingPluginChannel(this, MiaoMessage.NORMALCHANNEL, this);
Bukkit.getMessenger().registerOutgoingPluginChannel(this, MiaoMessage.NORMALCHANNEL);
Bukkit.getMessenger().registerIncomingPluginChannel(this, MiaoMessage.NORMAL_CHANNEL, this);
Bukkit.getMessenger().registerOutgoingPluginChannel(this, MiaoMessage.NORMAL_CHANNEL);
}
}
@ -88,6 +87,7 @@ public class MiaoChat extends JavaPlugin implements Executor, PluginMessageListe
return getChatConfig().getServername();
case "bserver":
return ServerName;
default:
}
return "未知的参数";
}
@ -134,7 +134,7 @@ public class MiaoChat extends JavaPlugin implements Executor, PluginMessageListe
public void onPluginMessageReceived(String channel, Player player, byte[] message) {
if (MiaoMessage.CHANNEL.equals(channel)) {
send(message);
} else if (MiaoMessage.NORMALCHANNEL.equals(channel)) {
} else if (MiaoMessage.NORMAL_CHANNEL.equals(channel)) {
for (Player p : C.Player.getOnlinePlayers()) {
p.sendMessage(MiaoMessage.decode(message).getJson());
}

View File

@ -1,7 +1,6 @@
package pw.yumc.MiaoChat;
import java.net.InetSocketAddress;
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
@ -18,13 +17,16 @@ import net.md_5.bungee.event.EventHandler;
import pw.yumc.MiaoChat.bungee.FileConfig;
import pw.yumc.MiaoChat.bungee.Log;
/**
* @author MiaoWoo
*/
public class MiaoChatBungee extends Plugin implements Listener {
private Map<InetSocketAddress, Set<ServerInfo>> groups;
private FileConfig config;
@EventHandler
public void handle(final PluginMessageEvent event) {
if (event.getTag().equals(MiaoMessage.CHANNEL) || event.getTag().equals(MiaoMessage.NORMALCHANNEL)) {
if (event.getTag().equals(MiaoMessage.CHANNEL) || event.getTag().equals(MiaoMessage.NORMAL_CHANNEL)) {
InetSocketAddress origin = event.getSender().getAddress();
if (groups.containsKey(origin)) {
groups.get(origin).forEach(server -> {
@ -45,43 +47,40 @@ public class MiaoChatBungee extends Plugin implements Listener {
public void loadGroup() {
groups = new HashMap<>();
Map<String, ServerInfo> temp = getProxy().getServers();
Set<ServerInfo> unused = new HashSet<>();
Set<ServerInfo> unused = new HashSet<>(temp.values());
Configuration groupSel = config.getSection("Groups");
Collection<String> groupname = groupSel.getKeys();
groupname.forEach(gname -> {
Set<String> servers = new HashSet<>(groupSel.getStringList(gname));
Set<ServerInfo> sers = new HashSet<>();
servers.forEach(sname -> sers.add(temp.get(sname)));
sers.remove(null);
servers.forEach(sname -> {
ServerInfo isadd = temp.get(sname);
if (isadd != null) {
unused.remove(isadd);
groups.put(isadd.getAddress(), sers);
groupSel.getKeys().forEach(groupName -> {
Set<String> servers = new HashSet<>(groupSel.getStringList(groupName));
Set<ServerInfo> serverInfos = new HashSet<>();
servers.forEach(s -> {
if (temp.containsKey(s)) {
ServerInfo serverInfo = temp.get(s);
unused.remove(serverInfo);
groups.put(serverInfo.getAddress(), serverInfos);
}
});
});
unused.forEach(unser -> groups.put(unser.getAddress(), unused));
unused.forEach(serverInfo -> groups.put(serverInfo.getAddress(), unused));
}
@Override
public void onEnable() {
loadGroup();
getProxy().registerChannel(MiaoMessage.CHANNEL);
getProxy().registerChannel(MiaoMessage.NORMALCHANNEL);
getProxy().registerChannel(MiaoMessage.NORMAL_CHANNEL);
getProxy().getPluginManager().registerListener(this, this);
getProxy().getPluginManager().registerCommand(this, new Command("MiaoChat", "MiaoChat.admin", "mct") {
@Override
public void execute(CommandSender commandSender, String[] args) {
if (args.length > 0) {
switch (args[0].toLowerCase()) {
case "reload":
config.reload();
loadGroup();
commandSender.sendMessage("§a配置文件已重载!");
return;
case "version":
default:
case "reload":
config.reload();
loadGroup();
commandSender.sendMessage("§a配置文件已重载!");
return;
case "version":
default:
}
}
commandSender.sendMessage("§6插件版本: §av" + getDescription().getVersion());

View File

@ -1,16 +1,24 @@
package pw.yumc.MiaoChat;
import com.google.common.io.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.charset.StandardCharsets;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import lombok.SneakyThrows;
/**
* Created on 16-9-8.
* @author MiaoWoo
*/
public class MiaoMessage {
public static final String CHANNEL = "MiaoChat:MiaoChat".toLowerCase();
public static final String NORMALCHANNEL = "MiaoChat:MiaoChatNM".toLowerCase();
public static final String CHANNEL = "MiaoChat:Default".toLowerCase();
public static final String NORMAL_CHANNEL = "MiaoChat:Normal".toLowerCase();
private static final int MAX_MESSAGE_LENGTH = 32000;
private String json;
private MiaoMessage(String json) {
@ -21,19 +29,33 @@ public class MiaoMessage {
return new MiaoMessage(in).encode();
}
@SneakyThrows
public static MiaoMessage decode(byte[] in) {
final ByteArrayDataInput input = ByteStreams.newDataInput(in);
return new MiaoMessage(input.readUTF());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
copy(new GZIPInputStream(new ByteArrayInputStream(in)), baos);
return new MiaoMessage(baos.toString("UTF-8"));
}
public String getJson() {
return json;
}
@SneakyThrows
public byte[] encode() {
if (json.getBytes().length > 32000) { return null; }
final ByteArrayDataOutput out = ByteStreams.newDataOutput();
out.writeUTF(json);
return out.toByteArray();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
copy(new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)), new GZIPOutputStream(baos));
if (baos.size() > MAX_MESSAGE_LENGTH) { return null; }
return baos.toByteArray();
}
@SneakyThrows
private static void copy(InputStream input, OutputStream output) {
byte[] buffer = new byte[1024];
int n;
while ((n = input.read(buffer)) != -1) {
output.write(buffer, 0, n);
}
input.close();
output.close();
}
}

View File

@ -28,6 +28,9 @@ import pw.yumc.YumCore.statistic.Statistics;
import pw.yumc.YumCore.tellraw.Tellraw;
import pw.yumc.YumCore.update.SubscribeTask;
/**
* @author MiaoWoo
*/
public class ChatListener implements Listener {
public static Set<Player> offList = new HashSet<>();
private static Pattern ITEM_PATTERN = Pattern.compile("%([i1-9]?)");
@ -111,7 +114,7 @@ public class ChatListener implements Listener {
byte[] mm = MiaoMessage.encode(tr.toJsonString());
// 数据流等于NULL代表数据超长
if (mm == null) {
p.sendPluginMessage(P.instance, MiaoMessage.NORMALCHANNEL, MiaoMessage.encode(tr.toOldMessageFormat()));
p.sendPluginMessage(P.instance, MiaoMessage.NORMAL_CHANNEL, MiaoMessage.encode(tr.toOldMessageFormat()));
} else {
p.sendPluginMessage(P.instance, MiaoMessage.CHANNEL, mm);
}

View File

@ -7,9 +7,9 @@ Version: 1.0
Groups:
#普通分组
normal:
- 'sc1'
- 'sc2'
# - 'sc1'
# - 'sc2'
#游戏分组
games:
- 'bw1'
- 'sg1'
# - 'bw1'
# - 'sg1'