SPIGOT-6304: Removed the detection of legacy text based on color codes
Apparently there are items and plugins out there that still use legacy color codes within text components, and which thereby break this heuristic. Our remaining approach to differentiate between legacy (plain) and modern (JSON-based) text is to check if a particular text can be parsed as JSON-based text. This approach is not perfect either as there are ambiguous cases that it cannot resolve correctly. However, these cases are hopefully rare enough in practice that this approach remains suitable.
This commit is contained in:
parent
24c79a1446
commit
ff2b9440f5
@ -24,7 +24,6 @@ public final class CraftChatMessage {
|
|||||||
|
|
||||||
private static final Pattern LINK_PATTERN = Pattern.compile("((?:(?:https?):\\/\\/)?(?:[-\\w_\\.]{2,}\\.[a-z]{2,4}.*?(?=[\\.\\?!,;:]?(?:[" + String.valueOf(org.bukkit.ChatColor.COLOR_CHAR) + " \\n]|$))))");
|
private static final Pattern LINK_PATTERN = Pattern.compile("((?:(?:https?):\\/\\/)?(?:[-\\w_\\.]{2,}\\.[a-z]{2,4}.*?(?=[\\.\\?!,;:]?(?:[" + String.valueOf(org.bukkit.ChatColor.COLOR_CHAR) + " \\n]|$))))");
|
||||||
private static final Map<Character, EnumChatFormat> formatMap;
|
private static final Map<Character, EnumChatFormat> formatMap;
|
||||||
private static final String COLOR_CHAR_STRING = String.valueOf(ChatColor.COLOR_CHAR);
|
|
||||||
|
|
||||||
static {
|
static {
|
||||||
Builder<Character, EnumChatFormat> builder = ImmutableMap.builder();
|
Builder<Character, EnumChatFormat> builder = ImmutableMap.builder();
|
||||||
@ -216,9 +215,6 @@ public final class CraftChatMessage {
|
|||||||
private static IChatBaseComponent fromJSONOrString(String message, boolean nullable, boolean keepNewlines) {
|
private static IChatBaseComponent fromJSONOrString(String message, boolean nullable, boolean keepNewlines) {
|
||||||
if (message == null) message = "";
|
if (message == null) message = "";
|
||||||
if (nullable && message.isEmpty()) return null;
|
if (nullable && message.isEmpty()) return null;
|
||||||
if (isLegacy(message)) {
|
|
||||||
return fromString(message, keepNewlines)[0];
|
|
||||||
} else {
|
|
||||||
IChatBaseComponent component = fromJSONOrNull(message);
|
IChatBaseComponent component = fromJSONOrNull(message);
|
||||||
if (component != null) {
|
if (component != null) {
|
||||||
return component;
|
return component;
|
||||||
@ -226,7 +222,6 @@ public final class CraftChatMessage {
|
|||||||
return fromString(message, keepNewlines)[0];
|
return fromString(message, keepNewlines)[0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static String fromJSONOrStringToJSON(String message) {
|
public static String fromJSONOrStringToJSON(String message) {
|
||||||
return fromJSONOrStringToJSON(message, false);
|
return fromJSONOrStringToJSON(message, false);
|
||||||
@ -247,10 +242,6 @@ public final class CraftChatMessage {
|
|||||||
public static String fromJSONOrStringToJSON(String message, boolean nullable, boolean keepNewlines, int maxLength, boolean checkJsonContentLength) {
|
public static String fromJSONOrStringToJSON(String message, boolean nullable, boolean keepNewlines, int maxLength, boolean checkJsonContentLength) {
|
||||||
if (message == null) message = "";
|
if (message == null) message = "";
|
||||||
if (nullable && message.isEmpty()) return null;
|
if (nullable && message.isEmpty()) return null;
|
||||||
if (isLegacy(message)) {
|
|
||||||
message = trimMessage(message, maxLength);
|
|
||||||
return fromStringToJSON(message, keepNewlines);
|
|
||||||
} else {
|
|
||||||
// If the input can be parsed as JSON, we use that:
|
// If the input can be parsed as JSON, we use that:
|
||||||
IChatBaseComponent component = fromJSONOrNull(message);
|
IChatBaseComponent component = fromJSONOrNull(message);
|
||||||
if (component != null) {
|
if (component != null) {
|
||||||
@ -269,7 +260,6 @@ public final class CraftChatMessage {
|
|||||||
return fromStringToJSON(message, keepNewlines);
|
return fromStringToJSON(message, keepNewlines);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
public static String trimMessage(String message, int maxLength) {
|
public static String trimMessage(String message, int maxLength) {
|
||||||
if (message != null && message.length() > maxLength) {
|
if (message != null && message.length() > maxLength) {
|
||||||
@ -279,14 +269,6 @@ public final class CraftChatMessage {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Heuristic detection of legacy (plain) text.
|
|
||||||
// May produce false-negatives: I.e. a return value of false does not imply that the text represents modern (JSON-based) text.
|
|
||||||
// We also consider empty Strings as legacy. The component deserializer cannot parse them (produces null).
|
|
||||||
private static boolean isLegacy(String message) {
|
|
||||||
// assert: message != null
|
|
||||||
return message.trim().isEmpty() || message.contains(COLOR_CHAR_STRING);
|
|
||||||
}
|
|
||||||
|
|
||||||
public static String fromStringToJSON(String message) {
|
public static String fromStringToJSON(String message) {
|
||||||
return fromStringToJSON(message, false);
|
return fromStringToJSON(message, false);
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user