@@ -11,19 +11,23 @@ script.on_event(defines.events.on_player_selected_area, function (event)
1111 if event .item == " AreaPaste-paste-planner" then
1212 local player_index = event .player_index
1313 local player = game .get_player (player_index )
14+ --- @cast player - nil
1415 local copy_source = try_get_copy_source (player )
1516 if copy_source then
1617 local copy_source_name = copy_source .name
1718 local player_position = player .position
1819 local force = player .force
1920 local player_insert = player .insert
20- local spill_item_stack = player .surface .spill_item_stack
21+ local surface = player .surface
22+ local spill_item_stack = surface .spill_item_stack
2123 local simple_item_stack = {}
2224 for _ , entity in pairs (event .entities ) do
23- if entity .name == copy_source_name then
25+ if entity .valid and entity .name == copy_source_name then
26+ local entity_position = entity .position
27+ -- `copy_settings` raises an event, so every LuaObject could be invalidated by this call
2428 local returned_items = entity .copy_settings (copy_source , player )
25- if next (returned_items ) then
26- if player .can_reach_entity (entity ) then
29+ if next (returned_items ) and surface . valid then
30+ if entity . valid and player .can_reach_entity (entity ) then
2731 for name , count in pairs (returned_items ) do
2832 simple_item_stack .name = name
2933 simple_item_stack .count = count
@@ -34,7 +38,6 @@ script.on_event(defines.events.on_player_selected_area, function (event)
3438 end
3539 end
3640 else
37- local entity_position = entity .position
3841 for name , count in pairs (returned_items ) do
3942 simple_item_stack .name = name
4043 simple_item_stack .count = count
@@ -43,6 +46,9 @@ script.on_event(defines.events.on_player_selected_area, function (event)
4346 end
4447 end
4548 end
49+ if not copy_source .valid then
50+ break
51+ end
4652 end
4753 end
4854 end
0 commit comments