Advertisement
fergenbergel

itemProcurement2.0

Aug 23rd, 2021
467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.66 KB | None | 0 0
  1.  
  2. --Opens the file for writing out errors.
  3. outputfile = "manualAttentionNeeded"
  4. local file = fs.open( outputfile, "w" )
  5.  
  6. local colony = peripheral.find("colonyIntegrator") -- Finds the peripheral if one is connected
  7. if colony == nil then error("colonyIntegrator not found") end
  8. if not colony.isInColony then error("Block is not in a colony") end
  9.  
  10. local refinedstorage = peripheral.find("rsBridge") -- Finds the peripheral if one is connected
  11. if refinedstorage == nil then error("rsBridge not found") end
  12.  
  13. --Coordinates of builder hut blocks
  14. first_builder_coords = {y=69, x=227, z=-2291}   --Jade
  15. second_builder_coords = {y=64, x=220, z=-2315}  --Ayleen
  16. third_builder_coords = {y=64, x=346, z=-2305}   --Billy
  17.  
  18. first_builder_items_needed
  19. second_builder_items_needed
  20. third_builder_items_needed
  21.  
  22. function getBuilderWorkOrders ()
  23.     work_orders = colony.getWorkOrders()
  24.     file.write(textutils.serialize(work_orders))
  25.     for order in pairs(work_orders) do
  26.         if work_orders[order]["isClaimed"] == true then
  27.             print("Claimed " .. work_orders[order]["id"])
  28.             if work_orders[order]["builder"] == first_builder_coords then
  29.                 first_builder_items_needed = colony.getWorkOrderResources(work_orders[order][id])
  30.             elseif work_orders[order]["builder"] == second_builder_coords then
  31.                 second_builder_items_needed = colony.getWorkOrderResources(work_orders[order][id])
  32.             elseif work_orders[order]["builder"] == third_builder_coords then
  33.                 third_builder_items_needed = colony.getWorkOrderResources(work_orders[order][id])
  34.             else
  35.                 print("Coordinates do not match for id " .. work_orders[order]["id"])
  36.             end
  37.         else print("Not Claimed" .. work_orders[order]["id"])
  38.     end
  39. end
  40.  
  41. getBuilderWorkOrders()
  42.  
  43. items_needed = {items_needed_jade, items_needed_ayleen, items_needed_billy}
  44.  
  45. print("\nCreating missing items...")
  46. --Loop through each builder
  47. for i in pairs(items_needed) do
  48.     --Loop through each item needed.
  49.     for index in pairs(items_needed[i]) do
  50.         local item_needed_name = items_needed[i][index]["item"]["name"]
  51.         local item_needed_count = items_needed[i][index]["item"]["count"]
  52.         local item_needed_status = items_needed[i][index]["status"]
  53.         local item_storage_count = refinedstorage.getItem({name = item_needed_name})["count"]
  54.        
  55.         --Make sure the item is needed by the builder
  56.         if item_needed_status ~= "NOT_NEEDED" then
  57.             if item_storage_count == nil then
  58.                 item_storage_count = 0
  59.             end
  60.            
  61.             --If the item has a crafting recipe and there is not enough already in storage, craft the remainder needed.
  62.             if refinedstorage.hasPattern({name=item_needed_name}) and item_needed_count - item_storage_count > 0 then
  63.                 --Write out what item and how many are in storage out of the total needed.
  64.                 file.write("\n")
  65.                 file.write(textutils.serialize(item_needed_name))
  66.                 file.write(" " .. textutils.serialize(item_storage_count) .. "/" .. textutils.serialize(item_needed_count))
  67.                 file.write("\nPreparing to create " .. item_needed_count - item_storage_count)
  68.                 refinedstorage.craftItem({name=item_needed_name}, item_needed_count - item_storage_count)
  69.             end
  70.            
  71.             if not refinedstorage.hasPattern({name=item_needed_name}) and item_needed_count - item_storage_count > 0 then --Pattern does not exist, and needs to be created manually.
  72.                 file.write("\nPattern Needed for " .. item_needed_name)
  73.             end
  74.         end
  75.     end
  76. end
  77.  
  78. print("Waiting 30s for items to finish crafting...")
  79. sleep(0)
  80. print("Outputing Items...")
  81.  
  82. i = 0
  83. index = 0
  84.  
  85. for i in pairs(items_needed) do
  86.     --Loop through each item needed.
  87.     for index in pairs(items_needed[i]) do
  88.         local item_needed_name = items_needed[i][index]["item"]["name"]
  89.         local item_needed_count = items_needed[i][index]["item"]["count"]
  90.         local item_needed_status = items_needed[i][index]["status"]
  91.         local output_side = nil
  92.        
  93.         --Calculate Output Side based on building.
  94.         if i == 1 then
  95.             output_side = "up"
  96.         elseif i == 2 then
  97.             output_side = "north"
  98.         elseif i == 3 then
  99.             output_side = "west"
  100.         else
  101.             output_side = "north"
  102.         end
  103.        
  104.         --Make sure the item is needed by the builder
  105.         if item_needed_status ~= "NOT_NEEDED" then
  106.            
  107.             --Extract as many of the item as we need, up to the amount available.
  108.             refinedstorage.extractItem({name=item_needed_name}, item_needed_count, output_side)
  109.         end
  110.     end
  111. end
  112.  
  113. print("Please view " .. outputfile .. " for information on missing items.")
  114.  
  115. file.close()
  116.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement