public static void RenderColoredConsoleMessage(this string message) { var output = Console.Out; int start = 0; int end = message.Length - 1; for (int i = 0; i < end; i++) { if (message[i] != '&' && message[i] != '§') { continue; } // Validate color code char colorCode = message[i + 1]; if (!ChatColor.TryParse(colorCode, out var color)) { continue; } // Print text with previous color if (start != i) { output.Write(message.AsSpan(start, i - start)); } // Change color if (colorCode == 'r') { Console.ResetColor(); } else { Console.ForegroundColor = color.ConsoleColor.Value; } // Skip color code i++; start = i + 1; } // Print remaining text if any if (start != message.Length) { output.Write(message.AsSpan(start)); } Console.ResetColor(); }
private void Init(JObject obj) { if (obj["text"] != null) { Text = obj.Value <string>("text"); } // Try to parse enumerations if (obj["color"] != null) { var color = obj.Value <string>("color").ToUpper(); ChatColor c; if (!ChatColor.TryParse(color, out c)) { this.Color = ChatColor.RESET; } else { this.Color = c; } } else { this.Color = ChatColor.RESET; } if (obj["clickEvent"] != null) { var eventSpec = obj["clickEvent"]; var action = eventSpec.Value <string>("action").ToUpper(); this.ChatActionValue = eventSpec.Value <string>("value"); ChatActionType c; if (!ChatActionType.TryParse(action, out c)) { this.Action = ChatActionType.NONE; } else { this.Action = c; } } else { this.Action = ChatActionType.NONE; } if (obj["hoverEvent"] != null) { var eventSpec = obj["hoverEvent"]; var action = eventSpec.Value <string>("action").ToUpper(); this.ChatHoverActionValue = eventSpec.Value <string>("value"); ChatHoverActionType c; if (!ChatHoverActionType.TryParse(action, out c)) { this.HoverAction = ChatHoverActionType.NONE; } else { this.HoverAction = c; } } else { this.HoverAction = ChatHoverActionType.NONE; } //Parse booleans if (obj["bold"] != null) { Bold = obj.Value <bool>("bold"); } if (obj["italic"] != null) { Italic = obj.Value <bool>("italic"); } if (obj["underlined"] != null) { Underlined = obj.Value <bool>("underlined"); } if (obj["strikethrough"] != null) { StrikeThrough = obj.Value <bool>("strikethrough"); } if (obj["obfuscated"] != null) { Obfuscated = obj.Value <bool>("obfuscated"); } if (obj["extra"] != null) { this.SubMessages = new List <ChatMessage>(); foreach (object o in (JArray)obj["extra"]) { if (o.GetType() == typeof(JValue)) { this.SubMessages.Add(new ChatMessage((string)((JValue)o).Value)); } else { this.SubMessages.Add(new ChatMessage((JObject)o)); } } } }