Files
barotrauma-localmods/QuickStackToBag/Lua/Cyka/hotkeyrepair.lua
PhatPhuckDave 747eb4f6a4 Refactor all hotkeys into init
So that we can share scripts around themselves without hooking multiple
times accidentally
2025-03-30 15:12:52 +02:00

66 lines
1.9 KiB
Lua

---@return Barotrauma.Item|nil, Barotrauma.Items.Components.Repairable|nil
local function getRepairableObjectInFocus()
-- Make sure we have a controlled character
local controlledCharacter = Character.Controlled
if not controlledCharacter then
-- MyModGlobal.debugPrint("No controlled character")
return nil, nil
end
-- Check if we have a selected item - this is necessary for repair interfaces to show
local selectedItem = controlledCharacter.SelectedItem
if not selectedItem then
-- MyModGlobal.debugPrint("No selected item")
return nil, nil
end
-- Check if the selected item is in fact the repairable object itself
for _, component in pairs(selectedItem.Components) do
if component.name == "Repairable" then
-- Check if repair interface should be shown
if component:ShouldDrawHUD(controlledCharacter) then
return selectedItem, component
end
end
end
-- Nothing found
return nil, nil
end
---@return boolean
local function clickRepairButton()
local item, repairableComponent = getRepairableObjectInFocus()
if not item or not repairableComponent then
-- MyModGlobal.debugPrint("No repairable object in focus")
return false
end
-- Access the controlled character
local controlledCharacter = Character.Controlled
if not controlledCharacter then
-- MyModGlobal.debugPrint("No controlled character")
return false
end
-- Call the StartRepairing method directly
-- The second parameter (FixActions.Repair = 1) indicates a repair action
local result = repairableComponent:StartRepairing(controlledCharacter, 1)
-- MyModGlobal.debugPrint("StartRepairing result: " .. tostring(result))
return result
end
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
return {
tryRepair = tryRepair
}