LeFauxMatt / StardewMods

Stardew Valley mods
MIT License
23 stars 38 forks source link

[Better Chests] [Search Items] Compatibility with Satchels mod #89

Closed LiamKarlMitchell closed 7 months ago

LiamKarlMitchell commented 7 months ago

Better Chests seems to break the Satchels mod when Search Items feature is enabled.

When I press Escape button whilst in a chest with a Satchel menu open it does not trigger the cleanup/closed logic of the menu as it abruptly closes out of the menus.

When I disable "Search Items" in Better Chests -> Main configuration then this does not happen.

Possibly because the input is suppressed, or due to the way the menus are closed.

SearchItems.cs OnButtonPressed

case SButton.Escape when this.itemGrabMenuManager.CurrentMenu.readyToClose():
    Game1.playSound("bigDeSelect");
    this.itemGrabMenuManager.CurrentMenu.exitThisMenu();
    this.inputHelper.Suppress(e.Button);
    return;

Is it possible to add additional code if another mod is loaded and to run the menus cleanupBeforeExit method if it is open? Or to make sure any child menus are closed in turn?

The satchels have a NetBool isOpen which is not cleared.

Reproduction steps: 1) Open a chest 2) Open a satchel by right clicking it from inventory 3) Press Escape 4) Open Inventory 5) Right clicking the satchel does not open it anymore.

LiamKarlMitchell commented 7 months ago

IClickableMenu exitThisMenu() does not seem to propagate to child menus perhaps it needs to exit them first without playing sound.

But I am not sure how to patch this function.

    public void exitThisMenu(bool playSound = true)
    {
      if (this._childMenu != null)
      {
          if (this._childMenu.IsActive())
          {
              this._childMenu.exitThisMenuNoSound();
          }
      }
LiamKarlMitchell commented 7 months ago

Alternatively, if satchel mod is present, enumerating over the players Satchels in inventory or current storage/chest that have isOpen set and setting it to false might fix it.

LiamKarlMitchell commented 7 months ago

Checking if there is a child menu and closing it seems to sort it, but children can have children so this may not be ideal. E.g. InventoryMenu -> SatchelMenu -> Satchel Auto Pickup Filter

                if (this.itemGrabMenuManager.CurrentMenu != null
                    && this.itemGrabMenuManager.CurrentMenu.GetChildMenu() != null)
                {
                    this.itemGrabMenuManager.CurrentMenu.GetChildMenu().exitThisMenuNoSound();
                }

                if (this.itemGrabMenuManager.CurrentMenu != null)
                {
                    this.itemGrabMenuManager.CurrentMenu.exitThisMenu();
                }

image

I modified the Satchel class to show a message in game UI when it is cleared.

SatchelMenu.cs

        protected override void cleanupBeforeExit()
        {
            Game1.addHUDMessage(new HUDMessage("Cleanup before exit", HUDMessage.error_type));
            base.cleanupBeforeExit();
            satchel.isOpen.Value = false;
        }

image

    private void CloseMenus(IClickableMenu? menu = null)
    {
        if (menu != null)
        {
            var childMenu = menu.GetChildMenu();
            if (childMenu != null)
            {
                this.CloseMenus(childMenu);
                childMenu.exitThisMenuNoSound();
            }
        }
        else if (this.itemGrabMenuManager.CurrentMenu != null)
        {
            this.CloseMenus(this.itemGrabMenuManager.CurrentMenu);
            this.itemGrabMenuManager.CurrentMenu?.exitThisMenu();
        }
    }

Also the sound Game1.playSound("bigDeSelect"); probably only needs to play if the search input was in focus at time of pressing escape?

LeFauxMatt commented 7 months ago

I don't believe this is something I can fix from Better Chests. I started trying to implement a fix, but even when I disable SearchItems from my mod, the issue still occurs.

LiamKarlMitchell commented 7 months ago

I have encountered other ways to trigger this issue as well, other things that close menus/ui (farm house visits mod), sometimes when I am in the fridge.

Perhaps the problem lies with the satchel.isOpen value? What is the purpose of the value if the menu knows what satchel is open I wonder?