1- using System . Diagnostics ;
2- using System . Text ;
1+ using On . OTAPI ;
32using Terraria ;
43using TerrariaApi . Server ;
54using TShockAPI ;
6- using Utils = TShockAPI . Utils ;
75
86namespace 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}
0 commit comments