Skip to content

Commit

Permalink
Merge pull request #129 from Controllerdestiny/main
Browse files Browse the repository at this point in the history
更新: EconomicsAPI 添加颜色渐变消息发送API
  • Loading branch information
Controllerdestiny authored May 19, 2024
2 parents 699cc8c + 8d93425 commit d7d9fda
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 9 deletions.
3 changes: 2 additions & 1 deletion EconomicsAPI/Command.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using EconomicsAPI.Attributes;
using EconomicsAPI.Extensions;
using TShockAPI;

namespace EconomicsAPI;
Expand All @@ -9,7 +10,7 @@ internal class Command
[CommandMap("查询", EconomicsPerm.QueryCurrency)]
public void QueryCurrency(CommandArgs args)
{
args.Player.SendInfoMessage(string.Format(Economics.Setting.QueryFormat,
args.Player.SendGradientMsg(string.Format(Economics.Setting.QueryFormat,
Economics.Setting.CurrencyName,
Economics.CurrencyManager.GetUserCurrency(args.Player.Name)));
}
Expand Down
3 changes: 3 additions & 0 deletions EconomicsAPI/Configured/Setting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,7 @@ public class Setting

[JsonProperty("查询提示")]
public string QueryFormat = "[c/FFA500:你当前拥有{0}{1}个]";

[JsonProperty("渐变颜色")]
public List<string> GradientColor = new();
}
34 changes: 31 additions & 3 deletions EconomicsAPI/Economics.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using EconomicsAPI.Configured;
using EconomicsAPI.DB;
using EconomicsAPI.EventArgs;
using EconomicsAPI.EventArgs.PlayerEventArgs;
using EconomicsAPI.Events;
using EconomicsAPI.Extensions;
Expand Down Expand Up @@ -49,7 +48,7 @@ public Economics(Main game) : base(game)

public override void Initialize()
{
Setting = ConfigHelper.LoadConfig<Setting>(ConfigPATH);
LoadConfig();
CurrencyManager = new CurrencyManager();
Helper.InitPluginAttributes();
ServerApi.Hooks.NetGreetPlayer.Register(this, OnGreet);
Expand All @@ -61,7 +60,36 @@ public override void Initialize()
ServerApi.Hooks.GameUpdate.Register(this, OnUpdate);
GetDataHandlers.KillMe.Register(OnKillMe);
PlayerHandler.OnPlayerCountertop += PlayerHandler_OnPlayerCountertop;
TShockAPI.Hooks.GeneralHooks.ReloadEvent += (_) => Setting = ConfigHelper.LoadConfig(ConfigPATH, Setting);
TShockAPI.Hooks.GeneralHooks.ReloadEvent += (_) => LoadConfig();
}

private void LoadConfig()
{
if (!File.Exists(ConfigPATH))
Setting.GradientColor = new List<string>()
{
"[c/40c900:{0}]",
"[c/00c427:{0}]",
"[c/00be3b:{0}]",
"[c/00b650:{0}]",
"[c/00ad5f:{0}]",
"[c/00a569:{0}]",
"[c/009d70:{0}]",
"[c/009575:{0}]",
"[c/008d78:{0}]",
"[c/008579:{0}]",
"[c/007e7a:{0}]",
"[c/007779:{0}]",
"[c/007078:{0}]",
"[c/006976:{0}]",
"[c/006373:{0}]",
"[c/005c71:{0}]",
"[c/00556e:{0}]",
"[c/004f6b:{0}]",
"[c/004765:{0}]",
"[c/00405c:{0}]"
};
Setting = ConfigHelper.LoadConfig(ConfigPATH, Setting);
}

private void PlayerHandler_OnPlayerCountertop(PlayerCountertopArgs args)
Expand Down
6 changes: 6 additions & 0 deletions EconomicsAPI/Extensions/TSPlayerExt.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ namespace EconomicsAPI.Extensions;

public static class TSPlayerExt
{

public static void SendCombatMsg(this TSPlayer player, string text, Color color)
{
player.TPlayer.SendCombatMsg(text, color);
}

public static void SendGradientMsg(this TSPlayer player, string text)
{
player.SendInfoMessage(Utils.Helper.GetGradientText(text));
}

public static void ExecCommand(this TSPlayer player, string cmd)
{
player.tempGroup = new SuperAdminGroup();
Expand Down
33 changes: 28 additions & 5 deletions EconomicsAPI/Utils/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using EconomicsAPI.Attributes;
using EconomicsAPI.EventArgs.PlayerEventArgs;
using EconomicsAPI.Extensions;
using Microsoft.Xna.Framework;
using Rests;
using System.Reflection;
using System.Text;
Expand All @@ -11,6 +12,31 @@ namespace EconomicsAPI.Utils;

public class Helper
{
//代码来自于CaiLib
public static string GetGradientText(string text)
{
string result = "";
var info = Terraria.UI.Chat.ChatManager.ParseMessage(text, Color.White);
var colors = Economics.Setting.GradientColor.OrderBy(x => Guid.NewGuid()).ToList();
foreach (var item in info)
{
var index = 0;
for(int i = 0; i< item.Text.Length; i++)
{
if (index >= colors.Count)
{
result += item.Text[i];
index = 0;
}
else
{
result += Economics.Setting.GradientColor[index].SFormat(item.Text[i]);
}
index++;
}
}
return result;
}
/// <summary>
///
/// </summary>
Expand Down Expand Up @@ -76,11 +102,8 @@ public static void CountertopUpdate(PlayerCountertopArgs args)
string down = new('\n', Economics.Setting.StatusTextShiftDown);
string Left = new(' ', Economics.Setting.StatusTextShiftLeft);
sb.AppendLine(down);
args.Messages.OrderBy(x => x.Order).ForEach(x => sb.AppendLine(x.Message + Left));
if (args.Player != null)
{
args.Player.SendData(PacketTypes.Status, sb.ToString(), 0, 1);
}
args.Messages.OrderBy(x => x.Order).ForEach(x => sb.AppendLine(GetGradientText(x.Message) + Left));
args.Player?.SendData(PacketTypes.Status, sb.ToString(), 0, 1);
}

/// <summary>
Expand Down

0 comments on commit d7d9fda

Please sign in to comment.