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> <modelVersion>4.0.0</modelVersion>
<artifactId>MiaoChat</artifactId> <artifactId>MiaoChat</artifactId>
<version>1.8.8</version> <version>1.8.9</version>
<parent> <parent>
<groupId>pw.yumc</groupId> <groupId>pw.yumc</groupId>
@ -13,8 +13,10 @@
</parent> </parent>
<properties> <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> <update.changes>
§619-05-31 §a增强: 跨服聊天压缩数据 增加可传输内容大小;
§619-05-29 §c修复: 1.7.10不兼容的问题;
§619-02-23 §c修复: BungeeCord 兼容性问题; §619-02-23 §c修复: BungeeCord 兼容性问题;
</update.changes> </update.changes>
<update.changelog> <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.ByteArrayDataInput;
import com.google.common.io.ByteArrayDataOutput; import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams; import com.google.common.io.ByteStreams;
import me.clip.placeholderapi.PlaceholderAPI; import me.clip.placeholderapi.PlaceholderAPI;
import me.clip.placeholderapi.PlaceholderHook; import me.clip.placeholderapi.PlaceholderHook;
import pw.yumc.MiaoChat.config.ChatConfig; import pw.yumc.MiaoChat.config.ChatConfig;
@ -74,8 +73,8 @@ public class MiaoChat extends JavaPlugin implements Executor, PluginMessageListe
Bukkit.getPluginManager().registerEvents(this, this); Bukkit.getPluginManager().registerEvents(this, this);
Bukkit.getMessenger().registerIncomingPluginChannel(this, MiaoMessage.CHANNEL, this); Bukkit.getMessenger().registerIncomingPluginChannel(this, MiaoMessage.CHANNEL, this);
Bukkit.getMessenger().registerOutgoingPluginChannel(this, MiaoMessage.CHANNEL); Bukkit.getMessenger().registerOutgoingPluginChannel(this, MiaoMessage.CHANNEL);
Bukkit.getMessenger().registerIncomingPluginChannel(this, MiaoMessage.NORMALCHANNEL, this); Bukkit.getMessenger().registerIncomingPluginChannel(this, MiaoMessage.NORMAL_CHANNEL, this);
Bukkit.getMessenger().registerOutgoingPluginChannel(this, MiaoMessage.NORMALCHANNEL); Bukkit.getMessenger().registerOutgoingPluginChannel(this, MiaoMessage.NORMAL_CHANNEL);
} }
} }
@ -88,6 +87,7 @@ public class MiaoChat extends JavaPlugin implements Executor, PluginMessageListe
return getChatConfig().getServername(); return getChatConfig().getServername();
case "bserver": case "bserver":
return ServerName; return ServerName;
default:
} }
return "未知的参数"; return "未知的参数";
} }
@ -134,7 +134,7 @@ public class MiaoChat extends JavaPlugin implements Executor, PluginMessageListe
public void onPluginMessageReceived(String channel, Player player, byte[] message) { public void onPluginMessageReceived(String channel, Player player, byte[] message) {
if (MiaoMessage.CHANNEL.equals(channel)) { if (MiaoMessage.CHANNEL.equals(channel)) {
send(message); send(message);
} else if (MiaoMessage.NORMALCHANNEL.equals(channel)) { } else if (MiaoMessage.NORMAL_CHANNEL.equals(channel)) {
for (Player p : C.Player.getOnlinePlayers()) { for (Player p : C.Player.getOnlinePlayers()) {
p.sendMessage(MiaoMessage.decode(message).getJson()); p.sendMessage(MiaoMessage.decode(message).getJson());
} }

View File

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

View File

@ -1,16 +1,24 @@
package pw.yumc.MiaoChat; package pw.yumc.MiaoChat;
import com.google.common.io.ByteArrayDataInput; import java.io.ByteArrayInputStream;
import com.google.common.io.ByteArrayDataOutput; import java.io.ByteArrayOutputStream;
import com.google.common.io.ByteStreams; 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. * Created on 16-9-8.
* @author MiaoWoo
*/ */
public class MiaoMessage { public class MiaoMessage {
public static final String CHANNEL = "MiaoChat:MiaoChat".toLowerCase(); public static final String CHANNEL = "MiaoChat:Default".toLowerCase();
public static final String NORMALCHANNEL = "MiaoChat:MiaoChatNM".toLowerCase(); public static final String NORMAL_CHANNEL = "MiaoChat:Normal".toLowerCase();
private static final int MAX_MESSAGE_LENGTH = 32000;
private String json; private String json;
private MiaoMessage(String json) { private MiaoMessage(String json) {
@ -21,19 +29,33 @@ public class MiaoMessage {
return new MiaoMessage(in).encode(); return new MiaoMessage(in).encode();
} }
@SneakyThrows
public static MiaoMessage decode(byte[] in) { public static MiaoMessage decode(byte[] in) {
final ByteArrayDataInput input = ByteStreams.newDataInput(in); ByteArrayOutputStream baos = new ByteArrayOutputStream();
return new MiaoMessage(input.readUTF()); copy(new GZIPInputStream(new ByteArrayInputStream(in)), baos);
return new MiaoMessage(baos.toString("UTF-8"));
} }
public String getJson() { public String getJson() {
return json; return json;
} }
@SneakyThrows
public byte[] encode() { public byte[] encode() {
if (json.getBytes().length > 32000) { return null; } ByteArrayOutputStream baos = new ByteArrayOutputStream();
final ByteArrayDataOutput out = ByteStreams.newDataOutput(); copy(new ByteArrayInputStream(json.getBytes(StandardCharsets.UTF_8)), new GZIPOutputStream(baos));
out.writeUTF(json); if (baos.size() > MAX_MESSAGE_LENGTH) { return null; }
return out.toByteArray(); 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.tellraw.Tellraw;
import pw.yumc.YumCore.update.SubscribeTask; import pw.yumc.YumCore.update.SubscribeTask;
/**
* @author MiaoWoo
*/
public class ChatListener implements Listener { public class ChatListener implements Listener {
public static Set<Player> offList = new HashSet<>(); public static Set<Player> offList = new HashSet<>();
private static Pattern ITEM_PATTERN = Pattern.compile("%([i1-9]?)"); private static Pattern ITEM_PATTERN = Pattern.compile("%([i1-9]?)");
@ -111,7 +114,7 @@ public class ChatListener implements Listener {
byte[] mm = MiaoMessage.encode(tr.toJsonString()); byte[] mm = MiaoMessage.encode(tr.toJsonString());
// 数据流等于NULL代表数据超长 // 数据流等于NULL代表数据超长
if (mm == 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 { } else {
p.sendPluginMessage(P.instance, MiaoMessage.CHANNEL, mm); p.sendPluginMessage(P.instance, MiaoMessage.CHANNEL, mm);
} }

View File

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