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新增了自定义渐变色的子命令 #46

Merged
merged 1 commit into from
Apr 23, 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
72 changes: 48 additions & 24 deletions RainbowChat/Configuration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Text;
using TShockAPI;
using System.Globalization;
using Newtonsoft.Json.Linq;

namespace RainbowChat
{
Expand All @@ -11,60 +12,53 @@ internal class Configuration
// 配置文件存放路径
public static readonly string FilePath = Path.Combine(TShock.SavePath, "RainbowChat.json");

[JsonProperty("渐变开始颜色")]
private string? _gradientStartColorHex = "#615EF2";
[JsonProperty("使用说明")]
public string Text = "权限名(rainbowchat.use) /rc 渐变 用指令修改的颜色不会写进配置文件,这里改的是全体默认渐变色,开启【随机色】渐变会默认失效";

[JsonProperty("渐变结束颜色")]
private string? _gradientEndColorHex = "#D6C053";
[JsonProperty("进服自动开启渐变色")]
public bool Enable = false;

[JsonProperty("修改渐变开始颜色")]
public Microsoft.Xna.Framework.Color GradientStartColor
{
get => HexToXnaColor(_gradientStartColorHex!);
set => _gradientStartColorHex = XnaColorToHex(value);
}
[JsonConverter(typeof(ColorJsonConverter))]
public Microsoft.Xna.Framework.Color GradientStartColor { get; set; }

[JsonProperty("修改渐变结束颜色")]
public Microsoft.Xna.Framework.Color GradientEndColor
[JsonConverter(typeof(ColorJsonConverter))]
public Microsoft.Xna.Framework.Color GradientEndColor { get; set; }

//赋个默认值 避免色盲不会调色
public Configuration()
{
get => HexToXnaColor(_gradientEndColorHex!);
set => _gradientEndColorHex = XnaColorToHex(value);
GradientStartColor = new Microsoft.Xna.Framework.Color(r: 166, g: 213, b: 234);
GradientEndColor = new Microsoft.Xna.Framework.Color(r: 245, g: 247, b: 175);
}

#region 色彩转换辅助方法
#region 色彩辅助方法

// 将十六进制颜色字符串转换为Microsoft.Xna.Framework.Color
private static Microsoft.Xna.Framework.Color HexToXnaColor(string hexColor)
{
if (hexColor.StartsWith("#"))
hexColor = hexColor.Substring(1);

byte r, g, b, a;
byte r, g, b;
switch (hexColor.Length)
{
case 6:
r = byte.Parse(hexColor.Substring(0, 2), NumberStyles.HexNumber);
g = byte.Parse(hexColor.Substring(2, 2), NumberStyles.HexNumber);
b = byte.Parse(hexColor.Substring(4, 2), NumberStyles.HexNumber);
a = 255; // 默认全透明
break;
case 8:
r = byte.Parse(hexColor.Substring(0, 2), NumberStyles.HexNumber);
g = byte.Parse(hexColor.Substring(2, 2), NumberStyles.HexNumber);
b = byte.Parse(hexColor.Substring(4, 2), NumberStyles.HexNumber);
a = byte.Parse(hexColor.Substring(6, 2), NumberStyles.HexNumber);
break;
default:
throw new ArgumentException("Invalid hexadecimal color string.");
throw new ArgumentException("无效的十六进制颜色字符串。");
}

return new Microsoft.Xna.Framework.Color(r, g, b, a);
}

// 将Microsoft.Xna.Framework.Color转换为十六进制颜色字符串
private static string XnaColorToHex(Microsoft.Xna.Framework.Color xnaColor)
{
return $"#{xnaColor.PackedValue.ToString("X8").Substring(2)}";
return new Microsoft.Xna.Framework.Color(r, g, b);
}
#endregion

Expand Down Expand Up @@ -102,5 +96,35 @@ public static Configuration Read(string path)
}
}
#endregion


#region 用于反序列化的JsonConverter 使Config看得简洁

internal class ColorJsonConverter : JsonConverter<Color>
{
public override void WriteJson(JsonWriter writer, Color value, JsonSerializer serializer)
{
JObject colorObject = new JObject
{
["R"] = value.R,
["G"] = value.G,
["B"] = value.B,
};

colorObject.WriteTo(writer);
}

public override Color ReadJson(JsonReader reader, Type objectType, Color existingValue, bool hasExistingValue, JsonSerializer serializer)
{
JObject colorObject = JObject.Load(reader);

byte r = colorObject["R"].Value<byte>();
byte g = colorObject["G"].Value<byte>();
byte b = colorObject["B"].Value<byte>();

return new Color(r, g, b);
}
}
#endregion
}
}
31 changes: 17 additions & 14 deletions RainbowChat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
## 更新日志

```
1.0.4
更新了Config,把没用的几个值移除
加入了进服自动开启渐变色
(默认为关闭,这是影响所有玩家进服后是否自动开启的)
加入了子命令方便玩家在游戏内设置渐变颜色值(只能改自己的)

1.0.3
更新了子命令:/rc 渐变 与 /rc 随机
加入了配置文件专门调整渐变色值用(你直管改RGBA值就行了,A是透明度,RGB是红绿蓝)
Expand All @@ -19,29 +25,26 @@
| /rainbowchat 或 /rc | rainbowchat.use | 查看菜单 |
| /rainbowchat 或 /rc 渐变| rainbowchat.use | 开关彩虹聊天【渐变色】功能 |
| /rainbowchat 或 /rc 随机| rainbowchat.use | 开关彩虹聊天【随机色】功能 |
| /rc 渐变 开始 xxx,xxx,xxx| rainbowchat.use | 更改渐变色开始值 |
| /rc 渐变 结束 xxx,xxx,xxx| rainbowchat.use | 更改渐变色结束值 |



## 配置

```json
{
"渐变开始颜色": "#615EF2",
"渐变结束颜色": "#D6C053",
"使用说明": "权限名(rainbowchat.use) /rc 渐变 用指令修改的颜色不会写进配置文件,这里改的是全体默认渐变色,开启【随机】渐变会默认失效",
"进服自动开启渐变色": false,
"修改渐变开始颜色": {
"packedValue": 4294073953,
"R": 97,
"G": 94,
"B": 242,
"A": 255,
"PackedValue": 4294073953
"R": 166,
"G": 213,
"B": 234
},
"修改渐变结束颜色": {
"packedValue": 4283678934,
"R": 214,
"G": 192,
"B": 83,
"A": 255,
"PackedValue": 4283678934
"R": 245,
"G": 247,
"B": 175
}
}
```
Expand Down
121 changes: 114 additions & 7 deletions RainbowChat/RainbowChat.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using Microsoft.Xna.Framework;
using MySqlX.XDevAPI.Relational;
using System.Reflection;
using Terraria;
using TerrariaApi.Server;
Expand All @@ -12,13 +13,13 @@ public class RainbowChat : TerrariaPlugin
{
public override string Name => "【五彩斑斓聊天】 Rainbow Chat";

public override string Author => "Professor X制作,nnt升级/汉化,肝帝熙恩、羽学更新1449";
public override string Author => "Professor 修改:熙恩 羽学";

public override string Description => "使玩家每次说话的颜色不一样.";

public override Version Version => new Version(1, 0, 3);
public override Version Version => new Version(1, 0, 4);

public RainbowChat(Main game): base(game)
public RainbowChat(Main game) : base(game)
{
}

Expand All @@ -35,6 +36,8 @@ public override void Initialize()
GeneralHooks.ReloadEvent += LoadConfig;
ServerApi.Hooks.GameInitialize.Register(this, OnInitialize);
ServerApi.Hooks.ServerChat.Register(this, OnChat);
ServerApi.Hooks.ServerLeave.Register(this, OnServerLeave);
ServerApi.Hooks.ServerJoin.Register(this, OnServerJoin);
}

protected override void Dispose(bool disposing)
Expand All @@ -44,6 +47,8 @@ protected override void Dispose(bool disposing)
// 注销事件
ServerApi.Hooks.GameInitialize.Deregister(this, OnInitialize);
ServerApi.Hooks.ServerChat.Deregister(this, OnChat);
ServerApi.Hooks.ServerLeave.Deregister(this, OnServerLeave);
ServerApi.Hooks.ServerJoin.Deregister(this, OnServerJoin);
}

// 调用基类的Dispose方法
Expand All @@ -68,12 +73,54 @@ private static void LoadConfig(ReloadEventArgs args = null!)
}
#endregion

//自动为加入服务器的玩家开启渐变聊天
private void OnServerJoin(JoinEventArgs args)
{
if (args == null || TShock.Players[args.Who] == null)
{
return;
}

TSPlayer player = TShock.Players[args.Who];

if (!player.mute && player.HasPermission(Permissions.canchat))
{
if (Config.Enable)
{

_Gradient[player.Index] = true;
player.SendSuccessMessage("您的渐变聊天功能已自动开启.");

}
}
}

//退出服务器的玩家关闭渐变与随机聊天
private void OnServerLeave(LeaveEventArgs args)
{
if (args == null || TShock.Players[args.Who] == null)
{
return;
}

TSPlayer player = TShock.Players[args.Who];

if (!player.mute && player.HasPermission(Permissions.canchat))
{

_Gradient[player.Index] = false;
_rainbowChat[player.Index] = false;

}
}


private void OnChat(ServerChatEventArgs e)
{
var gradientStartColor = Config.GradientStartColor;
var gradientEndColor = Config.GradientEndColor;

if (e.Handled ) { return; }
if (e.Handled) { return; }
if ((e.Text.StartsWith(TShock.Config.Settings.CommandSpecifier) || e.Text.StartsWith(TShock.Config.Settings.CommandSilentSpecifier))) { return; }

TSPlayer player = TShock.Players[e.Who];
Expand All @@ -100,7 +147,11 @@ private void RainbowChatCallback(CommandArgs e)

if (e.Parameters.Count == 0)
{
player.SendSuccessMessage(string.Format("可用命令:/rc 随机 或 /rc 渐变"));
player.SendSuccessMessage(string.Format("<彩虹聊天>,可用子命令:" +
"\n/rc 随机 - 开启随机单色整句(不能和渐变同开)" +
"\n/rc 渐变 - 开启随机整句花色(与随机色相对)" +
"\n/rc 渐变 开始 R,G,B - 设置渐变聊天起始颜色" +
"\n/rc 渐变 结束 R,G,B - 设置渐变聊天结束颜色", Color.Yellow));
return;
}

Expand All @@ -114,10 +165,66 @@ private void RainbowChatCallback(CommandArgs e)
break;
case "gradient":
case "渐变":
GradientChat(e);
if (e.Parameters.Count >= 3)
{
string mode = e.Parameters[1].ToLower();

if (mode == "开始" || mode == "begin")
{
if (e.Parameters.Count >= 3)
{
string[] colorComponents = e.Parameters[2].Split(',');
if (colorComponents.Length == 3 && byte.TryParse(colorComponents[0], out byte r) && byte.TryParse(colorComponents[1], out byte g) && byte.TryParse(colorComponents[2], out byte b))
{
Config.GradientStartColor = new Color(r, g, b);
player.SendSuccessMessage($"渐变开始颜色已设置为 R:{r}, G:{g}, B:{b}");
}
else
{
player.SendErrorMessage("无效的起始颜色值。请使用格式如 'R,G,B'(逗号分隔)的颜色表示法。");
}
}
else
{
player.SendErrorMessage("起始颜色值缺失。请使用如 '/rc 渐变 开始 255,0,0' 的格式。");
}
}
else if (mode == "结束" || mode == "end")
{
if (e.Parameters.Count >= 3)
{
string[] colorComponents = e.Parameters[2].Split(',');
if (colorComponents.Length == 3 && byte.TryParse(colorComponents[0], out byte r) && byte.TryParse(colorComponents[1], out byte g) && byte.TryParse(colorComponents[2], out byte b))
{
Config.GradientEndColor = new Color(r, g, b);
player.SendSuccessMessage($"渐变结束颜色已设置为 R:{r}, G:{g}, B:{b}");
}
else
{
player.SendErrorMessage("无效的结束颜色值。请使用格式如 'R,G,B'(逗号分隔)的颜色表示法。");
}
}
else
{
player.SendErrorMessage("结束颜色值缺失。请使用如 '/rc 渐变 结束 0,255,0' 的格式。");
}
}
else
{
GradientChat(e);
}
}
else
{
GradientChat(e);
}
break;
default:
player.SendErrorMessage("无效的子命令,可用子命令:/rc 随机 或 /rc 渐变.");
player.SendMessage("<彩虹聊天>,可用子命令:" +
"\n/rc 随机 - 开启随机单色整句(不能和渐变同开)" +
"\n/rc 渐变 - 开启随机整句花色(与随机色相对)" +
"\n/rc 渐变 开始 R,G,B - 设置渐变聊天起始颜色" +
"\n/rc 渐变 结束 R,G,B - 设置渐变聊天结束颜色", Color.Yellow);
return;
}
}
Expand Down
Loading