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 ItemAdded event call in Player::AddItem #2262

Merged
merged 2 commits into from
Nov 15, 2023
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
22 changes: 21 additions & 1 deletion Exiled.API/Extensions/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ namespace Exiled.API.Extensions
using System;
using System.Reflection;

using HarmonyLib;

using LiteNetLib.Utils;

/// <summary>
Expand All @@ -18,7 +20,7 @@ namespace Exiled.API.Extensions
public static class ReflectionExtensions
{
/// <summary>
/// Invoke a static method.
/// Invokes a static method.
/// </summary>
/// <param name="type">The method type.</param>
/// <param name="methodName">The method name.</param>
Expand All @@ -30,6 +32,24 @@ public static void InvokeStaticMethod(this Type type, string methodName, object[
type.GetMethod(methodName, flags)?.Invoke(null, param);
}

/// <summary>
/// Invokes a static event.
/// </summary>
/// <param name="type">The event type.</param>
/// <param name="eventName">The event name.</param>
/// <param name="param">The event arguments.</param>
public static void InvokeStaticEvent(this Type type, string eventName, object[] param)
{
var eventDelegate = (MulticastDelegate)type.GetField(eventName, AccessTools.all).GetValue(null);
if (eventDelegate != null)
{
foreach (var handler in eventDelegate.GetInvocationList())
{
handler.Method.Invoke(handler.Target, param);
}
}
}

/// <summary>
/// Copy all properties from the source class to the target one.
/// </summary>
Expand Down
2 changes: 2 additions & 0 deletions Exiled.API/Features/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2552,6 +2552,8 @@ public Item AddItem(ItemBase itemBase, Item item = null)
acquisitionConfirmationTrigger.AcquisitionAlreadyReceived = false;
}

typeof(InventoryExtensions).InvokeStaticEvent(nameof(InventoryExtensions.OnItemAdded), new object[] { ReferenceHub, itemBase, null });

ItemsValue.Add(item);

Inventory.SendItemsNextFrame = true;
Expand Down
Loading