Refactor all hotkeys into init

So that we can share scripts around themselves without hooking multiple
times accidentally
This commit is contained in:
2025-03-30 15:11:59 +02:00
parent 5e3161f35f
commit 747eb4f6a4
6 changed files with 78 additions and 59 deletions

View File

@@ -13,7 +13,7 @@ local utils = require("Cyka.utils")
---@param inventory Barotrauma.ItemInventory
---@param itemTree table<string, ItemLocation[]>
---@param depth number
---@return table
---@return table<string, ItemLocation[]>
local function buildItemTree(inventory, itemTree, depth)
itemTree = itemTree or {}
depth = depth or 0
@@ -79,7 +79,7 @@ end
-- We would like to fill larger stacks first
---@param itemTree table<string, ItemLocation[]>
---@return table<string, ItemLocation[]>
local function sortItemtreeBySlots(itemTree)
local function sortItemTree(itemTree)
for _, item in pairs(itemTree) do
table.sort(item, function(a, b)
---@cast a ItemLocation
@@ -176,12 +176,17 @@ local function getOpenContainers()
return { selectedItem }
end
---@param inventory Barotrauma.ItemInventory
---@param character Barotrauma.Character
---@return table<string, ItemLocation[]>, string
local function tryBuildItemTree(inventory)
local function tryBuildCharacterItemTree(character)
local itemTree = {}
-- MyModGlobal.debugPrint(string.format("Preparing to stack items into the bag..."))
local bagSlot = inventory.slots[8]
local inventory = character.Inventory
if not inventory or not inventory.slots then
return itemTree, "Character has no inventory"
end
local bagSlot = inventory.slots[MyModGlobal.CONFIG.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
@@ -214,18 +219,12 @@ local function quickStackItems(character)
MyModGlobal.debugPrint("Quick stack function called")
local inventory = character.Inventory
if not inventory or not inventory.slots then
MyModGlobal.debugPrint("Character has no inventory")
return
end
local itemTree, err = tryBuildItemTree(inventory)
local itemTree, err = tryBuildCharacterItemTree(character)
if err then
MyModGlobal.debugPrint(string.format("Error building item tree: %s", err))
return
end
itemTree = sortItemtreeBySlots(itemTree)
itemTree = sortItemTree(itemTree)
--DumpTable(itemTree)
local toMove = {}
@@ -257,7 +256,7 @@ local function quickStackItems(character)
toMove = utils.enqueueSlot(slot, toMove)
local after = #toMove
MyModGlobal.debugPrint(string.format("Enqueued %d items from the inventory slot %d", after - before,
slotid))
slotid))
end
end
end
@@ -277,16 +276,14 @@ local function quickStackItems(character)
local errors = tryMoveItems(toMove, itemTree)
for _, error in ipairs(errors) do
print(string.format("Error stacking item: %s", error))
MyModGlobal.debugPrint(string.format("Error stacking item: %s", error))
end
end
-- Hook into player control to listen for key press
Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable)
if not PlayerInput.KeyHit(MyModGlobal.CONFIG.QUICKSTACK_KEYS) then return end
local character = instance
if not character then return end
quickStackItems(character)
end, Hook.HookMethodType.After)
return {
buildItemTree = buildItemTree,
sortItemtreeBySlots = sortItemTree,
tryMoveItem = tryMoveItem,
tryMoveItems = tryMoveItems,
quickStackItems = quickStackItems
}