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;
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;
}

View File

@ -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();
}