local function is_valid_mining_drill(entity) return entity and entity.valid and entity.type == "mining-drill" end local function get_drill_quality(drill) if drill.prototype.quality then return drill.prototype.quality end if drill.supports_quality and drill.quality then return drill.quality end return 1 end local function update_inventory_quality(inventory, quality) if not (inventory and inventory.valid) then return false end local changed = false for i = 1, #inventory do local stack = inventory[i] if stack and stack.valid_for_read then local item_proto = game.item_prototypes[stack.name] if item_proto and item_proto.has_quality then if not stack.quality or stack.quality ~= quality then local new_stack = { name = stack.name, count = stack.count, quality = quality } inventory.remove(stack) inventory.insert(new_stack) changed = true end end end end return changed end local function update_mining_drills() if not settings.global["qmsa-enable-mod"].value then return end for _, surface in pairs(game.surfaces) do local drills = surface.find_entities_filtered { type = "mining-drill" } for _, drill in ipairs(drills) do if not is_valid_mining_drill(drill) then goto continue end if drill.name == "big-mining-drill" and not settings.global["qmsa-affect-big-drills"].value then goto continue end if drill.status == defines.entity_status.mining or drill.status == defines.entity_status.working then local drill_quality = get_drill_quality(drill) update_inventory_quality(drill.output_inventory, drill_quality) if drill.get_output_inventories then for _, inv in pairs(drill.get_output_inventories()) do update_inventory_quality(inv, drill_quality) end end end ::continue:: end end end script.on_init(function() end) script.on_configuration_changed(function(data) if data.mod_changes and data.mod_changes["quality-mining-space-age"] then end end) script.on_nth_tick(settings.global["qmsa-check-interval"].value or 60, function() pcall(update_mining_drills) end) script.on_event(defines.events.on_runtime_mod_setting_changed, function(event) if event.setting:find("qmsa%-") then pcall(update_mining_drills) end end)