MineMcMine

ME Exporter

Apr 9th, 2022 (edited)
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.81 KB | None | 0 0
  1. --[[
  2.     Using: CC: Tweaked, Plethora
  3.     pastebin run uaq2nM4K
  4.     pastebin get uaq2nM4K export
  5. ]]--
  6.  
  7. if pcall(os.loadAPI, "jLib/jFuncs") then
  8. else
  9.     shell.run("pastebin get kqBFGcfV jLib/jFuncs")
  10.     os.loadAPI("jLib/jFuncs")
  11. end
  12. local recipeFileName = "jLib/MEExporterRecipes"
  13. if(fs.exists(recipeFileName)) then
  14.     fs.delete(recipeFileName )
  15. end
  16. shell.run("pastebin get NBmGUktC " .. recipeFileName)
  17. local file = fs.open(recipeFileName, "r")
  18. local recipes = textutils.unserialize(file.readAll())
  19.  
  20. local interface = peripheral.find("appliedenergistics2:interface")
  21.  
  22. local function waitForCPU()
  23.     while(true) do
  24.         for _, cpu in pairs(interface.getCraftingCPUs()) do
  25.             if(not cpu.busy) then
  26.                 return
  27.             end
  28.         end
  29.         os.sleep(1)
  30.     end
  31. end
  32.  
  33. local function stackUpME(allItems)
  34.     local allTasks = {}
  35.     for _, listItem in pairs(allItems) do
  36.         local aeItem = interface.findItem(listItem)
  37.    
  38.         if(aeItem == nil) then
  39.             print("Missing " .. listItem.name .. " in ME System")
  40.             return nil
  41.         else
  42.             local aeItemMeta = aeItem.getMetadata()
  43.    
  44.             if(aeItemMeta.count < listItem.count) then
  45.                 print("Crafting " .. listItem.count - aeItemMeta.count .. " " .. listItem.name .. "-" .. listItem.damage .. " in ME System")
  46.                 waitForCPU()
  47.                 local task = aeItem.craft(listItem.count - aeItemMeta.count)
  48.                 if(task.status() == "missing") then
  49.                     print("No materials!")
  50.                     return nil
  51.                 end
  52.                 table.insert(allTasks , task)
  53.             end
  54.         end
  55.     end
  56.     return allTasks
  57. end
  58.  
  59. local function exportFromME(allItems)
  60.     for _, listItem in pairs(allItems) do
  61.         local aeItem = interface.findItem(listItem)
  62.    
  63.         if(aeItem == nil) then
  64.             print("Missing " .. listItem.name .. " in ME System")
  65.         else
  66.             local aeItemMeta = aeItem.getMetadata()
  67.    
  68.             if(aeItemMeta.count < listItem.count) then
  69.                 print("Crafting not done, error. " .. listItem.count - aeItemMeta.count .. " " .. listItem.name .. "-" .. listItem.damage .. " in ME System")
  70.                 return
  71.             else
  72.                 --print(string.format("%s: Got %s, Need %s", listItem.name, aeItemMeta.count, listItem.count))
  73.                 local toExport = listItem.count
  74.                 while (toExport > 0) do
  75.                     toExport = toExport - aeItem.export("left", 64)
  76.                 end
  77.             end
  78.         end
  79.     end
  80. end
  81.  
  82. local args = {...}
  83. if(args[1] == nil) then
  84.     print("Need a recipe name as argument.")
  85.     return
  86. end
  87.  
  88. if(args[2] == nil) then
  89.     args[2] = args[1]
  90.     args[1] = 1
  91. end
  92.  
  93. local allItems = recipes[args[2]]
  94. if(allItems == nil) then
  95.     print("Could not find recipe " .. args[2])
  96.     return
  97. end
  98.  
  99. for _, item in pairs(allItems) do
  100.     item.count = item.count * args[1]
  101. end
  102.  
  103. local tasks = stackUpME(allItems)
  104. if(tasks) then
  105.     local done = false
  106.     while(not done) do
  107.         os.sleep(1)
  108.         done = true
  109.         for _, task in pairs(tasks) do
  110.             if(not task.isFinished()) then
  111.                 done = false
  112.             end
  113.         end
  114.     end
  115.     exportFromME(allItems)
  116. end
  117.  
  118.  
  119.  
Add Comment
Please, Sign In to add comment