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
require("Cyka.quickstack")
require("Cyka.fabricatorstack")
require("Cyka.quickbuy")
require("Cyka.hotkeyrepair")
local quickstack = require("Cyka.quickstack")
local fabricatorstack = require("Cyka.fabricatorstack")
local quickbuy = require("Cyka.quickbuy")
local hotkeyrepair = require("Cyka.hotkeyrepair")
local cursormacroer = require("Cyka.cursormacroer")
require("Cyka.xpticker")
require("Cyka.cursormacroer")
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.RegisterType("Barotrauma.Items.Components.Repairable")
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
end
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
local function tryStackCursorItem()
MyModGlobal.debugPrint("Shift + Left Click detected")
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("Item inventory: " .. tostring(itemInv))
MyModGlobal.debugPrint("Item: " .. tostring(item))
end, Hook.HookMethodType.After)
end
return {
tryStackCursorItem = tryStackCursorItem
}

View File

@@ -62,18 +62,8 @@ local function getSelectedRecipeRequirements(fabricator)
}
end
-- Hook into player control to listen for key press
Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable)
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
---@param character Barotrauma.Character
local function tryStackFabricator(character)
---@type Barotrauma.CharacterInventory
local inventory = character.Inventory
if not inventory then
@@ -171,4 +161,8 @@ Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptab
previous = item.Prefab.Identifier
end
MyModGlobal.DumpTable(items)
end, Hook.HookMethodType.After)
end
return {
tryStackFabricator = tryStackFabricator
}

View File

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

View File

@@ -28,11 +28,7 @@ local function getCurrentStore()
return stores, nil
end
-- Example: Add a key binding to buy all items in the current store
-- 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 function tryBuy()
local cargoManager = Game.GameSession.Campaign.CargoManager
if not cargoManager then
MyModGlobal.debugPrint("No cargo manager available")
@@ -73,4 +69,8 @@ Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptab
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 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
}