Skip to content
Merged
Show file tree
Hide file tree
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
14 changes: 12 additions & 2 deletions EXILED/Exiled.Events/EventArgs/Player/UsedItemEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,14 @@ public class UsedItemEventArgs : IPlayerEvent, IUsableEvent
/// <param name="item">
/// <inheritdoc cref="Item" />
/// </param>
public UsedItemEventArgs(ReferenceHub player, UsableItem item)
/// <param name="causedByHolstering">
/// <inheritdoc cref="CausedByHolstering"/>
/// </param>
public UsedItemEventArgs(ReferenceHub player, UsableItem item, bool causedByHolstering)
{
Player = Player.Get(player);
Usable = Item.Get(item) is Usable usable ? usable : null;
Usable = Item.Get(item) as Usable;
CausedByHolstering = causedByHolstering;
}

/// <summary>
Expand All @@ -46,5 +50,11 @@ public UsedItemEventArgs(ReferenceHub player, UsableItem item)
/// Gets the player who used the item.
/// </summary>
public Player Player { get; }

/// <summary>
/// Gets a value indicating whether this event was triggered by a player switching items (true) or by waiting after using the item (false).
/// </summary>
/// <remarks>Use this value if you wish to keep the bug where you could switch items quickly to skip this event.</remarks>
public bool CausedByHolstering { get; }
}
}
2 changes: 1 addition & 1 deletion EXILED/Exiled.Events/Handlers/Internal/Round.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ namespace Exiled.Events.Handlers.Internal
internal static class Round
{
/// <inheritdoc cref="Handlers.Player.OnUsedItem" />
public static void OnServerOnUsingCompleted(ReferenceHub hub, UsableItem usable) => Handlers.Player.OnUsedItem(new (hub, usable));
public static void OnServerOnUsingCompleted(ReferenceHub hub, UsableItem usable) => Handlers.Player.OnUsedItem(new (hub, usable, false));

/// <inheritdoc cref="Handlers.Server.OnWaitingForPlayers" />
public static void OnWaitingForPlayers()
Expand Down
58 changes: 58 additions & 0 deletions EXILED/Exiled.Events/Patches/Events/Player/UsedItemByHolstering.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// -----------------------------------------------------------------------
// <copyright file="UsedItemByHolstering.cs" company="ExMod Team">
// Copyright (c) ExMod Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

namespace Exiled.Events.Patches.Events.Player
{
using System.Collections.Generic;
using System.Reflection.Emit;

using Exiled.API.Features;
using Exiled.API.Features.Pools;
using Exiled.Events.Attributes;
using Exiled.Events.EventArgs.Player;
using HarmonyLib;
using InventorySystem.Items.Usables;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="Consumable.OnHolstered" />.
/// Adds an alternative trigger of the <see cref="Handlers.Player.UsedItem" /> event.
/// </summary>
[EventPatch(typeof(Handlers.Player), nameof(Handlers.Player.UsedItem))]
[HarmonyPatch(typeof(Consumable), nameof(Consumable.OnHolstered))]
public class UsedItemByHolstering
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);

// after ServerRemoveSelf, which lines up with before the ret
newInstructions.InsertRange(newInstructions.Count - 2, new CodeInstruction[]
{
// this.Owner
new(OpCodes.Ldarg_0),
new(OpCodes.Callvirt, PropertyGetter(typeof(Consumable), nameof(Consumable.Owner))),

// this (Consumable inherits UsableItem)
new(OpCodes.Ldarg_0),

// true
new(OpCodes.Ldc_I4_1),

// OnUsedItem(new UsedItemEventArgs(ReferenceHub, UsableItem, bool));
new(OpCodes.Newobj, Constructor(typeof(UsedItemEventArgs), new[] { typeof(ReferenceHub), typeof(UsableItem), typeof(bool) })),
new(OpCodes.Call, Method(typeof(Handlers.Player), nameof(Handlers.Player.OnUsedItem))),
});

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];

ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
}
}
Loading