SHARE
TWEET

electronics_assembler.lua

a guest Dec 11th, 2019 93 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local component = require('component')
  2. local db = component.database
  3. local export_bus = component.me_exportbus
  4. local controller = component.me_controller
  5. local hashItem = db.computeHash
  6.  
  7.  
  8. local chest_side = 4
  9.  
  10. local currentQueue = {}
  11.  
  12. local function loadDatabase()
  13.   print('populating database ...')
  14.   for i,v in ipairs(controller.getCraftables()) do
  15.     db.clear(i)
  16.     local item = v.getItemStack()
  17.     if (item.name:match("^opencomputers:.*")) then
  18.       print("adding item: " .. item.label)
  19.       controller.store({label = item.label}, db.address, 1, 1)
  20.     end
  21.   end  
  22.   print('done')
  23. end
  24.  
  25. local function getDbItems()
  26.   local items = {}
  27.   items.data = {}
  28.   for i=1,81,1 do
  29.     local item = db.get(i)
  30.     if item ~= nil then
  31.       items.data[i] = item
  32.     else
  33.       items.count = i - 1
  34.       break
  35.     end
  36.   end
  37.   return items
  38. end
  39.  
  40. local function listDbItems()
  41.   local items = getDbItems()
  42.   for i=1,items.count,1 do
  43.     print(items.data[i].label)
  44.   end
  45. end
  46.  
  47. local function queueItem(hash)
  48.   table.insert(currentQueue, hash)  
  49. end
  50.  
  51. local function exportQueue()
  52.   print("starting craft...")
  53.   for i,hash in ipairs(currentQueue) do
  54.     local item = db.get(db.indexOf(hash))
  55.     local exists = controller.getItemsInNetwork({label = item.label}).n > 1
  56.     if (not exists) then
  57.       print("item '" .. item.label .. "' does does not exist in the network. Requesting autocraft...")
  58.       local requestedItem = controller.getCraftables({label = item.label})[1].request()
  59.       while (not requestedItem.isDone() and not requestedItem.isCanceled()) do
  60.         print('...waiting for autocrafting request to complete')
  61.         os.sleep(1)
  62.       end
  63.       print('request complete!')
  64.     end
  65.     print('exporting ' .. item.label .. '')
  66.     export_bus.setExportConfiguration(chest_side, 1, db.address, db.indexOf(hash))
  67.     export_bus.exportIntoSlot(chest_side, i)
  68.   end
  69.   print('export complete. ready to assemble')
  70. end
  71.  
  72. loadDatabase()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top