diff options
-rw-r--r-- | docs/release-notes.md | 1 | ||||
-rw-r--r-- | src/SMAPI/Metadata/CoreAssetPropagator.cs | 38 |
2 files changed, 38 insertions, 1 deletions
diff --git a/docs/release-notes.md b/docs/release-notes.md index ae078e20..bbf04556 100644 --- a/docs/release-notes.md +++ b/docs/release-notes.md @@ -48,6 +48,7 @@ * Added option to suppress update checks for a specific mod in `StardewModdingAPI.config.json`. * Added absolute pixels to `ICursorPosition`. * Added support for reading/writing `ISemanticVersion` to JSON. + * Added support for reloading NPC schedules through the content API. * Improved update alerts to use update key order when choosing a display URL. * Fixed assets loaded by temporary content managers not being editable by mods. * Fixed assets not reloaded consistently when the player switches language. diff --git a/src/SMAPI/Metadata/CoreAssetPropagator.cs b/src/SMAPI/Metadata/CoreAssetPropagator.cs index 7ca0bd82..12abeb10 100644 --- a/src/SMAPI/Metadata/CoreAssetPropagator.cs +++ b/src/SMAPI/Metadata/CoreAssetPropagator.cs @@ -325,6 +325,10 @@ namespace StardewModdingAPI.Metadata if (this.IsInFolder(key, "Portraits")) return this.ReloadNpcPortraits(content, key); + // dynamic data + if (this.IsInFolder(key, "Characters\\schedules")) + return this.ReloadNpcSchedules(content, key); + return false; } @@ -333,7 +337,7 @@ namespace StardewModdingAPI.Metadata ** Private methods *********/ /**** - ** Reload methods + ** Reload texture methods ****/ /// <summary>Reload the sprites for matching pets or horses.</summary> /// <typeparam name="TAnimal">The animal type.</typeparam> @@ -502,6 +506,38 @@ namespace StardewModdingAPI.Metadata } /**** + ** Reload data methods + ****/ + /// <summary>Reload the schedules for matching NPCs.</summary> + /// <param name="content">The content manager through which to reload the asset.</param> + /// <param name="key">The asset key to reload.</param> + /// <returns>Returns whether any assets were reloaded.</returns> + private bool ReloadNpcSchedules(LocalizedContentManager content, string key) + { + // get NPCs + string name = Path.GetFileName(key); + NPC[] villagers = this.GetCharacters().Where(npc => npc.Name == name && npc.isVillager()).ToArray(); + if (!villagers.Any()) + return false; + + // update schedule + foreach (NPC villager in villagers) + { + // reload schedule + villager.Schedule = villager.getSchedule(Game1.dayOfMonth); + + // switch to new schedule if needed + int lastScheduleTime = villager.Schedule.Keys.Where(p => p <= Game1.timeOfDay).OrderByDescending(p => p).FirstOrDefault(); + if (lastScheduleTime != 0) + { + this.Reflection.GetField<int>(villager, "scheduleTimeToTry").SetValue(this.Reflection.GetField<int>(typeof(NPC), "NO_TRY").GetValue()); // use time that's passed in to checkSchedule + villager.checkSchedule(lastScheduleTime); + } + } + return true; + } + + /**** ** Helpers ****/ /// <summary>Reload the texture for an animated sprite.</summary> |