1
1
/*
2
- Copyright (C) 2020 DeathCradle
2
+ Copyright (C) 2020-2024 SignatureBeef & sors89
3
3
4
4
This file is part of Open Terraria API v3 (OTAPI)
5
5
@@ -26,46 +26,52 @@ You should have received a copy of the GNU General Public License
26
26
using Mono . Cecil . Cil ;
27
27
28
28
/// <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 .
30
30
/// </summary>
31
- [ Modification ( ModType . PreMerge , "Hooking npc boss bags " ) ]
31
+ [ Modification ( ModType . PreMerge , "Hooking NPC Boss Bag drops " ) ]
32
32
[ MonoMod . MonoModIgnore ]
33
33
void HookNpcBossBag ( ModFramework . ModFwModder modder )
34
34
{
35
35
// 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 (
39
42
#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 ) )
41
44
#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 ) )
43
46
#endif
44
- ) ;
47
+ ) ;
45
48
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 ) ;
49
52
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." ) ;
51
54
52
- var ins = instructions . First ( ) ;
55
+ var ins = instructions . First ( ) ;
53
56
54
- ins . Operand = callback ;
57
+ ins . Operand = callback ;
55
58
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
+ ) ;
60
65
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
+ }
69
75
}
70
76
71
77
namespace OTAPI
@@ -142,4 +148,4 @@ Terraria.NPC npc
142
148
}
143
149
}
144
150
}
145
- }
151
+ }
0 commit comments