From 434921a6680a739405d8b04d76c9c67e5f5e082b Mon Sep 17 00:00:00 2001 From: Mohammad Shoaib Khan Date: Sat, 18 Feb 2023 12:36:39 +0530 Subject: [PATCH] Added ability to add entries specific to a certain farm type. The entries with 'farm' name will be applied to all farm types and if we want to add an entry specific to a farm type, like to beach farm layout, we can use 'farm_beach' name for this. --- stardew-access/Features/StaticTiles.cs | 134 ++++++++++++++----------- 1 file changed, 78 insertions(+), 56 deletions(-) diff --git a/stardew-access/Features/StaticTiles.cs b/stardew-access/Features/StaticTiles.cs index 87fa154..0c058e7 100644 --- a/stardew-access/Features/StaticTiles.cs +++ b/stardew-access/Features/StaticTiles.cs @@ -77,18 +77,17 @@ namespace stardew_access.Features return getStaticTileInfoAtWithCategory(x, y).name; } - public (string? name, CATEGORY category) getStaticTileInfoAtWithCategory(int x, int y) - { + public (string? name, CATEGORY category) getStaticTileInfoAtWithCategory(int x, int y) { List allData = new List(); if (customTilesData != null) allData.Add(customTilesData); if (staticTilesData != null) allData.Add(staticTilesData); - foreach (JObject data in allData) - { + foreach (JObject data in allData) { foreach (KeyValuePair location in data) { - if (location.Key.Contains("||") && MainClass.ModHelper != null) + string locationName = location.Key; + if (locationName.Contains("||") && MainClass.ModHelper != null) { // Mod Specific Tiles // We can add tiles that only get detected when the specified mod is loaded. @@ -109,69 +108,92 @@ namespace stardew_access.Features // . // . // } - string uniqueModID = location.Key.Substring(location.Key.LastIndexOf("||") + 2); - string locationName = location.Key.Remove(location.Key.LastIndexOf("||")); + string uniqueModID = locationName.Substring(locationName.LastIndexOf("||") + 2); + locationName = locationName.Remove(locationName.LastIndexOf("||")); bool isLoaded = MainClass.ModHelper.ModRegistry.IsLoaded(uniqueModID); if (!isLoaded) continue; // Skip if the specified mod is not loaded - if (!Game1.currentLocation.Name.ToLower().Equals(locationName.ToLower())) continue; } - else if (!Game1.currentLocation.Name.ToLower().Equals(location.Key.ToLower())) continue; - if (location.Value != null) - foreach (var tile in ((JObject)location.Value)) + if (locationName.Contains("_") && locationName.ToLower().StartsWith("farm_")) + { + string farmType = locationName.Substring(locationName.LastIndexOf("_") + 1); + int farmTypeIndex = getFarmTypeIndex(farmType); + locationName = locationName.Remove(locationName.LastIndexOf("_")); + + if (farmTypeIndex != Game1.whichFarm) continue; // Skip if current farm type does not matches + } + + if (!Game1.currentLocation.Name.ToLower().Equals(locationName.ToLower())) continue; + if (location.Value == null) continue; + + foreach (var tile in ((JObject)location.Value)) + { + if (tile.Value == null) continue; + + JToken? tileXArray = tile.Value["x"]; + JToken? tileYArray = tile.Value["y"]; + JToken? tileType = tile.Value["type"]; + + if (tileXArray == null || tileYArray == null || tileType == null) + continue; + + bool isXPresent = false; + bool isYPresent = false; + + foreach (var item in tileXArray) { - if (tile.Value == null) + if (short.Parse(item.ToString()) != x) continue; - JToken? tileXArray = tile.Value["x"]; - JToken? tileYArray = tile.Value["y"]; - JToken? tileType = tile.Value["type"]; - - if (tileXArray == null || tileYArray == null || tileType == null) - continue; - - bool isXPresent = false; - bool isYPresent = false; - - foreach (var item in tileXArray) - { - if (short.Parse(item.ToString()) == x) - { - isXPresent = true; - break; - } - } - - foreach (var item in tileYArray) - { - if (short.Parse(item.ToString()) == y) - { - isYPresent = true; - break; - } - } - - if (isXPresent && isYPresent) - { - string key = tile.Key; - if (key.Contains('[') && key.Contains(']')) - { - int i1 = key.IndexOf('['); - int i2 = key.LastIndexOf(']'); - - if (i1 < i2) - { - key = key.Remove(i1, ++i2 - i1); - } - } - - return (key.Trim(), CATEGORY.FromString(tileType.ToString().ToLower())); - } + isXPresent = true; + break; } + + foreach (var item in tileYArray) + { + if (short.Parse(item.ToString()) != y) + continue; + + isYPresent = true; + break; + } + + if (isXPresent && isYPresent) + { + string key = tile.Key; + if (key.Contains('[') && key.Contains(']')) + { + int i1 = key.IndexOf('['); + int i2 = key.LastIndexOf(']'); + + if (i1 < i2) + { + key = key.Remove(i1, ++i2 - i1); + } + } + + return (key.Trim(), CATEGORY.FromString(tileType.ToString().ToLower())); + } + } } } return (null, CATEGORY.Others); } + + private int getFarmTypeIndex(string farmType) + { + return farmType.ToLower() switch + { + "default" => 0, + "riverlands" => 1, + "forest" => 2, + "mountains" => 3, + "combat" => 4, + "fourcorners" => 5, + "beach" => 6, + _ => 0, + }; + } } }