diff --git a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java index 1ea48781..a1d3b127 100644 --- a/proxy/src/main/java/net/md_5/bungee/BungeeCord.java +++ b/proxy/src/main/java/net/md_5/bungee/BungeeCord.java @@ -488,7 +488,9 @@ public class BungeeCord extends ProxyServer { getConsole().sendMessage( message ); // TODO: Here too - broadcast( new Chat( ChatConverter.toJSONChat( message ) ) ); + for (String msg : ChatConverter.toJSONChat( message )) { + broadcast( new Chat( msg ) ); + } } public void addConnection(UserConnection con) diff --git a/proxy/src/main/java/net/md_5/bungee/ChatConverter.java b/proxy/src/main/java/net/md_5/bungee/ChatConverter.java index 108dfefb..83369b51 100644 --- a/proxy/src/main/java/net/md_5/bungee/ChatConverter.java +++ b/proxy/src/main/java/net/md_5/bungee/ChatConverter.java @@ -14,9 +14,10 @@ public class ChatConverter { private static final char COLOR_CHAR = '\u00A7'; private static final Pattern url = Pattern.compile("^(?:(https?)://)?([-\\w_\\.]{2,}\\.[a-z]{2,4})(/\\S*)?$"); - public static String toJSONChat(String txt) { + public static String[] toJSONChat(String txt) { Message msg = new Message(); - ArrayList parts = new ArrayList(); + ArrayList parts = new ArrayList(); + StringBuilder outBuffer = new StringBuilder("["); StringBuilder buf = new StringBuilder(); Matcher matcher = url.matcher(txt); for (int i = 0; i < txt.length(); i++) { @@ -27,7 +28,7 @@ public class ChatConverter { if (matcher.region(i, pos).find()) { //Web link handling msg.text = buf.toString(); buf = new StringBuilder(); - parts.add(msg); + outBuffer = append(parts, outBuffer, msg); Message old = msg; msg = new Message(old); msg.clickEvent = new ClickEvent(); @@ -39,7 +40,7 @@ public class ChatConverter { msg.text = urlString; msg.clickEvent.value = "http://" + urlString; } - parts.add(msg); + outBuffer = append(parts, outBuffer, msg); i += pos - i - 1; msg = new Message(old); continue; @@ -54,7 +55,7 @@ public class ChatConverter { } msg.text = buf.toString(); buf = new StringBuilder(); - parts.add(msg); + outBuffer = append(parts, outBuffer, msg); msg = new Message(msg); switch(c) { case 'k': @@ -87,8 +88,26 @@ public class ChatConverter { } } msg.text = buf.toString(); - parts.add(msg); - return gson.toJson(parts); + append(parts, outBuffer, msg); + + parts.add(outBuffer.append("]").toString()); + String[] pArray = new String[parts.size()]; + parts.toArray(pArray); + return pArray; + } + + private static StringBuilder append(ArrayList parts, StringBuilder outBuffer, Message part) { + String p = gson.toJson(part); + if (p.length() + outBuffer.length() + 1 >= Short.MAX_VALUE - 20) { + outBuffer.append("]"); + parts.add(outBuffer.toString()); + outBuffer = new StringBuilder("["); + } + if (outBuffer.length() != 1) { + outBuffer.append(","); + } + outBuffer.append(p); + return outBuffer; } } diff --git a/proxy/src/main/java/net/md_5/bungee/UserConnection.java b/proxy/src/main/java/net/md_5/bungee/UserConnection.java index ae16163b..1e4919b1 100644 --- a/proxy/src/main/java/net/md_5/bungee/UserConnection.java +++ b/proxy/src/main/java/net/md_5/bungee/UserConnection.java @@ -256,7 +256,7 @@ public final class UserConnection implements ProxiedPlayer @Override public synchronized void disconnect(String reason) { - disconnect0( ChatConverter.toJSONChat( reason ) ); + disconnect0( ChatConverter.toJSONChat( reason )[0] ); } public synchronized void disconnect0(String reason) @@ -283,7 +283,9 @@ public final class UserConnection implements ProxiedPlayer @Override public void sendMessage(String message) { - unsafe().sendPacket( new Chat( ChatConverter.toJSONChat( message ) ) ); + for(String msg : ChatConverter.toJSONChat( message )) { + unsafe().sendPacket( new Chat( msg ) ); + } } @Override diff --git a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java index 8ddcd628..05ab8e98 100644 --- a/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java +++ b/proxy/src/main/java/net/md_5/bungee/connection/InitialHandler.java @@ -397,7 +397,7 @@ public class InitialHandler extends PacketHandler implements PendingConnection { if ( !ch.isClosed() ) { - unsafe().sendPacket( new Kick( ChatConverter.toJSONChat( reason ) ) ); + unsafe().sendPacket( new Kick( ChatConverter.toJSONChat( reason )[0] ) ); ch.close(); } }