From 34e656765329a8d83e25d85228010ea8012ffdb8 Mon Sep 17 00:00:00 2001 From: Skylandia Date: Sat, 1 Feb 2025 10:23:52 +1300 Subject: [PATCH 1/6] Add formatting to console output --- .../Protocol/Message/ChatParser.cs | 34 ++++++++++++++----- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/MinecraftClient/Protocol/Message/ChatParser.cs b/MinecraftClient/Protocol/Message/ChatParser.cs index c4ef983740..bd4595bfeb 100644 --- a/MinecraftClient/Protocol/Message/ChatParser.cs +++ b/MinecraftClient/Protocol/Message/ChatParser.cs @@ -406,14 +406,22 @@ private static string TranslateString(string rulename, List using_data) else return "[" + rulename + "] " + string.Join(" ", using_data); } + private readonly static Dictionary FormattingCodes = new() { + {"obfuscated", "k"}, + {"bold", "l"}, + {"strikethrough", "m"}, + {"underline", "n"}, + {"italic", "o"}, + }; + /// /// Use a JSON Object to build the corresponding string /// /// JSON object to convert - /// Allow parent color code to affect child elements (set to "" for function init) + /// Allow parent formatting codes to affect child elements (set to "" for function init) /// Container for links from JSON serialized text /// returns the Minecraft-formatted string - private static string JSONData2String(Json.JSONData data, string colorcode, List? links) + private static string JSONData2String(Json.JSONData data, string formatting, List? links) { string extra_result = ""; switch (data.Type) @@ -421,7 +429,15 @@ private static string JSONData2String(Json.JSONData data, string colorcode, List case Json.JSONData.DataType.Object: if (data.Properties.ContainsKey("color")) { - colorcode = Color2tag(JSONData2String(data.Properties["color"], "", links)); + formatting += Color2tag(JSONData2String(data.Properties["color"], "", links)); + } + + foreach (var pair in FormattingCodes) + { + if (data.Properties.ContainsKey(pair.Key) && data.Properties[pair.Key].StringValue == "true") + { + formatting += "§" + pair.Value; + } } if (data.Properties.ContainsKey("clickEvent") && links != null) @@ -440,12 +456,12 @@ private static string JSONData2String(Json.JSONData data, string colorcode, List { Json.JSONData[] extras = data.Properties["extra"].DataArray.ToArray(); foreach (Json.JSONData item in extras) - extra_result = extra_result + JSONData2String(item, colorcode, links) + "§r"; + extra_result = extra_result + JSONData2String(item, formatting, links) + "§r"; } if (data.Properties.ContainsKey("text")) { - return colorcode + JSONData2String(data.Properties["text"], colorcode, links) + extra_result; + return formatting + JSONData2String(data.Properties["text"], formatting, links) + extra_result; } else if (data.Properties.ContainsKey("translate")) { @@ -457,11 +473,11 @@ private static string JSONData2String(Json.JSONData data, string colorcode, List Json.JSONData[] array = data.Properties["with"].DataArray.ToArray(); for (int i = 0; i < array.Length; i++) { - using_data.Add(JSONData2String(array[i], colorcode, links)); + using_data.Add(JSONData2String(array[i], formatting, links)); } } - return colorcode + + return formatting + TranslateString(JSONData2String(data.Properties["translate"], "", links), using_data) + extra_result; } @@ -471,13 +487,13 @@ private static string JSONData2String(Json.JSONData data, string colorcode, List string result = ""; foreach (Json.JSONData item in data.DataArray) { - result += JSONData2String(item, colorcode, links); + result += JSONData2String(item, formatting, links); } return result; case Json.JSONData.DataType.String: - return colorcode + data.StringValue; + return formatting + data.StringValue; } return ""; From c79b1cfd0ffa37576faaf683a42fb96bdb3b522c Mon Sep 17 00:00:00 2001 From: Skylandia Date: Sat, 1 Feb 2025 10:24:20 +1300 Subject: [PATCH 2/6] Simple formatting support for discord --- MinecraftClient/ChatBots/DiscordBridge.cs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/MinecraftClient/ChatBots/DiscordBridge.cs b/MinecraftClient/ChatBots/DiscordBridge.cs index b46ad770ab..b16ab540fe 100644 --- a/MinecraftClient/ChatBots/DiscordBridge.cs +++ b/MinecraftClient/ChatBots/DiscordBridge.cs @@ -2,6 +2,7 @@ using System.Collections.Generic; using System.IO; using System.Linq; +using System.Text.RegularExpressions; using System.Threading.Tasks; using Brigadier.NET.Builder; using DSharpPlus; @@ -177,12 +178,21 @@ private void Disconnect() return instance; } + private string GetDiscordText(string text) { + text = Regex.Replace(text, "§l(.*?§r)", "**$1**"); + text = Regex.Replace(text, "§m(.*?§r)", "~~$1~~"); + text = Regex.Replace(text, "§n(.*?§r)", "__$1__"); + text = Regex.Replace(text, "§o(.*?§r)", "*$1*"); + text = Regex.Replace(text, "§.", ""); + return text; + } + public override void GetText(string text) { if (!CanSendMessages()) return; - text = GetVerbatim(text).Trim(); + text = GetDiscordText(text).Trim(); // Stop the crash when an empty text is recived somehow if (string.IsNullOrEmpty(text)) From c1e5141cf654d4f6362789920f1cc634834d6fe4 Mon Sep 17 00:00:00 2001 From: Skylandia Date: Sat, 8 Mar 2025 22:22:38 +1300 Subject: [PATCH 3/6] fixes --- .../Protocol/Message/ChatParser.cs | 85 +++++++++++++------ 1 file changed, 60 insertions(+), 25 deletions(-) diff --git a/MinecraftClient/Protocol/Message/ChatParser.cs b/MinecraftClient/Protocol/Message/ChatParser.cs index bd4595bfeb..251671f701 100644 --- a/MinecraftClient/Protocol/Message/ChatParser.cs +++ b/MinecraftClient/Protocol/Message/ChatParser.cs @@ -423,6 +423,7 @@ private static string TranslateString(string rulename, List using_data) /// returns the Minecraft-formatted string private static string JSONData2String(Json.JSONData data, string formatting, List? links) { + string message = ""; string extra_result = ""; switch (data.Type) { @@ -434,9 +435,16 @@ private static string JSONData2String(Json.JSONData data, string formatting, Lis foreach (var pair in FormattingCodes) { - if (data.Properties.ContainsKey(pair.Key) && data.Properties[pair.Key].StringValue == "true") + if (data.Properties.ContainsKey(pair.Key)) { - formatting += "§" + pair.Value; + if (data.Properties[pair.Key].StringValue == "true") + { + formatting += "§" + pair.Value; + } + else if (data.Properties[pair.Key].StringValue == "false") + { + formatting = formatting.Replace("§" + pair.Value, ""); + } } } @@ -456,12 +464,12 @@ private static string JSONData2String(Json.JSONData data, string formatting, Lis { Json.JSONData[] extras = data.Properties["extra"].DataArray.ToArray(); foreach (Json.JSONData item in extras) - extra_result = extra_result + JSONData2String(item, formatting, links) + "§r"; + extra_result += JSONData2String(item, "§r" + formatting, links); } if (data.Properties.ContainsKey("text")) { - return formatting + JSONData2String(data.Properties["text"], formatting, links) + extra_result; + message = JSONData2String(data.Properties["text"], formatting, links); } else if (data.Properties.ContainsKey("translate")) { @@ -477,11 +485,9 @@ private static string JSONData2String(Json.JSONData data, string formatting, Lis } } - return formatting + - TranslateString(JSONData2String(data.Properties["translate"], "", links), using_data) + - extra_result; + message = TranslateString(JSONData2String(data.Properties["translate"], formatting, links), using_data); } - else return extra_result; + break; case Json.JSONData.DataType.Array: string result = ""; @@ -490,26 +496,59 @@ private static string JSONData2String(Json.JSONData data, string formatting, Lis result += JSONData2String(item, formatting, links); } - return result; + message = result; + break; case Json.JSONData.DataType.String: - return formatting + data.StringValue; + message = data.StringValue; + break; } - return ""; + return formatting + message + extra_result; } - private static string NbtToString(Dictionary nbt) + private static string NbtToString(Dictionary nbt, string formatting) { if (nbt.Count == 1 && nbt.TryGetValue("", out object? rootMessage)) { - // Nameless root tag - return (string)rootMessage; + return formatting + (string)rootMessage; } string message = string.Empty; - string colorCode = string.Empty; StringBuilder extraBuilder = new StringBuilder(); + + // Build the formatting prefix first so extras can inherit it + foreach (var kvp in nbt) + { + string key = kvp.Key; + object value = kvp.Value; + + switch (key) + { + case "color": + { + if (nbt.TryGetValue("color", out object color)) + { + formatting += Color2tag((string)color); + } + } + break; + case "italic": + case "bold": + case "underline": + case "strikethrough": + { + if ((byte) value > 0) + { + formatting += "§" + FormattingCodes[key]; + } else { + formatting = formatting.Replace("§" + FormattingCodes[key], ""); + } + } + break; + } + } + foreach (var kvp in nbt) { string key = kvp.Key; @@ -537,7 +576,7 @@ private static string NbtToString(Dictionary nbt) _ => (Dictionary)extras[i] }; - extraBuilder.Append(NbtToString(extraDict) + "§r"); + extraBuilder.Append(NbtToString(extraDict, "§r" + formatting)); } } break; @@ -570,18 +609,14 @@ private static string NbtToString(Dictionary nbt) } } break; - case "color": - { - if (nbt.TryGetValue("color", out object color)) - { - colorCode = Color2tag((string)color); - } - } - break; } } - return colorCode + message + extraBuilder.ToString(); + return formatting + message + extraBuilder.ToString(); + } + private static string NbtToString(Dictionary nbt) + { + return NbtToString(nbt, String.Empty); } } } \ No newline at end of file From 9d626e1fe5237d2643ce4e7c24ccbc63cf86a7b4 Mon Sep 17 00:00:00 2001 From: Anon Date: Fri, 13 Jun 2025 10:36:34 +1200 Subject: [PATCH 4/6] don't use buggy discord formatting by default --- MinecraftClient/ChatBots/DiscordBridge.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MinecraftClient/ChatBots/DiscordBridge.cs b/MinecraftClient/ChatBots/DiscordBridge.cs index b16ab540fe..0db3267e80 100644 --- a/MinecraftClient/ChatBots/DiscordBridge.cs +++ b/MinecraftClient/ChatBots/DiscordBridge.cs @@ -178,7 +178,7 @@ private void Disconnect() return instance; } - private string GetDiscordText(string text) { + private static string GetDiscordText(string text) { text = Regex.Replace(text, "§l(.*?§r)", "**$1**"); text = Regex.Replace(text, "§m(.*?§r)", "~~$1~~"); text = Regex.Replace(text, "§n(.*?§r)", "__$1__"); @@ -192,7 +192,7 @@ public override void GetText(string text) if (!CanSendMessages()) return; - text = GetDiscordText(text).Trim(); + text = GetVerbatim(text).Trim(); // Stop the crash when an empty text is recived somehow if (string.IsNullOrEmpty(text)) From d1c885c283ac8e9abd7261877d7822d1c6d3395e Mon Sep 17 00:00:00 2001 From: Skylandia Date: Fri, 13 Jun 2025 13:33:24 +1200 Subject: [PATCH 5/6] cleanup output and fix formatting sometimes not being applied --- MinecraftClient/Protocol/Message/ChatParser.cs | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/MinecraftClient/Protocol/Message/ChatParser.cs b/MinecraftClient/Protocol/Message/ChatParser.cs index 251671f701..1cb69ee6ec 100644 --- a/MinecraftClient/Protocol/Message/ChatParser.cs +++ b/MinecraftClient/Protocol/Message/ChatParser.cs @@ -430,6 +430,9 @@ private static string JSONData2String(Json.JSONData data, string formatting, Lis case Json.JSONData.DataType.Object: if (data.Properties.ContainsKey("color")) { + // Remove old colors + formatting = Regex.Replace(formatting, "§[0-9a-f]", ""); + // Add the new color formatting += Color2tag(JSONData2String(data.Properties["color"], "", links)); } @@ -504,6 +507,8 @@ private static string JSONData2String(Json.JSONData data, string formatting, Lis break; } + // Formatting before a reset character is ignored + formatting = Regex.Replace(formatting, ".*§r(.*)", "$1"); return formatting + message + extra_result; } From 4462ef02851a3a9c30ade510a1ce3c9b577cf607 Mon Sep 17 00:00:00 2001 From: Skylandia <52195906+Skylandia@users.noreply.github.com> Date: Sun, 15 Jun 2025 22:56:19 +1200 Subject: [PATCH 6/6] don't ignore the reset character --- MinecraftClient/Protocol/Message/ChatParser.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/MinecraftClient/Protocol/Message/ChatParser.cs b/MinecraftClient/Protocol/Message/ChatParser.cs index 1cb69ee6ec..513c50ed58 100644 --- a/MinecraftClient/Protocol/Message/ChatParser.cs +++ b/MinecraftClient/Protocol/Message/ChatParser.cs @@ -508,7 +508,7 @@ private static string JSONData2String(Json.JSONData data, string formatting, Lis } // Formatting before a reset character is ignored - formatting = Regex.Replace(formatting, ".*§r(.*)", "$1"); + formatting = Regex.Replace(formatting, ".*(§r.*)", "$1"); return formatting + message + extra_result; } @@ -624,4 +624,4 @@ private static string NbtToString(Dictionary nbt) return NbtToString(nbt, String.Empty); } } -} \ No newline at end of file +}