Skip to content

Commit a7a4ea2

Browse files
SignatureBeefsors89
andcommitted
Expand boss bag hook to DropItemLocalPerClientAndSetNPCMoneyTo0
see fix drop boss bag hook not working properly #131 Co-Authored-By: Sors <[email protected]>
1 parent 2d636c6 commit a7a4ea2

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

OTAPI.Patcher/Targets/PCServerTarget.cs

+1
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ public async Task PatchAsync()
211211
mm.Module.GetType("Terraria.NPC").CreateHooks(mm);
212212
mm.Module.GetType("Terraria.WorldGen").CreateHooks(mm);
213213
mm.Module.GetType("Terraria.Chat.ChatHelper").CreateHooks(mm);
214+
mm.Module.GetType("Terraria.GameContent.ItemDropRules.CommonCode").CreateHooks(mm);
214215
mm.Module.GetType("Terraria.IO.WorldFile").CreateHooks(mm);
215216
mm.Module.GetType("Terraria.Net.NetManager").CreateHooks(mm);
216217
mm.Module.GetType("Terraria.Projectile").CreateHooks(mm);

OTAPI.Scripts/Mods/HookNpcBossBag.Server.cs

+34-28
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
Copyright (C) 2020 DeathCradle
2+
Copyright (C) 2020-2024 SignatureBeef & sors89
33
44
This 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
2626
using 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]
3333
void 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

7177
namespace OTAPI
@@ -142,4 +148,4 @@ Terraria.NPC npc
142148
}
143149
}
144150
}
145-
}
151+
}

0 commit comments

Comments
 (0)