Skip to content

Commit

Permalink
Merge pull request #319 from ACaiCat/master
Browse files Browse the repository at this point in the history
添加插件: Cai奖励箱
  • Loading branch information
ACaiCat authored Jul 17, 2024
2 parents 67e489e + 0f952f6 commit 0031c6c
Show file tree
Hide file tree
Showing 6 changed files with 334 additions and 0 deletions.
5 changes: 5 additions & 0 deletions CaiRewardChest/CaiRewardChest.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>
12 changes: 12 additions & 0 deletions CaiRewardChest/Chest.cs
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];
}
75 changes: 75 additions & 0 deletions CaiRewardChest/Db.cs
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;");
}
}
195 changes: 195 additions & 0 deletions CaiRewardChest/Plugin.cs
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);
}
}
38 changes: 38 additions & 0 deletions CaiRewardChest/README.md
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
9 changes: 9 additions & 0 deletions Plugin.sln
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ZHIPlayerManager", "ZHIPlay
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SpawnInfra", "SpawnInfra\SpawnInfra.csproj", "{723DB1E5-AB0B-495F-AD1C-D67142E41441}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "CaiRewardChest", "CaiRewardChest\CaiRewardChest.csproj", "{77E718D8-82FA-43E9-A396-04A5A9717051}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "CNPCShop", "CNPCShop\CNPCShop.csproj", "{8A6FA1CC-8299-41D0-A060-676D26594215}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "SessionSentinel", "SessionSentinel\SessionSentinel.csproj", "{F863132D-5ED4-4C74-A752-342D67CEF597}"
Expand Down Expand Up @@ -1020,6 +1021,14 @@ Global
{723DB1E5-AB0B-495F-AD1C-D67142E41441}.Release|Any CPU.Build.0 = Release|Any CPU
{723DB1E5-AB0B-495F-AD1C-D67142E41441}.Release|x64.ActiveCfg = Release|Any CPU
{723DB1E5-AB0B-495F-AD1C-D67142E41441}.Release|x64.Build.0 = Release|Any CPU
{77E718D8-82FA-43E9-A396-04A5A9717051}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{77E718D8-82FA-43E9-A396-04A5A9717051}.Debug|Any CPU.Build.0 = Debug|Any CPU
{77E718D8-82FA-43E9-A396-04A5A9717051}.Debug|x64.ActiveCfg = Debug|Any CPU
{77E718D8-82FA-43E9-A396-04A5A9717051}.Debug|x64.Build.0 = Debug|Any CPU
{77E718D8-82FA-43E9-A396-04A5A9717051}.Release|Any CPU.ActiveCfg = Release|Any CPU
{77E718D8-82FA-43E9-A396-04A5A9717051}.Release|Any CPU.Build.0 = Release|Any CPU
{77E718D8-82FA-43E9-A396-04A5A9717051}.Release|x64.ActiveCfg = Release|Any CPU
{77E718D8-82FA-43E9-A396-04A5A9717051}.Release|x64.Build.0 = Release|Any CPU
{8A6FA1CC-8299-41D0-A060-676D26594215}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{8A6FA1CC-8299-41D0-A060-676D26594215}.Debug|Any CPU.Build.0 = Debug|Any CPU
{8A6FA1CC-8299-41D0-A060-676D26594215}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down

0 comments on commit 0031c6c

Please sign in to comment.