-
Notifications
You must be signed in to change notification settings - Fork 11
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 #319 from ACaiCat/master
添加插件: Cai奖励箱
- Loading branch information
Showing
6 changed files
with
334 additions
and
0 deletions.
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,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,12 @@ | ||
using Terraria; | ||
|
||
namespace CaiRewardChest; | ||
|
||
public class RewardChest | ||
{ | ||
public int ChestId; | ||
public List<int> HasOpenPlayer = new(); | ||
public int X => Main.chest[ChestId].x; | ||
public int Y => Main.chest[ChestId].y; | ||
public Chest Chest => Main.chest[ChestId]; | ||
} |
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,75 @@ | ||
using System.Data; | ||
using MySql.Data.MySqlClient; | ||
using Terraria; | ||
using TShockAPI; | ||
using TShockAPI.DB; | ||
|
||
namespace CaiRewardChest; | ||
|
||
public static class Db | ||
{ | ||
public static IDbConnection DbConnection => TShock.DB; | ||
|
||
public static void Init() | ||
{ | ||
SqlTableCreator sqlTableCreator = new(DbConnection, | ||
DbConnection.GetSqlType() == SqlType.Sqlite ? new SqliteQueryCreator() : new MysqlQueryCreator()); | ||
|
||
sqlTableCreator.EnsureTableStructure(new SqlTable("CaiRewardChest", | ||
new SqlColumn("ChestID", MySqlDbType.Int32) { Length = 8 }, | ||
new SqlColumn("X", MySqlDbType.Int32) { Length = 255 }, | ||
new SqlColumn("Y", MySqlDbType.Int32) { Length = 255 }, | ||
new SqlColumn("HasOpened", MySqlDbType.String) { Length = 25555 }) | ||
); | ||
} | ||
|
||
|
||
public static RewardChest? GetChestByPos(int x, int y) | ||
{ | ||
int chestId = Chest.FindChest(x, y); | ||
return GetChestById(chestId); | ||
} | ||
|
||
public static RewardChest? GetChestById(int chestId) | ||
{ | ||
using QueryResult result = DbConnection.QueryReader("SELECT * FROM CaiRewardChest WHERE ChestID=@0;", chestId); | ||
if (result.Read()) | ||
{ | ||
RewardChest? chest = new() | ||
{ | ||
ChestId = chestId, | ||
HasOpenPlayer = result.Get<string>("HasOpened") | ||
.Split(',') | ||
.Where(s => !string.IsNullOrEmpty(s)) | ||
.Select(int.Parse) | ||
.ToList() | ||
}; | ||
return chest; | ||
} | ||
|
||
return null; | ||
} | ||
|
||
|
||
public static void AddChest(int chestId, int x, int y) | ||
{ | ||
DbConnection.Query("INSERT INTO CaiRewardChest (ChestID, X, Y, HasOpened) VALUES (@0, @1, @2,@3);", chestId, x, | ||
y, ""); | ||
} | ||
|
||
public static void UpdateChest(RewardChest chest) | ||
{ | ||
DbConnection.Query("UPDATE CaiRewardChest SET HasOpened=@0 WHERE ChestID=@1", | ||
string.Join(',', chest.HasOpenPlayer), chest.ChestId); | ||
} | ||
|
||
public static void DelChest(int chestId) | ||
{ | ||
DbConnection.Query("DELETE FROM CaiRewardChest WHERE ChestID=@0;", chestId); | ||
} | ||
|
||
public static void ClearDb() | ||
{ | ||
DbConnection.Query("DELETE FROM CaiRewardChest;"); | ||
} | ||
} |
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,195 @@ | ||
using System.Diagnostics; | ||
using System.Text; | ||
using Terraria; | ||
using TerrariaApi.Server; | ||
using TShockAPI; | ||
using Utils = TShockAPI.Utils; | ||
|
||
namespace CaiRewardChest; | ||
|
||
[ApiVersion(2, 1)] | ||
public class CaiRewardChest : TerrariaPlugin | ||
{ | ||
public override string Author => "Cai"; | ||
|
||
public override string Description => "奖励箱!!"; | ||
|
||
public override string Name => "CaiRewardChest"; | ||
|
||
public override Version Version => new(1, 0, 0, 0); | ||
|
||
public CaiRewardChest(Main game) | ||
: base(game) | ||
{ | ||
} | ||
|
||
|
||
public override void Initialize() | ||
{ | ||
Db.Init(); | ||
GetDataHandlers.ChestOpen.Register(OnChestOpen); | ||
Commands.ChatCommands.Add(new Command("CaiRewardChest.Admin", InitChest, "初始化奖励箱")); | ||
Commands.ChatCommands.Add(new Command("CaiRewardChest.Admin", ClearChest, "清空奖励箱")); | ||
Commands.ChatCommands.Add(new Command("CaiRewardChest.Admin", DeleteChest, "删除奖励箱")); | ||
Commands.ChatCommands.Add(new Command("CaiRewardChest.Admin", AddChest, "添加奖励箱")); | ||
Commands.ChatCommands.Add(new Command("CaiRewardChest.Admin", EditChest, "编辑奖励箱")); | ||
} | ||
|
||
private void EditChest(CommandArgs args) | ||
{ | ||
args.Player.SendInfoMessage("[i:48]请打开需要编辑的奖励箱~"); | ||
args.Player.SetData("WaitChestSetting", "Edit"); | ||
} | ||
|
||
private void AddChest(CommandArgs args) | ||
{ | ||
args.Player.SendInfoMessage("[i:48]请打开需要添加的奖励箱~"); | ||
args.Player.SetData("WaitChestSetting", "Add"); | ||
} | ||
|
||
private void DeleteChest(CommandArgs args) | ||
{ | ||
args.Player.SendInfoMessage("[i:48]请打开需要删除的奖励箱~"); | ||
args.Player.SetData("WaitChestSetting", "Del"); | ||
} | ||
|
||
|
||
private void OnChestOpen(object? sender, GetDataHandlers.ChestOpenEventArgs e) | ||
{ | ||
try | ||
{ | ||
RewardChest? chest = Db.GetChestByPos(e.X, e.Y); | ||
|
||
if (e.Player.ContainsData("WaitChestSetting")) | ||
{ | ||
if (e.Player.GetData<string>("WaitChestSetting") == "Del") | ||
{ | ||
e.Player.RemoveData("WaitChestSetting"); | ||
if (chest == null) | ||
{ | ||
e.Player.SendWarningMessage("[i:48]这个箱子好像不是奖励箱捏~"); | ||
e.Handled = true; | ||
return; | ||
} | ||
|
||
Db.DelChest(chest.ChestId); | ||
e.Player.SendSuccessMessage("[i:48]你删除了这个奖励箱~"); | ||
e.Handled = true; | ||
return; | ||
} | ||
|
||
if (e.Player.GetData<string>("WaitChestSetting") == "Add") | ||
{ | ||
e.Player.RemoveData("WaitChestSetting"); | ||
if (chest != null) | ||
{ | ||
e.Player.SendWarningMessage("[i:48]这个箱子好像是奖励箱捏~"); | ||
e.Handled = true; | ||
return; | ||
} | ||
|
||
Db.AddChest(Chest.FindChest(e.X, e.Y), e.X, e.Y); | ||
e.Player.SendSuccessMessage("[i:48]你添加了一个奖励箱~"); | ||
e.Handled = true; | ||
return; | ||
} | ||
|
||
if (e.Player.GetData<string>("WaitChestSetting") == "Edit") | ||
{ | ||
e.Player.RemoveData("WaitChestSetting"); | ||
if (chest != null) return; | ||
e.Player.SendWarningMessage("[i:48]这个箱子好像不是奖励箱捏~"); | ||
e.Handled = true; | ||
return; | ||
} | ||
} | ||
|
||
|
||
if (chest == null) return; | ||
e.Handled = true; | ||
|
||
if (chest.HasOpenPlayer.Contains(e.Player.Account.ID)) | ||
{ | ||
e.Player.SendWarningMessage( | ||
$"[i:{WorldGen.GetChestItemDrop(chest.X, chest.Y, Main.tile[chest.X, chest.Y].type)}]你已经领取过这个奖励箱啦!"); | ||
return; | ||
} | ||
|
||
GiveItem(chest, e); | ||
} | ||
|
||
catch (Exception ex) | ||
{ | ||
Console.WriteLine(ex); | ||
} | ||
} | ||
|
||
public static int InventorySlotAvailableCount(TSPlayer plr) | ||
{ | ||
int num = 0; | ||
if (plr.RealPlayer) | ||
for (int index = 0; index < 50; ++index) | ||
if (plr.TPlayer.inventory[index] == null || !plr.TPlayer.inventory[index].active || | ||
plr.TPlayer.inventory[index].Name == "") | ||
++num; | ||
return num; | ||
} | ||
|
||
public void GiveItem(RewardChest chest, GetDataHandlers.ChestOpenEventArgs args) | ||
{ | ||
int chestType = WorldGen.GetChestItemDrop(chest.X, chest.Y, Main.tile[chest.X, chest.Y].type); | ||
if (InventorySlotAvailableCount(args.Player) >= | ||
chest.Chest.item.Count(i => i != null && i.netID != 0 && i.stack != 0) + 1) | ||
{ | ||
foreach (Item? i in chest.Chest.item) args.Player.GiveItem(i.netID, i.stack, i.prefix); | ||
List<string> itemsReceived = chest.Chest.item | ||
.Where(i => i != null && i.netID != 0 && i.stack != 0) | ||
.Select(i => TShock.Utils.ItemTag(i)).ToList(); | ||
|
||
|
||
itemsReceived.Add(TShock.Utils.ItemTag(new Item() | ||
{ | ||
netID = chestType, | ||
stack = 1 | ||
})); | ||
args.Player.GiveItem(chestType, 1, 0); | ||
args.Player.SendSuccessMessage($"[i:{chestType}]你打开了一个奖励箱: " + | ||
$"" + string.Join(", ", itemsReceived)); | ||
chest.HasOpenPlayer.Add(args.Player.Account.ID); | ||
Db.UpdateChest(chest); | ||
} | ||
else | ||
{ | ||
args.Player.SendWarningMessage($"[i:{chestType}]你的背包格子不够哦," + | ||
$"还需要清空{chest.Chest.item.Count(i => i != null && i.netID != 0 && i.stack != 0) + 1 - InventorySlotAvailableCount(args.Player)}个格子!"); | ||
} | ||
} | ||
|
||
private void ClearChest(CommandArgs args) | ||
{ | ||
Db.ClearDb(); | ||
args.Player.SendSuccessMessage("[i:48]奖励箱已全部清除~"); | ||
} | ||
|
||
private void InitChest(CommandArgs args) | ||
{ | ||
Db.ClearDb(); | ||
int count = 0; | ||
foreach (Chest? chest in Main.chest) | ||
if (chest != null) | ||
{ | ||
Db.AddChest(Chest.FindChest(chest.x, chest.y), chest.x, chest.y); | ||
count++; | ||
} | ||
|
||
args.Player.SendSuccessMessage($"[i:48]奖励箱初始化完成,共添加{count}个奖励箱~"); | ||
} | ||
|
||
|
||
protected override void Dispose(bool disposing) | ||
{ | ||
if (disposing) GetDataHandlers.ChestOpen.UnRegister(OnChestOpen); | ||
|
||
base.Dispose(disposing); | ||
} | ||
} |
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,38 @@ | ||
# CaiRewardChest 奖励箱 | ||
|
||
- 作者: Cai | ||
- 出处: 本仓库 | ||
- 还在为服务器箱子只能领取一次而烦恼吗,试试Cai的奖励箱. | ||
- 新建地图后使用`/初始化奖励箱`可以把全地图的箱子变成每个玩家只能领取一次的奖励箱, | ||
- 而且只要点一下就能领取,并且会给予玩家对应的箱子凋落物 | ||
- 当然,你也可以使用`/添加奖励箱`和`/编辑奖励箱`设置属于自己的奖励箱 | ||
|
||
> 注意: 请在生成地图后使用`/初始化奖励箱`,否则可能会误将玩家的箱子标记 | ||
## 更新日志 | ||
|
||
``` | ||
1.0.0 | ||
添加插件 | ||
``` | ||
|
||
## 指令 | ||
|
||
| 语法 | 权限 | 说明 | | ||
|---------|:--------------------:|:------------------:| | ||
| /初始化奖励箱 | CaiRewardChest.Admin | 将全图箱子设为奖励箱(仅刚开服适用) | | ||
| /添加奖励箱 | CaiRewardChest.Admin | 添加一个奖励箱 | | ||
| /编辑奖励箱 | CaiRewardChest.Admin | 编辑一个奖励箱 | | ||
| /删除奖励箱 | CaiRewardChest.Admin | 删除一个奖励箱 | | ||
|
||
## 配置 | ||
|
||
```json | ||
无 | ||
``` | ||
|
||
## 反馈 | ||
|
||
- 优先发issued -> 共同维护的插件库:https://github.com/Controllerdestiny/TShockPlugin | ||
- 次优先:TShock官方群:816771079 | ||
- 大概率看不到但是也可以:国内社区trhub.cn ,bbstr.net , tr.monika.love |
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