-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #167 from THEXN/master
添加插件:EndureBoost 物品一定数量后长时间buff
- Loading branch information
Showing
6 changed files
with
314 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<Import Project="..\template.targets" /> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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官方群等 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters