From 17d72639739ceac51ae60c3d138e55080d694791 Mon Sep 17 00:00:00 2001 From: Garry Newman Date: Tue, 14 May 2019 16:05:28 +0100 Subject: [PATCH] Added InventoryDef.GetRecipesContainingThis() --- Facepunch.Steamworks/Structs/InventoryDef.cs | 19 +++++++++++++++++++ .../Structs/InventoryRecipe.cs | 5 +++++ 2 files changed, 24 insertions(+) diff --git a/Facepunch.Steamworks/Structs/InventoryDef.cs b/Facepunch.Steamworks/Structs/InventoryDef.cs index f93368e..45e6028 100644 --- a/Facepunch.Steamworks/Structs/InventoryDef.cs +++ b/Facepunch.Steamworks/Structs/InventoryDef.cs @@ -201,5 +201,24 @@ public int LocalBasePrice } public string LocalBasePriceFormatted => Utility.FormatPrice( SteamInventory.Currency, LocalPrice / 100.0 ); + + InventoryRecipe[] _recContaining; + + /// + /// Return a list of recepies that contain this item + /// + public InventoryRecipe[] GetRecipesContainingThis() + { + if ( _recContaining != null ) return _recContaining; + + var allRec = SteamInventory.Definitions + .Select( x => x.GetRecipes() ) + .Where( x => x != null ) + .SelectMany( x => x ); + + _recContaining = allRec.Where( x => x.ContainsIngredient( this ) ).ToArray(); + return _recContaining; + } + } } \ No newline at end of file diff --git a/Facepunch.Steamworks/Structs/InventoryRecipe.cs b/Facepunch.Steamworks/Structs/InventoryRecipe.cs index 9131a7d..03384e8 100644 --- a/Facepunch.Steamworks/Structs/InventoryRecipe.cs +++ b/Facepunch.Steamworks/Structs/InventoryRecipe.cs @@ -82,5 +82,10 @@ internal static InventoryRecipe FromString( string part, InventoryDef Result ) r.Ingredients = parts.Select( x => Ingredient.FromString( x ) ).Where( x => x.DefinitionId != 0 ).ToArray(); return r; } + + internal bool ContainsIngredient( InventoryDef inventoryDef ) + { + return Ingredients.Any( x => x.DefinitionId == inventoryDef.Id ); + } } } \ No newline at end of file