Fix some book translation failures (#661)

Book pages can be sent as plain text rather than JSON. The text library doesn't use lenient parsing, so this fails, and the book isn't visible in the inventory.

This will convert the text into JSON if it's not already, before feeding it to the text library.
This commit is contained in:
Arktisfox 2020-05-25 21:19:37 -04:00 committed by GitHub
parent d0545c57c4
commit 0178492b59
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 1 deletions

View File

@ -55,7 +55,7 @@ public class BookPagesTranslator extends NbtItemStackTranslator {
CompoundTag pageTag = new CompoundTag("");
pageTag.put(new StringTag("photoname", ""));
pageTag.put(new StringTag("text", MessageUtils.getBedrockMessage(textTag.getValue())));
pageTag.put(new StringTag("text", MessageUtils.getBedrockMessageLenient(textTag.getValue())));
pages.add(pageTag);
}

View File

@ -137,6 +137,23 @@ public class MessageUtils {
}
}
/**
* Verifies the message is valid JSON in case it's plaintext. Works around GsonComponentSeraializer not using lenient mode.
* See https://wiki.vg/Chat for messages sent in lenient mode, and for a description on leniency.
*
* @param message Potentially lenient JSON message
* @return Bedrock formatted message
*/
public static String getBedrockMessageLenient(String message) {
if (isMessage(message)) {
return getBedrockMessage(message);
} else {
final JsonObject obj = new JsonObject();
obj.addProperty("text", message);
return getBedrockMessage(obj.toString());
}
}
public static String getBedrockMessage(String message) {
Component component = phraseJavaMessage(message);
return LegacyComponentSerializer.legacy().serialize(component);