diff --git a/UIInfoSuite2/Infrastructure/Tools.cs b/UIInfoSuite2/Infrastructure/Tools.cs index c1690cdc..39e2afa0 100644 --- a/UIInfoSuite2/Infrastructure/Tools.cs +++ b/UIInfoSuite2/Infrastructure/Tools.cs @@ -9,6 +9,7 @@ using StardewValley.GameData.FruitTrees; using StardewValley.Menus; using StardewValley.TerrainFeatures; +using StardewValley.WorldMaps; using SObject = StardewValley.Object; namespace UIInfoSuite2.Infrastructure; @@ -303,4 +304,11 @@ public static IEnumerable GetDaysFromCondition(GameStateQuery.ParsedGameSta return days.Count == 0 ? null : days.Max(); } + + public static MapAreaPosition? GetMapPositionDataSafe(GameLocation location, Point position) + { + MapAreaPosition? mapAreaPosition = WorldMapManager.GetPositionData(location, position)?.Data; + + return mapAreaPosition ?? WorldMapManager.GetPositionData(Game1.getFarm(), Point.Zero)?.Data; + } } diff --git a/UIInfoSuite2/UIElements/LocationOfTownsfolk.cs b/UIInfoSuite2/UIElements/LocationOfTownsfolk.cs index 4640ef50..0b5a6426 100644 --- a/UIInfoSuite2/UIElements/LocationOfTownsfolk.cs +++ b/UIInfoSuite2/UIElements/LocationOfTownsfolk.cs @@ -117,7 +117,7 @@ private void OnUpdateTicked(object? sender, UpdateTickedEventArgs e) { foreach (NPC? character in loc.characters) { - if (character.isVillager()) + if (character.IsVillager) { _townsfolk.Add(character); } @@ -311,13 +311,18 @@ private static void DrawNPC(NPC character, List namesToShow) } Rectangle headShot = character.GetHeadShot(); - MapAreaPosition? mapPosition = - WorldMapManager.GetPositionData( - Game1.player.currentLocation, - new Point((int)location.Value.X, (int)location.Value.Y) - ) ?? - WorldMapManager.GetPositionData(Game1.getFarm(), Point.Zero); - MapRegion? mapRegion = mapPosition.Region; + MapAreaPosition? mapPosition = Tools.GetMapPositionDataSafe( + Game1.player.currentLocation, + new Point((int)location.Value.X, (int)location.Value.Y) + ); + + if (mapPosition is null) + { + ModEntry.MonitorObject.LogOnce($"Unable to draw headshot for {character.Name}"); + return; + } + + MapRegion mapRegion = mapPosition.Region; Rectangle mapBounds = mapRegion.GetMapPixelBounds(); var offsetLocation = new Vector2( location.Value.X + mapBounds.X - headShot.Width, @@ -356,21 +361,18 @@ private static void DrawNPC(NPC character, List namesToShow) private static Vector2? GetMapCoordinatesForNPC(NPC character) { var playerNormalizedTile = new Point(Math.Max(0, Game1.player.TilePoint.X), Math.Max(0, Game1.player.TilePoint.Y)); - MapAreaPosition playerMapAreaPosition = - WorldMapManager.GetPositionData(Game1.player.currentLocation, playerNormalizedTile) ?? - WorldMapManager.GetPositionData(Game1.getFarm(), Point.Zero); + MapAreaPosition? playerMapAreaPosition = Tools.GetMapPositionDataSafe(Game1.player.currentLocation, playerNormalizedTile); // ^^ Regarding that ?? clause... If the player is in the farmhouse or barn or any building on the farm, GetPositionData is // going to return null. Thus the fallback to pretending the player is on the farm. However, it seems to me that // Game1.player.currentLocation.GetParentLocation() would be the safer long-term bet. But rule number 1 of modding is this: // the game code is always right, even when it's wrong. var characterNormalizedTile = new Point(Math.Max(0, character.TilePoint.X), Math.Max(0, character.TilePoint.Y)); - MapAreaPosition characterMapAreaPosition = - WorldMapManager.GetPositionData(character.currentLocation, characterNormalizedTile); + MapAreaPosition? characterMapAreaPosition = Tools.GetMapPositionDataSafe(character.currentLocation, characterNormalizedTile); if (playerMapAreaPosition != null && characterMapAreaPosition != null && - !(characterMapAreaPosition.Region.Id != playerMapAreaPosition.Region.Id)) + characterMapAreaPosition.Region.Id == playerMapAreaPosition.Region.Id) { return characterMapAreaPosition.GetMapPixelPosition(character.currentLocation, characterNormalizedTile); } diff --git a/UIInfoSuite2/UIInfoSuite2.csproj b/UIInfoSuite2/UIInfoSuite2.csproj index b38b7119..f3107bdf 100644 --- a/UIInfoSuite2/UIInfoSuite2.csproj +++ b/UIInfoSuite2/UIInfoSuite2.csproj @@ -1,6 +1,6 @@  - 2.3.6 + 2.3.7 net6.0 enable $(SolutionDir)\Releases @@ -18,8 +18,8 @@ - - + + diff --git a/UIInfoSuite2/manifest.json b/UIInfoSuite2/manifest.json index aae448e3..90698090 100644 --- a/UIInfoSuite2/manifest.json +++ b/UIInfoSuite2/manifest.json @@ -1,7 +1,7 @@ { "Name": "UI Info Suite 2", "Author": "Annosz", - "Version": "2.3.6", + "Version": "2.3.7", "Description": "Adds a useful information to the user interface. Based on Cdaragorn's excellent UI Info Suite.", "UniqueID": "Annosz.UiInfoSuite2", "EntryDll": "UIInfoSuite2.dll",