diff --git a/EndureBoost/Configuration.cs b/EndureBoost/Configuration.cs new file mode 100644 index 000000000..798f4fe10 --- /dev/null +++ b/EndureBoost/Configuration.cs @@ -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 Potions { get; set; } = new List(); + [JsonProperty("其他物品")] + public List Stations { get; set; } = new List(); + + 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(sr.ReadToEnd()) ?? new(); + } +} diff --git a/EndureBoost/EndureBoost.cs b/EndureBoost/EndureBoost.cs new file mode 100644 index 000000000..ca0634e4a --- /dev/null +++ b/EndureBoost/EndureBoost.cs @@ -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; + } + } +} diff --git a/EndureBoost/EndureBoost.csproj b/EndureBoost/EndureBoost.csproj new file mode 100644 index 000000000..639adc026 --- /dev/null +++ b/EndureBoost/EndureBoost.csproj @@ -0,0 +1,5 @@ + + + + + \ No newline at end of file diff --git a/EndureBoost/README.md b/EndureBoost/README.md new file mode 100644 index 000000000..88ac7d0a3 --- /dev/null +++ b/EndureBoost/README.md @@ -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官方群等 \ No newline at end of file diff --git a/Plugin.sln b/Plugin.sln index d4dcc0c79..59daef409 100644 --- a/Plugin.sln +++ b/Plugin.sln @@ -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 @@ -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 diff --git a/README.md b/README.md index f1a9798dc..7cc83b785 100644 --- a/README.md +++ b/README.md @@ -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
Economics.RPG | | [Economics.Skill](Economics.Skill/README.md) | 技能插件 | EconomicsAPI
Economics.RPG | @@ -126,6 +126,7 @@ | [SignInSign](SignInSign/README.md) | 告示牌登录插件 | 无 | | [WeaponPlusCostCoin](WeaponPlusCostCoin/README.md) | 武器强化钱币版 | 无 | | [Respawn](Respawn/README.md) | 原地复活 | 无 | +| [EndureBoost](EndureBoost/README.md) | 物品一定数量后长时间buff | 无 |