From 07e6996d7216a492d6ca1f4e4b9e96a3bd489496 Mon Sep 17 00:00:00 2001 From: xien <2383759126@qq.com> Date: Fri, 12 Apr 2024 14:57:33 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E6=8F=92=E4=BB=B6?= =?UTF-8?q?=EF=BC=9Arainbowchat?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Plugin.sln | 10 ++ RainbowChat/README.md | 26 ++++++ RainbowChat/RainbowChat.cs | 164 +++++++++++++++++++++++++++++++++ RainbowChat/RainbowChat.csproj | 5 + 4 files changed, 205 insertions(+) create mode 100644 RainbowChat/README.md create mode 100644 RainbowChat/RainbowChat.cs create mode 100644 RainbowChat/RainbowChat.csproj diff --git a/Plugin.sln b/Plugin.sln index 7982c2110..8c4060073 100644 --- a/Plugin.sln +++ b/Plugin.sln @@ -40,6 +40,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MapTp", "MapTp\MapTp.csproj EndProject Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RandRespawn", "RandRespawn\RandRespawn.csproj", "{44DF105C-3615-4B15-AE72-7835C44A3864}" EndProject +Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "RainbowChat", "RainbowChat\RainbowChat.csproj", "{C62A01BB-F454-40D9-AC45-5CBA14B5E5A6}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -176,6 +178,14 @@ Global {44DF105C-3615-4B15-AE72-7835C44A3864}.Release|Any CPU.Build.0 = Release|Any CPU {44DF105C-3615-4B15-AE72-7835C44A3864}.Release|x64.ActiveCfg = Release|Any CPU {44DF105C-3615-4B15-AE72-7835C44A3864}.Release|x64.Build.0 = Release|Any CPU + {C62A01BB-F454-40D9-AC45-5CBA14B5E5A6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {C62A01BB-F454-40D9-AC45-5CBA14B5E5A6}.Debug|Any CPU.Build.0 = Debug|Any CPU + {C62A01BB-F454-40D9-AC45-5CBA14B5E5A6}.Debug|x64.ActiveCfg = Debug|Any CPU + {C62A01BB-F454-40D9-AC45-5CBA14B5E5A6}.Debug|x64.Build.0 = Debug|Any CPU + {C62A01BB-F454-40D9-AC45-5CBA14B5E5A6}.Release|Any CPU.ActiveCfg = Release|Any CPU + {C62A01BB-F454-40D9-AC45-5CBA14B5E5A6}.Release|Any CPU.Build.0 = Release|Any CPU + {C62A01BB-F454-40D9-AC45-5CBA14B5E5A6}.Release|x64.ActiveCfg = Release|Any CPU + {C62A01BB-F454-40D9-AC45-5CBA14B5E5A6}.Release|x64.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE diff --git a/RainbowChat/README.md b/RainbowChat/README.md new file mode 100644 index 000000000..1a0c194c5 --- /dev/null +++ b/RainbowChat/README.md @@ -0,0 +1,26 @@ +# Rainbow Chat 五彩斑斓聊天 + +- 作者: Professor X制作,nnt升级/汉化,肝帝熙恩更新1449 +- 出处: 无 +- 使玩家每次说话的颜色不一样. + +## 更新日志 + +``` +暂无 +``` + +## 指令 + +| 语法 | 权限 | 说明 | +| -------------- | :-----------------: | :------: | +| /rainbowchat或/rc | rainbowchat.use | 开关彩虹聊天功能 | + +## 配置 + +```json +暂无 +``` +## 反馈 +- 共同维护的插件库:https://github.com/THEXN/TShockPlugin/ +- 国内社区trhub.cn 或 TShock官方群等 \ No newline at end of file diff --git a/RainbowChat/RainbowChat.cs b/RainbowChat/RainbowChat.cs new file mode 100644 index 000000000..a5bb4e313 --- /dev/null +++ b/RainbowChat/RainbowChat.cs @@ -0,0 +1,164 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Linq; +using System.Reflection; +using System.Text; +using Microsoft.Xna.Framework; +using Terraria; +using TerrariaApi.Server; +using TShockAPI; + +namespace RainbowChat; + +[ApiVersion(2, 1)] +public class RainbowChat : TerrariaPlugin +{ + private Random _rand = new Random(); + + private bool[] _rainbowChat = new bool[255]; + + public override string Name => "【五彩斑斓聊天】 Rainbow Chat"; + + public override string Author => "Professor X制作,nnt升级/汉化,肝帝熙恩更新1449"; + + public override string Description => "使玩家每次说话的颜色不一样."; + + public override Version Version => new Version(1, 0, 2); + + public RainbowChat(Main game) + : base(game) + { + } + + public override void Initialize() + { + // 注册事件 + ServerApi.Hooks.GameInitialize.Register(this, OnInitialize); + ServerApi.Hooks.ServerChat.Register(this, OnChat); + } + + protected override void Dispose(bool disposing) + { + if (disposing) + { + // 注销事件 + ServerApi.Hooks.GameInitialize.Deregister(this, OnInitialize); + ServerApi.Hooks.ServerChat.Deregister(this, OnChat); + } + + // 调用基类的Dispose方法 + base.Dispose(disposing); + } + + + + private void OnInitialize(EventArgs args) + { + Commands.ChatCommands.Add(new Command("rainbowchat.use", RainbowChatCallback, "rainbowchat", "rc")); + } + + private void OnChat(ServerChatEventArgs e) + { + // 如果事件已经被处理,直接返回 + if (e.Handled) + { + return; + } + + TSPlayer player = TShock.Players[e.Who]; + if (player == null || !player.HasPermission(Permissions.canchat) || player.mute) + { + return; + } + + // 检查是否是命令,如果是,不进行处理 + if (e.Text.StartsWith(TShock.Config.Settings.CommandSpecifier) || + e.Text.StartsWith(TShock.Config.Settings.CommandSilentSpecifier)) + { + return; + } + + // 检查是否启用了彩虹聊天功能 + if (!_rainbowChat[player.Index]) + { + return; + } + + // 获取颜色列表 + List colors = GetColors(); + + // 使用string.Join和Lambda表达式来构建彩色消息 + string coloredMessage = string.Join(" ", e.Text.Split(' ').Select((word, index) => + TShock.Utils.ColorTag(word, colors[_rand.Next(colors.Count)]))); + + // 发送彩色消息给所有玩家 + TSPlayer.All.SendMessage(string.Format(TShock.Config.Settings.ChatFormat, player.Group.Name, player.Group.Prefix, player.Name, player.Group.Suffix, coloredMessage), player.Group.R, player.Group.G, player.Group.B); + + // 发送原始消息到服务器日志 + TSPlayer.Server.SendMessage(string.Format(TShock.Config.Settings.ChatFormat, player.Group.Name, player.Group.Prefix, player.Name, player.Group.Suffix, e.Text), player.Group.R, player.Group.G, player.Group.B); + + // 标记事件为已处理 + e.Handled = true; + } + + + private void RainbowChatCallback(CommandArgs e) + { + // 获取当前玩家的彩虹聊天状态 + bool currentState = _rainbowChat[e.Player.Index]; + + // 切换状态 + _rainbowChat[e.Player.Index] = !currentState; + + // 如果没有提供参数,则只更新当前玩家的状态 + if (e.Parameters.Count == 0) + { + e.Player.SendSuccessMessage(string.Format("你 {0} 彩虹聊天.", currentState ? "已关闭" : "已开启")); + return; + } + + // 查找玩家 + List players = TSPlayer.FindByNameOrID(e.Parameters[0]); + if (players.Count == 0) + { + e.Player.SendErrorMessage("错误的玩家!"); + return; + } + + // 确保只有一个匹配的玩家 + if (players.Count > 1) + { + e.Player.SendMultipleMatchError(players.Select(p => p.Name)); + return; + } + + // 切换找到的玩家的彩虹聊天状态 + _rainbowChat[players[0].Index] = !_rainbowChat[players[0].Index]; + + // 发送成功消息 + e.Player.SendSuccessMessage(string.Format("{0} 已 {1} 彩虹聊天.", players[0].Name, currentState ? "已关闭" : "已开启")); + } + + + private List GetColors() + { + List colors = new List(); + + // 获取Color类型的所有静态颜色属性 + PropertyInfo[] properties = typeof(Color).GetProperties(BindingFlags.Static | BindingFlags.Public); + + foreach (PropertyInfo property in properties) + { + // 确保属性是Color类型且可读 + if (property.PropertyType == typeof(Color) && property.CanRead) + { + // 添加静态颜色属性到列表中 + colors.Add((Color)property.GetValue(null)); + } + } + + return colors; + } + +} diff --git a/RainbowChat/RainbowChat.csproj b/RainbowChat/RainbowChat.csproj new file mode 100644 index 000000000..639adc026 --- /dev/null +++ b/RainbowChat/RainbowChat.csproj @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file From 92836535a08018dade25268c0c0d12be1376d56d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=82=9D=E5=B8=9D=E7=86=99=E6=81=A9?= <111548550+THEXN@users.noreply.github.com> Date: Fri, 12 Apr 2024 14:59:18 +0800 Subject: [PATCH 2/2] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index e3e594d39..ec0b5e40e 100644 --- a/README.md +++ b/README.md @@ -41,3 +41,4 @@ | [BanNpc](https://github.com/Controllerdestiny/TShockPlugin/tree/master/BanNpc) | 阻止怪物生成 | 无 | | [MapTeleport](https://github.com/Controllerdestiny/TShockPlugin/tree/master/MapTp) | 双击大地图传送 | 无 | | [RandReSpawn](https://github.com/Controllerdestiny/TShockPlugin/tree/master/RandRespawn) | 随机出生点 | 无 | +| [RainbowChat](https://github.com/Controllerdestiny/TShockPlugin/tree/master/RainbowChat) | 每次说话颜色不一样| 无 |