Skip to content

Commit aaa01b9

Browse files
committed
Fix & Small change on UsingAndCancellingItemUse
1 parent 21560f6 commit aaa01b9

File tree

4 files changed

+33
-30
lines changed

4 files changed

+33
-30
lines changed

EXILED/Exiled.Events/EventArgs/Player/CancelledItemUseEventArgs.cs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ namespace Exiled.Events.EventArgs.Player
1010
using API.Features;
1111
using Exiled.API.Features.Items;
1212
using Exiled.Events.EventArgs.Interfaces;
13+
using InventorySystem.Items.Usables;
1314

1415
/// <summary>
1516
/// Contains all information before a player cancels usage of an item.
@@ -19,14 +20,16 @@ public class CancelledItemUseEventArgs : IPlayerEvent, IUsableEvent
1920
/// <summary>
2021
/// Initializes a new instance of the <see cref="CancelledItemUseEventArgs" /> class.
2122
/// </summary>
22-
/// <param name="player">The player who's stopping the use of an item.</param>
23-
/// <param name="item">
23+
/// <param name="hub">
24+
/// <inheritdoc cref="Player" />
25+
/// </param>
26+
/// <param name="usableItem">
2427
/// <inheritdoc cref="Usable" />
2528
/// </param>
26-
public CancelledItemUseEventArgs(Player player, Item item)
29+
public CancelledItemUseEventArgs(ReferenceHub hub, UsableItem usableItem)
2730
{
28-
Player = player;
29-
Usable = item is Usable usable ? usable : null;
31+
Player = Player.Get(hub);
32+
Usable = Item.Get<Usable>(usableItem);
3033
}
3134

3235
/// <summary>

EXILED/Exiled.Events/EventArgs/Player/CancellingItemUseEventArgs.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,15 @@ public class CancellingItemUseEventArgs : IPlayerEvent, IDeniableEvent, IUsableE
2020
/// <summary>
2121
/// Initializes a new instance of the <see cref="CancellingItemUseEventArgs" /> class.
2222
/// </summary>
23-
/// <param name="player">The player who's stopping the use of an item.</param>
23+
/// <param name="hub">
24+
/// <inheritdoc cref="Player" />
25+
/// </param>
2426
/// <param name="item">
2527
/// <inheritdoc cref="UsedItemEventArgs.Item" />
2628
/// </param>
27-
public CancellingItemUseEventArgs(Player player, UsableItem item)
29+
public CancellingItemUseEventArgs(ReferenceHub hub, UsableItem item)
2830
{
29-
Player = player;
31+
Player = Player.Get(hub);
3032
Usable = Item.Get<Usable>(item);
3133
}
3234

EXILED/Exiled.Events/EventArgs/Player/UsingItemEventArgs.cs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,18 @@ public class UsingItemEventArgs : IPlayerEvent, IDeniableEvent, IUsableEvent
2121
/// <summary>
2222
/// Initializes a new instance of the <see cref="UsingItemEventArgs" /> class.
2323
/// </summary>
24-
/// <param name="player">The player who's going to use the item.</param>
24+
/// <param name="hub">
25+
/// <inheritdoc cref="Player" />
26+
/// </param>
2527
/// <param name="cooldown">
2628
/// <inheritdoc cref="Cooldown" />
2729
/// </param>
2830
/// <param name="item">
2931
/// <inheritdoc cref="UsedItemEventArgs.Item" />
3032
/// </param>
31-
public UsingItemEventArgs(Player player, UsableItem item, float cooldown)
33+
public UsingItemEventArgs(ReferenceHub hub, UsableItem item, float cooldown)
3234
{
33-
Player = player;
35+
Player = Player.Get(hub);
3436
Usable = Item.Get(item) is Usable usable ? usable : null;
3537
Cooldown = cooldown;
3638
}

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

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ namespace Exiled.Events.Patches.Events.Player
2020
using HarmonyLib;
2121

2222
using InventorySystem.Items.Usables;
23+
using LabApi.Events.Arguments.PlayerEvents;
2324
using Utils.Networking;
2425

2526
using static HarmonyLib.AccessTools;
@@ -53,9 +54,8 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
5354
index,
5455
new CodeInstruction[]
5556
{
56-
// Player.Get(referenceHub)
57+
// referenceHub
5758
new CodeInstruction(OpCodes.Ldloc_0).MoveLabelsFrom(newInstructions[index]),
58-
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),
5959

6060
// usableItem
6161
new(OpCodes.Ldloc_1),
@@ -90,14 +90,11 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
9090
index,
9191
new[]
9292
{
93-
// Player.Get(referenceHub)
93+
// referenceHub
9494
new CodeInstruction(OpCodes.Ldloc_0),
95-
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),
9695

97-
// handler.CurrentUsable.Item
98-
new(OpCodes.Ldloc_2),
99-
new(OpCodes.Ldflda, Field(typeof(PlayerHandler), nameof(PlayerHandler.CurrentUsable))),
100-
new(OpCodes.Ldfld, Field(typeof(CurrentlyUsedItem), nameof(CurrentlyUsedItem.Item))),
96+
// usableItem
97+
new(OpCodes.Ldloc_1),
10198

10299
// CancellingItemUseEventArgs ev = new(Player, UsableItem)
103100
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(CancellingItemUseEventArgs))[0]),
@@ -114,28 +111,27 @@ private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstructi
114111
new(OpCodes.Brfalse_S, returnLabel),
115112
});
116113

117-
offset = 3;
114+
// right before the LabAPI event "new PlayerCancelledUsingItemEventArgs(...)"
115+
offset = -2;
118116
index = newInstructions.FindLastIndex(
119-
instruction => instruction.Calls(Method(typeof(NetworkUtils), nameof(NetworkUtils.SendToAuthenticated)))) + offset;
120-
/* // TODO: FIX THE CRASH ISSUE
117+
instruction => instruction.opcode == OpCodes.Newobj) + offset;
118+
121119
newInstructions.InsertRange(
122120
index,
123121
new[]
124122
{
125-
// Player.Get(referenceHub)
123+
// referenceHub
126124
new CodeInstruction(OpCodes.Ldloc_0),
127-
new(OpCodes.Call, Method(typeof(Player), nameof(Player.Get), new[] { typeof(ReferenceHub) })),
128125

129-
// evCancellingItemUseEventArgs.Item
130-
new(OpCodes.Ldloc_S, evCancellingItemUseEventArgs.LocalIndex),
131-
new(OpCodes.Callvirt, PropertyGetter(typeof(CancellingItemUseEventArgs), nameof(CancellingItemUseEventArgs.Item))),
126+
// usableItem
127+
new(OpCodes.Ldloc_1),
132128

133-
// CancellingItemUseEventArgs ev = new(Player, UsableItem)
129+
// CancelledItemUseEventArgs ev = new(ReferenceHub, UsableItem)
134130
new(OpCodes.Newobj, GetDeclaredConstructors(typeof(CancelledItemUseEventArgs))[0]),
135131

136-
// Handlers.Player.OnCancellingItemUse(ev)
132+
// Handlers.Player.OnCancelledItemUse(ev)
137133
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnCancelledItemUse))),
138-
});*/
134+
});
139135

140136
newInstructions[newInstructions.Count - 1].WithLabels(returnLabel);
141137

0 commit comments

Comments
 (0)