Skip to content

Commit

Permalink
Merge pull request #167 from THEXN/master
Browse files Browse the repository at this point in the history
添加插件:EndureBoost 物品一定数量后长时间buff
  • Loading branch information
Controllerdestiny authored Jun 7, 2024
2 parents cb46f3e + 1ddcb41 commit 9be5343
Show file tree
Hide file tree
Showing 6 changed files with 314 additions and 1 deletion.
60 changes: 60 additions & 0 deletions EndureBoost/Configuration.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using Newtonsoft.Json;
using TShockAPI;

public class Configuration
{
public static readonly string FilePath = Path.Combine(TShock.SavePath, "EndureBoost.json");
[JsonProperty("猪猪储钱罐")]
public bool bank = true;
[JsonProperty("保险箱")]
public bool bank2 = true;
[JsonProperty("护卫熔炉")]
public bool bank3 = false;
[JsonProperty("虚空宝藏袋")]
public bool bank4 = false;
[JsonProperty("持续时间(s)")]
public int duration = 3600;

public class Potion
{
[JsonProperty("药水id")]
public int[] ItemID { get; set; }
[JsonProperty("药水数量")]
public int RequiredStack { get; set; }
}

public class Station
{
[JsonProperty("物品id")]
public int[] Type { get; set; }
[JsonProperty("物品数量")]
public int RequiredStack { get; set; }
[JsonProperty("给buff的id")]
public int BuffType { get; set; }
}
[JsonProperty("药水")]
public List<Potion> Potions { get; set; } = new List<Potion>();
[JsonProperty("其他物品")]
public List<Station> Stations { get; set; } = new List<Station>();

public void Write(string path)
{
using (var fs = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Write))
{
var str = JsonConvert.SerializeObject(this, Formatting.Indented);
using (var sw = new StreamWriter(fs))
{
sw.Write(str);
}
}
}

public static Configuration Read(string path)
{
if (!File.Exists(path))
return new Configuration();
using var fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read);
using var sr = new StreamReader(fs);
return JsonConvert.DeserializeObject<Configuration>(sr.ReadToEnd()) ?? new();
}
}
172 changes: 172 additions & 0 deletions EndureBoost/EndureBoost.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
using System;
using System.IO;
using Terraria;
using TerrariaApi.Server;
using TShockAPI;
using TShockAPI.Hooks;

namespace Plugin
{
[ApiVersion(2, 1)]
public class EndureBoost : TerrariaPlugin
{
public static Configuration Config;

public override string Author => "肝帝熙恩";

public override string Description => "一定数量后长时间buff";

public override string Name => "EndureBoost";

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

public EndureBoost(Main game) : base(game)
{
LoadConfig();
}

public override void Initialize()
{
GeneralHooks.ReloadEvent += ReloadConfig;
ServerApi.Hooks.ServerJoin.Register(this, OnServerJoin);
GetDataHandlers.PlayerSpawn += Rebirth;
Commands.ChatCommands.Add(new Command("EndureBoost", SetPlayerBuffcmd, "ebbuff","ldbuff","loadbuff"));
}

private void SetPlayerBuffcmd(CommandArgs args)
{
TSPlayer player = args.Player;
SetPlayerBuff(player);
}

private static void LoadConfig()
{
Config = Configuration.Read(Configuration.FilePath);
Config.Write(Configuration.FilePath);
}

private static void ReloadConfig(ReloadEventArgs args)
{
LoadConfig();
args.Player.SendSuccessMessage("[{0}] 重新加载配置完毕。", typeof(EndureBoost).Name);
}

protected override void Dispose(bool disposing)
{
if (disposing)
{
Commands.ChatCommands.RemoveAll(x => x.CommandDelegate == SetPlayerBuffcmd);
GeneralHooks.ReloadEvent -= ReloadConfig;
ServerApi.Hooks.ServerJoin.Deregister(this, OnServerJoin);
GetDataHandlers.PlayerSpawn -= Rebirth;
}
base.Dispose(disposing);
}

private void OnServerJoin(JoinEventArgs args)
{
TSPlayer playerBuff = TShock.Players[args.Who];
if (playerBuff == null)
{
return;
}
SetPlayerBuff(playerBuff);
}

private void Rebirth(object o, GetDataHandlers.SpawnEventArgs args)
{
TSPlayer player = args.Player;
SetPlayerBuff(player);
}

private void SetPlayerBuff(TSPlayer player)
{

// 处理 potions
foreach (var potion in Config.Potions)
{
foreach (var itemId in potion.ItemID)
{
int itemCount = 0;

// 检查背包中的物品
for (int i = 0; i < 58; i++)
{
if (player.TPlayer.inventory[i].type == itemId)
{
itemCount += player.TPlayer.inventory[i].stack;
}
}

// 检查不同存储区中的物品
CheckBanksForItem(player, itemId, ref itemCount);

if (itemCount >= potion.RequiredStack)
{
int buffType = GetBuffIDByItemID(itemId); // 获取物品的 buff 类型
if (buffType != 0)
{
player.SetBuff(buffType, Config.duration*60);
}
}
}
}

// 处理 stations
foreach (var station in Config.Stations)
{
foreach (var itemId in station.Type)
{
int itemCount = 0;

// 检查背包中的物品
for (int i = 0; i < 58; i++)
{
if (player.TPlayer.inventory[i].type == itemId)
{
itemCount += player.TPlayer.inventory[i].stack;
}
}

// 检查不同存储区中的物品
CheckBanksForItem(player, itemId, ref itemCount);

if (itemCount >= station.RequiredStack)
{
player.SetBuff(station.BuffType, Config.duration*60);
}
}
}
}

private void CheckBanksForItem(TSPlayer player, int itemId, ref int itemCount)
{
for (int j = 0; j < 40; j++)
{
if (player.TPlayer.bank.item[j].type == itemId && Config.bank)// 检查猪猪储钱罐
{
itemCount += player.TPlayer.bank.item[j].stack;
}
if (player.TPlayer.bank2.item[j].type == itemId && Config.bank2)// 检查保险箱
{
itemCount += player.TPlayer.bank2.item[j].stack;
}
if (player.TPlayer.bank3.item[j].type == itemId && Config.bank3)// 检查护卫熔炉
{
itemCount += player.TPlayer.bank3.item[j].stack;
}
if (player.TPlayer.bank4.item[j].type == itemId && Config.bank4)// 检查虚空宝藏袋
{
itemCount += player.TPlayer.bank4.item[j].stack;
}
}
}

private int GetBuffIDByItemID(int itemId)
{
Item item = new Item();
item.SetDefaults(itemId);
return item.buffType;
}
}
}
5 changes: 5 additions & 0 deletions EndureBoost/EndureBoost.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>
65 changes: 65 additions & 0 deletions EndureBoost/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
# EndureBoost 物品一定数量后长时间buff

- 作者: 肝帝熙恩
- 出处: 无
- 当玩家背包某些物品到达一定数量后,给与指定buff

## 更新日志

```
暂无
```

## 指令

| 语法 | 权限 | 说明 |
| -------------- | :-----------------: | :------: |
| /ebbuff,/ldbuff,/loadbuff" | 立即刷新长时间buff状态|

## 配置

```json
{
"猪猪储钱罐": false,// 示例
"保险箱": false,
"护卫熔炉": false,
"虚空宝藏袋": true,
"持续时间(s)": 3600,
"药水": [
{
"药水id": [
288,
289
],//可以是一个数组,也就是里面可以放单个或者多个物品id
"药水数量": 30
},
{
"药水id": [
290
],
"药水数量": 200
}
],
"其他物品": [
{
"物品id": [
2,
3
],
"物品数量": 3,
"给buff的id": 87
},
{
"物品id": [
5
],
"物品数量": 3,
"给buff的id": 89
}
]
}
```

## 反馈
- 共同维护的插件库: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 @@ -166,6 +166,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Economics.WeaponPlus", "Eco
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Respawn", "Respawn\Respawn.csproj", "{E4792205-8095-4333-AFE7-79B64CD02935}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EndureBoost", "EndureBoost\EndureBoost.csproj", "{B7ED7F50-82BB-4865-B338-041871A33E42}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -806,6 +808,14 @@ Global
{E4792205-8095-4333-AFE7-79B64CD02935}.Release|Any CPU.Build.0 = Release|Any CPU
{E4792205-8095-4333-AFE7-79B64CD02935}.Release|x64.ActiveCfg = Release|Any CPU
{E4792205-8095-4333-AFE7-79B64CD02935}.Release|x64.Build.0 = Release|Any CPU
{B7ED7F50-82BB-4865-B338-041871A33E42}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{B7ED7F50-82BB-4865-B338-041871A33E42}.Debug|Any CPU.Build.0 = Debug|Any CPU
{B7ED7F50-82BB-4865-B338-041871A33E42}.Debug|x64.ActiveCfg = Debug|Any CPU
{B7ED7F50-82BB-4865-B338-041871A33E42}.Debug|x64.Build.0 = Debug|Any CPU
{B7ED7F50-82BB-4865-B338-041871A33E42}.Release|Any CPU.ActiveCfg = Release|Any CPU
{B7ED7F50-82BB-4865-B338-041871A33E42}.Release|Any CPU.Build.0 = Release|Any CPU
{B7ED7F50-82BB-4865-B338-041871A33E42}.Release|x64.ActiveCfg = Release|Any CPU
{B7ED7F50-82BB-4865-B338-041871A33E42}.Release|x64.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
| [ChattyBridge](ChattyBridge/README.md) | 用于跨服聊天 ||
| [EconomicsAPI](EconomicsAPI/README.md) | 经济插件前置 ||
| [Economics.RPG](Economics.RPG/README.md) | RPG | EconomicsAPI |
| [Economics.WeaponPlus](Economics.WeaponPlus/README.md) | 强化武器 | EconomicsAPI |
| [Economics.WeaponPlus](Economics.WeaponPlus/README.md) | 强化武器 | EconomicsAPI |
| [Economics.Deal](Economics.RPG/README.md) | 交易插件 | EconomicsAPI |
| [Economics.Shop](Economics.Shop/README.md) | 商店插件 | EconomicsAPI<br>Economics.RPG |
| [Economics.Skill](Economics.Skill/README.md) | 技能插件 | EconomicsAPI<br>Economics.RPG |
Expand Down Expand Up @@ -126,6 +126,7 @@
| [SignInSign](SignInSign/README.md) | 告示牌登录插件 ||
| [WeaponPlusCostCoin](WeaponPlusCostCoin/README.md) | 武器强化钱币版 ||
| [Respawn](Respawn/README.md) | 原地复活 ||
| [EndureBoost](EndureBoost/README.md) | 物品一定数量后长时间buff ||

</Details>

Expand Down

0 comments on commit 9be5343

Please sign in to comment.