Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix drop boss bag hook not working properly #131

Closed
wants to merge 10 commits into from
53 changes: 18 additions & 35 deletions OTAPI.Scripts/Mods/HookNpcBossBag.Server.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,44 +28,21 @@ You should have received a copy of the GNU General Public License
/// <summary>
/// @doc Creates Hooks.NPC.BossBag. Allows plugins to cancel boss bag items.
/// </summary>
[Modification(ModType.PreMerge, "Hooking npc boss bags")]

[Modification(ModType.PreMerge, "Hooking NPC Boss Bag")]
[MonoMod.MonoModIgnore]
void HookNpcBossBag(ModFramework.ModFwModder modder)
{
// replace NewItem calls, and handle the -1 result to cancel the method from actioning.
var csr = modder.GetILCursor(() => Terraria.GameContent.ItemDropRules.CommonCode.DropItemLocalPerClientAndSetNPCMoneyTo0(default, default, default, true));
csr.GotoNext(i => i.OpCode == OpCodes.Call && i.Operand is MethodReference mref && mref.Name == "NewItem" &&
mref.DeclaringType.FullName == "Terraria.Item");
csr.Emit(OpCodes.Ldarg_0);

var csr = modder.GetILCursor(() => (new Terraria.NPC()).DropItemInstanced(default, default, 0, 0, false));
var callback = csr.Module.ImportReference(
#if TerrariaServer_EntitySourcesActive || Terraria_EntitySourcesActive || tModLoader_EntitySourcesActive
modder.GetMethodDefinition(() => OTAPI.Hooks.NPC.InvokeBossBag(null, 0, 0, 0, 0, 0, 0, false, 0, false, false, null))
csr.Next.Operand = modder.GetMethodDefinition(() => OTAPI.Hooks.NPC.InvokeBossBag(default, default, default, default, default, default, default, default, default, default, default, default));
#else
modder.GetMethodDefinition(() => OTAPI.Hooks.NPC.InvokeBossBag(0, 0, 0, 0, 0, 0, false, 0, false, false, null))
csr.Next.Operand = modder.GetMethodDefinition(() => OTAPI.Hooks.NPC.InvokeBossBag(default, default, default, default, default, default, default, default, default, default, default));
#endif
);

var instructions = csr.Body.Instructions.Where(x => x.OpCode == OpCodes.Call
&& x.Operand is MethodReference mref && mref.Name == "NewItem"
&& x.Next.OpCode == OpCodes.Stloc_0);

if (instructions.Count() != 1) throw new NotSupportedException("Only one server NewItem call expected in DropBossBags.");

var ins = instructions.First();

ins.Operand = callback;

csr.Goto(ins);
csr.EmitAll(
new { OpCodes.Ldarg_0 }
);

csr.Goto(ins.Next.Next);
csr.EmitAll(
new { OpCodes.Ldloc_0 },
new { OpCodes.Ldc_I4_M1 },
new { OpCodes.Ceq },
new { OpCodes.Brfalse_S, Operand = ins.Next.Next },
new { OpCodes.Ret }
);
}

namespace OTAPI
Expand Down Expand Up @@ -132,14 +109,20 @@ Terraria.NPC npc
};
BossBag?.Invoke(null, args);
if (args.Result == HookResult.Cancel)
return -1;
{
#if TerrariaServer_EntitySourcesActive || Terraria_EntitySourcesActive || tModLoader_EntitySourcesActive
return Terraria.Item.NewItem(Source, args.X, args.Y, args.Width, args.Height, args.Type, args.Stack, args.NoBroadcast, args.Pfix, args.NoGrabDelay, args.ReverseLookup);
#else
return Terraria.Item.NewItem(args.X, args.Y, args.Width, args.Height, args.Type, args.Stack, args.NoBroadcast, args.Pfix, args.NoGrabDelay, args.ReverseLookup);
#endif
}
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i may not be up to scratch with these hooks nowadays, but if it was cancelled, should a new item be created still? And if so, why?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

According to how "HookResult.Cancel" tend to do, we should not let the new item be created, and i completely agree with that. Sorry, it was because of my lack of knowledge that i overlooked it.


#if TerrariaServer_EntitySourcesActive || Terraria_EntitySourcesActive || tModLoader_EntitySourcesActive
return Terraria.Item.NewItem(Source, args.X, args.Y, args.Width, args.Height, args.Type, args.Stack, args.NoBroadcast, args.Pfix, args.NoGrabDelay, args.ReverseLookup);
return Terraria.Item.NewItem(Source, X, Y, Width, Height, Type, Stack, noBroadcast, pfix, noGrabDelay, reverseLookup);
#else
return Terraria.Item.NewItem(args.X, args.Y, args.Width, args.Height, args.Type, args.Stack, args.NoBroadcast, args.Pfix, args.NoGrabDelay, args.ReverseLookup);
return Terraria.Item.NewItem(X, Y, Width, Height, Type, Stack, NoBroadcast, Pfix, NoGrabDelay, ReverseLookup);
#endif
}
}
}
}
}