11/*
2- Copyright (C) 2020 DeathCradle
2+ Copyright (C) 2020-2024 SignatureBeef & sors89
33
44This file is part of Open Terraria API v3 (OTAPI)
55
@@ -26,46 +26,52 @@ You should have received a copy of the GNU General Public License
2626using Mono . Cecil . Cil ;
2727
2828/// <summary>
29- /// @doc Creates Hooks.NPC.BossBag. Allows plugins to cancel boss bag items .
29+ /// @doc Creates Hooks.NPC.BossBag. Allows plugins to customize boss loot as well as the distribution .
3030/// </summary>
31- [ Modification ( ModType . PreMerge , "Hooking npc boss bags " ) ]
31+ [ Modification ( ModType . PreMerge , "Hooking NPC Boss Bag drops " ) ]
3232[ MonoMod . MonoModIgnore ]
3333void HookNpcBossBag ( ModFramework . ModFwModder modder )
3434{
3535 // replace NewItem calls, and handle the -1 result to cancel the method from actioning.
36-
37- var csr = modder . GetILCursor ( ( ) => ( new Terraria . NPC ( ) ) . DropItemInstanced ( default , default , 0 , 0 , false ) ) ;
38- var callback = csr . Module . ImportReference (
36+ foreach ( var csr in new [ ] {
37+ modder . GetILCursor ( ( ) => ( new Terraria . NPC ( ) ) . DropItemInstanced ( default , default , 0 , 0 , false ) ) ,
38+ modder . GetILCursor ( ( ) => Terraria . GameContent . ItemDropRules . CommonCode . DropItemLocalPerClientAndSetNPCMoneyTo0 ( default , default , default , default ) )
39+ } )
40+ {
41+ var callback = csr . Module . ImportReference (
3942#if TerrariaServer_EntitySourcesActive || Terraria_EntitySourcesActive || tModLoader_EntitySourcesActive
40- modder . GetMethodDefinition ( ( ) => OTAPI . Hooks . NPC . InvokeBossBag ( null , 0 , 0 , 0 , 0 , 0 , 0 , false , 0 , false , false , null ) )
43+ modder . GetMethodDefinition ( ( ) => OTAPI . Hooks . NPC . InvokeBossBag ( null , 0 , 0 , 0 , 0 , 0 , 0 , false , 0 , false , false , null ) )
4144#else
42- modder . GetMethodDefinition ( ( ) => OTAPI . Hooks . NPC . InvokeBossBag ( 0 , 0 , 0 , 0 , 0 , 0 , false , 0 , false , false , null ) )
45+ modder . GetMethodDefinition ( ( ) => OTAPI . Hooks . NPC . InvokeBossBag ( 0 , 0 , 0 , 0 , 0 , 0 , false , 0 , false , false , null ) )
4346#endif
44- ) ;
47+ ) ;
4548
46- var instructions = csr . Body . Instructions . Where ( x => x . OpCode == OpCodes . Call
47- && x . Operand is MethodReference mref && mref . Name == "NewItem"
48- && x . Next . OpCode == OpCodes . Stloc_0 ) ;
49+ var instructions = csr . Body . Instructions . Where ( x => x . OpCode == OpCodes . Call
50+ && x . Operand is MethodReference mref && mref . Name == "NewItem"
51+ && x . Next . OpCode == OpCodes . Stloc_0 ) ;
4952
50- if ( instructions . Count ( ) != 1 ) throw new NotSupportedException ( "Only one server NewItem call expected in DropBossBags." ) ;
53+ if ( instructions . Count ( ) != 1 ) throw new NotSupportedException ( "Only one server NewItem call expected in DropBossBags." ) ;
5154
52- var ins = instructions . First ( ) ;
55+ var ins = instructions . First ( ) ;
5356
54- ins . Operand = callback ;
57+ ins . Operand = callback ;
5558
56- csr . Goto ( ins ) ;
57- csr . EmitAll (
58- new { OpCodes . Ldarg_0 }
59- ) ;
59+ csr . Goto ( ins ) ;
60+ csr . EmitAll (
61+ // reference to the npc works for both the instance, and the static method, only since the latter has the
62+ // variable first in the argument list.
63+ new { OpCodes . Ldarg_0 }
64+ ) ;
6065
61- csr . Goto ( ins . Next . Next ) ;
62- csr . EmitAll (
63- new { OpCodes . Ldloc_0 } ,
64- new { OpCodes . Ldc_I4_M1 } ,
65- new { OpCodes . Ceq } ,
66- new { OpCodes . Brfalse_S , Operand = ins . Next . Next } ,
67- new { OpCodes . Ret }
68- ) ;
66+ csr . Goto ( ins . Next . Next ) ;
67+ csr . EmitAll (
68+ new { OpCodes . Ldloc_0 } ,
69+ new { OpCodes . Ldc_I4_M1 } ,
70+ new { OpCodes . Ceq } ,
71+ new { OpCodes . Brfalse_S , Operand = ins . Next . Next } ,
72+ new { OpCodes . Ret }
73+ ) ;
74+ }
6975}
7076
7177namespace OTAPI
@@ -142,4 +148,4 @@ Terraria.NPC npc
142148 }
143149 }
144150 }
145- }
151+ }
0 commit comments