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}) 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,