using System; using System.Text; namespace TrueCraft.API { /// /// Provides constants and functions for working with chat formatting. /// public static class ChatFormat { /// /// The following text should be obfuscated. /// public const string Obfuscated = "§k"; /// /// The following text should be bold. /// public const string Bold = "§l"; /// /// The following text should be striked-through. /// public const string Strikethrough = "§m"; /// /// The following text should be underlined. /// public const string Underline = "§n"; /// /// The following text should be italicized. /// public const string Italic = "§o"; /// /// The following text should be reset to normal. /// public const string Reset = "§r"; /// /// Returns whether the specified chat code is a valid formatting one. /// /// /// public static bool IsValid(string code) { if (string.IsNullOrEmpty(code)) return false; var comparison = StringComparison.InvariantCultureIgnoreCase; return code.Equals(ChatFormat.Obfuscated, comparison) || code.Equals(ChatFormat.Bold, comparison) || code.Equals(ChatFormat.Strikethrough, comparison) || code.Equals(ChatFormat.Underline, comparison) || code.Equals(ChatFormat.Italic, comparison) || code.Equals(ChatFormat.Reset, comparison); } /// /// Removes any format codes from a chat string. /// /// /// public static string Remove(string text) { if (string.IsNullOrEmpty(text)) return string.Empty; var builder = new StringBuilder(text.Length); for (int i = 0; i < text.Length; i++) { if (text[i] == '§') { i++; var code = new string('§', text[i]); if (IsValid(code)) continue; else builder.Append(code); } builder.Append(text[i]); } return builder.ToString(); } } }