Ejemplo n.º 1
0
        /// <summary>
        /// Adapted from https://github.com/Digus/StardewValleyMods/blob/master/PFMAutomate/Automate/CustomProducerMachine.cs
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public bool SetInput(IStorage input)
        {
            if (IsMassProducer)
            {
                MassProductionMachineDefinition mpm = ModEntry.GetMPMMachine(Machine.name, Machine.GetMassProducerKey());

                foreach (ITrackedStack trackedStack in input.GetItems())
                {
                    if (trackedStack.Sample is SObject objectInput && !objectInput.bigCraftable.Value &&
                        ProducerController.GetProducerItem(Machine.Name, objectInput) is ProducerRule producerRule &&
                        !PFMCompatability.IsInputExcluded(producerRule, mpm, objectInput))
                    {
                        ProducerConfig producerConfig = mpm.GetBaseProducerConfig();

                        if (producerConfig == null || (producerConfig.CheckLocationCondition(Location) && producerConfig.CheckSeasonCondition()))
                        {
                            List <InputInfo> inputsRequired = InputInfo.ConvertPFMInputs(producerRule, objectInput);

                            if (inputsRequired.Count > 0 &&
                                input.TryGetIngredient(objectInput.ParentSheetIndex, mpm.Settings.CalculateInputRequired(inputsRequired.First()), out IConsumable inputConsumable))
                            {
                                objectInput = inputConsumable.Sample as SObject;
                                List <IConsumable> requiredFuels = GetRequiredFuels(inputsRequired, mpm.Settings, input);

                                if (requiredFuels != null)
                                {
                                    try
                                    {
                                        Dictionary <int, int> fuelQuantities = new Dictionary <int, int>();

                                        foreach (InputInfo inputInfo in inputsRequired)
                                        {
                                            if (inputInfo.IsFuel)
                                            {
                                                fuelQuantities.Add(inputInfo.ID, mpm.Settings.CalculateInputRequired(inputInfo));
                                            }
                                        }

                                        Func <int, int, bool> fuelSearch   = (i, q) => input.TryGetIngredient(i, fuelQuantities[i], out IConsumable fuel);
                                        OutputConfig          outputConfig = PFMCompatability.ProduceOutput(producerRule, mpm.Settings, Machine, fuelSearch, null, Location, producerConfig,
                                                                                                            objectInput, inputQuantity: mpm.Settings.CalculateInputRequired(inputsRequired.First()), noSoundAndAnimation: true,
                                                                                                            inputInfo: inputsRequired);

                                        if (outputConfig != null)
                                        {
                                            inputConsumable.Take();
                                            requiredFuels.ForEach(f => f.Reduce());
                                            List <IConsumable> outputRequiredFuels = GetRequiredFuels(InputInfo.ConvertPFMInputs(outputConfig), mpm.Settings, input);
                                            outputRequiredFuels.ForEach(f => f.Reduce());
                                            return(true);
                                        }
                                    }
                                    catch (RestrictionException) { /* No action needed */ }
                                }
                            }
                        }
                    }
                }
            }
            else
            {
                foreach (ITrackedStack trackedStack in input.GetItems())
                {
                    if (trackedStack.Sample is SObject objectInput && !objectInput.bigCraftable.Value &&
                        ProducerController.GetProducerItem(Machine.Name, objectInput) is ProducerRule producerRule &&
                        !ProducerRuleController.IsInputExcluded(producerRule, objectInput))
                    {
                        ProducerConfig producerConfig = ProducerController.GetProducerConfig(Machine.name);

                        if (producerConfig == null || (producerConfig.CheckLocationCondition(Location) && producerConfig.CheckSeasonCondition()))
                        {
                            if (input.TryGetIngredient(objectInput.ParentSheetIndex, producerRule.InputStack, out IConsumable inputConsumable))
                            {
                                objectInput = inputConsumable.Sample as SObject;
                                List <IConsumable> requiredFuels = GetRequiredFuels(InputInfo.ConvertPFMInputs(producerRule, objectInput), null, input);

                                if (requiredFuels != null)
                                {
                                    try
                                    {
                                        Func <int, int, bool> fuelSearch   = (i, q) => input.TryGetIngredient(i, q, out IConsumable fuel);
                                        OutputConfig          outputConfig = ProducerRuleController.ProduceOutput(producerRule, Machine, fuelSearch, null, Location, producerConfig,
                                                                                                                  objectInput, noSoundAndAnimation: true);

                                        if (outputConfig != null)
                                        {
                                            inputConsumable.Take();
                                            requiredFuels.ForEach(f => f.Reduce());
                                            List <IConsumable> outputRequiredFuels = GetRequiredFuels(InputInfo.ConvertPFMInputs(outputConfig), null, input);
                                            outputRequiredFuels.ForEach(f => f.Reduce());
                                            return(true);
                                        }
                                    }
                                    catch (RestrictionException) { /* No action needed */ }
                                }
                            }
                        }
                    }
                }
            }

            return(false);
        }