From d8ee422405614a9b5d56ae0f42a003835fc57a8c Mon Sep 17 00:00:00 2001 From: Jesse Plamondon-Willard Date: Sun, 1 Jul 2018 17:37:19 -0400 Subject: add support for reloading NPC schedules through the content API --- src/SMAPI/Metadata/CoreAssetPropagator.cs | 38 ++++++++++++++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) (limited to 'src/SMAPI/Metadata') 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 ****/ /// Reload the sprites for matching pets or horses. /// The animal type. @@ -501,6 +505,38 @@ namespace StardewModdingAPI.Metadata return false; } + /**** + ** Reload data methods + ****/ + /// Reload the schedules for matching NPCs. + /// The content manager through which to reload the asset. + /// The asset key to reload. + /// Returns whether any assets were reloaded. + 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(villager, "scheduleTimeToTry").SetValue(this.Reflection.GetField(typeof(NPC), "NO_TRY").GetValue()); // use time that's passed in to checkSchedule + villager.checkSchedule(lastScheduleTime); + } + } + return true; + } + /**** ** Helpers ****/ -- cgit