/// <summary> /// Adapted from https://github.com/Digus/StardewValleyMods/blob/master/PFMAutomate/Automate/CustomProducerMachine.cs /// </summary> /// <param name="item"></param> internal void Reset(Item item) { PFMCompatability.ClearProduction(Machine, Location); MassProductionMachineDefinition mpm = null; ProducerConfig producerConfig; if (IsMassProducer) { mpm = ModEntry.GetMPMMachine(Machine.name, Machine.GetMassProducerKey()); producerConfig = ProducerController.GetProducerConfig(mpm.BaseProducerName); } else { producerConfig = ProducerController.GetProducerConfig(Machine.name); } if (producerConfig == null) { return; } else if (producerConfig.NoInputStartMode != null || producerConfig.IncrementStatsOnOutput.Count > 0) { producerConfig.IncrementStats(item); if (producerConfig.NoInputStartMode == NoInputStartMode.Placement) { if (ProducerController.GetProducerItem(Machine.Name, null) is ProducerRule producerRule) { try { if (producerConfig.CheckLocationCondition(Location) && producerConfig.CheckSeasonCondition()) { if (mpm != null) { PFMCompatability.ProduceOutput(producerRule, mpm.Settings, Machine, (i, q) => true, null, Location, producerConfig); } else { ProducerRuleController.ProduceOutput(producerRule, Machine, (i, q) => true, null, Location, producerConfig); } } } catch (RestrictionException) { /*No action needed*/ } } } } }
/// <summary> /// Gets the output. /// Adapted from https://github.com/Digus/StardewValleyMods/blob/master/PFMAutomate/Automate/CustomProducerMachine.cs /// </summary> /// <returns></returns> public ITrackedStack GetOutput() { PFMCompatability.PrepareOutput(Machine, Location, Game1.getFarmer(Machine.owner)); return(new TrackedItem(Machine.heldObject.Value, onEmpty: Reset)); }
/// <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); }