From 0acdc1c578020a719c394c387a02d6192c1a7db2 Mon Sep 17 00:00:00 2001 From: Mohammad Shoaib Date: Thu, 3 Feb 2022 17:26:50 +0530 Subject: [PATCH] Added demolish to buildsel --- stardew-access/CustomCommands.cs | 40 ++++- stardew-access/ModEntry.cs | 6 + .../Patches/BuildingNAnimalMenuPatches.cs | 151 +++++++++++++++++- 3 files changed, 195 insertions(+), 2 deletions(-) diff --git a/stardew-access/CustomCommands.cs b/stardew-access/CustomCommands.cs index 16761af..d337e66 100644 --- a/stardew-access/CustomCommands.cs +++ b/stardew-access/CustomCommands.cs @@ -360,9 +360,47 @@ namespace stardew_access } else { - MainClass.monitor.Log($"Available buildings:{toPrint}\nOpen the command menu and use pageup and pagedown to check the list", LogLevel.Info); + MainClass.monitor.Log($"Available buildings:{toPrint}\nOpen command menu and use pageup and pagedown to check the list", LogLevel.Info); } }); + + helper.ConsoleCommands.Add("buildsel", "Select the building index which you want to upgrade/demolish/paint", (string commmand, string[] args) => + { + + if (Game1.activeClickableMenu is not CarpenterMenu || !BuildingNAnimalMenuPatches.isOnFarm) + { + MainClass.monitor.Log($"Cannot list buildings.", LogLevel.Info); + return; + } + + string? indexInString = args.ElementAtOrDefault(0); + if (indexInString == null) + { + MainClass.monitor.Log("Enter the index of the building too!! Use buildlist command to get the index.", LogLevel.Info); + return; + } + + int index; + bool isParsable = int.TryParse(indexInString, out index); + + if (!isParsable) + { + MainClass.monitor.Log("Index can only be a number.", LogLevel.Info); + return; + } + + if (BuildingNAnimalMenuPatches.availableBuildings[index] == null) + { + MainClass.monitor.Log($"No building found with index {index}. Use buildlist command to get the index.", LogLevel.Info); + return; + } + + if (BuildingNAnimalMenuPatches.isConstructing) { } + else if (BuildingNAnimalMenuPatches.isDemolishing) { BuildingNAnimalMenuPatches.Demolish(BuildingNAnimalMenuPatches.availableBuildings[index]); } + else if (BuildingNAnimalMenuPatches.isUpgrading) { } + else if (BuildingNAnimalMenuPatches.isMoving) { } + else if (BuildingNAnimalMenuPatches.isPainting) { } + }); #endregion helper.ConsoleCommands.Add("refsr", "Refresh screen reader", (string commmand, string[] args) => diff --git a/stardew-access/ModEntry.cs b/stardew-access/ModEntry.cs index a8b4389..6629903 100644 --- a/stardew-access/ModEntry.cs +++ b/stardew-access/ModEntry.cs @@ -8,6 +8,7 @@ using AutoHotkey.Interop; using System.Runtime.InteropServices; using stardew_access.ScreenReader; using Microsoft.Xna.Framework; +using StardewValley.Buildings; namespace stardew_access { @@ -66,6 +67,11 @@ namespace stardew_access { BuildingNAnimalMenuPatches.marked[i] = Vector2.Zero; } + + for (int i = 0; i < BuildingNAnimalMenuPatches.availableBuildings.Length; i++) + { + BuildingNAnimalMenuPatches.availableBuildings[i] = null; + } #endregion helper.Events.Input.ButtonPressed += this.OnButtonPressed; diff --git a/stardew-access/Patches/BuildingNAnimalMenuPatches.cs b/stardew-access/Patches/BuildingNAnimalMenuPatches.cs index 268e573..f0ea8bf 100644 --- a/stardew-access/Patches/BuildingNAnimalMenuPatches.cs +++ b/stardew-access/Patches/BuildingNAnimalMenuPatches.cs @@ -2,18 +2,22 @@ using Microsoft.Xna.Framework; using StardewModdingAPI; using StardewValley; using StardewValley.Buildings; +using StardewValley.Locations; using StardewValley.Menus; +using StardewValley.Objects; namespace stardew_access.Patches { internal class BuildingNAnimalMenuPatches { internal static Vector2[] marked = new Vector2[10]; - internal static Building[] availableBuildings = new Building[100]; + internal static Building?[] availableBuildings = new Building[100]; + internal static CarpenterMenu? carpenterMenu = null; internal static string carpenterMenuQuery = ""; internal static bool isSayingBlueprintInfo = false; internal static string prevBlueprintInfo = ""; internal static bool isOnFarm = false, isUpgrading = false, isDemolishing = false, isPainting = false, isConstructing = false, isMoving = false; + internal static void CarpenterMenuPatch( CarpenterMenu __instance, bool ___onFarm, List ___ingredients, int ___price, List ___blueprints, int ___currentBlueprintIndex, bool ___upgrading, bool ___demolishing, bool ___moving, @@ -22,6 +26,7 @@ namespace stardew_access.Patches try { isOnFarm = ___onFarm; + carpenterMenu = __instance; if (!___onFarm) { isUpgrading = false; @@ -187,6 +192,150 @@ namespace stardew_access.Patches await Task.Delay(300); isSayingBlueprintInfo = false; } + + public static void Demolish(Building? toDemolish) + { + if (toDemolish == null) + return; + // This code is taken from the game's code (CarpenterMenu.cs::654) + Farm farm = Game1.getLocationFromName("Farm") as Farm; + Action buildingLockFailed = delegate + { + if (isDemolishing) + { + Game1.addHUDMessage(new HUDMessage(Game1.content.LoadString("Strings\\UI:Carpenter_CantDemolish_LockFailed"), Color.Red, 3500f)); + } + }; + Action continueDemolish = delegate + { + if (isDemolishing && toDemolish != null && farm.buildings.Contains(toDemolish)) + { + if ((int)toDemolish.daysOfConstructionLeft > 0 || (int)toDemolish.daysUntilUpgrade > 0) + { + Game1.addHUDMessage(new HUDMessage(Game1.content.LoadString("Strings\\UI:Carpenter_CantDemolish_DuringConstruction"), Color.Red, 3500f)); + } + else if (toDemolish.indoors.Value != null && toDemolish.indoors.Value is AnimalHouse && (toDemolish.indoors.Value as AnimalHouse).animalsThatLiveHere.Count > 0) + { + Game1.addHUDMessage(new HUDMessage(Game1.content.LoadString("Strings\\UI:Carpenter_CantDemolish_AnimalsHere"), Color.Red, 3500f)); + } + else if (toDemolish.indoors.Value != null && toDemolish.indoors.Value.farmers.Any()) + { + Game1.addHUDMessage(new HUDMessage(Game1.content.LoadString("Strings\\UI:Carpenter_CantDemolish_PlayerHere"), Color.Red, 3500f)); + } + else + { + if (toDemolish.indoors.Value != null && toDemolish.indoors.Value is Cabin) + { + foreach (Farmer current in Game1.getAllFarmers()) + { + if (current.currentLocation != null && current.currentLocation.Name == (toDemolish.indoors.Value as Cabin).GetCellarName()) + { + Game1.addHUDMessage(new HUDMessage(Game1.content.LoadString("Strings\\UI:Carpenter_CantDemolish_PlayerHere"), Color.Red, 3500f)); + return; + } + } + } + if (toDemolish.indoors.Value is Cabin && (toDemolish.indoors.Value as Cabin).farmhand.Value.isActive()) + { + Game1.addHUDMessage(new HUDMessage(Game1.content.LoadString("Strings\\UI:Carpenter_CantDemolish_FarmhandOnline"), Color.Red, 3500f)); + } + else + { + toDemolish.BeforeDemolish(); + Chest chest = null; + if (toDemolish.indoors.Value is Cabin) + { + List list = (toDemolish.indoors.Value as Cabin).demolish(); + if (list.Count > 0) + { + chest = new Chest(playerChest: true); + chest.fixLidFrame(); + chest.items.Set(list); + } + } + if (farm.destroyStructure(toDemolish)) + { + _ = (int)toDemolish.tileY; + _ = (int)toDemolish.tilesHigh; + Game1.flashAlpha = 1f; + toDemolish.showDestroyedAnimation(Game1.getFarm()); + Game1.playSound("explosion"); + Utility.spreadAnimalsAround(toDemolish, farm); + DelayedAction.functionAfterDelay(carpenterMenu.returnToCarpentryMenu, 1500); + // freeze = true; + if (chest != null) + { + farm.objects[new Vector2((int)toDemolish.tileX + (int)toDemolish.tilesWide / 2, (int)toDemolish.tileY + (int)toDemolish.tilesHigh / 2)] = chest; + } + } + } + } + } + }; + if (toDemolish != null) + { + if (toDemolish.indoors.Value != null && toDemolish.indoors.Value is Cabin && !Game1.IsMasterGame) + { + Game1.addHUDMessage(new HUDMessage(Game1.content.LoadString("Strings\\UI:Carpenter_CantDemolish_LockFailed"), Color.Red, 3500f)); + toDemolish = null; + return; + } + if (!carpenterMenu.CanDemolishThis(toDemolish)) + { + toDemolish = null; + return; + } + if (!Game1.IsMasterGame && !carpenterMenu.hasPermissionsToDemolish(toDemolish)) + { + toDemolish = null; + return; + } + } + if (toDemolish != null && toDemolish.indoors.Value is Cabin) + { + Cabin cabin = toDemolish.indoors.Value as Cabin; + if (cabin.farmhand.Value != null && (bool)cabin.farmhand.Value.isCustomized) + { + Game1.currentLocation.createQuestionDialogue(Game1.content.LoadString("Strings\\UI:Carpenter_DemolishCabinConfirm", cabin.farmhand.Value.Name), Game1.currentLocation.createYesNoResponses(), delegate (Farmer f, string answer) + { + if (answer == "Yes") + { + Game1.activeClickableMenu = carpenterMenu; + Game1.player.team.demolishLock.RequestLock(continueDemolish, buildingLockFailed); + } + else + { + DelayedAction.functionAfterDelay(carpenterMenu.returnToCarpentryMenu, 1000); + } + }); + return; + } + } + if (toDemolish != null) + { + Game1.player.team.demolishLock.RequestLock(continueDemolish, buildingLockFailed); + } + } + + public static void Contstruct(Building toCunstruct) + { + + } + + public static void Upgrade(Building toUpgrade) + { + + } + + public static void Paint(Building toPaint) + { + + } + + public static void Move(Building toMove, Vector2 position) + { + + } } } \ No newline at end of file