From 2d061e220b0f52ea85803efadef13bd729661358 Mon Sep 17 00:00:00 2001 From: Mohammad Shoaib Khan Date: Mon, 20 Feb 2023 13:34:03 +0530 Subject: [PATCH] Organized code --- stardew-access/Patches/DonationMenuPatches.cs | 99 +------------------ stardew-access/Patches/GameMenuPatches.cs | 12 +-- stardew-access/Patches/InventoryUtils.cs | 41 +++++++- stardew-access/Patches/MenuPatches.cs | 98 +++++++----------- 4 files changed, 81 insertions(+), 169 deletions(-) diff --git a/stardew-access/Patches/DonationMenuPatches.cs b/stardew-access/Patches/DonationMenuPatches.cs index a49be83..fd83808 100644 --- a/stardew-access/Patches/DonationMenuPatches.cs +++ b/stardew-access/Patches/DonationMenuPatches.cs @@ -79,7 +79,8 @@ namespace stardew_access.Patches else { // Player Inventory - int i = narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y); + int i = InventoryUtils.narrateHoveredSlotAndReturnIndex(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, + handleHighlightedItem: true, highlightedItemPrefix: "Donatable "); if (i != -9999) { bool isPrimaryInfoKeyPressed = MainClass.Config.PrimaryInfoKey.JustPressed(); // For donating hovered item @@ -148,74 +149,7 @@ namespace stardew_access.Patches } } - // Returns the index of the hovered item or -9999 - internal static int narrateHoveredItemInInventory(InventoryMenu inventoryMenu, List inventory, IList actualInventory, int x, int y) - { - #region Narrate hovered item - for (int i = 0; i < inventory.Count; i++) - { - if (inventory[i].containsPoint(x, y)) - { - string toSpeak = ""; - if ((i + 1) <= actualInventory.Count) - { - if (actualInventory[i] != null) - { - string name = actualInventory[i].DisplayName; - int stack = actualInventory[i].Stack; - string quality = ""; - - #region Add quality of item - if (actualInventory[i] is StardewValley.Object && ((StardewValley.Object)actualInventory[i]).Quality > 0) - { - int qualityIndex = ((StardewValley.Object)actualInventory[i]).Quality; - if (qualityIndex == 1) - { - quality = "Silver quality"; - } - else if (qualityIndex == 2 || qualityIndex == 3) - { - quality = "Gold quality"; - } - else if (qualityIndex >= 4) - { - quality = "Iridium quality"; - } - } - #endregion - - if (inventoryMenu.highlightMethod(inventoryMenu.actualInventory[i])) - name = $"Donatable {name}"; - - if (stack > 1) - toSpeak = $"{stack} {name} {quality}"; - else - toSpeak = $"{name} {quality}"; - } - else - { - // For empty slot - toSpeak = "Empty Slot"; - } - } - else - { - // For empty slot - toSpeak = "Empty Slot"; - } - - if (museumQueryKey != $"{toSpeak}:{i}") - { - museumQueryKey = $"{toSpeak}:{i}"; - MainClass.ScreenReader.Say(toSpeak, true); - } - return i; - } - } - #endregion - return -9999; - } - + #region These methods are taken from the game's source code, https://github.com/veywrn/StardewValley/blob/3ff171b6e9e6839555d7881a391b624ccd820a83/StardewValley/Multiplayer.cs#L1331-L1395 internal static void globalChatInfoMessage(string messageKey, params string[] args) { @@ -303,33 +237,8 @@ namespace stardew_access.Patches } else { - if (InventoryUtils.narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y)) + if (InventoryUtils.narrateHoveredSlot(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y)) return; - // for (int i = 0; i < __instance.inventory.inventory.Count; i++) - // { - // if (!__instance.inventory.inventory[i].containsPoint(x, y)) - // continue; - // - // if (__instance.inventory.actualInventory[i] == null) - // toSpeak = "Empty slot"; - // else - // { - // toSpeak = $"{__instance.inventory.actualInventory[i].Stack} {__instance.inventory.actualInventory[i].DisplayName}"; - // - // if (!__instance.inventory.highlightMethod(__instance.inventory.actualInventory[i])) - // { - // toSpeak = $"{toSpeak} not usable here"; - // } - // } - // - // if (fieldOfficeMenuQuery != $"{toSpeak}:{i}") - // { - // fieldOfficeMenuQuery = $"{toSpeak}:{i}"; - // MainClass.ScreenReader.Say(toSpeak, true); - // } - // - // return; - // } for (int i = 0; i < __instance.pieceHolders.Count; i++) { diff --git a/stardew-access/Patches/GameMenuPatches.cs b/stardew-access/Patches/GameMenuPatches.cs index a3018a8..f5d0ea2 100644 --- a/stardew-access/Patches/GameMenuPatches.cs +++ b/stardew-access/Patches/GameMenuPatches.cs @@ -240,7 +240,7 @@ namespace stardew_access.Patches #endregion #region Narrate hovered item - if (InventoryUtils.narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, hoverPrice: __instance.hoverPrice)) + if (InventoryUtils.narrateHoveredSlot(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, hoverPrice: __instance.hoverPrice)) { shopMenuQueryKey = ""; return; @@ -395,7 +395,7 @@ namespace stardew_access.Patches #endregion #region Narrate hovered item - if (InventoryUtils.narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y)) + if (InventoryUtils.narrateHoveredSlot(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y)) geodeMenuQueryKey = ""; #endregion } @@ -572,14 +572,14 @@ namespace stardew_access.Patches #endregion #region Narrate hovered item - if (InventoryUtils.narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, true)) + if (InventoryUtils.narrateHoveredSlot(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, true)) { gameMenuQueryKey = ""; itemGrabMenuQueryKey = ""; return; } - if (InventoryUtils.narrateHoveredItemInInventory(__instance.ItemsToGrabMenu, __instance.ItemsToGrabMenu.inventory, __instance.ItemsToGrabMenu.actualInventory, x, y, true)) + if (InventoryUtils.narrateHoveredSlot(__instance.ItemsToGrabMenu, __instance.ItemsToGrabMenu.inventory, __instance.ItemsToGrabMenu.actualInventory, x, y, true)) { gameMenuQueryKey = ""; itemGrabMenuQueryKey = ""; @@ -854,7 +854,7 @@ namespace stardew_access.Patches #endregion #region Narrate hovered item - if (InventoryUtils.narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y)) + if (InventoryUtils.narrateHoveredSlot(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y)) { gameMenuQueryKey = ""; craftingPageQueryKey = ""; @@ -1065,7 +1065,7 @@ namespace stardew_access.Patches #endregion #region Narrate hovered item - if (InventoryUtils.narrateHoveredItemInInventory(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, true)) + if (InventoryUtils.narrateHoveredSlot(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y, true)) { gameMenuQueryKey = ""; inventoryPageQueryKey = ""; diff --git a/stardew-access/Patches/InventoryUtils.cs b/stardew-access/Patches/InventoryUtils.cs index 3362e4c..e4d0089 100644 --- a/stardew-access/Patches/InventoryUtils.cs +++ b/stardew-access/Patches/InventoryUtils.cs @@ -9,7 +9,20 @@ namespace stardew_access.Patches internal static string hoveredItemQueryKey = ""; internal static int prevSlotIndex = -999; - internal static bool narrateHoveredItemInInventory(InventoryMenu inventoryMenu, List inventory, IList actualInventory, int x, int y, bool giveExtraDetails = false, int hoverPrice = -1, int extraItemToShowIndex = -1, int extraItemToShowAmount = -1) + internal static bool narrateHoveredSlot(InventoryMenu inventoryMenu, List inventory, IList actualInventory, int x, int y, + bool giveExtraDetails = false, int hoverPrice = -1, int extraItemToShowIndex = -1, int extraItemToShowAmount = -1, + bool handleHighlightedItem = false, String highlightedItemPrefix = "", String highlightedItemSuffix = "") + { + if (narrateHoveredSlotAndReturnIndex(inventoryMenu, inventory, actualInventory, x, y, + giveExtraDetails = false, hoverPrice = -1, extraItemToShowIndex = -1, extraItemToShowAmount = -1, + handleHighlightedItem = false, highlightedItemPrefix = "", highlightedItemSuffix = "") == -999) + return false; + + return true; + } + internal static int narrateHoveredSlotAndReturnIndex(InventoryMenu inventoryMenu, List inventory, IList actualInventory, int x, int y, + bool giveExtraDetails = false, int hoverPrice = -1, int extraItemToShowIndex = -1, int extraItemToShowAmount = -1, + bool handleHighlightedItem = false, String highlightedItemPrefix = "", String highlightedItemSuffix = "") { for (int i = 0; i < inventory.Count; i++) { @@ -20,11 +33,15 @@ namespace stardew_access.Patches // For empty slot checkAndSpeak("Empty Slot", i); prevSlotIndex = i; - return true; + return i; } string toSpeak = ""; - string name = $"{actualInventory[i].DisplayName}{handleUnHighlightedItem(inventoryMenu.highlightMethod(actualInventory[i]), i)}"; + bool isHighlighted = inventoryMenu.highlightMethod(actualInventory[i]); + + string namePrefix = handleHighlightedItemPrefix(isHighlighted, highlightedItemPrefix); + string nameSuffix = $"{handleHighlightedItemSuffix(isHighlighted, highlightedItemSuffix)}{handleUnHighlightedItem(isHighlighted, i)}"; + string name = $"{namePrefix}{actualInventory[i].DisplayName}{nameSuffix}"; int stack = actualInventory[i].Stack; string quality = getQualityFromItem(actualInventory[i]); string healthNStamine = getHealthNStaminaFromItem(actualInventory[i]); @@ -51,11 +68,11 @@ namespace stardew_access.Patches checkAndSpeak(toSpeak, i); prevSlotIndex = i; - return true; + return i; } // If no slot is hovered - return false; + return -999; } private static void checkAndSpeak(String toSpeak, int hoveredInventoryIndex) @@ -158,6 +175,20 @@ namespace stardew_access.Patches return $"Sell Price: {price} g"; } + private static String handleHighlightedItemPrefix(bool isHighlighted, String prefix) + { + if (!isHighlighted) return ""; + + return prefix; + } + + private static String handleHighlightedItemSuffix(bool isHighlighted, String suffix) + { + if (!isHighlighted) return ""; + + return suffix; + } + private static String handleUnHighlightedItem(bool isHighlighted, int hoveredInventoryIndex) { if (isHighlighted) return ""; diff --git a/stardew-access/Patches/MenuPatches.cs b/stardew-access/Patches/MenuPatches.cs index 739f83e..ff1535d 100644 --- a/stardew-access/Patches/MenuPatches.cs +++ b/stardew-access/Patches/MenuPatches.cs @@ -129,37 +129,40 @@ namespace stardew_access.Patches } else { - for (int i = 0; i < __instance.inventory.inventory.Count; i++) - { - if (!__instance.inventory.inventory[i].containsPoint(x, y)) - continue; - - if (__instance.inventory.actualInventory[i] == null) - toSpeak = "Empty slot"; - else - { - toSpeak = $"{__instance.inventory.actualInventory[i].Stack} {__instance.inventory.actualInventory[i].DisplayName}"; - - if (!__instance.inventory.highlightMethod(__instance.inventory.actualInventory[i])) - { - toSpeak = $"{toSpeak} not usable here"; - } - - if (prevSlotIndex != i) - { - prevSlotIndex = i; - Game1.playSound("invalid-selection"); - } - } - - if (forgeMenuQuery != $"{toSpeak}:{i}") - { - forgeMenuQuery = $"{toSpeak}:{i}"; - MainClass.ScreenReader.Say(toSpeak, true); - } - + if (InventoryUtils.narrateHoveredSlot(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y)) return; - } + + // for (int i = 0; i < __instance.inventory.inventory.Count; i++) + // { + // if (!__instance.inventory.inventory[i].containsPoint(x, y)) + // continue; + // + // if (__instance.inventory.actualInventory[i] == null) + // toSpeak = "Empty slot"; + // else + // { + // toSpeak = $"{__instance.inventory.actualInventory[i].Stack} {__instance.inventory.actualInventory[i].DisplayName}"; + // + // if (!__instance.inventory.highlightMethod(__instance.inventory.actualInventory[i])) + // { + // toSpeak = $"{toSpeak} not usable here"; + // } + // + // if (prevSlotIndex != i) + // { + // prevSlotIndex = i; + // Game1.playSound("invalid-selection"); + // } + // } + // + // if (forgeMenuQuery != $"{toSpeak}:{i}") + // { + // forgeMenuQuery = $"{toSpeak}:{i}"; + // MainClass.ScreenReader.Say(toSpeak, true); + // } + // + // return; + // } } @@ -300,40 +303,9 @@ namespace stardew_access.Patches if (Game1.player.pantsItem.Value != null) toSpeak = $"{toSpeak}: {Game1.player.pantsItem.Value.DisplayName}"; } - else - { - for (int i = 0; i < __instance.inventory.inventory.Count; i++) - { - if (!__instance.inventory.inventory[i].containsPoint(x, y)) - continue; - if (__instance.inventory.actualInventory[i] == null) - toSpeak = "Empty slot"; - else - { - toSpeak = $"{__instance.inventory.actualInventory[i].Stack} {__instance.inventory.actualInventory[i].DisplayName}"; - - if (!__instance.inventory.highlightMethod(__instance.inventory.actualInventory[i])) - { - toSpeak = $"{toSpeak} not usable here"; - } - - if (prevSlotIndex != i) - { - prevSlotIndex = i; - Game1.playSound("invalid-selection"); - } - } - - if (tailoringMenuQuery != $"{toSpeak}:{i}") - { - tailoringMenuQuery = $"{toSpeak}:{i}"; - MainClass.ScreenReader.Say(toSpeak, true); - } - - return; - } - } + if (InventoryUtils.narrateHoveredSlot(__instance.inventory, __instance.inventory.inventory, __instance.inventory.actualInventory, x, y)) + return; if (tailoringMenuQuery != toSpeak)