Skip to content

Commit

Permalink
Merge pull request #109 from Controllerdestiny/main
Browse files Browse the repository at this point in the history
更新: 为EconomicsAPI 添加货币更改事件
  • Loading branch information
Controllerdestiny authored May 11, 2024
2 parents 3382a9e + 03ef86b commit fa4728c
Show file tree
Hide file tree
Showing 13 changed files with 130 additions and 30 deletions.
2 changes: 1 addition & 1 deletion Economics.Skill/Skill.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
using Economics.Skill.Events;
using Economics.Skill.Setting;
using EconomicsAPI.Configured;
using EconomicsAPI.EventArgs;
using EconomicsAPI.EventArgs.PlayerEventArgs;
using System.Reflection;
using Terraria;
using TerrariaApi.Server;
Expand Down
3 changes: 3 additions & 0 deletions EconomicsAPI/Configured/Setting.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@ public class Setting
[JsonProperty("死亡掉落率")]
public float DeathDropRate = 0f;

[JsonProperty("显示信息")]
public bool StatusText = true;

[JsonProperty("查询提示")]
public string QueryFormat = "[c/FFA500:你当前拥有{0}{1}个]";
}
45 changes: 43 additions & 2 deletions EconomicsAPI/DB/CurrencyManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
using MySql.Data.MySqlClient;
using EconomicsAPI.Enumerates;
using EconomicsAPI.EventArgs.PlayerEventArgs;
using EconomicsAPI.Events;
using MySql.Data.MySqlClient;
using System.Data;
using TShockAPI;
using TShockAPI.DB;
Expand Down Expand Up @@ -58,6 +61,25 @@ public long GetUserCurrency(string name)
}

public void AddUserCurrency(string name, long num)
{
var player = Economics.ServerPlayers.Find(x => x.Name == name && x.Active);
if (player != null)
{
var args = new PlayerCurrencyUpdateArgs()
{
Change = num,
CurrentType = CurrencyUpdateType.Added,
Player = player,
Current = Currency.TryGetValue(name, out var cur) ? cur : 0
};
if (PlayerHandler.PlayerCurrencyUpdate(args))
return;
num = args.Change;
}
Add(name, num);
}

private void Add(string name, long num)
{
if (Currency.ContainsKey(name))
Currency[name] += num;
Expand All @@ -70,7 +92,7 @@ public void ClearUserCurrency(string name)
Currency[name] = 0;
}

public bool DelUserCurrency(string name, long num)
public bool Del(string name, long num)
{
if (GetUserCurrency(name) >= num)
{
Expand All @@ -80,6 +102,25 @@ public bool DelUserCurrency(string name, long num)
return false;
}

public bool DelUserCurrency(string name, long num)
{
var player = Economics.ServerPlayers.Find(x => x.Name == name && x.Active);
if (player != null)
{
var args = new PlayerCurrencyUpdateArgs()
{
Change = num,
CurrentType = CurrencyUpdateType.Delete,
Player = player,
Current = Currency.TryGetValue(name, out var cur) ? cur : 0
};
if (PlayerHandler.PlayerCurrencyUpdate(args))
return true;
num = args.Change;
}
return Del(name, num);
}

public void Reset()
{
database.Query("delete from Economics");
Expand Down
12 changes: 7 additions & 5 deletions EconomicsAPI/Economics.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using EconomicsAPI.Configured;
using EconomicsAPI.DB;
using EconomicsAPI.EventArgs;
using EconomicsAPI.EventArgs.PlayerEventArgs;
using EconomicsAPI.Events;
using EconomicsAPI.Extensions;
using EconomicsAPI.Model;
Expand Down Expand Up @@ -66,12 +67,12 @@ public override void Initialize()
private void PlayerHandler_OnPlayerCountertop(PlayerCountertopArgs args)
{
args.Messages.Add(new($"当前延迟:{args.Ping.GetPing():F1}", 7));
args.Messages.Add(new($"玩家名称:{args.Ping.TSPlayer.Name}", 1));
args.Messages.Add(new($"{Setting.CurrencyName}数量:{CurrencyManager.GetUserCurrency(args.Ping.TSPlayer.Name)}", 3));
args.Messages.Add(new($"玩家名称:{args.Player.Name}", 1));
args.Messages.Add(new($"{Setting.CurrencyName}数量:{CurrencyManager.GetUserCurrency(args.Player.Name)}", 3));
args.Messages.Add(new($"在线人数:{TShock.Utils.GetActivePlayerCount()}/{Main.maxPlayers}", 4));
args.Messages.Add(new($"世界名称:{Main.worldName}", 9));
args.Messages.Add(new($"当前生命:{args.Ping.TSPlayer.TPlayer.statLife}/{args.Ping.TSPlayer.TPlayer.statLifeMax}", 5));
args.Messages.Add(new($"当前魔力:{args.Ping.TSPlayer.TPlayer.statMana} / {args.Ping.TSPlayer.TPlayer.statManaMax}", 6));
args.Messages.Add(new($"当前生命:{args.Player.TPlayer.statLife}/{args.Player.TPlayer.statLifeMax}", 5));
args.Messages.Add(new($"当前魔力:{args.Player.TPlayer.statMana} / {args.Player.TPlayer.statManaMax}", 6));
}

private void OnKillMe(object? sender, GetDataHandlers.KillMeEventArgs e)
Expand All @@ -94,8 +95,9 @@ private void OnUpdate(System.EventArgs args)
var status = new PlayerCountertopArgs()
{
Ping = data,
Player = data.TSPlayer
};
if (!PlayerHandler.PlayerCountertopUpdate(status))
if (Setting.StatusText && !PlayerHandler.PlayerCountertopUpdate(status))
Helper.CountertopUpdate(status);
});
}
Expand Down
10 changes: 10 additions & 0 deletions EconomicsAPI/Enumerates/CurrencyUpdateType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@


namespace EconomicsAPI.Enumerates;

public enum CurrencyUpdateType
{
Added,

Delete
}
12 changes: 12 additions & 0 deletions EconomicsAPI/EventArgs/BaseEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace EconomicsAPI.EventArgs;

public class BaseEventArgs
{
public bool Handler { get; set; } = false;
}
14 changes: 0 additions & 14 deletions EconomicsAPI/EventArgs/PlayerCountertopArgs.cs

This file was deleted.

13 changes: 13 additions & 0 deletions EconomicsAPI/EventArgs/PlayerEventArgs/BasePlayerEventArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using TShockAPI;

namespace EconomicsAPI.EventArgs.PlayerEventArgs;

public class BasePlayerEventArgs : BaseEventArgs
{
public TSPlayer Player { get; init; }

Check warning on line 12 in EconomicsAPI/EventArgs/PlayerEventArgs/BasePlayerEventArgs.cs

View workflow job for this annotation

GitHub Actions / Build

Non-nullable property 'Player' must contain a non-null value when exiting constructor. Consider declaring the property as nullable.
}
10 changes: 10 additions & 0 deletions EconomicsAPI/EventArgs/PlayerEventArgs/PlayerCountertopArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
using EconomicsAPI.Model;

namespace EconomicsAPI.EventArgs.PlayerEventArgs;

public class PlayerCountertopArgs : BasePlayerEventArgs
{
public List<CountertopMessage> Messages { get; init; } = new();

public PingData Ping { get; init; } = new();
}
13 changes: 13 additions & 0 deletions EconomicsAPI/EventArgs/PlayerEventArgs/PlayerCurrencyUpdateArgs.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using EconomicsAPI.Enumerates;
using TShockAPI;

namespace EconomicsAPI.EventArgs.PlayerEventArgs;

public class PlayerCurrencyUpdateArgs: BasePlayerEventArgs
{
public long Current { get; init; }

public CurrencyUpdateType CurrentType { get; init; }

public long Change { get; init; }
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
using Terraria;
using TShockAPI;

namespace EconomicsAPI.EventArgs;
namespace EconomicsAPI.EventArgs.PlayerEventArgs;

public class PlayerKillNpcArgs : System.EventArgs
public class PlayerKillNpcArgs : BasePlayerEventArgs
{
public TSPlayer Player { get; }

public NPC Npc { get; }

public int Damage { get; }

public bool Handler { get; set; } = false;

internal PlayerKillNpcArgs(Player player, NPC npc, int damage)
{
Player = TShock.Players[player.whoAmI];
Expand Down
16 changes: 15 additions & 1 deletion EconomicsAPI/Events/PlayerHandler.cs
Original file line number Diff line number Diff line change
@@ -1,15 +1,29 @@
using EconomicsAPI.EventArgs;
using EconomicsAPI.EventArgs.PlayerEventArgs;
using TShockAPI;

namespace EconomicsAPI.Events;

public class PlayerHandler
{
public delegate void EventCallBack<in TArgs>(TArgs args) where TArgs : System.EventArgs;
public delegate void EventCallBack<in TArgs>(TArgs args) where TArgs : BaseEventArgs;

public static event EventCallBack<PlayerKillNpcArgs> OnPlayerKillNpc;

Check warning on line 11 in EconomicsAPI/Events/PlayerHandler.cs

View workflow job for this annotation

GitHub Actions / Build

Non-nullable event 'OnPlayerKillNpc' must contain a non-null value when exiting constructor. Consider declaring the event as nullable.

public static event EventCallBack<PlayerCountertopArgs> OnPlayerCountertop;

Check warning on line 13 in EconomicsAPI/Events/PlayerHandler.cs

View workflow job for this annotation

GitHub Actions / Build

Non-nullable event 'OnPlayerCountertop' must contain a non-null value when exiting constructor. Consider declaring the event as nullable.

public static event EventCallBack<PlayerCurrencyUpdateArgs> OnPlayerCurrencyUpdate;

internal static bool PlayerCurrencyUpdate(PlayerCurrencyUpdateArgs args)
{
if (OnPlayerCurrencyUpdate != null)
{
OnPlayerCurrencyUpdate(args);
return args.Handler;
}
return false;
}

internal static bool PlayerKillNpc(PlayerKillNpcArgs args)
{
if (OnPlayerKillNpc != null)
Expand Down
2 changes: 1 addition & 1 deletion EconomicsAPI/Utils/Helper.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using EconomicsAPI.Attributes;
using EconomicsAPI.EventArgs;
using EconomicsAPI.EventArgs.PlayerEventArgs;
using EconomicsAPI.Extensions;
using EconomicsAPI.Model;
using Rests;
Expand Down

0 comments on commit fa4728c

Please sign in to comment.