Skip to content

[BUG]: PickupCreated event isn't invoking properly #323

@OMEGA3065

Description

@OMEGA3065

Before You Report

  • I have searched for existing reports of this bug, and this bug report is unique.

Version

1.1.4

Description

First opened as a support question in the discord: Forum Post

Sometimes the LabApi.Events.Handlers.ServerEvents.PickupCreated event doesn't invoke.
Some things I have tested, and their results will be down in the Additional Information section.
With the help of SlejmUr (Discord: theverifieddickhead) we narrowed it down to Pickup.Dictionary.Contains(pickup) resulting in true and that in turn skips the event invocation.

To Reproduce

Logging for debug purposes. Some other stuff is in Additional Info.

using HarmonyLib;
using KpgPatreon;
using LabApi.Features;
using LabApi.Features.Console;
using LabApi.Loader.Features.Plugins;
using LabApi.Loader.Features.Plugins.Enums;
using InventorySystem.Items.Pickups;
using LabApi.Features.Wrappers;

namespace Test;

[HarmonyPatch(typeof(ItemPickupBase), "Start")]
public static class Patch
{
    public static bool Prefix(ItemPickupBase __instance)
    {
        Logger.Info($"START -> {(Pickup.Dictionary.ContainsKey(__instance) ? "PRESENT" : "ABSENT ")} TYPE: {__instance.GetType().FullName} [SERIAL:{__instance.Info.Serial}/{__instance.ItemId.SerialNumber}] [#{__instance.GetHashCode()}]");
        return true;
    }
}

[HarmonyPatch(typeof(Pickup), "AddPickup")]
public static class Patch2
{
    public static bool Prefix(ref ItemPickupBase pickup)
    {
        Logger.Warn($"ADDED {(Pickup.Dictionary.ContainsKey(pickup) ? "PRESENT" : "ABSENT ")} TYPE: {pickup.GetType().FullName} [SERIAL:{pickup.Info.Serial}/{pickup.ItemId.SerialNumber}] [#{pickup.GetHashCode()}]");
        return true;
    }
}


[HarmonyPatch(typeof(Pickup), "RemovePickup")]
public static class Patch3
{
    public static bool Prefix(ref ItemPickupBase pickup)
    {
        Logger.Error($"REMOVAL {(Pickup.Dictionary.ContainsKey(pickup) ? "PRESENT" : "ABSENT ")} TYPE: {pickup.GetType().FullName} [SERIAL:{pickup.Info.Serial}/{pickup.ItemId.SerialNumber}] [#{pickup.GetHashCode()}]");
        return true;
    }
}


public class CustomItemLibPlugin : Plugin<Config>
{
    public static CustomItemLibPlugin Instance { get; private set; }
    public override string Name => "TestItemPlugin";
    public override string Description => "Plugin for testing PickupAdded and PickupRemoved events of LabAPI";

    public override string Author => "OMEGA3065";

    public override Version Version => new(1, 0, 0);
    public override LoadPriority Priority => LoadPriority.Highest;
    public override Version RequiredApiVersion => new(LabApiProperties.CompiledVersion);
    
    public CustomItemLibPlugin()
    {
        Instance = this;
    }

    private Harmony _harmony;

    public override void Enable()
    {
        _harmony = new Harmony("test.plugin.lab.api.pickup.eventtest");
        _harmony.PatchAll();

        LabApi.Events.Handlers.ServerEvents.PickupCreated += (ev) => Logger.Info($"EVENT {(Pickup.Dictionary.ContainsKey(ev.Pickup.Base) ? "PRESENT" : "ABSENT ")} TYPE: {ev.Pickup.Base.GetType().FullName} [SERIAL:{ev.Pickup.Base.Info.Serial}/{ev.Pickup.Base.ItemId.SerialNumber}] [#{ev.Pickup.Base.GetHashCode()}]");
    }

    public override void Disable()
    {
        _harmony.UnpatchAll();
        LabApi.Events.Handlers.ServerEvents.PickupCreated -= (ev) => Logger.Info($"EVENT {(Pickup.Dictionary.ContainsKey(ev.Pickup.Base) ? "PRESENT" : "ABSENT ")} TYPE: {ev.Pickup.Base.GetType().FullName} [SERIAL:{ev.Pickup.Base.Info.Serial}/{ev.Pickup.Base.ItemId.SerialNumber}] [#{ev.Pickup.Base.GetHashCode()}]");
    }
}

Expected Behavior

The event should be invoked for some cases where it doesn't.

Additional Information

Testing Results:
Dropping items -> No event invocation
Upgrading items in 914 -> Sometimes the event is invoked, inconsistent results at best

  • Upgrading keycards (tested on the setting Fine) -> No invoke
  • Upgrading weapons (tested on the setting Fine) -> Invoked when upgrading a weapon with when the weapon on the input side contained Ammo
    • Com18 with ammo -> WAS called
    • Fsp9 without ammo -> wasn't called
    • Crossvec with ammo -> WAS called
  • Dropping items on death -> event was invoked
  • Swapping items (picking up armor which replaces the one in the player's inventory) -> event was invoked
  • Map item spawning (lockers, armories, etc.) -> event was invoked
    After the above I tried dropping ammo which didn't cause an invocation of the event either.

[NOTE]: No errors in console or the text log file either

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions