From ee2b8bae27e74a8c6d0ec9c5a47ac31c27e1d6ab Mon Sep 17 00:00:00 2001 From: Liza Carvelli Date: Fri, 17 Nov 2023 15:13:13 +0100 Subject: [PATCH] Fix inventory slots not being filled to 999 if no free inventory slots available --- Workshoppa/Windows/CeruleumTankWindow.cs | 10 ++++--- Workshoppa/Windows/RepairKitWindow.cs | 8 +++-- Workshoppa/Windows/ShopWindow.cs | 5 ++-- Workshoppa/WorkshopPlugin.GameFunctions.cs | 34 ++++++++++++++++++++-- Workshoppa/Workshoppa.csproj | 2 +- 5 files changed, 47 insertions(+), 12 deletions(-) diff --git a/Workshoppa/Windows/CeruleumTankWindow.cs b/Workshoppa/Windows/CeruleumTankWindow.cs index e8f2cca..b83e875 100644 --- a/Workshoppa/Windows/CeruleumTankWindow.cs +++ b/Workshoppa/Windows/CeruleumTankWindow.cs @@ -106,10 +106,12 @@ internal sealed class CeruleumTankWindow : ShopWindow if (PurchaseState != null) { HandleNextPurchaseStep(); - - ImGui.Text($"Buying {FormatStackCount(PurchaseState.ItemsLeftToBuy)}..."); - if (ImGuiComponents.IconButtonWithText(FontAwesomeIcon.Times, "Cancel Auto-Buy")) - CancelAutoPurchase(); + if (PurchaseState != null) + { + ImGui.Text($"Buying {FormatStackCount(PurchaseState.ItemsLeftToBuy)}..."); + if (ImGuiComponents.IconButtonWithText(FontAwesomeIcon.Times, "Cancel Auto-Buy")) + CancelAutoPurchase(); + } } else { diff --git a/Workshoppa/Windows/RepairKitWindow.cs b/Workshoppa/Windows/RepairKitWindow.cs index 5c7d3a2..59509f2 100644 --- a/Workshoppa/Windows/RepairKitWindow.cs +++ b/Workshoppa/Windows/RepairKitWindow.cs @@ -101,9 +101,11 @@ internal sealed class RepairKitWindow : ShopWindow if (PurchaseState != null) { HandleNextPurchaseStep(); - - if (ImGuiComponents.IconButtonWithText(FontAwesomeIcon.Times, "Cancel Auto-Buy")) - CancelAutoPurchase(); + if (PurchaseState != null) + { + if (ImGuiComponents.IconButtonWithText(FontAwesomeIcon.Times, "Cancel Auto-Buy")) + CancelAutoPurchase(); + } } else { diff --git a/Workshoppa/Windows/ShopWindow.cs b/Workshoppa/Windows/ShopWindow.cs index 153fb10..8e7f700 100644 --- a/Workshoppa/Windows/ShopWindow.cs +++ b/Workshoppa/Windows/ShopWindow.cs @@ -157,7 +157,8 @@ internal abstract class ShopWindow : LImGui.LWindow, IDisposable if (ItemForSale == null || PurchaseState == null) return; - if (!_plugin.HasFreeInventorySlot()) + int maxStackSize = _plugin.DetermineMaxStackSize(ItemForSale.ItemId); + if (maxStackSize == 0 && !_plugin.HasFreeInventorySlot()) { _pluginLog.Warning($"No free inventory slots, can't buy more {ItemForSale.ItemName}"); PurchaseState = null; @@ -168,7 +169,7 @@ internal abstract class ShopWindow : LImGui.LWindow, IDisposable if (PurchaseState.NextStep <= DateTime.Now && _gameGui.TryGetAddonByName(_addonName, out AtkUnitBase* addonShop)) { - int buyNow = Math.Min(PurchaseState.ItemsLeftToBuy, 99); + int buyNow = Math.Min(PurchaseState.ItemsLeftToBuy, maxStackSize); _pluginLog.Information($"Buying {buyNow}x {ItemForSale.ItemName}"); FirePurchaseCallback(addonShop, buyNow); diff --git a/Workshoppa/WorkshopPlugin.GameFunctions.cs b/Workshoppa/WorkshopPlugin.GameFunctions.cs index 9e45111..73fb70c 100644 --- a/Workshoppa/WorkshopPlugin.GameFunctions.cs +++ b/Workshoppa/WorkshopPlugin.GameFunctions.cs @@ -42,7 +42,8 @@ partial class WorkshopPlugin if (npcIds.Contains(GetNpcId(obj))) { o = obj; - float distance = Vector3.Distance(localPlayerPosition.Value, obj.Position + new Vector3(0, -2, 0)); + float distance = Vector3.Distance(localPlayerPosition.Value, + obj.Position + new Vector3(0, -2, 0)); if (distance > 0.01) return distance; } @@ -62,7 +63,8 @@ partial class WorkshopPlugin private unsafe AtkUnitBase* GetCompanyCraftingLogAddon() { - if (_gameGui.TryGetAddonByName("CompanyCraftRecipeNoteBook", out var addon) && LAddon.IsAddonReady(addon)) + if (_gameGui.TryGetAddonByName("CompanyCraftRecipeNoteBook", out var addon) && + LAddon.IsAddonReady(addon)) return addon; return null; @@ -244,4 +246,32 @@ partial class WorkshopPlugin return count; } + + public unsafe int DetermineMaxStackSize(uint itemId) + { + var inventoryManger = InventoryManager.Instance(); + if (inventoryManger == null) + return 0; + + int max = 0; + for (InventoryType t = InventoryType.Inventory1; t <= InventoryType.Inventory4; ++t) + { + var container = inventoryManger->GetInventoryContainer(t); + for (int i = 0; i < container->Size; ++i) + { + var item = container->GetInventorySlot(i); + if (item == null || item->ItemID == 0) + return 99; + + if (item->ItemID == itemId) + { + max += (999 - (int)item->Quantity); + if (max >= 99) + break; + } + } + } + + return Math.Min(99, max); + } } diff --git a/Workshoppa/Workshoppa.csproj b/Workshoppa/Workshoppa.csproj index d66f6eb..5615527 100644 --- a/Workshoppa/Workshoppa.csproj +++ b/Workshoppa/Workshoppa.csproj @@ -1,7 +1,7 @@ net7.0-windows - 3.7 + 3.8 11.0 enable true