Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local building_prototype_name = "small-factory"
- script.on_event(defines.events.on_preplayer_mined_item, function(event)
- --event.entity
- --event.player_index
- if EntityOk(event.entity) and building.name == building_prototype_name and PlayerOk(game.players[event.player_index]) then
- StoreBuilding(game.players[event.player_index], event.entity)
- end
- end)
- function StoreBuilding(player, building)
- player.print(building_prototype_name .. " is being mined")
- local blueprint_simple_stack = {name = "blueprint", count = 1}
- local inv = player.get_inventory(defines.inventory.player_main)
- if inv.can_insert(blueprint_simple_stack) then
- inv.insert(blueprint_simple_stack)
- for slot_index = 1, #inv, 1 do
- local item_stack = inv[slot_index]
- if item_stack.valid and item_stack.valid_for_read and item_stack.name == "blueprint" then
- local bp_entities = item_stack.get_blueprint_entities()
- if not bp_entities then
- local blueprint = item_stack
- local entities = {}
- local ent = {entity_number = 1--[[unique number in current blueprint]], name = building_prototype_name, position = {1,1}}
- table.insert(entities, ent)
- blueprint.set_blueprint_entities(entities)
- local blueprint_name
- if building.backer_name and string.len(building.backer_name) > 0 then
- blueprint_name = building.backer_name
- else
- blueprint_name = building_prototype_name --or surface name, or smth else
- end
- blueprint.label = blueprint_name
- blueprint.allow_manual_label_change = false -- blueprint cannot be renamed
- building.destroy()
- return
- end
- end
- end
- end
- end
- function EntityOk(entity)
- return (entity and entity.valid)
- end
- function PlayerOk(entity)
- return (entity and entity.valid and entity.connected)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement