SPIGOT-7804: Fix written book serialization

* Account for null/missing values when deserializing the 'resolved' and
  'generation' fields.
* Serialize the book pages as JSON strings.
* Avoid redundant conversion from strings to components to JSON and back to
  components during deserialization.
  Add CraftChatMessage.fromJSONOrString that accepts a maxLength argument
  and remove the no longer used fromJSONOrStringToJSON, fromStringToJSON,
  and fromJSONComponent helper methods.

By: blablubbabc <lukas@wirsindwir.de>
This commit is contained in:
CraftBukkit/Spigot
2024-06-30 16:27:23 +10:00
parent 8821aa372c
commit bc75f0bcb5
3 changed files with 19 additions and 58 deletions

View File

@@ -231,51 +231,26 @@ public final class CraftChatMessage {
}
public static IChatBaseComponent fromJSONOrString(String message, boolean nullable, boolean keepNewlines) {
return fromJSONOrString(message, nullable, keepNewlines, Integer.MAX_VALUE, false);
}
public static IChatBaseComponent fromJSONOrString(String message, boolean nullable, boolean keepNewlines, int maxLength, boolean checkJsonContentLength) {
if (message == null) message = "";
if (nullable && message.isEmpty()) return null;
IChatBaseComponent component = fromJSONOrNull(message);
if (component != null) {
return component;
} else {
return fromString(message, keepNewlines)[0];
}
}
public static String fromJSONOrStringToJSON(String message) {
return fromJSONOrStringToJSON(message, false);
}
public static String fromJSONOrStringToJSON(String message, boolean keepNewlines) {
return fromJSONOrStringToJSON(message, false, keepNewlines, Integer.MAX_VALUE, false);
}
public static String fromJSONOrStringOrNullToJSON(String message) {
return fromJSONOrStringOrNullToJSON(message, false);
}
public static String fromJSONOrStringOrNullToJSON(String message, boolean keepNewlines) {
return fromJSONOrStringToJSON(message, true, keepNewlines, Integer.MAX_VALUE, false);
}
public static String fromJSONOrStringToJSON(String message, boolean nullable, boolean keepNewlines, int maxLength, boolean checkJsonContentLength) {
if (message == null) message = "";
if (nullable && message.isEmpty()) return null;
// If the input can be parsed as JSON, we use that:
IChatBaseComponent component = fromJSONOrNull(message);
if (component != null) {
if (checkJsonContentLength) {
String content = fromComponent(component);
String trimmedContent = trimMessage(content, maxLength);
if (content != trimmedContent) { // identity comparison is fine here
if (content != trimmedContent) { // Identity comparison is fine here
// Note: The resulting text has all non-plain text features stripped.
return fromStringToJSON(trimmedContent, keepNewlines);
return fromString(trimmedContent, keepNewlines)[0];
}
}
return message;
return component;
} else {
// Else we interpret the input as legacy text:
message = trimMessage(message, maxLength);
return fromStringToJSON(message, keepNewlines);
return fromString(message, keepNewlines)[0];
}
}
@@ -287,25 +262,6 @@ public final class CraftChatMessage {
}
}
public static String fromStringToJSON(String message) {
return fromStringToJSON(message, false);
}
public static String fromStringToJSON(String message, boolean keepNewlines) {
IChatBaseComponent component = CraftChatMessage.fromString(message, keepNewlines)[0];
return CraftChatMessage.toJSON(component);
}
public static String fromStringOrNullToJSON(String message) {
IChatBaseComponent component = CraftChatMessage.fromStringOrNull(message);
return CraftChatMessage.toJSONOrNull(component);
}
public static String fromJSONComponent(String jsonMessage) {
IChatBaseComponent component = CraftChatMessage.fromJSONOrNull(jsonMessage);
return CraftChatMessage.fromComponent(component);
}
public static String fromComponent(IChatBaseComponent component) {
if (component == null) return "";
StringBuilder out = new StringBuilder();