public bool lookForIngredient(RecipeBlueprint srb, IngredientBlueprint ib, IStorage input, out IConsumable consumable) { return(input.TryGetIngredient((t) => { var item = t.Sample; item.Stack = t.Count; if (item is StardewValley.Object obj) { return srb.fitsIngredient(item, srb.materials); } return false; }, ib.stack, out consumable)); }
/// <summary>Provide input to the machine.</summary> /// <param name="input">The available items.</param> /// <returns>Returns whether the machine started processing an item.</returns> public bool SetInput(IStorage input) { if (!Machine.blueprint.production.Exists(p => p.materials != null && p.materials.Count > 0)) { return(false); } if (Machine.blueprint.conditionaldropin && !Machine.checkedToday) { Machine.meetsConditions = PyUtils.CheckEventConditions(Machine.conditions, this); Machine.checkedToday = true; } if (Machine.blueprint.conditionaldropin && !Machine.meetsConditions) { return(false); } List <IConsumable> consumables = new List <IConsumable>(); IConsumable starterItem = null; if (Machine.blueprint.starter is IngredientBlueprint ib && Machine.starterRecipe is RecipeBlueprint srb && !lookForIngredient(srb, ib, input, out starterItem)) { return(false); } if (starterItem is IConsumable) { consumables.Add(starterItem); } List <RecipeBlueprint> checkedRecipes = new List <RecipeBlueprint>(); List <IngredientBlueprint> checkedIngredients = new List <IngredientBlueprint>(); RecipeBlueprint recipe = null; StardewValley.Object dropIn = null; foreach (ITrackedStack item in input.GetItems()) { if (item.Sample is StardewValley.Object obj && Machine.findRecipeForItemType(obj) is RecipeBlueprint rb && !checkedRecipes.Contains(rb)) { checkedRecipes.Add(rb); List <IConsumable> iMaterials = new List <IConsumable>(); bool hasAll = true; foreach (IngredientBlueprint ing in rb.materials) { IConsumable nextItem = null; if (!checkedIngredients.Contains(ing) && lookForIngredient(rb, ing, input, out nextItem)) { iMaterials.Add(nextItem); } else { if (!checkedIngredients.Contains(ing)) { checkedIngredients.Add(ing); } hasAll = false; break; } } if (hasAll) { recipe = rb; dropIn = obj; consumables.AddRange(iMaterials); break; } } } if (recipe is RecipeBlueprint foundRecipe) { List <Item> materials = new List <Item>(); foreach (IConsumable con in consumables) { materials.Add(con.Take()); } Machine.startProduction(dropIn, recipe, new List <IList <Item> >() { materials }); return(true); } return(false); }