-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRecipeVariantCollector.cs
More file actions
72 lines (59 loc) · 1.98 KB
/
Copy pathRecipeVariantCollector.cs
File metadata and controls
72 lines (59 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using Vintagestory.API.Client;
using Vintagestory.API.Common;
namespace QuickCraft;
internal static class RecipeVariantCollector
{
private static readonly Dictionary<string, GridRecipe[]> recipesByOutput = new();
public static void ClearCache()
{
recipesByOutput.Clear();
}
public static GridRecipe[] IncludeSameOutputVariants(ICoreClientAPI api, IEnumerable<GridRecipe?> recipes)
{
GridRecipe[] baseRecipes = recipes
.Where(recipe => recipe != null)
.Cast<GridRecipe>()
.ToArray();
if (baseRecipes.Length == 0)
{
return Array.Empty<GridRecipe>();
}
HashSet<GridRecipe> result = new(baseRecipes);
foreach (string outputKey in baseRecipes.Select(GetOutputKey).Where(key => key != null).Cast<string>().Distinct())
{
foreach (GridRecipe recipe in GetRecipesForOutput(api, outputKey))
{
result.Add(recipe);
}
}
return result.ToArray();
}
private static GridRecipe[] GetRecipesForOutput(ICoreClientAPI api, string outputKey)
{
if (recipesByOutput.TryGetValue(outputKey, out GridRecipe[]? cached))
{
return cached;
}
GridRecipe[] recipes = api.World.GridRecipes?
.Where(recipe => GetOutputKey(recipe) == outputKey)
.ToArray() ?? Array.Empty<GridRecipe>();
recipesByOutput[outputKey] = recipes;
return recipes;
}
private static string? GetOutputKey(GridRecipe? recipe)
{
CraftingRecipeIngredient? output = recipe?.Output;
if (output == null)
{
return null;
}
ItemStack? stack = output.ResolvedItemStack;
AssetLocation? code = stack?.Collectible?.Code ?? output.Code;
if (code == null)
{
return null;
}
EnumItemClass itemClass = stack?.Class ?? output.Type;
return itemClass + ":" + code;
}
}