From a157943187996f8f05e35f163c4afb9973d3f372 Mon Sep 17 00:00:00 2001 From: Minecon724 Date: Wed, 1 Jan 2025 12:10:42 +0100 Subject: [PATCH] JSON in motd --- .../java/eu/m724/tweaks/motd/MotdManager.java | 22 ++++++++++++++++--- src/main/resources/motd sets/example.txt | 2 ++ 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/src/main/java/eu/m724/tweaks/motd/MotdManager.java b/src/main/java/eu/m724/tweaks/motd/MotdManager.java index 1748ae8..0ac9c79 100644 --- a/src/main/java/eu/m724/tweaks/motd/MotdManager.java +++ b/src/main/java/eu/m724/tweaks/motd/MotdManager.java @@ -1,5 +1,5 @@ /* - * Copyright (C) 2024 Minecon724 + * Copyright (C) 2025 Minecon724 * Tweaks724 is licensed under the GNU General Public License. See the LICENSE.md file * in the project root for the full license text. */ @@ -11,6 +11,9 @@ import com.comphenix.protocol.ProtocolLibrary; import com.comphenix.protocol.events.*; import com.comphenix.protocol.reflect.StructureModifier; import com.google.gson.JsonElement; +import com.google.gson.JsonParseException; +import com.google.gson.JsonParser; +import eu.m724.tweaks.DebugLogger; import eu.m724.tweaks.TweaksConfig; import eu.m724.tweaks.TweaksPlugin; import net.md_5.bungee.api.chat.TextComponent; @@ -64,8 +67,21 @@ public class MotdManager { // MOTDs are split with an empty line motds = Arrays.stream(fileContent.split("\n\n")) - .map(s -> { - JsonElement json = ComponentSerializer.toJson(TextComponent.fromLegacy(s.strip())); + .map(entry -> { + entry = entry.strip(); + JsonElement json = null; + + try { + json = JsonParser.parseString(entry); + DebugLogger.fine("JSON line: " + entry); + } catch (JsonParseException e) { + DebugLogger.fine("Not a JSON line: " + entry); + } + + if (json == null) { + json = ComponentSerializer.toJson(TextComponent.fromLegacy(entry)); + } + return Component.Serializer.fromJson(json, RegistryAccess.EMPTY); }) .toArray(Component[]::new); diff --git a/src/main/resources/motd sets/example.txt b/src/main/resources/motd sets/example.txt index 0970723..d4486bf 100644 --- a/src/main/resources/motd sets/example.txt +++ b/src/main/resources/motd sets/example.txt @@ -7,3 +7,5 @@ Random MOTD for every ping Or just one line. If it doesn't fit it will be passed onto the second line. Put your own files in this directory + +["",{"text":"Also supports","color":"#A18D94"},{"text":" JSON","color":"#FF00FF"},{"text":"\n"},{"text":"Java","color":"dark_aqua"},{"text":"Script ","color":"aqua"},{"text":"Object","color":"gray"},{"text":" Notation","color":"red"}] \ No newline at end of file