public static void MoveOldPregnancyData() { if (Context.IsMainPlayer) { if (FarmerData?.PregnancyData == null || FarmerData.PregnancyData.Count == 0) { return; } AnimalHusbandryModEntry.monitor.Log($"Migrating pregnancy data from old format to the new one.", LogLevel.Info); FarmerData?.PregnancyData?.RemoveAll(p => { try { FarmAnimal farmAnimal = Utility.getAnimal(p.Id); if (farmAnimal != null) { farmAnimal.allowReproduction.Value = p.AllowReproductionBeforeInsemination; PregnancyController.AddPregnancy(farmAnimal, p.DaysUntilBirth); farmAnimal.allowReproduction.Value = false; } else { AnimalHusbandryModEntry.monitor.Log($"The animal id '{p.Id}' was not found in the game and its pregnancy data is being discarded.", LogLevel.Warn); } return(true); } catch (Exception e) { AnimalHusbandryModEntry.monitor.Log($"Unexpected error while trying to migrate pregnancy data of animal id '{p.Id}'. The pregnancy data will be kept on the old format but will not be used.", LogLevel.Warn); AnimalHusbandryModEntry.monitor.Log($"Message from pregnancy error above: {e.Message}"); return(false); } }); } }
/// <summary>Raised before the game is saved.</summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnSaving(object sender, SavingEventArgs e) { if (!_isEnabled) { return; } EventsLoader.CheckUnseenEvents(); if (!DataLoader.ModConfig.DisablePregnancy) { PregnancyController.UpdatePregnancy(); } }
public static bool DoFunction(MilkPail __instance, GameLocation location, int x, int y, int power, StardewValley.Farmer who) { if (!IsInseminationSyringe(__instance)) { return(true); } string inseminationSyringeId = __instance.modData[InseminationSyringeKey]; BaseToolDoFunction(__instance, location, x, y, power, who); __instance.CurrentParentTileIndex = InitialParentTileIndex; __instance.indexOfMenuItemView.Value = IndexOfMenuItemView; Animals.TryGetValue(inseminationSyringeId, out FarmAnimal animal); if (animal != null) { who.Stamina -= ((float)4f - (float)who.FarmingLevel * 0.2f); int daysUtillBirth = ((ImpregnatableAnimalItem)DataLoader.AnimalData.GetAnimalItem(animal)).MinimumDaysUtillBirth.Value; if (!DataLoader.ModConfig.DisableContestBonus && AnimalContestController.HasFertilityBonus(animal)) { daysUtillBirth -= (int)Math.Round(daysUtillBirth / 10.0, MidpointRounding.AwayFromZero); } PregnancyController.AddPregnancy(animal, daysUtillBirth); animal.allowReproduction.Value = false; --__instance.attachments[0].Stack; if (__instance.attachments[0].Stack <= 0) { Game1.showGlobalMessage(DataLoader.i18n.Get("Tool.InseminationSyringe.ItemConsumed", new { itemName = __instance.attachments[0].DisplayName })); __instance.attachments[0] = null; } Animals[inseminationSyringeId] = (FarmAnimal)null; } if (Game1.activeClickableMenu == null) { who.CanMove = true; who.completelyStopAnimatingOrDoingAction(); } else { who.Halt(); } who.UsingTool = false; who.canReleaseTool = true; DataLoader.Helper.Reflection.GetMethod(__instance, "finish").Invoke(); return(false); }
/// <summary>Raised after the game begins a new day (including when the player loads a save).</summary> /// <param name="sender">The event sender.</param> /// <param name="e">The event data.</param> private void OnDayStarted(object sender, DayStartedEventArgs e) { if (!_isEnabled) { return; } if (!DataLoader.ModConfig.DisableAnimalContest) { EventsLoader.CheckEventDay(); } if (!DataLoader.ModConfig.DisablePregnancy) { PregnancyController.CheckForBirth(); } }
/// <summary>Raised after the game begins a new day (including when the player loads a save).</summary> /// <param name="sender">The event sender.</param> /// <param name="e">The event data.</param> private void OnDayStarted(object sender, DayStartedEventArgs e) { if (!IsEnabled) { return; } DataLoader.LivingWithTheAnimalsChannel.CheckChannelDay(); if (!DataLoader.ModConfig.DisableMeat) { DataLoader.RecipeLoader.MeatFridayChannel.CheckChannelDay(); } if (!DataLoader.ModConfig.DisablePregnancy) { PregnancyController.CheckForBirth(); PregnancyController.UpdatePregnancy(); } }
/// <summary>Raised before the game is saved.</summary> /// <param name="sender"></param> /// <param name="e"></param> private void OnSaving(object sender, SavingEventArgs e) { if (!_isEnabled) { return; } if (Context.IsMainPlayer) { ItemUtility.RemoveModdedItemAnywhere(ParticipantRibbonOverrides.ParticipantRibbonKey); } EventsLoader.CheckUnseenEvents(); if (!DataLoader.ModConfig.DisablePregnancy && Context.IsMainPlayer) { PregnancyController.UpdatePregnancy(); } FarmerLoader.SaveData(); }
public override void DoFunction(GameLocation location, int x, int y, int power, StardewValley.Farmer who) { this.lastUser = who; Game1.recentMultiplayerRandom = new Random((int)(short)Game1.random.Next((int)short.MinValue, 32768)); this.CurrentParentTileIndex = InitialParentTileIndex; this.indexOfMenuItemView.Value = IndexOfMenuItemView; if (this._animal != null) { who.Stamina -= ((float)4f - (float)who.FarmingLevel * 0.2f); int daysUtillBirth = ((ImpregnatableAnimalItem)DataLoader.AnimalData.GetAnimalItem(this._animal)).MinimumDaysUtillBirth.Value; if (!DataLoader.ModConfig.DisableContestBonus && AnimalContestController.HasFertilityBonus(_animal)) { daysUtillBirth -= (int)Math.Round(daysUtillBirth / 10.0, MidpointRounding.AwayFromZero); } PregnancyController.AddPregnancy(new PregnancyItem(this._animal.myID.Value, daysUtillBirth, this._animal.allowReproduction.Value)); this._animal.allowReproduction.Value = false; --this.attachments[0].Stack; if (this.attachments[0].Stack <= 0) { Game1.showGlobalMessage(DataLoader.i18n.Get("Tool.InseminationSyringe.ItemConsumed", new { itemName = this.attachments[0].DisplayName })); this.attachments[0] = (StardewValley.Object)null; } this._animal = (FarmAnimal)null; } if (Game1.activeClickableMenu == null) { who.CanMove = true; who.completelyStopAnimatingOrDoingAction(); } else { who.Halt(); } who.UsingTool = false; who.canReleaseTool = true; DataLoader.Helper.Reflection.GetMethod(this, "finish").Invoke(); }
public static bool beginUsing(MilkPail __instance, GameLocation location, int x, int y, StardewValley.Farmer who, ref bool __result) { if (!IsInseminationSyringe(__instance)) { return(true); } string inseminationSyringeId = __instance.modData[InseminationSyringeKey]; x = (int)who.GetToolLocation(false).X; y = (int)who.GetToolLocation(false).Y; Rectangle rectangle = new Rectangle(x - Game1.tileSize / 2, y - Game1.tileSize / 2, Game1.tileSize, Game1.tileSize); if (!DataLoader.ModConfig.DisablePregnancy) { if (location is Farm) { foreach (FarmAnimal farmAnimal in (location as Farm).animals.Values) { if (farmAnimal.GetBoundingBox().Intersects(rectangle)) { Animals[inseminationSyringeId] = farmAnimal; break; } } } else if (location is AnimalHouse) { foreach (FarmAnimal farmAnimal in (location as AnimalHouse).animals.Values) { if (farmAnimal.GetBoundingBox().Intersects(rectangle)) { Animals[inseminationSyringeId] = farmAnimal; break; } } } } Animals.TryGetValue(inseminationSyringeId, out FarmAnimal animal); if (animal != null) { string dialogue = ""; if (__instance.attachments[0] == null) { Game1.showRedMessage(DataLoader.i18n.Get("Tool.InseminationSyringe.Empty")); Animals[inseminationSyringeId] = null; } else if (AnimalExtension.GetAnimalFromType(animal.type.Value) == null) { dialogue = DataLoader.i18n.Get("Tool.InseminationSyringe.CantBeInseminated", new { animalName = animal.displayName }); } else if (IsEggAnimal(animal)) { dialogue = DataLoader.i18n.Get("Tool.InseminationSyringe.EggAnimal", new { animalName = animal.displayName }); } else if (!((ImpregnatableAnimalItem)DataLoader.AnimalData.GetAnimalItem(animal)).MinimumDaysUtillBirth.HasValue) { dialogue = DataLoader.i18n.Get("Tool.InseminationSyringe.CantBeInseminated", new { animalName = animal.displayName }); } else if (animal.isBaby()) { dialogue = DataLoader.i18n.Get("Tool.InseminationSyringe.TooYoung", new { animalName = animal.displayName }); } else if (PregnancyController.IsAnimalPregnant(animal)) { int daysUntilBirth = animal.GetDaysUntilBirth().Value; if (daysUntilBirth > 1) { dialogue = DataLoader.i18n.Get("Tool.InseminationSyringe.AlreadyPregnant", new { animalName = animal.displayName, numberOfDays = daysUntilBirth }); } else { dialogue = DataLoader.i18n.Get("Tool.InseminationSyringe.ReadyForBirth", new { animalName = animal.displayName }); } } else if (!CheckCorrectProduct(animal, __instance.attachments[0])) { var data = DataLoader.Helper.Content.Load <Dictionary <int, string> >(@"Data\ObjectInformation.xnb", ContentSource.GameContent); string produceName = data[animal.defaultProduceIndex.Value].Split('/')[4]; dialogue = DataLoader.i18n.Get("Tool.InseminationSyringe.CorrectItem", new { itemName = produceName }); } else if (PregnancyController.CheckBuildingLimit(animal)) { dialogue = DataLoader.i18n.Get("Tool.InseminationSyringe.BuildingLimit", new { buildingType = animal.displayHouse }); } else { animal.doEmote(16, true); if (who != null && Game1.player.Equals(who)) { if (animal.sound.Value != null) { ICue animalSound = Game1.soundBank.GetCue(animal.sound.Value); animalSound.Play(); } DelayedAction.playSoundAfterDelay("fishingRodBend", 300, location); DelayedAction.playSoundAfterDelay("fishingRodBend", 1200, location); } animal.pauseTimer = 1500; } if (dialogue.Length > 0) { if (who != null && Game1.player.Equals(who)) { DelayedAction.showDialogueAfterDelay(dialogue, 150); } Animals[inseminationSyringeId] = null; } } who.Halt(); int currentFrame = who.FarmerSprite.currentFrame; if (animal != null) { who.FarmerSprite.animateOnce(287 + who.FacingDirection, 50f, 4); } else { who.FarmerSprite.animateOnce(new FarmerSprite.AnimationFrame[1] { new FarmerSprite.AnimationFrame(currentFrame, 0, false, who.FacingDirection == 3, new AnimatedSprite.endOfAnimationBehavior(StardewValley.Farmer.useTool), true) }); } who.FarmerSprite.oldFrame = currentFrame; who.UsingTool = true; who.CanMove = false; __result = true; return(false); }
/********* ** Public methods *********/ /// <summary>The mod entry point, called after the mod is first loaded.</summary> /// <param name="helper">Provides simplified APIs for writing mods.</param> public override void Entry(IModHelper helper) { ModHelper = helper; monitor = Monitor; if (this.Helper.ModRegistry.IsLoaded("DIGUS.BUTCHER")) { Monitor.Log("Animal Husbandry Mod can't run along side its older version, ButcherMod. " + "You need to copy the 'data' directory from the ButcherMod directory, into the AnimalHusbandryMod directory, then delete the ButcherMod directory. " + "Animal Husbandry Mod won't load until this is done.", LogLevel.Error); } else { DataLoader = new DataLoader(helper); _meatCleaverSpawnKey = DataLoader.ModConfig.AddMeatCleaverToInventoryKey; _inseminationSyringeSpawnKey = DataLoader.ModConfig.AddInseminationSyringeToInventoryKey; _feedingBasketSpawnKey = DataLoader.ModConfig.AddFeedingBasketToInventoryKey; SaveEvents.AfterLoad += DataLoader.ToolsLoader.ReplaceOldTools; SaveEvents.AfterLoad += (x, y) => FarmerLoader.LoadData(); SaveEvents.AfterLoad += (x, y) => DataLoader.ToolsLoader.LoadMail(); TimeEvents.AfterDayStarted += (x, y) => DataLoader.LivingWithTheAnimalsChannel.CheckChannelDay(); //TimeEvents.AfterDayStarted += (x, y) => EventsLoader.CheckEventDay(); if (!DataLoader.ModConfig.DisableMeat) { TimeEvents.AfterDayStarted += (x, y) => DataLoader.RecipeLoader.MeatFridayChannel.CheckChannelDay(); ModHelper.ConsoleCommands.Add("player_addallmeatrecipes", "Add all meat recipes to the player.", DataLoader.RecipeLoader.AddAllMeatRecipes); } if (_meatCleaverSpawnKey != null || _inseminationSyringeSpawnKey != null || _feedingBasketSpawnKey != null) { ControlEvents.KeyPressed += this.ControlEvents_KeyPress; } if (!DataLoader.ModConfig.DisablePregnancy) { TimeEvents.AfterDayStarted += (x, y) => PregnancyController.CheckForBirth(); SaveEvents.BeforeSave += (x, y) => PregnancyController.UpdatePregnancy(); } var harmony = HarmonyInstance.Create("Digus.AnimalHusbandryMod"); try { var farmAnimalPet = typeof(FarmAnimal).GetMethod("pet"); var animalQueryMenuExtendedPet = typeof(AnimalQueryMenuExtended).GetMethod("Pet"); harmony.Patch(farmAnimalPet, new HarmonyMethod(animalQueryMenuExtendedPet), null); } catch (Exception) { Monitor.Log("Erro patching the FarmAnimal 'pet' Method. Applying old method of opening the extended animal query menu.", LogLevel.Warn); MenuEvents.MenuChanged += (s, e) => { if (e.NewMenu is AnimalQueryMenu && !(e.NewMenu is AnimalQueryMenuExtended)) { Game1.activeClickableMenu = new AnimalQueryMenuExtended(this.Helper.Reflection.GetField <FarmAnimal>(e.NewMenu, "animal").GetValue()); } }; } if (!DataLoader.ModConfig.DisableRancherMeatPriceAjust) { var sellToStorePrice = typeof(StardewValley.Object).GetMethod("sellToStorePrice"); var sellToStorePricePrefix = typeof(MeatOverrides).GetMethod("sellToStorePrice"); harmony.Patch(sellToStorePrice, new HarmonyMethod(sellToStorePricePrefix), null); } if (!DataLoader.ModConfig.DisableMeat) { var objectIsPotentialBasicShippedCategory = typeof(StardewValley.Object).GetMethod("isPotentialBasicShippedCategory"); var meatOverridesIsPotentialBasicShippedCategory = typeof(MeatOverrides).GetMethod("isPotentialBasicShippedCategory"); harmony.Patch(objectIsPotentialBasicShippedCategory, new HarmonyMethod(meatOverridesIsPotentialBasicShippedCategory), null); var objectCountsForShippedCollection = typeof(StardewValley.Object).GetMethod("countsForShippedCollection"); var meatOverridesCountsForShippedCollection = typeof(MeatOverrides).GetMethod("countsForShippedCollection"); harmony.Patch(objectCountsForShippedCollection, new HarmonyMethod(meatOverridesCountsForShippedCollection), null); } //var addSpecificTemporarySprite = typeof(Event).GetMethod("addSpecificTemporarySprite"); //var addSpecificTemporarySprite = this.Helper.Reflection.GetMethod(new Event(), "addSpecificTemporarySprite").MethodInfo; //var addSpecificTemporarySpritePostfix = typeof(EventsOverrides).GetMethod("addSpecificTemporarySprite"); //harmony.Patch(addSpecificTemporarySprite, null, new HarmonyMethod(addSpecificTemporarySpritePostfix)); //var petCheckAction = typeof(Pet).GetMethod("checkAction"); //var petCheckActionPrefix = typeof(PetOverrides).GetMethod("checkAction"); //harmony.Patch(petCheckAction, new HarmonyMethod(petCheckActionPrefix), null); } }
public override bool beginUsing(GameLocation location, int x, int y, StardewValley.Farmer who) { x = (int)who.GetToolLocation(false).X; y = (int)who.GetToolLocation(false).Y; Rectangle rectangle = new Rectangle(x - Game1.tileSize / 2, y - Game1.tileSize / 2, Game1.tileSize, Game1.tileSize); if (Context.IsMainPlayer && !DataLoader.ModConfig.DisablePregnancy) { if (location is Farm) { foreach (FarmAnimal farmAnimal in (location as Farm).animals.Values) { if (farmAnimal.GetBoundingBox().Intersects(rectangle)) { this._animal = farmAnimal; break; } } } else if (location is AnimalHouse) { foreach (FarmAnimal farmAnimal in (location as AnimalHouse).animals.Values) { if (farmAnimal.GetBoundingBox().Intersects(rectangle)) { this._animal = farmAnimal; break; } } } } if (this._animal != null) { string dialogue = ""; if (this.attachments[0] == null) { Game1.showRedMessage(DataLoader.i18n.Get("Tool.InseminationSyringe.Empty")); this._animal = null; } else if (AnimalExtension.GetAnimalFromType(this._animal.type.Value) == null) { dialogue = DataLoader.i18n.Get("Tool.InseminationSyringe.CantBeInseminated", new { animalName = this._animal.displayName }); } else if (IsEggAnimal(this._animal)) { dialogue = DataLoader.i18n.Get("Tool.InseminationSyringe.EggAnimal", new { animalName = this._animal.displayName }); } else if (this._animal.isBaby()) { dialogue = DataLoader.i18n.Get("Tool.InseminationSyringe.TooYoung", new { animalName = this._animal.displayName }); } else if (PregnancyController.IsAnimalPregnant(this._animal.myID.Value)) { int daysUtillBirth = PregnancyController.GetPregnancyItem(this._animal.myID.Value).DaysUntilBirth; if (daysUtillBirth > 1) { dialogue = DataLoader.i18n.Get("Tool.InseminationSyringe.AlreadyPregnant", new { animalName = this._animal.displayName, numberOfDays = daysUtillBirth }); } else { dialogue = DataLoader.i18n.Get("Tool.InseminationSyringe.ReadyForBirth", new { animalName = this._animal.displayName }); } } else if (!CheckCorrectProduct(this._animal, this.attachments[0])) { var data = DataLoader.Helper.Content.Load <Dictionary <int, string> >(@"Data\ObjectInformation.xnb", ContentSource.GameContent); string produceName = data[this._animal.defaultProduceIndex.Value].Split('/')[4]; dialogue = DataLoader.i18n.Get("Tool.InseminationSyringe.CorrectItem", new { itemName = produceName }); } else if (PregnancyController.CheckBuildingLimit(this._animal)) { dialogue = DataLoader.i18n.Get("Tool.InseminationSyringe.BuildingLimit", new { buildingType = this._animal.displayHouse }); } else { this._animal.doEmote(16, true); if (this._animal.sound.Value != null) { Cue animalSound = Game1.soundBank.GetCue(this._animal.sound.Value); animalSound.Play(); } DelayedAction.playSoundAfterDelay("fishingRodBend", 300); DelayedAction.playSoundAfterDelay("fishingRodBend", 1200); this._animal.pauseTimer = 1500; } if (dialogue.Length > 0) { DelayedAction.showDialogueAfterDelay(dialogue, 150); this._animal = null; } } who.Halt(); int currentFrame = who.FarmerSprite.currentFrame; if (this._animal != null) { who.FarmerSprite.animateOnce(287 + who.FacingDirection, 50f, 4); } else { who.FarmerSprite.animateOnce(new FarmerSprite.AnimationFrame[1] { new FarmerSprite.AnimationFrame(currentFrame, 0, false, who.FacingDirection == 3, new AnimatedSprite.endOfAnimationBehavior(StardewValley.Farmer.useTool), true) }); } who.FarmerSprite.oldFrame = currentFrame; who.UsingTool = true; who.CanMove = false; return(true); }