Add hotkey repair buttoner

This commit is contained in:
2025-03-30 14:53:49 +02:00
parent b14df82d78
commit d10d295a80
2 changed files with 71 additions and 38 deletions

View File

@@ -12,6 +12,7 @@ MyModGlobal = {
QUICKSTACK_KEYS = Keys.F,
FABRICATOR_KEY = Keys.V,
MAX_BUY = Keys.B,
FIX = Keys.R,
NESTED_CONTAINERS = true,
DEBUG_MODE = true,
},
@@ -48,6 +49,10 @@ MyModGlobal.debugPrint = function(message)
end
require("Cyka.quickstack")
require("Cyka.fabricatorstack")
require("Cyka.quickbuy")
require("Cyka.hotkeyrepair")
require("Cyka.xpticker")
print(MyModGlobal.MOD_NAME .. " v" .. MyModGlobal.MOD_VERSION .. " loaded!")
@@ -64,6 +69,7 @@ LuaUserData.RegisterType("Barotrauma.PurchasedItem")
LuaUserData.RegisterType("Barotrauma.ItemPrefab")
LuaUserData.RegisterType("Barotrauma.Location+StoreInfo")
LuaUserData.MakeMethodAccessible(Descriptors["Barotrauma.CargoManager"], "GetConfirmedSoldEntities")
LuaUserData.RegisterType("Barotrauma.Items.Components.Repairable")
-- -- Register necessary type to access VisualSlot
-- LuaUserData.RegisterType("Barotrauma.VisualSlot")
@@ -125,41 +131,3 @@ LuaUserData.MakeMethodAccessible(Descriptors["Barotrauma.CargoManager"], "GetCon
--
-- return nil, nil, nil
-- end
-- -- Register necessary types for detecting repairable objects
-- LuaUserData.RegisterType("Barotrauma.Items.Components.Repairable")
-- LuaUserData.MakeFieldAccessible(Descriptors["Barotrauma.Items.Components.Repairable"], "RepairButton")
--
-- ---@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 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 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 isRepairButtonVisible()
-- local _, repairableComponent = getRepairableObjectInFocus()
-- if not repairableComponent then return false end
--
-- -- Check if the repair button exists and is visible
-- local repairButton = repairableComponent.RepairButton
-- return repairButton ~= nil and repairButton.Visible
-- end

View File

@@ -0,0 +1,65 @@
---@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
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 success = clickRepairButton()
if success then
-- MyModGlobal.debugPrint("Successfully clicked repair button")
else
-- MyModGlobal.debugPrint("Failed to click repair button")
end
end, Hook.HookMethodType.After)