Make quick stack stack to any arbitrary inventory

This commit is contained in:
2025-03-30 16:29:52 +02:00
parent d54787a9a8
commit 40c3f91485
2 changed files with 68 additions and 14 deletions

View File

@@ -96,12 +96,15 @@ end, Hook.HookMethodType.After)
local throttle = 0.1
local throttleTimer = 0
Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable)
if PlayerInput.IsShiftDown() and PlayerInput.SecondaryMouseButtonClicked() then
cursormacroer.setTargetInventory()
end
if not PlayerInput.IsAltDown() then return end
if Timer.GetTime() < throttleTimer then return end
throttleTimer = Timer.GetTime() + throttle
-- We can not use shift because holding shift means we're moving half a stack
-- Fuck me sideways
-- if not PlayerInput.IsShiftDown() then return end
if not PlayerInput.IsAltDown() then return end
-- if not PlayerInput.PrimaryMouseButtonClicked() then return end
cursormacroer.tryStackCursorItem()
end, Hook.HookMethodType.After)

View File

@@ -1,7 +1,7 @@
local quickstack = require("Cyka.quickstack")
local utils = require("Cyka.utils")
---@return Barotrauma.VisualSlot|nil, Barotrauma.ItemInventory|nil, Barotrauma.InventorySlot|nil
---@return Barotrauma.VisualSlot|nil, Barotrauma.Inventory|nil, Barotrauma.Inventory.ItemSlot|nil
local function getInventorySlotUnderCursor()
-- Make sure we have a controlled character
local controlledCharacter = Character.Controlled
@@ -47,11 +47,8 @@ local function getInventorySlotUnderCursor()
return nil, nil, nil
end
-- TODO: Make shift right click mark target inventory
-- So that we can stack items to boxes and shit
local targetInventory = nil
local function tryStackCursorItem()
MyModGlobal.debugPrint("Shift + Left Click detected")
local visualSlot, itemInv, slot = getInventorySlotUnderCursor()
if not visualSlot or not itemInv or not slot then
MyModGlobal.debugPrint(string.format("No inventory slot or item found"))
@@ -61,18 +58,47 @@ local function tryStackCursorItem()
-- MyModGlobal.debugPrint(string.format("Item inventory: %s", tostring(itemInv)))
-- MyModGlobal.debugPrint(string.format("Inventory slot: %s", tostring(slot)))
local controlledCharacter = Character.Controlled
if not controlledCharacter then
MyModGlobal.debugPrint("No controlled character found")
return
end
if not slot or not slot.items or not slot.items[1] then
MyModGlobal.debugPrint("No items in slot")
return
end
local itemTree, err = quickstack.tryBuildCharacterItemTree(controlledCharacter)
local inventory = targetInventory
MyModGlobal.debugPrint(string.format("Target inventory: %s", tostring(inventory)))
if not inventory then
local controlledCharacter = Character.Controlled
if not controlledCharacter then
MyModGlobal.debugPrint("No controlled character found")
return
end
local cinventory = controlledCharacter.Inventory
if not cinventory or not cinventory.slots then
MyModGlobal.debugPrint("No inventory found")
return
end
local bagSlot = cinventory.slots[MyModGlobal.BAG_SLOT]
if not bagSlot or not bagSlot.items or not bagSlot.items[1] then
MyModGlobal.debugPrint("No bag slot found")
return
end
local bagItem = bagSlot.items[1]
if not bagItem or not bagItem.OwnInventory then
MyModGlobal.debugPrint("Bag item has no own inventory")
return
end
local bagInventory = bagItem.OwnInventory
if not bagInventory or not bagInventory.slots then
MyModGlobal.debugPrint("Bag inventory has no slots")
return
end
inventory = bagInventory
end
if not inventory then
MyModGlobal.debugPrint("No inventory found")
return
end
local itemTree, err = quickstack.buildItemTree(inventory)
if err then
MyModGlobal.debugPrint(string.format("Error building item tree: %s", err))
return
@@ -84,6 +110,7 @@ local function tryStackCursorItem()
MyModGlobal.debugPrint(string.format("Enqueued item: %s", tostring(item)))
end
-- MyModGlobal.debugPrint(string.format("Enqueued %d items from the inventory slot", #itemsToMove))
-- MyModGlobal.DumpTable(itemTree)
local errors = quickstack.tryMoveItems(itemsToMove, itemTree, true)
for _, error in ipairs(errors) do
@@ -91,6 +118,30 @@ local function tryStackCursorItem()
end
end
local function setTargetInventory()
local visualSlot, itemInv, slot = getInventorySlotUnderCursor()
if not visualSlot or not itemInv or not slot then
MyModGlobal.debugPrint(string.format("No inventory slot or item found"))
return
end
if not slot.items or #slot.items == 0 then
MyModGlobal.debugPrint("Slot is empty")
return
end
local item = slot.items[1]
if not item then
MyModGlobal.debugPrint("Slot is empty")
return
end
if not item.OwnInventory then
MyModGlobal.debugPrint("Item has no own inventory")
return
end
print(string.format("Setting target inventory to %s", tostring(item.OwnInventory)))
targetInventory = item.OwnInventory
end
return {
tryStackCursorItem = tryStackCursorItem
tryStackCursorItem = tryStackCursorItem,
setTargetInventory = setTargetInventory
}