Fix rubberbanding and refactor a little

This commit is contained in:
2025-03-31 12:09:21 +02:00
parent 79bfa72afd
commit d7ceb9a4d6
2 changed files with 84 additions and 39 deletions

View File

@@ -24,6 +24,7 @@ MyModGlobal = {
AOEPICKUP = Keys.Y,
QICK_FABRICATOR = Keys.K,
QICK_DECONSTRUCTOR = Keys.J,
QICK_MEDICAL_FABRICATOR = Keys.M,
NESTED_CONTAINERS = true,
DEBUG_MODE = true,
},
@@ -166,10 +167,15 @@ end, Hook.HookMethodType.After)
Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable)
if not PlayerInput.KeyHit(MyModGlobal.CONFIG.QICK_FABRICATOR) then return end
quickaccess.tryAccessFabricator()
quickaccess.tryAccessFabricator(PlayerInput.IsShiftDown())
end, Hook.HookMethodType.After)
Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable)
if not PlayerInput.KeyHit(MyModGlobal.CONFIG.QICK_DECONSTRUCTOR) then return end
quickaccess.tryAccessDeconstructor()
quickaccess.tryAccessDeconstructor(PlayerInput.IsShiftDown())
end, Hook.HookMethodType.After)
Hook.Patch("Barotrauma.Character", "ControlLocalPlayer", function(instance, ptable)
if not PlayerInput.KeyHit(MyModGlobal.CONFIG.QICK_MEDICAL_FABRICATOR) then return end
quickaccess.tryAccessMedicalFabricator(PlayerInput.IsShiftDown())
end, Hook.HookMethodType.After)

View File

@@ -7,7 +7,7 @@ Hook.Add("Character_CanInteractWith_Item_Postfix", "YourModName.AllowRemoteInter
end)
-- Then patch the CanInteractWith method
Hook.Patch("YourModName", "Barotrauma.Character", "CanInteractWith",
Hook.Patch("Cyka", "Barotrauma.Character", "CanInteractWith",
{ "Barotrauma.Item", "System.Single&", "System.Boolean" },
function(_, ptable)
local result = Hook.Call("Character_CanInteractWith_Item_Postfix")
@@ -18,30 +18,54 @@ Hook.Patch("YourModName", "Barotrauma.Character", "CanInteractWith",
Hook.HookMethodType.After
)
local function tryAccessFabricator()
local cachedFabricator = nil
local cachedDeconstructor = nil
local cachedMedicalFabricator = nil
---@param prefabIdentifier string
---@param componentName string
---@return Barotrauma.Item, Barotrauma.Items.Components
local function findByComponent(prefabIdentifier, componentName)
local items = utils.enqueueAllSubmarineItems({}, function(item)
return item.Prefab.Identifier.Value == "fabricator", item.Prefab.Identifier.Value == "fabricator"
return item.Prefab.Identifier.Value == prefabIdentifier, item.Prefab.Identifier.Value == prefabIdentifier
end)
if #items == 0 then
MyModGlobal.debugPrint("No " .. componentName .. " found")
return nil, nil
end
if #items > 1 then
MyModGlobal.debugPrint("Multiple " .. componentName .. " found, using first one")
return nil, nil
end
---@type Barotrauma.Item
local item = items[1]
---@type Barotrauma.Items.Components
local component
for itcomponent in item.Components do
if itcomponent.Name == componentName then
component = itcomponent
break
end
end
if not component then
MyModGlobal.debugPrint("No " .. componentName .. " component found")
return
end
return item, component
end
---@param force boolean
local function tryAccessFabricator(force)
local fabricator = cachedFabricator
if not fabricator or force then
fabricator = findByComponent("fabricator", "Fabricator")
if not fabricator then
MyModGlobal.debugPrint("No fabricator found")
return
end
if #items > 1 then
MyModGlobal.debugPrint("Multiple fabricators found, using first one")
end
---@type Barotrauma.Item
local fabricator = items[1]
---@type Barotrauma.Items.Components.Fabricator
local fabricatorComponent
for component in fabricator.Components do
if component.Name == "Fabricator" then
fabricatorComponent = component
end
end
if not fabricatorComponent then
MyModGlobal.debugPrint("No fabricator component found")
return
cachedFabricator = fabricator
end
local me = Character.Controlled
@@ -49,35 +73,23 @@ local function tryAccessFabricator()
MyModGlobal.debugPrint("No controlled character found")
return
end
MyModGlobal.debugPrint("Selecting fabricator")
-- fabricatorComponent.Select(me)
fabricator.Prefab.GrabWhenSelected = false
me.SelectedItem = fabricator
end
local function tryAccessDeconstructor()
local items = utils.enqueueAllSubmarineItems({}, function(item)
return item.Prefab.Identifier.Value == "deconstructor", item.Prefab.Identifier.Value == "deconstructor"
end)
if #items == 0 then
---@param force boolean
local function tryAccessDeconstructor(force)
local deconstructor = cachedDeconstructor
if not deconstructor or force then
deconstructor = findByComponent("deconstructor", "Deconstructor")
if not deconstructor then
MyModGlobal.debugPrint("No deconstructor found")
return
end
if #items > 1 then
MyModGlobal.debugPrint("Multiple deconstructors found, using first one")
end
---@type Barotrauma.Item
local deconstructor = items[1]
---@type Barotrauma.Items.Components.Deconstructor
local deconstructorComponent
for component in deconstructor.Components do
if component.Name == "Deconstructor" then
deconstructorComponent = component
end
end
if not deconstructorComponent then
MyModGlobal.debugPrint("No deconstructor component found")
return
cachedDeconstructor = deconstructor
end
local me = Character.Controlled
@@ -85,12 +97,39 @@ local function tryAccessDeconstructor()
MyModGlobal.debugPrint("No controlled character found")
return
end
MyModGlobal.debugPrint("Selecting deconstructor")
-- deconstructorComponent.Select(me)
deconstructor.Prefab.GrabWhenSelected = false
me.SelectedItem = deconstructor
end
---@param force boolean
local function tryAccessMedicalFabricator(force)
local medicalFabricator = cachedMedicalFabricator
if not medicalFabricator or force then
medicalFabricator = findByComponent("medicalfabricator", "MedicalFabricator")
if not medicalFabricator then
MyModGlobal.debugPrint("No medical fabricator found")
return
end
cachedMedicalFabricator = medicalFabricator
end
local me = Character.Controlled
if not me then
MyModGlobal.debugPrint("No controlled character found")
return
end
MyModGlobal.debugPrint("Selecting medical fabricator")
-- medicalFabricatorComponent.Select(me)
medicalFabricator.Prefab.GrabWhenSelected = false
me.SelectedItem = medicalFabricator
end
return {
tryAccessFabricator = tryAccessFabricator,
tryAccessDeconstructor = tryAccessDeconstructor,
tryAccessMedicalFabricator = tryAccessMedicalFabricator,
}