diff --git a/QuickStackToBag/Lua/Autorun/init.lua b/QuickStackToBag/Lua/Autorun/init.lua index b49606a..1259ef6 100644 --- a/QuickStackToBag/Lua/Autorun/init.lua +++ b/QuickStackToBag/Lua/Autorun/init.lua @@ -75,7 +75,7 @@ LuaUserData.RegisterType("Barotrauma.VisualSlot") Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable) if not PlayerInput.KeyHit(MyModGlobal.CONFIG.QUICKSTACK_KEYS) then return end - quickstack.quickStackItems(character) + quickstack.quickStackItems(instance) end, Hook.HookMethodType.After) Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable) diff --git a/QuickStackToBag/Lua/Cyka/cursormacroer.lua b/QuickStackToBag/Lua/Cyka/cursormacroer.lua index 187f180..04f7204 100644 --- a/QuickStackToBag/Lua/Cyka/cursormacroer.lua +++ b/QuickStackToBag/Lua/Cyka/cursormacroer.lua @@ -1,3 +1,5 @@ +local quickstack = require("Cyka.quickstack") + ---@return Barotrauma.VisualSlot|nil, Barotrauma.ItemInventory|nil, Barotrauma.Item|nil local function getInventorySlotUnderCursor() -- Make sure we have a controlled character @@ -61,12 +63,31 @@ local function tryStackCursorItem() local visualSlot, itemInv, item = getInventorySlotUnderCursor() if not visualSlot or not itemInv or not item then - MyModGlobal.debugPrint("No inventory slot or item found") + MyModGlobal.debugPrint(string.format("No inventory slot or item found")) + return + end + MyModGlobal.debugPrint(string.format("Visual slot: %s", tostring(visualSlot))) + MyModGlobal.debugPrint(string.format("Item inventory: %s", tostring(itemInv))) + MyModGlobal.debugPrint(string.format("Item: %s", tostring(item))) + + local controlledCharacter = Character.Controlled + if not controlledCharacter then + MyModGlobal.debugPrint("No controlled character found") + return + end + + local itemTree, err = quickstack.tryBuildCharacterItemTree(controlledCharacter) + if err then + MyModGlobal.debugPrint(string.format("Error building item tree: %s", err)) + return + end + itemTree = quickstack.sortItemTree(itemTree) + + err = quickstack.tryMoveItem(item, itemTree, true) + if err then + MyModGlobal.debugPrint(string.format("Error moving item: %s", err)) return end - MyModGlobal.debugPrint("Visual slot: " .. tostring(visualSlot)) - MyModGlobal.debugPrint("Item inventory: " .. tostring(itemInv)) - MyModGlobal.debugPrint("Item: " .. tostring(item)) end return { diff --git a/QuickStackToBag/Lua/Cyka/quickstack.lua b/QuickStackToBag/Lua/Cyka/quickstack.lua index 1d427b5..6a00973 100644 --- a/QuickStackToBag/Lua/Cyka/quickstack.lua +++ b/QuickStackToBag/Lua/Cyka/quickstack.lua @@ -99,10 +99,12 @@ end ---@param item Barotrauma.Item ---@param itemTree table +---@param force boolean ---@return string -local function tryMoveItem(item, itemTree) +local function tryMoveItem(item, itemTree, force) + force = force or false local location = itemTree[item.Prefab.Identifier.Value] - if not location then return nil, "No locations for item, not stacking" end + if not location and not force then return nil, "No locations for item, not stacking (not forced)" end local moved = false -- First try to move to existing stacks @@ -129,11 +131,13 @@ end ---@param items Barotrauma.Item[] ---@param itemTree table +---@param force boolean ---@return string[] -local function tryMoveItems(items, itemTree) +local function tryMoveItems(items, itemTree, force) + force = force or false local errs = {} for _, item in ipairs(items) do - local err = tryMoveItem(item, itemTree) + local err = tryMoveItem(item, itemTree, force) -- oops, this one failed, continue... if err then errs[#errs + 1] = string.format("Failed to move item: %s", item.Prefab.Identifier.Value) @@ -186,7 +190,7 @@ local function tryBuildCharacterItemTree(character) return itemTree, "Character has no inventory" end - local bagSlot = inventory.slots[MyModGlobal.CONFIG.BAG_SLOT] + local bagSlot = inventory.slots[MyModGlobal.BAG_SLOT] if bagSlot then -- MyModGlobal.debugPrint(string.format("Bag slot found at index 8 with %d items.", #bagSlot.items)) if #bagSlot.items > 0 then @@ -196,13 +200,13 @@ local function tryBuildCharacterItemTree(character) -- MyModGlobal.debugPrint(string.format("Item has its own inventory, building item tree for it...")) itemTree = buildItemTree(item.OwnInventory, itemTree) else - return itemTree, "Bag does not have its own inventory." + return itemTree, "Bag does not have its own inventory" end else - return itemTree, "Bag slot is empty." + return itemTree, "Bag slot is empty" end else - return itemTree, "No bag slot found at index 8." + return itemTree, "No bag slot found at index " .. tostring(MyModGlobal.BAG_SLOT) end return itemTree, nil end @@ -217,6 +221,15 @@ local function quickStackItems(character) return end + -- local inventory = character.Inventory + -- for i, slot in ipairs(inventory.slots) do + -- if #slot.items > 0 then + -- local item = slot.items[1] + -- local identifier = item.Prefab.Identifier.Value + -- print(string.format("Item at slot %d is %s", i, identifier)) + -- end + -- end + MyModGlobal.debugPrint("Quick stack function called") local itemTree, err = tryBuildCharacterItemTree(character) @@ -228,14 +241,6 @@ local function quickStackItems(character) --DumpTable(itemTree) local toMove = {} - -- for i, slot in ipairs(inventory.slots) do - -- if #slot.items > 0 then - -- local item = slot.items[1] - -- local identifier = item.Prefab.Identifier.Value - -- print(string.format("Item at slot %d is %s", i, identifier)) - -- end - -- end - for _, slotid in ipairs(inventorySlotsToStack) do MyModGlobal.debugPrint(string.format("Processing inventory slot: %d", slotid)) local slot = inventory.slots[slotid] @@ -282,7 +287,8 @@ end return { buildItemTree = buildItemTree, - sortItemtreeBySlots = sortItemTree, + tryBuildCharacterItemTree = tryBuildCharacterItemTree, + sortItemTree = sortItemTree, tryMoveItem = tryMoveItem, tryMoveItems = tryMoveItems, quickStackItems = quickStackItems