Skip to content

fix(place_entity_next_to): consume the item, never place for free - #425

Open
hashbender wants to merge 2 commits into
JackHopkins:mainfrom
hashbender:fix/place-entity-next-to-consume-item
Open

hashbender wants to merge 2 commits into
JackHopkins:mainfrom
hashbender:fix/place-entity-next-to-consume-item

Conversation

@hashbender

Copy link
Copy Markdown

Problem

place_entity_next_to builds entities the character doesn't have:

# inventory: 5 wooden chests, no iron chests
ref = place_entity(Prototype.WoodenChest, position=Position(x=0, y=0))
place_entity_next_to(Prototype.IronChest, ref.position, direction=Direction.RIGHT, spacing=0)
# succeeds: an iron chest is built and nothing is removed

Calling it in a loop with 5 wooden chests in the inventory kept placing chests after the 5th, all for free.

Root cause

  • place_entity_next_to/server.lua:533 creates the entity first.
  • :547-549 then removes the item only if player.get_main_inventory().can_insert(item_stack). That checks whether there is room for the item, not whether the character holds it, and remove of a missing item just removes 0.
  • In the other branch (no room) it raises after the entity has already been built and leaves the item in the inventory.
  • place_entity gets this right: validate_inventory() before placing, then player.remove_item.

Fix

  • Raise No <item> in inventory. Current inventory: ... (the same message as place_entity) before placing if the character holds none.
  • Remove the item with player.remove_item{name = entity, count = 1}, as place_entity does.
  • If that removal fails, destroy the new entity and raise, so a failed call never leaves an entity behind.

Testing

Run against a Factorio 2.0.73 headless server:

  • New test_place_next_to_without_item_places_nothing fails on main ("DID NOT RAISE") and passes with the fix. New test_place_next_to_consumes_item passes on both.
  • Extra check: holding 5 wooden chests, repeated calls placed 8 of them on main (the loop's cap) and exactly 5 with the fix, which then raised "No wooden-chest in inventory".
  • tests/actions/test_place_entity_next_to.py and test_place_next_to_and_rotate.py: no new failures. test_adjacent_electric_mining_drills, test_collision_resolution_with_helpful_errors and test_belt_smart_collision_avoidance fail on main too on this map.
  • The no-room error branch couldn't be triggered from the test setup (a completely full inventory still let can_insert pass), so it's covered by reading the code rather than by a test.
  • ruff 0.11.13 format/check clean.

🤖 Generated with Claude Code

hashbender and others added 2 commits November 9, 2026 15:36
place_entity_next_to created the entity first and then removed the item
only if `get_main_inventory().can_insert(item)`. That checks for room, not
for the item, so a character holding none of the item could still place it
(and keep placing it), and when there was no room it raised after the entity
had already been built.

Check that the character holds the item before placing, remove it with
player.remove_item like place_entity does, and destroy the new entity if
the removal fails.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The test placed a boiler without holding one and only passed because
place_entity_next_to did not consume items.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant