[HarmonyPriority(801)] //Just before ProducerFrameworkMod. Can't use HarmonyBefore attribute, that wasn't working for some reason internal static bool performDropDownAction_Prefix(SObject __instance, Farmer who, bool __result) { if (string.IsNullOrEmpty(__instance.GetMassProducerKey())) { return(true); } MassProductionMachineDefinition mpm = ModEntry.GetMPMMachine(__instance.name, __instance.GetMassProducerKey()); if (mpm == null) { return(true); } if (ProducerController.GetProducerConfig(__instance.Name) is ProducerConfig producerConfig) { try { if (!producerConfig.CheckLocationCondition(who.currentLocation)) { throw new RestrictionException(ModEntry.Instance.Helper.Translation.Get("Message.Condition.Location")); } if (producerConfig.NoInputStartMode != null) { if (producerConfig.CheckSeasonCondition() && NoInputStartMode.Placement == producerConfig.NoInputStartMode) { if (ProducerController.GetProducerItem(__instance.Name, null) is ProducerRule producerRule) { PFMCompatability.ProduceOutput(producerRule, mpm.Settings, __instance, (i, q) => who.hasItemInInventory(i, q), who, who.currentLocation, producerConfig); } } return(__result = false); } } catch (RestrictionException e) { if (e.Message != null && who.IsLocalPlayer) { Game1.showRedMessage(e.Message); } return(__result = false); } } else if (StaticValues.SUPPORTED_VANILLA_MACHINES.ContainsKey(__instance.name) && StaticValues.SUPPORTED_VANILLA_MACHINES[__instance.name] == InputRequirement.NoInputsOnly) { IVanillaOverride vanillaOverride = VanillaOverrideList.GetFor(__instance.name); if (vanillaOverride != null && vanillaOverride.Manual_PerformDropDownAction(__instance, mpm)) { return(true); } } return(true); }
/// <summary> /// Check if an input is excluded by a producer rule or a mass production machine definition. /// Adapted from https://github.com/Digus/StardewValleyMods/blob/master/ProducerFrameworkMod/ProducerRuleController.cs /// </summary> /// <param name="producerRule">The producer rule to check.</param> /// <param name="mpm">The definition of the mass production machine to check.</param> /// <param name="input">The input to check.</param> /// <returns>True if it should be excluded.</returns> public static bool IsInputExcluded(ProducerRule producerRule, MassProductionMachineDefinition mpm, SObject input) { bool isExcludedByRule = producerRule.ExcludeIdentifiers != null && (producerRule.ExcludeIdentifiers.Contains(input.ParentSheetIndex.ToString()) || producerRule.ExcludeIdentifiers.Contains(input.Name) || producerRule.ExcludeIdentifiers.Contains(input.Category.ToString()) || producerRule.ExcludeIdentifiers.Intersect(input.GetContextTags()).Any()); bool isExcludedByMPM = mpm.BlacklistedInputKeys.Contains(input.ParentSheetIndex.ToString()) || mpm.BlacklistedInputKeys.Contains(input.Name) || mpm.BlacklistedInputKeys.Contains(input.Category.ToString()) || mpm.BlacklistedInputKeys.Intersect(input.GetContextTags()).Any(); return(isExcludedByRule || isExcludedByMPM); }
/// <summary> /// Sets up the mod. /// </summary> /// <param name="sender"></param> /// <param name="args"></param> private void OnSaveLoaded(object sender, SaveLoadedEventArgs args) { //Clear out old data if any exists MPMSettings.Clear(); MPMDefinitionSet.Clear(); if (MPMManager != null) { MPMManager.Clear(); } //Load content packs Monitor.Log("Loading content packs...", LogLevel.Info); string filepath = "upgrades.json"; foreach (IContentPack contentPack in Helper.ContentPacks.GetOwned()) { if (contentPack.HasFile(filepath)) { List <MPMSettings> loaded = contentPack.ReadJsonFile <List <MPMSettings> >(filepath); foreach (MPMSettings setting in loaded) { if (MPMSettings.ContainsKey(setting.Key)) { Monitor.Log($"Content pack {contentPack.Manifest.Name} {contentPack.Manifest.Version} tried to add upgrade settings for existing key '{setting.Key}'! " + "Settings left unchanged.", LogLevel.Warn); } else { MPMSettings.Add(setting.Key, setting); } } Monitor.Log($"Loaded content pack {contentPack.Manifest.Name} {contentPack.Manifest.Version} by {contentPack.Manifest.Author}.", LogLevel.Info); } else { Monitor.Log($"Failed reading content pack {contentPack.Manifest.Name} {contentPack.Manifest.Version}: missing file {filepath}.", LogLevel.Warn); } } //Create mail to send recipes MailManager.SetupMail(); //Set up machines to work with PFM Monitor.Log("Defining machines...", LogLevel.Info); MPMDefinitionSet = MassProductionMachineDefinition.Setup(MPMSettings); SeedMakerOverride.Initialize(); //Start manager, loading saved data MPMManager = new MPMManager(); Helper.Events.GameLoop.Saving += OnSave; }
[HarmonyPriority(Priority.First + 1)] //Just before ProducerFrameworkMod. Can't use HarmonyBefore attribute, that wasn't working for some reason public static bool DayUpdate_Prefix(SObject __instance, GameLocation location) { if (__instance == null || string.IsNullOrEmpty(__instance.GetMassProducerKey())) { return(true); } MassProductionMachineDefinition mpm = ModEntry.GetMPMMachine(__instance.name, __instance.GetMassProducerKey()); if (mpm == null) { return(true); } if (__instance.bigCraftable.Value) { if (ProducerController.GetProducerConfig(__instance.Name) is ProducerConfig producerConfig) { if (producerConfig != null) { if (ProducerController.GetProducerItem(__instance.Name, null) is ProducerRule producerRule) { if (!producerConfig.CheckSeasonCondition() || !producerConfig.CheckLocationCondition(location)) { ProducerRuleController.ClearProduction(__instance, location); return(false); } else if (producerConfig.NoInputStartMode != null) { if (producerConfig.NoInputStartMode == NoInputStartMode.DayUpdate || producerConfig.NoInputStartMode == NoInputStartMode.Placement) { if (__instance.heldObject.Value == null) { try { Farmer who = Game1.getFarmer((long)__instance.owner); PFMCompatability.ProduceOutput(producerRule, mpm.Settings, __instance, (i, q) => who.hasItemInInventory(i, q), who, who.currentLocation, producerConfig); } catch (RestrictionException) { //Does not show the restriction error since the machine is auto-starting. } } } return(false); } } } } } return(true); }
/// <summary> /// Adapted from https://github.com/Digus/StardewValleyMods/blob/master/ProducerFrameworkMod/ProducerRuleController.cs /// </summary> /// <param name="producer"></param> /// <param name="location"></param> /// <param name="who"></param> public static void PrepareOutput(SObject producer, GameLocation location, Farmer who) { if (string.IsNullOrEmpty(producer.GetMassProducerKey())) { return; } MassProductionMachineDefinition mpm = ModEntry.GetMPMMachine(producer.name, producer.GetMassProducerKey()); if (mpm == null) { return; } foreach (ProducerRule producerRule in ProducerController.GetProducerRules(mpm.BaseProducerName)) { if (producerRule.LookForInputWhenReady is InputSearchConfig inputSearchConfig) { if (producerRule.OutputConfigs.Find(o => o.OutputIndex == producer.heldObject.Value.ParentSheetIndex) is OutputConfig outputConfig) { SObject input = SearchInput(location, producer.tileLocation, inputSearchConfig); List <InputInfo> inputInfo = InputInfo.ConvertPFMInputs(producerRule, input); SObject output = OutputConfigController.CreateOutput(outputConfig, input, ProducerRuleController.GetRandomForProducing(producer.tileLocation)); output.Stack = mpm.Settings.CalculateOutputProduced(output.stack, inputInfo.ToArray()); if (mpm.Settings.Quality.HasValue) { if (mpm.Settings.Quality == QualitySetting.KeepInput) { output.Quality = input.Quality; } else { output.Quality = mpm.Settings.GetOutputQuality(); } } producer.heldObject.Value = output; OutputConfigController.LoadOutputName(outputConfig, producer.heldObject.Value, input, who); break; } } } }
[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); }