Skip to content

Belt link leftover loop never executes — unfitted items not compacted #15

@ILLISIS

Description

@ILLISIS

Bug

In module/edge/belt_link.lua, the push_belt_link function attempts to compact leftover items back to the front of item_stacks when not all items fit in the chest. However the loop bounds are inverted, so the loop never runs and the caller cannot determine what remains.

Code

-- belt_link.lua:111-114
if item_stacks_count > space then
    for index = 1, space - item_stacks_count do  -- e.g. space=3, count=5 → range 1,-2 → never runs
        item_stacks[index] = item_stacks[index + space]
    end

Why it's wrong

  • space = slots available in the chest (items successfully placed)
  • item_stacks_count = total items to place
  • When item_stacks_count > space, space - item_stacks_count is negative
  • In Lua, for i = 1, -2 never executes (positive step, limit less than start)
  • The leftover items at item_stacks[space+1 .. item_stacks_count] are never shifted to the front
  • item_stacks is left with nil holes at the front and stale data at the back — the caller cannot tell what remains

Fix

Swap the operands and clear the trailing slots after the shift:

if item_stacks_count > space then
    for index = 1, item_stacks_count - space do
        item_stacks[index] = item_stacks[index + space]
    end
    for index = item_stacks_count - space + 1, item_stacks_count do
        item_stacks[index] = nil
    end

Impact

Items that don't fit in the destination chest are silently lost rather than being returned to the source for retry on the next tick.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions