From 1aa87cd7712227a8a0bb66bb419ea827f6fbca4d Mon Sep 17 00:00:00 2001 From: Annosz Date: Mon, 6 Jun 2022 17:35:10 +0200 Subject: [PATCH] Try adding error handling to birthday icons --- UIInfoSuite2/UIElements/ShowBirthdayIcon.cs | 38 ++++++++++++++------- 1 file changed, 26 insertions(+), 12 deletions(-) diff --git a/UIInfoSuite2/UIElements/ShowBirthdayIcon.cs b/UIInfoSuite2/UIElements/ShowBirthdayIcon.cs index 31c5d566..96686376 100644 --- a/UIInfoSuite2/UIElements/ShowBirthdayIcon.cs +++ b/UIInfoSuite2/UIElements/ShowBirthdayIcon.cs @@ -92,12 +92,10 @@ private void CheckForGiftGiven(UpdateTickedEventArgs e) { if (e.IsOneSecond && _birthdayNPC != null && Game1.player?.friendshipData != null) { - Game1.player.friendshipData.FieldDict.TryGetValue(_birthdayNPC.Name, out var netRef); - Friendship birthdayNPCDetails = netRef; - if (birthdayNPCDetails != null) + Friendship friendship = GetFriendshipWithNPC(_birthdayNPC.Name); + if (friendship != null && friendship.GiftsToday == 1) { - if (birthdayNPCDetails.GiftsToday == 1) - _birthdayNPC = null; + _birthdayNPC = null; } } } @@ -110,9 +108,15 @@ private void CheckForBirthday() foreach (var character in location.characters) { if (character.isBirthday(Game1.currentSeason, Game1.dayOfMonth) && - !(!Game1.player.friendshipData.ContainsKey(character.Name) && - Game1.NPCGiftTastes.ContainsKey(character.Name))) + Game1.player.friendshipData.FieldDict.ContainsKey(character.Name)) { + if (HideBirthdayIfFullFriendShip) + { + Friendship friendship = GetFriendshipWithNPC(character.Name); + if (friendship != null && friendship.Points >= 2000) + break; + } + _birthdayNPC = character; break; } @@ -123,14 +127,24 @@ private void CheckForBirthday() } } - private void DrawBithdayIcon() + private static Friendship GetFriendshipWithNPC(string name) { - if (HideBirthdayIfFullFriendShip - && Game1.player.friendshipData.TryGetValue(_birthdayNPC.Name, out Friendship friendship) - && friendship.Points >= 2000) + try + { + Game1.player.friendshipData.FieldDict.TryGetValue(name, out var netRef); + Friendship birthdayNPCDetails = netRef; + return birthdayNPCDetails; + } + catch (Exception ex) { - return; + ModEntry.MonitorObject.Log("Error while getting information about the birthday of " + name + ": " + ex.Message + Environment.NewLine + ex.StackTrace, LogLevel.Error); } + + return null; + } + + private void DrawBithdayIcon() + { Rectangle headShot = _birthdayNPC.GetHeadShot(); Point iconPosition = IconHandler.Handler.GetNewIconPosition(); float scale = 2.9f;