diff --git a/BetterJunimos/BetterJunimos.cs b/BetterJunimos/BetterJunimos.cs index 70626e6..6a4fb41 100644 --- a/BetterJunimos/BetterJunimos.cs +++ b/BetterJunimos/BetterJunimos.cs @@ -191,7 +191,7 @@ private static bool AlternativeTexturesActive() { } private bool ShowPerfectionTracker(ButtonPressedEventArgs e) { - if (Game1.player.currentLocation is not Farm) return false; + if (!Game1.player.currentLocation.IsFarm) return false; if (Game1.activeClickableMenu != null) return false; if (!JunimoProgression.HutOnTile(e.Cursor.Tile)) return false; if (Helper.ModRegistry.Get("ceruleandeep.BetterJunimosForestry") != null) return false; @@ -255,7 +255,7 @@ void OnDayStarted(object sender, DayStartedEventArgs e) { Util.Payments.WereJunimosPaidToday = false; } - var huts = Game1.getFarm().buildings.OfType().ToList(); + var huts = Util.GetAllHuts(); // tag each hut chest so later we can tell whether a GrabMenu close is for a Junimo chest or some other chest foreach (var hut in huts) { @@ -288,9 +288,8 @@ void OnDayStarted(object sender, DayStartedEventArgs e) { private void CheckHutsForWagesAndProgressionItems() { var alreadyPaid = Util.Payments.WereJunimosPaidToday; - - var huts = Game1.getFarm().buildings.OfType(); - var junimoHuts = huts.ToList(); + + var junimoHuts = Util.GetAllHuts(); if (!junimoHuts.Any()) return; // Monitor.Log("Updating hut items", LogLevel.Debug); @@ -643,16 +642,21 @@ private static void AllowJunimoHutPurchasing() { } private void SpawnJunimoCommand() { - if (Game1.player.currentLocation.IsFarm || Game1.player.currentLocation.IsGreenhouse) { - var huts = Game1.getFarm().buildings.OfType(); - var junimoHuts = huts.ToList(); + var currentLocation = Game1.player.currentLocation; + + if (currentLocation.IsFarm || currentLocation.IsGreenhouse) { + var junimoHuts = Util.GetAllFarms() + .FindAll(farm => farm.Equals(currentLocation)) + .SelectMany(farm => farm.buildings.OfType()) + .ToList(); + if (!junimoHuts.Any()) { Util.SendMessage(Helper.Translation.Get("msg.cannot-spawn-without-hut")); return; } var hut = junimoHuts.ElementAt(Game1.random.Next(0, junimoHuts.Count)); - Util.SpawnJunimoAtPosition(Game1.player.currentLocation, Game1.player.Position, hut, Game1.random.Next(4, 100)); + Util.SpawnJunimoAtPosition(currentLocation, Game1.player.Position, hut, Game1.random.Next(4, 100)); } else { Util.SendMessage(Helper.Translation.Get("msg.cannot-spawn-here")); diff --git a/BetterJunimos/Patches/JunimoHutPatches.cs b/BetterJunimos/Patches/JunimoHutPatches.cs index 83bd9f4..a5c7a75 100644 --- a/BetterJunimos/Patches/JunimoHutPatches.cs +++ b/BetterJunimos/Patches/JunimoHutPatches.cs @@ -40,7 +40,7 @@ public static bool Prefix(JunimoHut __instance, ref bool __result) { private static bool SearchAroundHut(JunimoHut hut) { var id = Util.GetHutIdFromHut(hut); var radius = Util.CurrentWorkingRadius; - GameLocation farm = Game1.getFarm(); + GameLocation farm = Game1.currentLocation; // SearchHutGrid manages hut.lastKnownCropLocation and Util.Abilities.lastKnownCropLocations var foundWork = SearchHutGrid(hut, radius, farm, id); diff --git a/BetterJunimos/Utils/JunimoProgression.cs b/BetterJunimos/Utils/JunimoProgression.cs index 399e167..a1dd20d 100644 --- a/BetterJunimos/Utils/JunimoProgression.cs +++ b/BetterJunimos/Utils/JunimoProgression.cs @@ -496,7 +496,7 @@ internal void ListAvailableActions(Guid id) { public static bool HutOnTile(Vector2 pos) { - return Game1.getFarm().buildings.Any(b => b is JunimoHut && b.occupiesTile(pos)); + return Util.GetAllFarms().Any(farm => farm.buildings.Any(b => b is JunimoHut && b.occupiesTile(pos))); } private string Get(string key) { diff --git a/BetterJunimos/Utils/Util.cs b/BetterJunimos/Utils/Util.cs index 605ecf5..a2a4cee 100644 --- a/BetterJunimos/Utils/Util.cs +++ b/BetterJunimos/Utils/Util.cs @@ -30,6 +30,10 @@ public class Util { internal static JunimoProgression Progression; internal static JunimoGreenhouse Greenhouse; + public static List GetAllFarms() { + return Game1.locations.Where(loc => loc.IsFarm && loc.IsOutdoors).ToList(); + } + public static int CurrentWorkingRadius { get { if (!BetterJunimos.Config.JunimoPayment.WorkForWages) return BetterJunimos.Config.JunimoHuts.MaxRadius; @@ -39,15 +43,22 @@ public static int CurrentWorkingRadius { } public static List GetAllHuts() { - return Game1.getFarm().buildings.OfType().ToList(); + return GetAllFarms().SelectMany(farm => farm.buildings.OfType().ToList()).ToList(); } public static Guid GetHutIdFromHut(JunimoHut hut) { - return Game1.getFarm().buildings.GuidOf(hut); + return GetAllFarms().Select(farm => farm.buildings.GuidOf(hut)).ToList().Find(guid => guid != Guid.Empty); } public static JunimoHut GetHutFromId(Guid id) { - return Game1.getFarm().buildings[id] as JunimoHut; + foreach (var farm in GetAllFarms()) { + if (farm.buildings.TryGetValue(id, out var hut)) { + return hut as JunimoHut; + } + } + + BetterJunimos.SMonitor.Log($"Could not get hut from id ${id}", LogLevel.Error); + return null; } public static void AddItemToChest(GameLocation farm, Chest chest, SObject item) { @@ -74,7 +85,7 @@ public static void RemoveItemFromChest(Chest chest, Item item, int count = 1) { public static void SpawnJunimoAtHut(JunimoHut hut) { // I don't know why we're multiplying by 64 here var pos = new Vector2((float) hut.tileX.Value + 1, (float) hut.tileY.Value + 1) * 64f + new Vector2(0.0f, 32f); - SpawnJunimoAtPosition(Game1.getFarm(), pos, hut, hut.getUnusedJunimoNumber()); + SpawnJunimoAtPosition(Game1.player.currentLocation, pos, hut, hut.getUnusedJunimoNumber()); } public static void SpawnJunimoAtPosition(GameLocation location, Vector2 pos, JunimoHut hut, int junimoNumber) { @@ -133,8 +144,6 @@ public static void SpawnJunimoAtPosition(GameLocation location, Vector2 pos, Jun if (!Utility.isOnScreen(Utility.Vector2ToPoint(pos), 64, location)) return; location.playSound("junimoMeep1"); - - } /*