Skip to content

Commit cedac5d

Browse files
authored
Merge pull request #340 from ACaiCat/master
修复: CaiRewardChest玩家快速堆叠刷物品
2 parents 4fe3adb + a07ccbb commit cedac5d

File tree

3 files changed

+145
-126
lines changed

3 files changed

+145
-126
lines changed

CaiRewardChest/Plugin.cs

Lines changed: 17 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
using System.Diagnostics;
2-
using System.Text;
1+
using On.OTAPI;
32
using Terraria;
43
using TerrariaApi.Server;
54
using TShockAPI;
6-
using Utils = TShockAPI.Utils;
75

86
namespace CaiRewardChest;
97

@@ -28,13 +26,23 @@ public override void Initialize()
2826
{
2927
Db.Init();
3028
GetDataHandlers.ChestOpen.Register(OnChestOpen);
29+
Hooks.Chest.InvokeQuickStack += ChestOnInvokeQuickStack;
3130
Commands.ChatCommands.Add(new Command("CaiRewardChest.Admin", InitChest, "初始化奖励箱"));
3231
Commands.ChatCommands.Add(new Command("CaiRewardChest.Admin", ClearChest, "清空奖励箱"));
3332
Commands.ChatCommands.Add(new Command("CaiRewardChest.Admin", DeleteChest, "删除奖励箱"));
3433
Commands.ChatCommands.Add(new Command("CaiRewardChest.Admin", AddChest, "添加奖励箱"));
3534
Commands.ChatCommands.Add(new Command("CaiRewardChest.Admin", EditChest, "编辑奖励箱"));
3635
}
3736

37+
private bool ChestOnInvokeQuickStack(Hooks.Chest.orig_InvokeQuickStack orig, int playerid, Item item,
38+
int chestindex)
39+
{
40+
RewardChest? chest = Db.GetChestById(chestindex);
41+
if (chest != null)
42+
return false;
43+
return true;
44+
}
45+
3846
private void EditChest(CommandArgs args)
3947
{
4048
args.Player.SendInfoMessage("[i:48]请打开需要编辑的奖励箱~");
@@ -115,7 +123,7 @@ private void OnChestOpen(object? sender, GetDataHandlers.ChestOpenEventArgs e)
115123
return;
116124
}
117125

118-
GiveItem(chest, e);
126+
Utils.GiveItem(chest, e);
119127
}
120128

121129
catch (Exception ex)
@@ -124,46 +132,6 @@ private void OnChestOpen(object? sender, GetDataHandlers.ChestOpenEventArgs e)
124132
}
125133
}
126134

127-
public static int InventorySlotAvailableCount(TSPlayer plr)
128-
{
129-
int num = 0;
130-
if (plr.RealPlayer)
131-
for (int index = 0; index < 50; ++index)
132-
if (plr.TPlayer.inventory[index] == null || !plr.TPlayer.inventory[index].active ||
133-
plr.TPlayer.inventory[index].Name == "")
134-
++num;
135-
return num;
136-
}
137-
138-
public void GiveItem(RewardChest chest, GetDataHandlers.ChestOpenEventArgs args)
139-
{
140-
int chestType = WorldGen.GetChestItemDrop(chest.X, chest.Y, Main.tile[chest.X, chest.Y].type);
141-
if (InventorySlotAvailableCount(args.Player) >=
142-
chest.Chest.item.Count(i => i != null && i.netID != 0 && i.stack != 0) + 1)
143-
{
144-
foreach (Item? i in chest.Chest.item) args.Player.GiveItem(i.netID, i.stack, i.prefix);
145-
List<string> itemsReceived = chest.Chest.item
146-
.Where(i => i != null && i.netID != 0 && i.stack != 0)
147-
.Select(i => TShock.Utils.ItemTag(i)).ToList();
148-
149-
150-
itemsReceived.Add(TShock.Utils.ItemTag(new Item()
151-
{
152-
netID = chestType,
153-
stack = 1
154-
}));
155-
args.Player.GiveItem(chestType, 1, 0);
156-
args.Player.SendSuccessMessage($"[i:{chestType}]你打开了一个奖励箱: " +
157-
$"" + string.Join(", ", itemsReceived));
158-
chest.HasOpenPlayer.Add(args.Player.Account.ID);
159-
Db.UpdateChest(chest);
160-
}
161-
else
162-
{
163-
args.Player.SendWarningMessage($"[i:{chestType}]你的背包格子不够哦," +
164-
$"还需要清空{chest.Chest.item.Count(i => i != null && i.netID != 0 && i.stack != 0) + 1 - InventorySlotAvailableCount(args.Player)}个格子!");
165-
}
166-
}
167135

168136
private void ClearChest(CommandArgs args)
169137
{
@@ -188,8 +156,11 @@ private void InitChest(CommandArgs args)
188156

189157
protected override void Dispose(bool disposing)
190158
{
191-
if (disposing) GetDataHandlers.ChestOpen.UnRegister(OnChestOpen);
192-
159+
if (disposing)
160+
{
161+
GetDataHandlers.ChestOpen.UnRegister(OnChestOpen);
162+
Hooks.Chest.InvokeQuickStack -= ChestOnInvokeQuickStack;
163+
}
193164
base.Dispose(disposing);
194165
}
195166
}

CaiRewardChest/Utils.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
using Terraria;
2+
using TShockAPI;
3+
4+
namespace CaiRewardChest;
5+
6+
public class Utils
7+
{
8+
public static int InventorySlotAvailableCount(TSPlayer plr)
9+
{
10+
int num = 0;
11+
if (plr.RealPlayer)
12+
for (int index = 0; index < 50; ++index)
13+
if (plr.TPlayer.inventory[index] == null || !plr.TPlayer.inventory[index].active ||
14+
plr.TPlayer.inventory[index].Name == "")
15+
++num;
16+
return num;
17+
}
18+
public static void GiveItem(RewardChest chest, GetDataHandlers.ChestOpenEventArgs args)
19+
{
20+
int chestType = WorldGen.GetChestItemDrop(chest.X, chest.Y, Main.tile[chest.X, chest.Y].type);
21+
if (InventorySlotAvailableCount(args.Player) >=
22+
chest.Chest.item.Count(i => i != null && i.netID != 0 && i.stack != 0) + 1)
23+
{
24+
foreach (Item? i in chest.Chest.item) args.Player.GiveItem(i.netID, i.stack, i.prefix);
25+
List<string> itemsReceived = chest.Chest.item
26+
.Where(i => i != null && i.netID != 0 && i.stack != 0)
27+
.Select(i => TShock.Utils.ItemTag(i)).ToList();
28+
29+
30+
itemsReceived.Add(TShock.Utils.ItemTag(new Item()
31+
{
32+
netID = chestType,
33+
stack = 1
34+
}));
35+
args.Player.GiveItem(chestType, 1, 0);
36+
args.Player.SendSuccessMessage($"[i:{chestType}]你打开了一个奖励箱: " +
37+
$"" + string.Join(", ", itemsReceived));
38+
chest.HasOpenPlayer.Add(args.Player.Account.ID);
39+
Db.UpdateChest(chest);
40+
}
41+
else
42+
{
43+
args.Player.SendWarningMessage($"[i:{chestType}]你的背包格子不够哦," +
44+
$"还需要清空{chest.Chest.item.Count(i => i != null && i.netID != 0 && i.stack != 0) + 1 - InventorySlotAvailableCount(args.Player)}个格子!");
45+
}
46+
}
47+
}

0 commit comments

Comments
 (0)