Skip to content

Commit

Permalink
Fix Player::Inventory & Fix a NW Bug where OnAdded get call after OnR…
Browse files Browse the repository at this point in the history
…emoved (#2458)

* Fix

* Fix 2

* Fix 3
  • Loading branch information
louis1706 authored Apr 6, 2024
1 parent 2e48127 commit c189dfc
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 174 deletions.
5 changes: 2 additions & 3 deletions Exiled.API/Features/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1965,7 +1965,8 @@ public bool RemoveItem(Item item, bool destroy = true)
Inventory.NetworkCurItem = ItemIdentifier.None;

Inventory.UserInventory.Items.Remove(item.Serial);
ItemsValue.Remove(item);
typeof(InventoryExtensions).InvokeStaticEvent(nameof(InventoryExtensions.OnItemRemoved), new object[] { ReferenceHub, item.Base, null });

Inventory.SendItemsNextFrame = true;
}

Expand Down Expand Up @@ -2590,8 +2591,6 @@ public Item AddItem(ItemBase itemBase, Item item = null)

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

ItemsValue.Add(item);

Inventory.SendItemsNextFrame = true;
return item;
}
Expand Down
1 change: 1 addition & 0 deletions Exiled.Events/Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,7 @@ public override void OnDisabled()
CharacterClassManager.OnRoundStarted -= Handlers.Server.OnRoundStarted;

InventorySystem.InventoryExtensions.OnItemAdded -= Handlers.Player.OnItemAdded;
InventorySystem.InventoryExtensions.OnItemRemoved -= Handlers.Player.OnItemRemoved;

RagdollManager.OnRagdollSpawned -= Handlers.Internal.RagdollList.OnSpawnedRagdoll;
RagdollManager.OnRagdollRemoved -= Handlers.Internal.RagdollList.OnRemovedRagdoll;
Expand Down
20 changes: 18 additions & 2 deletions Exiled.Events/Handlers/Player.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Exiled.Events.Handlers
{
using Exiled.API.Features.Pickups;
#pragma warning disable IDE0079
#pragma warning disable IDE0060
#pragma warning disable SA1623 // Property summary documentation should match accessors
Expand Down Expand Up @@ -941,7 +942,15 @@ public class Player
/// <param name="itemBase">The added <see cref="InventorySystem.Items.ItemBase"/>.</param>
/// <param name="pickupBase">The <see cref="InventorySystem.Items.Pickups.ItemPickupBase"/> the <see cref="InventorySystem.Items.ItemBase"/> originated from, or <see langword="null"/> if the item was not picked up.</param>
public static void OnItemAdded(ReferenceHub referenceHub, InventorySystem.Items.ItemBase itemBase, InventorySystem.Items.Pickups.ItemPickupBase pickupBase)
=> ItemAdded.InvokeSafely(new ItemAddedEventArgs(referenceHub, itemBase, pickupBase));
{
ItemAddedEventArgs ev = new(referenceHub, itemBase, pickupBase);

ev.Item.ReadPickupInfo(ev.Pickup);

ev.Player.ItemsValue.Add(ev.Item);

ItemAdded.InvokeSafely(ev);
}

/// <summary>
/// Called after a <see cref="T:Exiled.API.Features.Player" /> has an item removed from their inventory.
Expand All @@ -950,7 +959,14 @@ public static void OnItemAdded(ReferenceHub referenceHub, InventorySystem.Items.
/// <param name="itemBase">The removed <see cref="InventorySystem.Items.ItemBase"/>.</param>
/// <param name="pickupBase">The <see cref="InventorySystem.Items.Pickups.ItemPickupBase"/> the <see cref="InventorySystem.Items.ItemBase"/> originated from, or <see langword="null"/> if the item was not picked up.</param>
public static void OnItemRemoved(ReferenceHub referenceHub, InventorySystem.Items.ItemBase itemBase, InventorySystem.Items.Pickups.ItemPickupBase pickupBase)
=> ItemRemoved.InvokeSafely(new ItemRemovedEventArgs(referenceHub, itemBase, pickupBase));
{
ItemRemovedEventArgs ev = new(referenceHub, itemBase, pickupBase);
ItemRemoved.InvokeSafely(ev);

ev.Player.ItemsValue.Remove(ev.Item);

API.Features.Items.Item.BaseToItem.Remove(itemBase);
}

/// <summary>
/// Called before a <see cref="API.Features.Player"/> enters in an environmental hazard.
Expand Down
65 changes: 65 additions & 0 deletions Exiled.Events/Patches/Fixes/FixOnAddedBeingCallAfterOnRemoved.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
// -----------------------------------------------------------------------
// <copyright file="FixOnAddedBeingCallAfterOnRemoved.cs" company="Exiled Team">
// Copyright (c) Exiled Team. All rights reserved.
// Licensed under the CC BY-SA 3.0 license.
// </copyright>
// -----------------------------------------------------------------------

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

using API.Features.Pools;
using Exiled.API.Features;
using HarmonyLib;
using InventorySystem;
using InventorySystem.Items;
using InventorySystem.Items.Pickups;

using static HarmonyLib.AccessTools;

/// <summary>
/// Patches <see cref="InventoryExtensions.ServerAddItem(Inventory, ItemType, ushort, ItemPickupBase)"/>.
/// Fix than NW call <see cref="InventoryExtensions.OnItemRemoved"/> before <see cref="InventoryExtensions.OnItemAdded"/> for AmmoItem.
/// </summary>
[HarmonyPatch(typeof(InventoryExtensions), nameof(InventoryExtensions.ServerAddItem))]
internal class FixOnAddedBeingCallAfterOnRemoved
{
private static IEnumerable<CodeInstruction> Transpiler(IEnumerable<CodeInstruction> instructions, ILGenerator generator)
{
List<CodeInstruction> newInstructions = ListPool<CodeInstruction>.Pool.Get(instructions);
/*
// Modify this
itemBase2.OnAdded(pickup);
Action<ReferenceHub, ItemBase, ItemPickupBase> onItemAdded = InventoryExtensions.OnItemAdded;
if (onItemAdded != null)
{
onItemAdded(inv._hub, itemBase2, pickup);
}
// To this
Action<ReferenceHub, ItemBase, ItemPickupBase> onItemAdded = InventoryExtensions.OnItemAdded;
if (onItemAdded != null)
{
onItemAdded(inv._hub, itemBase2, pickup);
}
itemBase2.OnAdded(pickup);
*/
int opCodesToMove = 3;
int offset = -2;
int indexOnAdded = newInstructions.FindIndex(instruction => instruction.Calls(Method(typeof(ItemBase), nameof(ItemBase.OnAdded)))) + offset;

offset = 1;
int indexInvoke = newInstructions.FindIndex(instruction => instruction.Calls(Method(typeof(Action<ReferenceHub, ItemBase, ItemPickupBase>), nameof(Action<ReferenceHub, ItemBase, ItemPickupBase>.Invoke)))) + offset;

newInstructions.InsertRange(indexInvoke, newInstructions.GetRange(indexOnAdded, opCodesToMove));
newInstructions[indexInvoke].MoveLabelsFrom(newInstructions[indexInvoke + opCodesToMove]);
newInstructions.RemoveRange(indexOnAdded, opCodesToMove);

for (int z = 0; z < newInstructions.Count; z++)
yield return newInstructions[z];
ListPool<CodeInstruction>.Pool.Return(newInstructions);
}
}
}
1 change: 0 additions & 1 deletion Exiled.Events/Patches/Fixes/FixPickupPreviousOwner.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ namespace Exiled.Events.Patches.Fixes
using InventorySystem;
using InventorySystem.Items.Firearms.Ammo;
using InventorySystem.Items.Pickups;
using VoiceChat;

using static HarmonyLib.AccessTools;

Expand Down
168 changes: 0 additions & 168 deletions Exiled.Events/Patches/Generic/InventoryControlPatch.cs

This file was deleted.

0 comments on commit c189dfc

Please sign in to comment.