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 CancelledItemUse::Item always being null #2174

Merged
merged 4 commits into from
Nov 12, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions Exiled.Events/Patches/Events/Player/UsingAndCancellingItemUse.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi

Label returnLabel = generator.DefineLabel();

LocalBuilder ev = generator.DeclareLocal(typeof(UsingItemEventArgs));
LocalBuilder evUsingItemEventArgs = generator.DeclareLocal(typeof(UsingItemEventArgs));
LocalBuilder evCancellingItemUseEventArgs = generator.DeclareLocal(typeof(CancellingItemUseEventArgs));

int offset = 2;
int index = newInstructions.FindIndex(
Expand All @@ -66,7 +67,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(UsingItemEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, ev.LocalIndex),
new(OpCodes.Stloc_S, evUsingItemEventArgs.LocalIndex),

// Handlers.Player.OnUsingItem(ev)
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnUsingItem))),
Expand All @@ -77,7 +78,7 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new(OpCodes.Brfalse_S, returnLabel),

// cooldown = ev.Cooldown
new(OpCodes.Ldloc_S, ev.LocalIndex),
new(OpCodes.Ldloc_S, evUsingItemEventArgs.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(UsingItemEventArgs), nameof(UsingItemEventArgs.Cooldown))),
new(OpCodes.Stloc_S, 4),
});
Expand All @@ -101,6 +102,8 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
// CancellingItemUseEventArgs ev = new(Player, UsableItem)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(CancellingItemUseEventArgs))[0]),
new(OpCodes.Dup),
new(OpCodes.Dup),
new(OpCodes.Stloc_S, evCancellingItemUseEventArgs.LocalIndex),

// Handlers.Player.OnCancellingItemUse(ev)
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnCancellingItemUse))),
Expand All @@ -122,10 +125,9 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
new CodeInstruction(OpCodes.Ldloc_0),
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),

// handler.CurrentUsable.Item
new(OpCodes.Ldloc_2),
new(OpCodes.Ldflda, Field(typeof(PlayerHandler), nameof(PlayerHandler.CurrentUsable))),
new(OpCodes.Ldfld, Field(typeof(CurrentlyUsedItem), nameof(CurrentlyUsedItem.Item))),
// evCancellingItemUseEventArgs.Item
new(OpCodes.Ldloc_S, evCancellingItemUseEventArgs.LocalIndex),
new(OpCodes.Callvirt, PropertyGetter(typeof(CancellingItemUseEventArgs), nameof(CancellingItemUseEventArgs.Item))),

// CancellingItemUseEventArgs ev = new(Player, UsableItem)
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(CancelledItemUseEventArgs))[0]),
Expand Down
Loading