Fix {motd} on 1.16+

This commit is contained in:
filoghost 2021-06-19 14:00:53 +02:00
parent f5d37f59df
commit 664c6c6151
2 changed files with 59 additions and 14 deletions

View File

@ -15,6 +15,7 @@
package com.gmail.filoghost.holographicdisplays.bridge.bungeecord; package com.gmail.filoghost.holographicdisplays.bridge.bungeecord;
import com.gmail.filoghost.holographicdisplays.disk.Configuration; import com.gmail.filoghost.holographicdisplays.disk.Configuration;
import org.bukkit.ChatColor;
public class BungeeServerInfo { public class BungeeServerInfo {
@ -79,14 +80,58 @@ public class BungeeServerInfo {
if (separatorIndex >= 0) { if (separatorIndex >= 0) {
String line1 = motd.substring(0, separatorIndex); String line1 = motd.substring(0, separatorIndex);
String line2 = motd.substring(separatorIndex + 1); String line2 = motd.substring(separatorIndex + 1);
this.motd1 = Configuration.pingerTrimMotd ? line1.trim() : line1; this.motd1 = Configuration.pingerTrimMotd ? trimWithColors(line1) : line1;
this.motd2 = Configuration.pingerTrimMotd ? line2.trim() : line2; this.motd2 = Configuration.pingerTrimMotd ? trimWithColors(line2) : line2;
} else { } else {
this.motd1 = Configuration.pingerTrimMotd ? motd.trim() : motd; this.motd1 = Configuration.pingerTrimMotd ? trimWithColors(motd) : motd;
this.motd2 = ""; 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() { public long getLastRequest() {
return lastRequest; return lastRequest;
} }

View File

@ -14,14 +14,13 @@
*/ */
package com.gmail.filoghost.holographicdisplays.bridge.bungeecord.serverpinger; package com.gmail.filoghost.holographicdisplays.bridge.bungeecord.serverpinger;
import java.lang.String; import com.gmail.filoghost.holographicdisplays.disk.ServerAddress;
import java.util.logging.Level; import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger;
import net.md_5.bungee.chat.ComponentSerializer;
import org.json.simple.JSONObject; import org.json.simple.JSONObject;
import org.json.simple.JSONValue; import org.json.simple.JSONValue;
import com.gmail.filoghost.holographicdisplays.disk.ServerAddress; import java.util.logging.Level;
import com.gmail.filoghost.holographicdisplays.util.ConsoleLogger;
public class PingResponse public class PingResponse
{ {
@ -60,12 +59,13 @@ public class PingResponse
if (descriptionObject != null) { if (descriptionObject != null) {
if (descriptionObject instanceof JSONObject) { if (descriptionObject instanceof JSONObject) {
Object text = ((JSONObject) descriptionObject).get("text"); String descriptionString = ((JSONObject) descriptionObject).toJSONString();
if (text != null) { try {
motd = text.toString(); motd = ComponentSerializer.parse(descriptionString)[0].toLegacyText();
} else { } catch (Exception e) {
motd = "Invalid ping response (text not found)"; ConsoleLogger.log(Level.WARNING, "Could not parse ping response: " + descriptionString, e);
} motd = "Invalid ping response";
}
} else { } else {
motd = descriptionObject.toString(); motd = descriptionObject.toString();
} }