Advertisement
Guest User

[Factorio] Convert mined entity into blueprint

a guest
Sep 22nd, 2016
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.89 KB | None | 0 0
  1. local building_prototype_name = "small-factory"
  2.  
  3. script.on_event(defines.events.on_preplayer_mined_item, function(event)
  4.   --event.entity
  5.   --event.player_index
  6.   if EntityOk(event.entity) and building.name == building_prototype_name and PlayerOk(game.players[event.player_index]) then
  7.     StoreBuilding(game.players[event.player_index], event.entity)
  8.   end
  9. end)
  10.  
  11. function StoreBuilding(player, building)
  12.   player.print(building_prototype_name .. " is being mined")
  13.   local blueprint_simple_stack = {name = "blueprint", count = 1}
  14.   local inv = player.get_inventory(defines.inventory.player_main)
  15.   if inv.can_insert(blueprint_simple_stack) then
  16.     inv.insert(blueprint_simple_stack)
  17.     for slot_index = 1, #inv, 1 do
  18.       local item_stack = inv[slot_index]
  19.       if item_stack.valid and item_stack.valid_for_read and item_stack.name == "blueprint" then
  20.         local bp_entities = item_stack.get_blueprint_entities()
  21.         if not bp_entities then
  22.           local blueprint = item_stack
  23.           local entities = {}
  24.           local ent = {entity_number = 1--[[unique number in current blueprint]], name = building_prototype_name, position = {1,1}}
  25.           table.insert(entities, ent)
  26.           blueprint.set_blueprint_entities(entities)
  27.           local blueprint_name
  28.           if building.backer_name and string.len(building.backer_name) > 0 then
  29.             blueprint_name = building.backer_name
  30.           else
  31.             blueprint_name = building_prototype_name --or surface name, or smth else
  32.           end
  33.           blueprint.label = blueprint_name
  34.           blueprint.allow_manual_label_change = false -- blueprint cannot be renamed
  35.           building.destroy()
  36.           return
  37.         end
  38.       end
  39.     end
  40.   end
  41. end
  42.  
  43. function EntityOk(entity)
  44.   return (entity and entity.valid)
  45. end
  46.  
  47. function PlayerOk(entity)
  48.   return (entity and entity.valid and entity.connected)
  49. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement