Shift clicking stacks item

W
This commit is contained in:
2025-03-30 15:19:38 +02:00
parent 747eb4f6a4
commit f37ef64739
3 changed files with 49 additions and 22 deletions

View File

@@ -75,7 +75,7 @@ LuaUserData.RegisterType("Barotrauma.VisualSlot")
Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable) Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable)
if not PlayerInput.KeyHit(MyModGlobal.CONFIG.QUICKSTACK_KEYS) then return end if not PlayerInput.KeyHit(MyModGlobal.CONFIG.QUICKSTACK_KEYS) then return end
quickstack.quickStackItems(character) quickstack.quickStackItems(instance)
end, Hook.HookMethodType.After) end, Hook.HookMethodType.After)
Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable) Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable)

View File

@@ -1,3 +1,5 @@
local quickstack = require("Cyka.quickstack")
---@return Barotrauma.VisualSlot|nil, Barotrauma.ItemInventory|nil, Barotrauma.Item|nil ---@return Barotrauma.VisualSlot|nil, Barotrauma.ItemInventory|nil, Barotrauma.Item|nil
local function getInventorySlotUnderCursor() local function getInventorySlotUnderCursor()
-- Make sure we have a controlled character -- Make sure we have a controlled character
@@ -61,12 +63,31 @@ local function tryStackCursorItem()
local visualSlot, itemInv, item = getInventorySlotUnderCursor() local visualSlot, itemInv, item = getInventorySlotUnderCursor()
if not visualSlot or not itemInv or not item then 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 return
end end
MyModGlobal.debugPrint("Visual slot: " .. tostring(visualSlot))
MyModGlobal.debugPrint("Item inventory: " .. tostring(itemInv))
MyModGlobal.debugPrint("Item: " .. tostring(item))
end end
return { return {

View File

@@ -99,10 +99,12 @@ end
---@param item Barotrauma.Item ---@param item Barotrauma.Item
---@param itemTree table<string, ItemLocation[]> ---@param itemTree table<string, ItemLocation[]>
---@param force boolean
---@return string ---@return string
local function tryMoveItem(item, itemTree) local function tryMoveItem(item, itemTree, force)
force = force or false
local location = itemTree[item.Prefab.Identifier.Value] 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 local moved = false
-- First try to move to existing stacks -- First try to move to existing stacks
@@ -129,11 +131,13 @@ end
---@param items Barotrauma.Item[] ---@param items Barotrauma.Item[]
---@param itemTree table<string, ItemLocation[]> ---@param itemTree table<string, ItemLocation[]>
---@param force boolean
---@return string[] ---@return string[]
local function tryMoveItems(items, itemTree) local function tryMoveItems(items, itemTree, force)
force = force or false
local errs = {} local errs = {}
for _, item in ipairs(items) do for _, item in ipairs(items) do
local err = tryMoveItem(item, itemTree) local err = tryMoveItem(item, itemTree, force)
-- oops, this one failed, continue... -- oops, this one failed, continue...
if err then if err then
errs[#errs + 1] = string.format("Failed to move item: %s", item.Prefab.Identifier.Value) 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" return itemTree, "Character has no inventory"
end end
local bagSlot = inventory.slots[MyModGlobal.CONFIG.BAG_SLOT] local bagSlot = inventory.slots[MyModGlobal.BAG_SLOT]
if bagSlot then if bagSlot then
-- MyModGlobal.debugPrint(string.format("Bag slot found at index 8 with %d items.", #bagSlot.items)) -- MyModGlobal.debugPrint(string.format("Bag slot found at index 8 with %d items.", #bagSlot.items))
if #bagSlot.items > 0 then 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...")) -- MyModGlobal.debugPrint(string.format("Item has its own inventory, building item tree for it..."))
itemTree = buildItemTree(item.OwnInventory, itemTree) itemTree = buildItemTree(item.OwnInventory, itemTree)
else else
return itemTree, "Bag does not have its own inventory." return itemTree, "Bag does not have its own inventory"
end end
else else
return itemTree, "Bag slot is empty." return itemTree, "Bag slot is empty"
end end
else else
return itemTree, "No bag slot found at index 8." return itemTree, "No bag slot found at index " .. tostring(MyModGlobal.BAG_SLOT)
end end
return itemTree, nil return itemTree, nil
end end
@@ -217,6 +221,15 @@ local function quickStackItems(character)
return return
end 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") MyModGlobal.debugPrint("Quick stack function called")
local itemTree, err = tryBuildCharacterItemTree(character) local itemTree, err = tryBuildCharacterItemTree(character)
@@ -228,14 +241,6 @@ local function quickStackItems(character)
--DumpTable(itemTree) --DumpTable(itemTree)
local toMove = {} 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 for _, slotid in ipairs(inventorySlotsToStack) do
MyModGlobal.debugPrint(string.format("Processing inventory slot: %d", slotid)) MyModGlobal.debugPrint(string.format("Processing inventory slot: %d", slotid))
local slot = inventory.slots[slotid] local slot = inventory.slots[slotid]
@@ -282,7 +287,8 @@ end
return { return {
buildItemTree = buildItemTree, buildItemTree = buildItemTree,
sortItemtreeBySlots = sortItemTree, tryBuildCharacterItemTree = tryBuildCharacterItemTree,
sortItemTree = sortItemTree,
tryMoveItem = tryMoveItem, tryMoveItem = tryMoveItem,
tryMoveItems = tryMoveItems, tryMoveItems = tryMoveItems,
quickStackItems = quickStackItems quickStackItems = quickStackItems