Advertisement
HandieAndy

item-extractor.lua

Jul 17th, 2023 (edited)
817
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.68 KB | Gaming | 0 0
  1. local me = peripheral.wrap("back")
  2. local EXPORT_SIDE = "south"
  3.  
  4. local args = {...}
  5.  
  6. local function starts_with(str, start)
  7.     return str:sub(1, #start) == start
  8. end
  9.  
  10. local function ends_with(str, ending)
  11.     return ending == "" or str:sub(-#ending) == ending
  12. end
  13.  
  14. local function waitForEnter()
  15.     print("Press [Enter] to continue.")
  16.     io.read()
  17. end
  18.  
  19. local function isAEOnline()
  20.     local usage, err = me.getEnergyUsage()
  21.     return usage ~= nil and usage > 0
  22. end
  23.  
  24. local function fetchItemLists(url)
  25.     local request = http.get(url)
  26.     if request == nil then
  27.         print("HTTP request failed. Please contact the administrator.")
  28.         return nil
  29.     end
  30.     if request.getResponseCode() ~= 200 then
  31.         print("HTTP error code " .. request.getResponseCode() .. " Your link is likely incorrect.")
  32.         return nil
  33.     end
  34.     local itemListText = request.readAll()
  35.     request.close()
  36.     local itemLists, msg = textutils.unserializeJSON(itemListText)
  37.     if not itemLists then
  38.         print("Failed to parse item-list JSON: " .. msg)
  39.         return nil
  40.     end
  41.     return itemLists
  42. end
  43.  
  44. local function findItem(items, name)
  45.     for _, item in pairs(items) do
  46.         if item.name == name then
  47.             return item
  48.         end
  49.     end
  50.     return nil
  51. end
  52.  
  53. local function attemptItemExport(name, count, side)
  54.     local func = function()
  55.         return me.exportItem(
  56.             {
  57.                 name=name,
  58.                 count=count
  59.             },
  60.             side
  61.         )
  62.     end
  63.     local success, result = pcall(func)
  64.     if success then
  65.         return result
  66.     else
  67.         return nil, result
  68.     end
  69. end
  70.  
  71. local function exportItem(name, count)
  72.     print("Exporting " .. count .. "x " .. name)
  73.     local totalTransferred = 0
  74.     while totalTransferred < count do
  75.         local amountToTransfer = count - totalTransferred
  76.         local items = me.listItems()
  77.         local item = findItem(items, name)
  78.         if item ~= nil and item.amount > 0 then
  79.             local count, err = attemptItemExport(name, amountToTransfer, EXPORT_SIDE)
  80.             if count ~= nil then
  81.                 print("  Transferred " .. count .. " items.")
  82.                 totalTransferred = totalTransferred + count
  83.                 if count == 0 then
  84.                     print("  Couldn't transfer any items. Please ensure there's space available for exporting.")
  85.                     waitForEnter()
  86.                 end
  87.             else
  88.                 print(" Transfer failed: " .. err)
  89.                 waitForEnter()
  90.             end
  91.         -- elseif craftableItem ~= nil and not startedCraftingRecently then
  92.         --     print("  No more items in the system; would you like to attempt to auto-craft " .. amountToTransfer .. " more?[yes/no]")
  93.         --     local response = io.read()
  94.         --     if response == "yes" then
  95.         --         me.craftItem({name=name, count=amountToTransfer})
  96.         --         startedCraftingRecently = true
  97.         --         while me.isItemCrafting({name=name}) do
  98.         --             print("  Crafting in progress...")
  99.         --             os.sleep(0.5)
  100.         --         end
  101.         --     end
  102.         else
  103.             print("  Item doesn't exist in the system. Please add " .. amountToTransfer .. " of this item to the system, or type\"skip\" to skip this item.")
  104.             local response = io.read()
  105.             if response == "skip" then
  106.                 -- "skip" by pretending the items were exported.
  107.                 totalTransferred = totalTransferred + amountToTransfer
  108.             end
  109.         end
  110.     end
  111. end
  112.  
  113. local function exportSchematic(schematic)
  114.     print("Exporting schematic\n \"" .. schematic.__NAME__ .. "\"")
  115.     for i = 1, schematic.__COUNT__ do
  116.         print("Iteration " .. i .. "/" .. schematic.__COUNT__ .. ":")
  117.         for name, count in pairs(schematic) do
  118.             if not (starts_with(name, "__") and ends_with(name, "__")) then
  119.                 exportItem(name, count)
  120.             end
  121.         end
  122.     end
  123. end
  124.  
  125. local function exportItemList(itemList)
  126.     print("Exporting items for schematic:\n\t" .. itemList.__NAME__)
  127.     for name, count in pairs(itemList) do
  128.         exportItem(name, count)
  129.     end
  130.     print("Done!")
  131. end
  132.  
  133. -- MAIN SCRIPT
  134. if not isAEOnline() then
  135.     print("Could not connect to the AE system.")
  136.     return
  137. end
  138. -- Check URL
  139. if #args < 1 or not http.checkURL(args[1]) then
  140.     print("Missing or invalid item-list URL.")
  141.     return
  142. end
  143.  
  144. local itemLists = fetchItemLists(args[1])
  145. if itemLists == nil then
  146.     print("Failed to retrieve item-list. Exiting.")
  147.     return
  148. end
  149.  
  150. for idx, itemList in pairs(itemLists) do
  151.     exportSchematic(itemList)
  152. end
  153.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement