Fix goto disasters

This commit is contained in:
2025-03-29 19:30:36 +01:00
parent b7f090626d
commit 7115a9fe36

View File

@@ -3,7 +3,7 @@ if SERVER then return end
-- Register necessary types and make fields accessible
LuaUserData.RegisterType("Barotrauma.Items.Components.ItemContainer+SlotRestrictions")
LuaUserData.RegisterType(
'System.Collections.Immutable.ImmutableArray`1[[Barotrauma.Items.Components.ItemContainer+SlotRestrictions, Barotrauma]]')
'System.Collections.Immutable.ImmutableArray`1[[Barotrauma.Items.Components.ItemContainer+SlotRestrictions, Barotrauma]]')
LuaUserData.MakeFieldAccessible(Descriptors['Barotrauma.Items.Components.ItemContainer'], 'slotRestrictions')
LuaUserData.MakeFieldAccessible(Descriptors['Barotrauma.ItemInventory'], 'slots')
LuaUserData.MakeFieldAccessible(Descriptors["Barotrauma.CharacterInventory"], "slots")
@@ -62,7 +62,7 @@ local function findContainersInInventory(character)
if CONFIG.CHECK_ALL_CONTAINERS then
for slotIndex, slot in ipairs(inventory.slots) do
-- Skip the primary bag slot as we already processed it
if slotIndex == CONFIG.PRIMARY_BAG_SLOT then goto continue end
if slotIndex == CONFIG.PRIMARY_BAG_SLOT then goto continueSlot end
-- Skip hand slots if configured to do so
if CONFIG.SKIP_HAND_SLOTS then
@@ -73,7 +73,7 @@ local function findContainersInInventory(character)
break
end
end
if isHandSlot then goto continue end
if isHandSlot then goto continueSlot end
end
for _, item in ipairs(slot.items) do
@@ -82,7 +82,7 @@ local function findContainersInInventory(character)
end
end
::continue::
::continueSlot::
end
end
@@ -124,35 +124,23 @@ local function tryStackItemInSlot(container, sourceItem, slotIndex)
return false
end
-- Function to quick stack items into containers
local function quickStackToContainers(character, containers)
if not character or #containers == 0 then return end
local playerInv = character.Inventory
local processedCount = 0
local stackedCount = 0
-- Process inventory slots
for slotIndex = 1, #playerInv.slots do
-- Skip hand slots if configured to do so
-- Function to process a single inventory slot
local function processInventorySlot(playerInv, slotIndex, containers, processedCount, stackedCount)
-- Check if this slot should be skipped (hand slots)
if CONFIG.SKIP_HAND_SLOTS then
local isHandSlot = false
for _, handSlot in ipairs(CONFIG.HAND_SLOTS) do
if slotIndex == handSlot then
isHandSlot = true
break
return processedCount, stackedCount
end
end
if isHandSlot then goto continue end
end
local slot = playerInv.slots[slotIndex]
if not slot then goto continue end
if not slot then return processedCount, stackedCount end
-- Process items in the slot
for i = #slot.items, 1, -1 do -- Iterate backwards to safely remove
local item = slot.items[i]
-- Declare success variable outside any potential goto jumps
local success = false
-- Skip containers themselves
@@ -164,10 +152,9 @@ local function quickStackToContainers(character, containers)
processedCount = processedCount + 1
if processedCount > CONFIG.MAX_ITEMS_TO_PROCESS then
print("QuickStack: Safety limit reached")
return stackedCount
return processedCount, stackedCount
end
-- Try to stack the item into the containers
-- Try each container in order
for _, container in ipairs(containers) do
local containerInv = container.OwnInventory
@@ -205,7 +192,26 @@ local function quickStackToContainers(character, containers)
::nextItem::
end
::continue::
return processedCount, stackedCount
end
-- Function to quick stack items into containers
local function quickStackToContainers(character, containers)
if not character or #containers == 0 then return 0 end
local playerInv = character.Inventory
local processedCount = 0
local stackedCount = 0
-- Process each inventory slot
for slotIndex = 1, #playerInv.slots do
processedCount, stackedCount = processInventorySlot(
playerInv,
slotIndex,
containers,
processedCount,
stackedCount
)
end
return stackedCount