/// <summary>
        /// Gets the current operation of the machine.
        /// Adapted from https://github.com/Digus/StardewValleyMods/blob/master/PFMAutomate/Automate/CustomProducerMachine.cs
        /// </summary>
        /// <returns></returns>
        public MachineState GetState()
        {
            if (Machine.heldObject.Value != null && Machine.minutesUntilReady <= 0 && Machine.readyForHarvest.Value)
            {
                return(MachineState.Done);
            }

            MassProductionMachineDefinition mpm;
            ProducerConfig producerConfig = GetCurrentConfig(out mpm);

            if (producerConfig != null)
            {
                if (!producerConfig.CheckWeatherCondition() || !producerConfig.CheckSeasonCondition() || !producerConfig.CheckLocationCondition(Location) ||
                    !producerConfig.CheckCurrentTimeCondition())
                {
                    return(MachineState.Disabled);
                }
                if (producerConfig.NoInputStartMode != null)
                {
                    //A no input machine is considered processing even while empty.
                    return(MachineState.Processing);
                }
            }
            else if (StaticValues.SUPPORTED_VANILLA_MACHINES.ContainsKey(Machine.name) &&
                     StaticValues.SUPPORTED_VANILLA_MACHINES[Machine.name] == InputRequirement.NoInputsOnly)
            {
                //A no input machine is considered processing even while empty.
                return(MachineState.Processing);
            }

            if (Machine.heldObject.Value == null)
            {
                return(MachineState.Empty);
            }

            return(MachineState.Processing);
        }
        /// <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);
        }
Example #3
0
        [HarmonyPriority(801)] //Just before ProducerFrameworkMod. Can't use HarmonyBefore attribute, that wasn't working for some reason
        internal static bool PerformObjectDropInAction(SObject __instance, Item dropInItem, bool probe, Farmer who, ref bool __result)
        {
            if (__instance.isTemporarilyInvisible || !(dropInItem is SObject))
            {
                return(false);
            }

            SObject input = dropInItem as SObject;
            bool    failLocationCondition = false;
            bool    failSeasonCondition   = false;

            MPMSettings upgradeSettings = ModEntry.GetSettingsFromItem(input.name);

            if (upgradeSettings != null)
            {
                if (!probe)
                {
                    //Change the machine's mass producer settings
                    MassProductionMachineDefinition mpm = ModEntry.GetMPMMachine(__instance.name, upgradeSettings.Key);

                    if (mpm == null)
                    {
                        Game1.showRedMessage("This cannot take that upgrade.");
                    }
                    else
                    {
                        string oldProducerKey = __instance.GetMassProducerKey();

                        if (!string.IsNullOrEmpty(oldProducerKey))
                        {
                            string         upgradeItemName = ModEntry.MPMSettings[oldProducerKey].UpgradeObject;
                            JsonAssets.Api jsonAssets      = ModEntry.Instance.Helper.ModRegistry.GetApi("spacechase0.JsonAssets") as JsonAssets.Api;
                            int            upgradeItemId   = jsonAssets.GetObjectId(upgradeItemName);

                            Game1.createItemDebris(new SObject(upgradeItemId, 1), __instance.TileLocation * Game1.tileSize, 0, who.currentLocation);
                        }

                        __instance.SetMassProducerKey(upgradeSettings.Key);
                        input.Stack -= 1;
                        __result     = input.Stack <= 0;

                        return(false);
                    }
                }
            }
            else
            {
                //Check if this is a valid input for the machine's use
                if (string.IsNullOrEmpty(__instance.GetMassProducerKey()))
                {
                    return(true);
                }

                if (__instance.heldObject.Value != null && !__instance.name.Equals("Crystalarium") || input.bigCraftable.Value)
                {
                    return(true);
                }

                MassProductionMachineDefinition mpm = ModEntry.GetMPMMachine(__instance.name, __instance.GetMassProducerKey());

                if (mpm == null)
                {
                    return(true);
                }

                if (StaticValues.SUPPORTED_VANILLA_MACHINES.ContainsKey(__instance.name))
                {
                    IVanillaOverride vanillaOverride = VanillaOverrideList.GetFor(__instance.name);

                    if (vanillaOverride != null)
                    {
                        bool overrideResult = vanillaOverride.Manual_PerformObjectDropInAction(__instance, input, probe, who, mpm);

                        //End early if a result has been found
                        if (overrideResult)
                        {
                            __result = input.Stack <= 0;
                            return(true);
                        }
                    }
                }

                ProducerConfig baseConfig = mpm.GetBaseProducerConfig();
                GameLocation   location   = who.currentLocation;

                if (baseConfig != null)
                {
                    //TOREVIEW: maybe have machines that can break these conditions?
                    if (!baseConfig.CheckLocationCondition(location))
                    {
                        failLocationCondition = true;
                    }
                    if (!baseConfig.CheckSeasonCondition())
                    {
                        failSeasonCondition = true;
                    }
                    if (baseConfig.NoInputStartMode != null)
                    {
                        return(true);
                    }
                }

                if (ProducerController.GetProducerItem(__instance.name, input) is ProducerRule producerRule)
                {
                    if (PFMCompatability.IsInputExcluded(producerRule, mpm, input))
                    {
                        return(true);
                    }

                    if (__instance.bigCraftable.Value && !probe && __instance.heldObject.Value == null)
                    {
                        __instance.scale.X = 5f;
                    }

                    try
                    {
                        if (failLocationCondition)
                        {
                            throw new RestrictionException("Machine can't be used in this location.");
                        }
                        if (failSeasonCondition)
                        {
                            throw new RestrictionException("Machine can't be used in this season.");
                        }

                        List <InputInfo> inputAndFuelInfo = InputInfo.ConvertPFMInputs(producerRule, input);
                        PFMCompatability.ValidateIfInputsLessThanRequired(producerRule, mpm.Settings, inputAndFuelInfo, who);

                        Dictionary <int, int> fuelQuantities = new Dictionary <int, int>();

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

                        Func <int, int, bool> fuelSearch   = (i, q) => who.hasItemInInventory(i, fuelQuantities[i]);
                        OutputConfig          outputConfig = PFMCompatability.ProduceOutput(producerRule, mpm.Settings, __instance,
                                                                                            fuelSearch, who, location, baseConfig, input, mpm.Settings.CalculateInputRequired(inputAndFuelInfo.First()), probe,
                                                                                            inputInfo: inputAndFuelInfo);

                        if (outputConfig != null)
                        {
                            if (!probe)
                            {
                                foreach (InputInfo inputInfo in inputAndFuelInfo)
                                {
                                    if (inputInfo.IsFuel)
                                    {
                                        RemoveItemsFromInventory(who, inputInfo.ID, mpm.Settings.CalculateInputRequired(inputInfo));
                                    }
                                }

                                List <InputInfo> outputConfigFuels = InputInfo.ConvertPFMInputs(outputConfig);

                                foreach (InputInfo fuel in outputConfigFuels)
                                {
                                    RemoveItemsFromInventory(who, fuel.ID, mpm.Settings.CalculateInputRequired(fuel));
                                }

                                input.Stack -= mpm.Settings.CalculateInputRequired(inputAndFuelInfo.First());
                                __result     = input.Stack <= 0;
                            }
                            else
                            {
                                __result = true;
                            }
                        }
                    }
                    catch (RestrictionException e)
                    {
                        __result = false;
                        if (e.Message != null && !probe && who.IsLocalPlayer)
                        {
                            Game1.showRedMessage(e.Message);
                        }
                    }
                    return(false);
                }
            }

            return(!failLocationCondition && !failSeasonCondition);
        }
        public bool SetInput(IStorage input)
        {
            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(producerRule, input);
                            if (requiredFuels != null)
                            {
                                try
                                {
                                    OutputConfig outputConfig = ProducerRuleController.ProduceOutput(producerRule, _machine,
                                                                                                     (i, q) => input.TryGetIngredient(i, q, out IConsumable fuel), null, Location,
                                                                                                     producerConfig, objectInput, noSoundAndAnimation: true);
                                    if (outputConfig != null)
                                    {
                                        inputConsumable.Take();
                                        requiredFuels.ForEach(f => f.Reduce());
                                        List <IConsumable> outputRequiredFuels = GetRequiredFuels(outputConfig, input);
                                        outputRequiredFuels.ForEach(f => f.Reduce());
                                        return(true);
                                    }
                                }
                                catch (RestrictionException) { /* No action needed */ }
                            }
                        }
                    }
                }
            }
            return(false);
        }
Example #5
0
        internal static bool PerformObjectDropInAction(Object __instance, Item dropInItem, bool probe, Farmer who, ref bool __result)
        {
            if (__instance.isTemporarilyInvisible || !(dropInItem is Object))
            {
                return(false);
            }
            Object input = (Object)dropInItem;

            bool failLocationCondition = false;
            bool failSeasonCondition   = false;

            if (__instance.heldObject.Value != null && !__instance.name.Equals("Crystalarium") || input.bigCraftable.Value)
            {
                return(true);
            }

            ProducerConfig producerConfig = ProducerController.GetProducerConfig(__instance.Name);

            GameLocation location = who.currentLocation;

            if (producerConfig != null)
            {
                if (!producerConfig.CheckLocationCondition(location))
                {
                    failLocationCondition = true;
                }
                if (!producerConfig.CheckSeasonCondition())
                {
                    failSeasonCondition = true;
                }
                if (producerConfig.NoInputStartMode != null)
                {
                    return(false);
                }
            }

            if (ProducerController.GetProducerItem(__instance.name, input) is ProducerRule producerRule)
            {
                if (ProducerRuleController.IsInputExcluded(producerRule, input))
                {
                    return(true);
                }

                if (__instance.bigCraftable.Value && !probe && __instance.heldObject.Value == null)
                {
                    __instance.scale.X = 5f;
                }

                try
                {
                    if (failLocationCondition)
                    {
                        throw new RestrictionException(DataLoader.Helper.Translation.Get("Message.Condition.Location"));
                    }
                    if (failSeasonCondition)
                    {
                        throw new RestrictionException(DataLoader.Helper.Translation.Get("Message.Condition.Season"));
                    }

                    ProducerRuleController.ValidateIfInputStackLessThanRequired(producerRule, input);
                    ProducerRuleController.ValidateIfAnyFuelStackLessThanRequired(producerRule, who);

                    OutputConfig outputConfig = ProducerRuleController.ProduceOutput(producerRule, __instance,
                                                                                     (i, q) => who.hasItemInInventory(i, q), who, location, producerConfig, input, probe);
                    if (outputConfig != null)
                    {
                        if (!probe)
                        {
                            foreach (var fuel in producerRule.FuelList)
                            {
                                RemoveItemsFromInventory(who, fuel.Item1, fuel.Item2);
                            }

                            foreach (var fuel in outputConfig.FuelList)
                            {
                                RemoveItemsFromInventory(who, fuel.Item1, fuel.Item2);
                            }

                            input.Stack -= producerRule.InputStack;
                            __result     = input.Stack <= 0;
                        }
                        else
                        {
                            __result = true;
                        }
                    }
                }
                catch (RestrictionException e)
                {
                    __result = false;
                    if (e.Message != null && !probe && who.IsLocalPlayer)
                    {
                        Game1.showRedMessage(e.Message);
                    }
                }
                return(false);
            }
            return(!failLocationCondition && !failSeasonCondition);
        }