From c5c00461d76600d40506d76b918e7a554a1d8657 Mon Sep 17 00:00:00 2001 From: Jiraya <177346249+intjiraya@users.noreply.github.com> Date: Sat, 30 Nov 2024 01:00:23 +0300 Subject: [PATCH 1/3] removing typeLookupTable --- EXILED/Exiled.CustomItems/API/Features/CustomItem.cs | 10 +--------- 1 file changed, 1 insertion(+), 9 deletions(-) diff --git a/EXILED/Exiled.CustomItems/API/Features/CustomItem.cs b/EXILED/Exiled.CustomItems/API/Features/CustomItem.cs index aa096dc85d..0645f91688 100644 --- a/EXILED/Exiled.CustomItems/API/Features/CustomItem.cs +++ b/EXILED/Exiled.CustomItems/API/Features/CustomItem.cs @@ -53,7 +53,6 @@ namespace Exiled.CustomItems.API.Features /// public abstract class CustomItem { - private static Dictionary typeLookupTable = new(); private static Dictionary stringLookupTable = new(); private static Dictionary idLookupTable = new(); @@ -163,12 +162,7 @@ public virtual ItemType Type /// /// The type. /// The matching the search, if not registered. - public static CustomItem? Get(Type t) - { - if (!typeLookupTable.ContainsKey(t)) - typeLookupTable.Add(t, Registered.FirstOrDefault(i => i.GetType() == t)); - return typeLookupTable[t]; - } + public static CustomItem? Get(Type t) => Registered.FirstOrDefault(i => i.GetType() == t); /// /// Tries to get a with a specific ID. @@ -770,7 +764,6 @@ public virtual void Give(Player player, Item item, bool displayMessage = true) /// public virtual void Init() { - typeLookupTable.Add(GetType(), this); stringLookupTable.Add(Name, this); idLookupTable.Add(Id, this); @@ -784,7 +777,6 @@ public virtual void Destroy() { UnsubscribeEvents(); - typeLookupTable.Remove(GetType()); stringLookupTable.Remove(Name); idLookupTable.Remove(Id); } From a64bcaa2429e97fd330cb95afcf6b3a6b8decb1d Mon Sep 17 00:00:00 2001 From: Jiraya <177346249+intjiraya@users.noreply.github.com> Date: Sun, 1 Dec 2024 15:25:40 +0300 Subject: [PATCH 2/3] Return collection of custom items by type --- .../API/Features/CustomItem.cs | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/EXILED/Exiled.CustomItems/API/Features/CustomItem.cs b/EXILED/Exiled.CustomItems/API/Features/CustomItem.cs index 0645f91688..f42d8ccdf8 100644 --- a/EXILED/Exiled.CustomItems/API/Features/CustomItem.cs +++ b/EXILED/Exiled.CustomItems/API/Features/CustomItem.cs @@ -158,11 +158,11 @@ public virtual ItemType Type } /// - /// Gets a with a specific type. + /// Retrieves a collection of instances that match a specified type. /// /// The type. - /// The matching the search, if not registered. - public static CustomItem? Get(Type t) => Registered.FirstOrDefault(i => i.GetType() == t); + /// An containing all registered of the specified type. + public static IEnumerable Get(Type t) => Registered.Where(i => i.GetType() == t); /// /// Tries to get a with a specific ID. @@ -195,16 +195,16 @@ public static bool TryGet(string name, out CustomItem? customItem) } /// - /// Tries to get a with a specific type. + /// Attempts to retrieve a collection of instances of a specified type. /// /// The of the item to look for. - /// The found , if not registered. - /// Returns a value indicating whether the was found. - public static bool TryGet(Type t, out CustomItem? customItem) + /// An output parameter that will contain the found instances, or an empty collection if none are registered. + /// A boolean value indicating whether any instances of the specified type were found. + public static bool TryGet(Type t, out IEnumerable customItems) { - customItem = Get(t); + customItems = Get(t); - return customItem is not null; + return customItems.Any(); } /// @@ -346,12 +346,16 @@ public static bool TryGive(Player player, uint id, bool displayMessage = true) /// The of the item to give. /// Indicates a value whether will be called when the player receives the or not. /// Returns a value indicating if the player was given the or not. + /// + /// This method will give the first registered of the specified type to the player. + /// If no items of the specified type are found, the method will return false. + /// public static bool TryGive(Player player, Type t, bool displayMessage = true) { - if (!TryGet(t, out CustomItem? item)) + if (!TryGet(t, out IEnumerable items)) return false; - item?.Give(player, displayMessage); + items.First().Give(player, displayMessage); return true; } From 9d8e63788a38830cabbb448736f2783292730760 Mon Sep 17 00:00:00 2001 From: Jiraya <177346249+intjiraya@users.noreply.github.com> Date: Sun, 1 Dec 2024 18:03:52 +0300 Subject: [PATCH 3/3] Deleting TryGet by type --- .../API/Features/CustomItem.cs | 21 ------------------- 1 file changed, 21 deletions(-) diff --git a/EXILED/Exiled.CustomItems/API/Features/CustomItem.cs b/EXILED/Exiled.CustomItems/API/Features/CustomItem.cs index f42d8ccdf8..1ad086c77c 100644 --- a/EXILED/Exiled.CustomItems/API/Features/CustomItem.cs +++ b/EXILED/Exiled.CustomItems/API/Features/CustomItem.cs @@ -339,27 +339,6 @@ public static bool TryGive(Player player, uint id, bool displayMessage = true) return true; } - /// - /// Gives to a specific a specic . - /// - /// The to give the item to. - /// The of the item to give. - /// Indicates a value whether will be called when the player receives the or not. - /// Returns a value indicating if the player was given the or not. - /// - /// This method will give the first registered of the specified type to the player. - /// If no items of the specified type are found, the method will return false. - /// - public static bool TryGive(Player player, Type t, bool displayMessage = true) - { - if (!TryGet(t, out IEnumerable items)) - return false; - - items.First().Give(player, displayMessage); - - return true; - } - /// /// Registers all the 's present in the current assembly. ///