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

添加插件: Economics.NPC #142

Merged
merged 1 commit into from
May 20, 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
18 changes: 18 additions & 0 deletions Economics.NPC/Config.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using Newtonsoft.Json;

namespace Economics.NPC;

public class Config
{
[JsonProperty("开启提示")]
public bool Prompt = true;

[JsonProperty("提示内容")]
public string PromptText = "你因击杀{0},获得额外奖励{1}{2}个";

[JsonProperty("额外奖励列表")]
public List<NpcOption> NPCS = new();

[JsonProperty("转换率更改")]
public Dictionary<int, double> AllocationRatio = new();
}
9 changes: 9 additions & 0 deletions Economics.NPC/Economics.NPC.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<Project Sdk="Microsoft.NET.Sdk">

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

<ItemGroup>
<ProjectReference Include="..\EconomicsAPI\EconomicsAPI.csproj" />
</ItemGroup>

</Project>
19 changes: 19 additions & 0 deletions Economics.NPC/NpcOption.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using Newtonsoft.Json;

namespace Economics.NPC;

public class NpcOption
{
[JsonProperty("怪物ID")]
public int ID { get; set; }

[JsonProperty("怪物名称")]
public string Name { get; set; } = string.Empty;

[JsonProperty("奖励货币")]
public long ExtraReward { get; set; }

[JsonProperty("按输出瓜分")]
public bool DynamicPartition { get; set; } = true;

}
78 changes: 78 additions & 0 deletions Economics.NPC/Plugin.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using EconomicsAPI.Configured;
using EconomicsAPI.Extensions;
using Microsoft.Xna.Framework;
using System.Reflection;
using Terraria;
using TerrariaApi.Server;
using TShockAPI.Hooks;

namespace Economics.NPC;

[ApiVersion(2, 1)]
public class Plugin : TerrariaPlugin
{
public override string Author => "少司命";

public override string Description => Assembly.GetExecutingAssembly().GetName().Name!;

public override string Name => Assembly.GetExecutingAssembly().GetName().Name!;

public override Version Version => Assembly.GetExecutingAssembly().GetName().Version!;

internal static string PATH = Path.Combine(EconomicsAPI.Economics.SaveDirPath, "NPC.json");

private static Config Config = new();

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

public override void Initialize()
{
LoadConfig();
EconomicsAPI.Events.PlayerHandler.OnPlayerKillNpc += OnPlayerKillNpc;
GeneralHooks.ReloadEvent += (_) => LoadConfig();
}

private void LoadConfig()
{
if(!File.Exists(PATH))
{
Config.NPCS.Add(new());
}
Config = ConfigHelper.LoadConfig(PATH, Config);
}

private void OnPlayerKillNpc(EconomicsAPI.EventArgs.PlayerEventArgs.PlayerKillNpcArgs args)
{
if (args.Npc == null || args.Player == null) return;
if (Config.AllocationRatio.TryGetValue(args.Npc.netID, out double ra))
{
double rw = args.Damage / args.Npc.lifeMax;
long Curr = Convert.ToInt64(rw * ra);
EconomicsAPI.Economics.CurrencyManager.AddUserCurrency(args.Player.Name, Curr);
args.Player.SendCombatMsg($"+{Curr}$", Color.AliceBlue);
args.Handler = true;
return;
}

var cfg = Config.NPCS.Find(f => f.ID == args.Npc.netID);
if (cfg != null)
{
if (cfg.DynamicPartition)
{
double rw = args.Damage / args.Npc.lifeMax;
long Curr = Convert.ToInt64(Math.Round(rw * cfg.ExtraReward));
EconomicsAPI.Economics.CurrencyManager.AddUserCurrency(args.Player.Name, Curr);
if (Config.Prompt)
args.Player.SendInfoMessage(Config.PromptText, args.Npc.GetFullNetName(), EconomicsAPI.Economics.Setting.CurrencyName, Curr);
}
else
{
EconomicsAPI.Economics.CurrencyManager.AddUserCurrency(args.Player.Name, cfg.ExtraReward);
if (Config.Prompt)
args.Player.SendInfoMessage(Config.PromptText, args.Npc.GetFullNetName(), EconomicsAPI.Economics.Setting.CurrencyName, cfg.ExtraReward);
}
}
}
}
40 changes: 40 additions & 0 deletions Economics.NPC/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# Economics.NPC 插件 自定义怪物奖励

- 作者: 少司命
- 出处: 无
- 配置 NPC 专属奖励

## 更新日志

```
```

## 指令


## 配置

```json
{
"开启提示": true,
"提示内容": "你因击杀{0},获得额外奖励{1}{2}个",
"额外奖励列表": [
{
"怪物ID": 390,
"怪物名称": "猪鲨",
"奖励货币": 100000,
"按输出瓜分": true // false 时每个人发10000奖励
}
],
"转换率更改": {
"50": 1.3 //id 和 转换率
}
}
```

## 反馈

- 共同维护的插件库:https://github.com/Controllerdestiny/TShockPlugin
- 国内社区 trhub.cn 或 TShock 官方群等
10 changes: 10 additions & 0 deletions Plugin.sln
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "WeaponPlus", "WeaponPlusCos
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Economics.Projectile", "Economics.Projectile\Economics.Projectile.csproj", "{1F11D206-B4CE-430B-A8BD-6D7C93BC4C8F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Economics.NPC", "Economics.NPC\Economics.NPC.csproj", "{40542E99-97E7-4A2F-A7FA-4405CAAF0967}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -756,6 +758,14 @@ Global
{1F11D206-B4CE-430B-A8BD-6D7C93BC4C8F}.Release|Any CPU.Build.0 = Release|Any CPU
{1F11D206-B4CE-430B-A8BD-6D7C93BC4C8F}.Release|x64.ActiveCfg = Release|Any CPU
{1F11D206-B4CE-430B-A8BD-6D7C93BC4C8F}.Release|x64.Build.0 = Release|Any CPU
{40542E99-97E7-4A2F-A7FA-4405CAAF0967}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{40542E99-97E7-4A2F-A7FA-4405CAAF0967}.Debug|Any CPU.Build.0 = Debug|Any CPU
{40542E99-97E7-4A2F-A7FA-4405CAAF0967}.Debug|x64.ActiveCfg = Debug|Any CPU
{40542E99-97E7-4A2F-A7FA-4405CAAF0967}.Debug|x64.Build.0 = Debug|Any CPU
{40542E99-97E7-4A2F-A7FA-4405CAAF0967}.Release|Any CPU.ActiveCfg = Release|Any CPU
{40542E99-97E7-4A2F-A7FA-4405CAAF0967}.Release|Any CPU.Build.0 = Release|Any CPU
{40542E99-97E7-4A2F-A7FA-4405CAAF0967}.Release|x64.ActiveCfg = Release|Any CPU
{40542E99-97E7-4A2F-A7FA-4405CAAF0967}.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 @@ -59,6 +59,7 @@
| [Economics.Skill](Economics.Skill/README.md) | 技能插件(未完成) | EconomicsAPI<br>Economics.RPG |
| [Economics.Regain](Economics.Regain/README.md) | 物品回收 | EconomicsAPI |
| [Economics.Projectile](Economics.Projectile/README.md) | 自定义弹幕 | EconomicsAPI<br>Economics.RPG |
| [Economics.NPC](Economics.NPC/README.md) | 自定义怪物奖励 | EconomicsAPI |
| [CreateSpawn](CreateSpawn/README.md) | 出生点建筑生成 | 无 |
| [AutoBroadcast](AutoBroadcast/README.md) | 自动广播 | 无 |
| [AutoTeam](AutoTeam/README.md) | 自动队伍 | 无 |
Expand Down
Loading