From 2ee9ffeeaddc3f10aa90bb2d9d59a29f7d7be94d Mon Sep 17 00:00:00 2001 From: hashbender <561911+hashbender@users.noreply.github.com> Date: Mon, 9 Nov 2026 15:36:48 -0800 Subject: [PATCH 1/2] fix(place_entity_next_to): consume the item, never place for free 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 --- .../agent/place_entity_next_to/server.lua | 19 ++++++++++++------- tests/actions/test_place_entity_next_to.py | 19 +++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/fle/env/tools/agent/place_entity_next_to/server.lua b/fle/env/tools/agent/place_entity_next_to/server.lua index 5d25766e6..0adf470da 100644 --- a/fle/env/tools/agent/place_entity_next_to/server.lua +++ b/fle/env/tools/agent/place_entity_next_to/server.lua @@ -159,6 +159,12 @@ storage.actions.place_entity_next_to = function(player_index, entity, ref_x, ref error("Invalid direction " .. direction .. " provided. Please use 0 (north), 4 (east), 8 (south), or 12 (west).") end + -- The entity is built from the character's inventory, so it must hold one + if player.get_item_count(entity) < 1 then + local inv_contents = storage.utils.format_inventory_for_error(player) + error("\"No " .. entity .. " in inventory. Current inventory: " .. inv_contents .. "\"") + end + -- Convert from Factorio 2.0 direction (0,4,8,12) to internal direction (0,1,2,3) for calculations local internal_direction = direction / 4 @@ -543,13 +549,12 @@ storage.actions.place_entity_next_to = function(player_index, entity, ref_x, ref end local placement_info = storage.utils.serialize_entity(new_entity) - - local item_stack = {name = entity, count = 1} - if player.get_main_inventory().can_insert(item_stack) then - player.get_main_inventory().remove(item_stack) - return placement_info - else + + -- Consume the item; if it can't be removed, undo the placement + if player.remove_item{name = entity, count = 1} < 1 then + new_entity.destroy() local inv_contents = storage.utils.format_inventory_for_error(player) - error("Not enough " .. entity .. " in inventory. Current inventory: " .. inv_contents) + error("\"No " .. entity .. " in inventory. Current inventory: " .. inv_contents .. "\"") end + return placement_info end diff --git a/tests/actions/test_place_entity_next_to.py b/tests/actions/test_place_entity_next_to.py index 4adee0bf7..e55882a23 100644 --- a/tests/actions/test_place_entity_next_to.py +++ b/tests/actions/test_place_entity_next_to.py @@ -1055,3 +1055,22 @@ def test_belt_smart_collision_avoidance(game): assert belt, "Belt should be placed with smart collision avoidance" print(f"✓ Belt placed at {belt.position} avoiding obstacles") + + +def test_place_next_to_consumes_item(game): + chest = game.place_entity(Prototype.WoodenChest, position=Position(x=0, y=0)) + before = game.inspect_inventory()[Prototype.WoodenChest] + game.place_entity_next_to( + Prototype.WoodenChest, chest.position, direction=Direction.RIGHT, spacing=0 + ) + assert game.inspect_inventory()[Prototype.WoodenChest] == before - 1 + + +def test_place_next_to_without_item_places_nothing(game): + chest = game.place_entity(Prototype.WoodenChest, position=Position(x=0, y=0)) + assert game.inspect_inventory().get(Prototype.IronChest, 0) == 0 + with pytest.raises(Exception, match="No iron-chest in inventory"): + game.place_entity_next_to( + Prototype.IronChest, chest.position, direction=Direction.RIGHT, spacing=0 + ) + assert not game.get_entities({Prototype.IronChest}) From ab7b542af1fd8b771fbad9ac3b17fedf3eed75fe Mon Sep 17 00:00:00 2001 From: hashbender <561911+hashbender@users.noreply.github.com> Date: Mon, 9 Nov 2026 19:23:02 -0800 Subject: [PATCH 2/2] test(rotate): give test_rotate_boiler a boiler 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 --- tests/actions/test_rotate.py | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/actions/test_rotate.py b/tests/actions/test_rotate.py index e8ba381a4..b129ff55d 100644 --- a/tests/actions/test_rotate.py +++ b/tests/actions/test_rotate.py @@ -9,6 +9,7 @@ def game(configure_game): return configure_game( inventory={ "iron-chest": 1, + "boiler": 1, "pipe": 10, "assembling-machine-2": 2, "transport-belt": 10,