Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

添加插件:RainbowChat 彩虹聊天 #11

Merged
merged 3 commits into from
Apr 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Plugin.sln
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) | 每次说话颜色不一样| 无 |
26 changes: 26 additions & 0 deletions RainbowChat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Rainbow Chat 五彩斑斓聊天

- 作者: Professor X制作,nnt升级/汉化,肝帝熙恩更新1449
- 出处: 无
- 使玩家每次说话的颜色不一样.

## 更新日志

```
暂无
```

## 指令

| 语法 | 权限 | 说明 |
| -------------- | :-----------------: | :------: |
| /rainbowchat或/rc | rainbowchat.use | 开关彩虹聊天功能 |

## 配置

```json
暂无
```
## 反馈
- 共同维护的插件库:https://github.com/THEXN/TShockPlugin/
- 国内社区trhub.cn 或 TShock官方群等
164 changes: 164 additions & 0 deletions RainbowChat/RainbowChat.cs
Original file line number Diff line number Diff line change
@@ -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<Color> 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<TSPlayer> 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<Color> GetColors()
{
List<Color> colors = new List<Color>();

// 获取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;
}

}
5 changes: 5 additions & 0 deletions RainbowChat/RainbowChat.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\template.targets" />

</Project>
Loading