Skip to content

Commit b642fe2

Browse files
committed
fix
1 parent f31a037 commit b642fe2

File tree

1 file changed

+48
-3
lines changed

1 file changed

+48
-3
lines changed

EXILED/Exiled.Events/Patches/Events/Player/UsedItemByHolstering.cs

Lines changed: 48 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,14 @@ namespace Exiled.Events.Patches.Events.Player
1010
using System.Collections.Generic;
1111
using System.Reflection.Emit;
1212

13+
using CustomPlayerEffects;
1314
using Exiled.API.Features;
1415
using Exiled.API.Features.Pools;
1516
using Exiled.Events.Attributes;
1617
using Exiled.Events.EventArgs.Player;
1718
using HarmonyLib;
1819
using InventorySystem.Items.Usables;
20+
using UnityEngine;
1921

2022
using static HarmonyLib.AccessTools;
2123

@@ -27,13 +29,56 @@ namespace Exiled.Events.Patches.Events.Player
2729
[HarmonyPatch(typeof(Consumable), nameof(Consumable.OnHolstered))]
2830
public class UsedItemByHolstering
2931
{
30-
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
32+
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
3133
{
3234
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);
3335

34-
// after ServerRemoveSelf, which lines up with before the ret
35-
newInstructions.InsertRange(newInstructions.Count - 1, new CodeInstruction[]
36+
LocalBuilder handler = generator.DeclareLocal(typeof(PlayerHandler));
37+
LocalBuilder curr = generator.DeclareLocal(typeof(CurrentlyUsedItem));
38+
39+
Label retLabel = generator.DefineLabel();
40+
41+
// add retLabel to return
42+
newInstructions[newInstructions.Count - 1].WithLabels(retLabel);
43+
44+
// before ServerRemoveSelf, 2 instructions behind the return instruction
45+
newInstructions.InsertRange(newInstructions.Count - 1 - 2, new CodeInstruction[]
3646
{
47+
// if (!UsableItemsController.Handlers.TryGetValue(Owner, out PlayerHandler handler) return;
48+
new(OpCodes.Ldsfld, Field(typeof(UsableItemsController), nameof(UsableItemsController.Handlers))),
49+
new(OpCodes.Ldarg_0),
50+
new(OpCodes.Callvirt, PropertyGetter(typeof(Consumable), nameof(Consumable.Owner))),
51+
new(OpCodes.Ldloca_S, handler),
52+
new(OpCodes.Callvirt, Method(typeof(Dictionary<ReferenceHub, PlayerHandler>), nameof(Dictionary<ReferenceHub, PlayerHandler>.TryGetValue))),
53+
new(OpCodes.Brfalse_S, retLabel),
54+
55+
// CurrentlyUsedItem curr = handler.CurrentUsable;
56+
new(OpCodes.Ldloc_S, handler),
57+
new(OpCodes.Ldfld, Field(typeof(PlayerHandler), nameof(PlayerHandler.CurrentUsable))),
58+
new(OpCodes.Stloc_S, curr),
59+
60+
// if (curr.ItemSerial == 0) return;
61+
new(OpCodes.Ldloc_S, curr),
62+
new(OpCodes.Ldfld, Field(typeof(CurrentlyUsedItem), nameof(CurrentlyUsedItem.ItemSerial))),
63+
new(OpCodes.Brfalse_S, retLabel),
64+
65+
// this check is to not call the event if the trigger was from UsableItemsController (the standard trigger for this event), contact @Someone on discord for more details
66+
// if (Time.timeSinceLevelLoad >= currentUsable.StartTime + (currentUsable.Item.UseTime / cons.ItemTypeId.GetSpeedMultiplier(hub))) return;
67+
new(OpCodes.Call, PropertyGetter(typeof(Time), nameof(Time.timeSinceLevelLoad))),
68+
new(OpCodes.Ldloc_S, curr),
69+
new(OpCodes.Ldfld, Field(typeof(CurrentlyUsedItem), nameof(CurrentlyUsedItem.StartTime))),
70+
new(OpCodes.Ldloc_S, curr),
71+
new(OpCodes.Ldfld, Field(typeof(CurrentlyUsedItem), nameof(CurrentlyUsedItem.Item))),
72+
new(OpCodes.Ldfld, Field(typeof(UsableItem), nameof(UsableItem.UseTime))),
73+
new(OpCodes.Ldarg_0),
74+
new(OpCodes.Ldfld, Field(typeof(Consumable), nameof(Consumable.ItemTypeId))),
75+
new(OpCodes.Ldarg_0),
76+
new(OpCodes.Callvirt, PropertyGetter(typeof(Consumable), nameof(Consumable.Owner))),
77+
new(OpCodes.Call, Method(typeof(UsableItemModifierEffectExtensions), nameof(UsableItemModifierEffectExtensions.GetSpeedMultiplier))),
78+
new(OpCodes.Div),
79+
new(OpCodes.Add),
80+
new(OpCodes.Bgt_Un_S, retLabel),
81+
3782
// this.Owner
3883
new(OpCodes.Ldarg_0),
3984
new(OpCodes.Callvirt, PropertyGetter(typeof(Consumable), nameof(Consumable.Owner))),

0 commit comments

Comments
 (0)