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

@@ -48,12 +48,12 @@ MyModGlobal.debugPrint = function(message)
end end
end end
require("Cyka.quickstack") local quickstack = require("Cyka.quickstack")
require("Cyka.fabricatorstack") local fabricatorstack = require("Cyka.fabricatorstack")
require("Cyka.quickbuy") local quickbuy = require("Cyka.quickbuy")
require("Cyka.hotkeyrepair") local hotkeyrepair = require("Cyka.hotkeyrepair")
local cursormacroer = require("Cyka.cursormacroer")
require("Cyka.xpticker") require("Cyka.xpticker")
require("Cyka.cursormacroer")
print(MyModGlobal.MOD_NAME .. " v" .. MyModGlobal.MOD_VERSION .. " loaded!") print(MyModGlobal.MOD_NAME .. " v" .. MyModGlobal.MOD_VERSION .. " loaded!")
@@ -72,3 +72,30 @@ LuaUserData.RegisterType("Barotrauma.Location+StoreInfo")
LuaUserData.MakeMethodAccessible(Descriptors["Barotrauma.CargoManager"], "GetConfirmedSoldEntities") LuaUserData.MakeMethodAccessible(Descriptors["Barotrauma.CargoManager"], "GetConfirmedSoldEntities")
LuaUserData.RegisterType("Barotrauma.Items.Components.Repairable") LuaUserData.RegisterType("Barotrauma.Items.Components.Repairable")
LuaUserData.RegisterType("Barotrauma.VisualSlot") 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)
end, Hook.HookMethodType.After)
Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable)
if not PlayerInput.KeyHit(MyModGlobal.CONFIG.FABRICATOR_KEY) then return end
fabricatorstack.tryStackFabricator(instance)
end, Hook.HookMethodType.After)
Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable)
if not PlayerInput.KeyHit(MyModGlobal.CONFIG.FIX) then return end
hotkeyrepair.tryRepair()
end, Hook.HookMethodType.After)
Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable)
if not PlayerInput.KeyHit(MyModGlobal.CONFIG.MAX_BUY) then return end
quickbuy.tryBuy()
end, Hook.HookMethodType.After)
Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable)
---@cast PlayerInput Barotrauma.PlayerInput
if not PlayerInput.IsShiftDown() then return end
if not PlayerInput.PrimaryMouseButtonClicked() then return end
cursormacroer.tryStackCursorItem()
end, Hook.HookMethodType.After)

View File

@@ -56,10 +56,7 @@ local function getInventorySlotUnderCursor()
return nil, nil, nil return nil, nil, nil
end end
Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable) local function tryStackCursorItem()
---@cast PlayerInput Barotrauma.PlayerInput
if not PlayerInput.IsShiftDown() then return end
if not PlayerInput.PrimaryMouseButtonClicked() then return end
MyModGlobal.debugPrint("Shift + Left Click detected") MyModGlobal.debugPrint("Shift + Left Click detected")
local visualSlot, itemInv, item = getInventorySlotUnderCursor() local visualSlot, itemInv, item = getInventorySlotUnderCursor()
@@ -70,4 +67,8 @@ Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptab
MyModGlobal.debugPrint("Visual slot: " .. tostring(visualSlot)) MyModGlobal.debugPrint("Visual slot: " .. tostring(visualSlot))
MyModGlobal.debugPrint("Item inventory: " .. tostring(itemInv)) MyModGlobal.debugPrint("Item inventory: " .. tostring(itemInv))
MyModGlobal.debugPrint("Item: " .. tostring(item)) MyModGlobal.debugPrint("Item: " .. tostring(item))
end, Hook.HookMethodType.After) end
return {
tryStackCursorItem = tryStackCursorItem
}

View File

@@ -62,18 +62,8 @@ local function getSelectedRecipeRequirements(fabricator)
} }
end end
-- Hook into player control to listen for key press ---@param character Barotrauma.Character
Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable) local function tryStackFabricator(character)
if not PlayerInput.KeyHit(MyModGlobal.CONFIG.FABRICATOR_KEY) then return end
-- TODO: Maybe get items from entire sub...?
-- There's no point getting recipes if we don't have all of this bullshit
---@type Barotrauma.Character
local character = instance
if not character then
MyModGlobal.debugPrint("Character instance is nil.")
return
end
---@type Barotrauma.CharacterInventory ---@type Barotrauma.CharacterInventory
local inventory = character.Inventory local inventory = character.Inventory
if not inventory then if not inventory then
@@ -171,4 +161,8 @@ Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptab
previous = item.Prefab.Identifier previous = item.Prefab.Identifier
end end
MyModGlobal.DumpTable(items) MyModGlobal.DumpTable(items)
end, Hook.HookMethodType.After) end
return {
tryStackFabricator = tryStackFabricator
}

View File

@@ -51,15 +51,15 @@ local function clickRepairButton()
return result return result
end end
Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable) local function tryRepair()
if not PlayerInput.KeyHit(MyModGlobal.CONFIG.FIX) then return end
-- MyModGlobal.debugPrint("Fix key pressed")
-- Try to click the repair button
local success = clickRepairButton() local success = clickRepairButton()
if success then if success then
-- MyModGlobal.debugPrint("Successfully clicked repair button") -- MyModGlobal.debugPrint("Successfully clicked repair button")
else else
-- MyModGlobal.debugPrint("Failed to click repair button") -- MyModGlobal.debugPrint("Failed to click repair button")
end end
end, Hook.HookMethodType.After) end
return {
tryRepair = tryRepair
}

View File

@@ -28,11 +28,7 @@ local function getCurrentStore()
return stores, nil return stores, nil
end end
-- Example: Add a key binding to buy all items in the current store local function tryBuy()
-- when the 'B' key is pressed
Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable)
if not PlayerInput.KeyHit(MyModGlobal.CONFIG.MAX_BUY) then return end
local cargoManager = Game.GameSession.Campaign.CargoManager local cargoManager = Game.GameSession.Campaign.CargoManager
if not cargoManager then if not cargoManager then
MyModGlobal.debugPrint("No cargo manager available") MyModGlobal.debugPrint("No cargo manager available")
@@ -73,4 +69,8 @@ Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptab
end end
end end
end end
end, Hook.HookMethodType.After) end
return {
tryBuy = tryBuy
}

View File

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