diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/BungeeServerInfo.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/BungeeServerInfo.java index 7d62109c..73ef75e9 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/BungeeServerInfo.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/BungeeServerInfo.java @@ -15,6 +15,7 @@ package com.gmail.filoghost.holographicdisplays.bridge.bungeecord; import com.gmail.filoghost.holographicdisplays.disk.Configuration; +import org.bukkit.ChatColor; public class BungeeServerInfo { @@ -79,14 +80,58 @@ public class BungeeServerInfo { if (separatorIndex >= 0) { String line1 = motd.substring(0, separatorIndex); String line2 = motd.substring(separatorIndex + 1); - this.motd1 = Configuration.pingerTrimMotd ? line1.trim() : line1; - this.motd2 = Configuration.pingerTrimMotd ? line2.trim() : line2; + this.motd1 = Configuration.pingerTrimMotd ? trimWithColors(line1) : line1; + this.motd2 = Configuration.pingerTrimMotd ? trimWithColors(line2) : line2; } else { - this.motd1 = Configuration.pingerTrimMotd ? motd.trim() : motd; + this.motd1 = Configuration.pingerTrimMotd ? trimWithColors(motd) : motd; this.motd2 = ""; } } + private static String trimWithColors(String s) { + if (s == null || s.isEmpty()) { + return s; + } + + int firstNonWhitespace = -1; + int lastNonWhitespace = -1; + + int length = s.length(); + boolean trimWhitespace = true; + for (int i = 0; i < length; i++) { + char c = s.charAt(i); + + if (c == ' ' && trimWhitespace) { + // Ignore space + } else if (c == ChatColor.COLOR_CHAR && i < length - 1) { + ChatColor chatColor = ChatColor.getByChar(s.charAt(i + 1)); + if (chatColor == null) { + continue; + } + if (chatColor == ChatColor.STRIKETHROUGH || chatColor == ChatColor.UNDERLINE) { + trimWhitespace = false; + } else if (chatColor == ChatColor.RESET) { + trimWhitespace = true; + } + i++; + } else { + if (firstNonWhitespace == -1) { + // Set only once + firstNonWhitespace = i; + } + lastNonWhitespace = i; + } + } + + if (firstNonWhitespace >= 0) { + return s.substring(0, firstNonWhitespace).replace(" ", "") + + s.substring(firstNonWhitespace, lastNonWhitespace + 1) + + s.substring(lastNonWhitespace + 1).replace(" ", ""); + } else { + return s; + } + } + public long getLastRequest() { return lastRequest; } diff --git a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/serverpinger/PingResponse.java b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/serverpinger/PingResponse.java index 53b0c2b9..924384b7 100644 --- a/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/serverpinger/PingResponse.java +++ b/Plugin/src/main/java/com/gmail/filoghost/holographicdisplays/bridge/bungeecord/serverpinger/PingResponse.java @@ -14,14 +14,13 @@ */ package com.gmail.filoghost.holographicdisplays.bridge.bungeecord.serverpinger; -import java.lang.String; -import java.util.logging.Level; - +import com.gmail.filoghost.holographicdisplays.disk.ServerAddress; +import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import net.md_5.bungee.chat.ComponentSerializer; import org.json.simple.JSONObject; import org.json.simple.JSONValue; -import com.gmail.filoghost.holographicdisplays.disk.ServerAddress; -import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger; +import java.util.logging.Level; public class PingResponse { @@ -60,12 +59,13 @@ public class PingResponse if (descriptionObject != null) { if (descriptionObject instanceof JSONObject) { - Object text = ((JSONObject) descriptionObject).get("text"); - if (text != null) { - motd = text.toString(); - } else { - motd = "Invalid ping response (text not found)"; - } + String descriptionString = ((JSONObject) descriptionObject).toJSONString(); + try { + motd = ComponentSerializer.parse(descriptionString)[0].toLegacyText(); + } catch (Exception e) { + ConsoleLogger.log(Level.WARNING, "Could not parse ping response: " + descriptionString, e); + motd = "Invalid ping response"; + } } else { motd = descriptionObject.toString(); }