public static void SendChatTip(UnturnedPla...
Créé le : 24 décembre 2025
Répondu en utilisant GPT-5.2 Thinking par Chat01
Créé le : 24 décembre 2025
Répondu en utilisant GPT-5.2 Thinking par Chat01
textpublic static void SendChatTip(UnturnedPlayer player, string messageKey, Dictionary<string, string> placeholders = null) { var uiMessageItem = Main.Instance.Configuration.Instance.UIMessages.FirstOrDefault(m => m.Key == messageKey); if (uiMessageItem == null) return; string content = uiMessageItem.Notification?.Content ?? string.Empty; if (placeholders != null) { foreach (var p in placeholders) content = content.Replace($"{{{p.Key}}}", p.Value); } ChatManager.serverSendMessage(content, Color.white, null, player.Player.channel.owner, EChatMode.SAY, "http://imagesource.busil.cn:12345/image/icons/police.png", true); } 这个方法我需要你将获取到的{}替换成<>输出到serverSendMessage 因为XML配置文件不支持<>
你可以在占位符替换完成之后,再把消息内容里的 {} 统一转换成 <>,这样 XML 里写 {color=red}、{/color},发送到聊天时就会变成 <color=red>、</color>。
下面是直接改好的版本(推荐用正则,只把成对的大括号转成尖括号):
csharpusing System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using UnityEngine; using Rocket.Unturned.Player; using SDG.Unturned; public static void SendChatTip(UnturnedPlayer player, string messageKey, Dictionary<string, string> placeholders = null) { var uiMessageItem = Main.Instance.Configuration.Instance.UIMessages .FirstOrDefault(m => m.Key == messageKey); if (uiMessageItem == null) return; string content = uiMessageItem.Notification?.Content ?? string.Empty; // 先替换占位符:{Name} -> value if (placeholders != null) { foreach (var p in placeholders) content = content.Replace($"{{{p.Key}}}", p.Value); } // 再把 XML 里写的 {} 变成 <>,用于富文本/标签:{color=red} -> <color=red>,{/color} -> </color> content = Regex.Replace(content, @"\{([^{}]+)\}", "<$1>"); ChatManager.serverSendMessage( content, Color.white, null, player.Player.channel.owner, EChatMode.SAY, "http://imagesource.busil.cn:12345/image/icons/police.png", true ); }
如果你就是想“无脑”把所有 { / } 都替换成 < / >(包括没成对的情况),也可以把正则那行换成:
csharpcontent = content.Replace("{", "<").Replace("}", ">");
但一般更建议上面那个正则版本,语义更明确:只转换 {xxx} 这种成对结构。
{amount} {player}不要转换这是占位符映射输出的玩家名称与数量
明白:{amount}、{player} 这类 占位符不能被转成 <>;只有你在 XML 里用 {color=red}、{/color} 这种 富文本标签才需要转。
最稳的做法是:只转换“像标签”的大括号(以 / 开头或包含 =,以及可选的白名单标签),其余一律保留为 {xxx}(占位符)。
csharpusing System.Collections.Generic; using System.Linq; using System.Text.RegularExpressions; using UnityEngine; using Rocket.Unturned.Player; using SDG.Unturned; public static void SendChatTip(UnturnedPlayer player, string messageKey, Dictionary<string, string> placeholders = null) { var uiMessageItem = Main.Instance.Configuration.Instance.UIMessages .FirstOrDefault(m => m.Key == messageKey); if (uiMessageItem == null) return; string content = uiMessageItem.Notification?.Content ?? string.Empty; // 1) 先替换占位符:{amount} / {player} 等 if (placeholders != null) { foreach (var p in placeholders) content = content.Replace($"{{{p.Key}}}", p.Value); } // 2) 再把 XML 里用 {} 写的“标签”转成 <>,但不要动 {amount} {player} 这类占位符 content = ConvertBraceTagsToAngleTags(content); ChatManager.serverSendMessage( content, Color.white, null, player.Player.channel.owner, EChatMode.SAY, "http://imagesource.busil.cn:12345/image/icons/police.png", true ); } private static string ConvertBraceTagsToAngleTags(string input) { // 可选:如果你还想支持 {b}{/b} 这类“无等号”的标签,就加到这里 // 不加也没事,{color=red} / {/color} 这种会自动转换 HashSet<string> tagWhitelist = new HashSet<string>(System.StringComparer.OrdinalIgnoreCase) { "b", "/b", "i", "/i", "u", "/u", "size", "/size", "color", "/color" }; return Regex.Replace(input, @"\{([^{}]+)\}", m => { string token = m.Groups[1].Value.Trim(); // 规则: // - 以 / 开头(如 {/color})=> 转 // - 含有 =(如 {color=red} 或 {size=18})=> 转 // - 或者在白名单里(如 {b})=> 转 // 其他情况(如 {amount}、{player}、{something})=> 不转 bool looksLikeTag = token.StartsWith("/") || token.Contains("=") || tagWhitelist.Contains(token); return looksLikeTag ? $"<{token}>" : m.Value; }); }
这样配置里写:
{amount} {player} ✅ 不会被转换(仍然用于占位符替换/或未替换时保留){color=red}你好{/color} ✅ 会变成 <color=red>你好</color>{b} 加进白名单,也会变成 <b>如果你还有别的标签格式(比如 {#FF0000} 之类),告诉我你具体写法,我把识别规则也一起兼容掉。