Advertisement
DaikiKaminari

exportApplied

Apr 11th, 2020
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.09 KB | None | 0 0
  1. --- INIT ---
  2.  
  3. --- CONFIG ---
  4. local wantedChestSide = "top"
  5. local wantedChestSlot = 2
  6. local interfaceSide = "left"
  7. local direction = "up" -- direction from interface to output container
  8.  
  9. --- GLOBAL VARIABLES ---
  10. local wantedChest = peripheral.wrap(wantedChestSide)
  11. if wantedChest == nil then
  12.     error("Wanted chest not found on side : " .. wantedChestSide)
  13. end
  14.  
  15. local interface = peripheral.wrap(interfaceSide)
  16. if interface == nil then
  17.     error("interface not found on side : " .. interfaceSide)
  18. end
  19.  
  20. --- UTILS ---
  21. local function displayPercentage(currentPercentage, total, itemSent)
  22.     local p = math.floor((itemSent/total) * 100, 1)
  23.     p = p - p % 10
  24.     if currentPercentage < p then
  25.         print(tostring(p) .. " %")
  26.         return p
  27.     else
  28.         return currentPercentage
  29.     end
  30. end
  31.  
  32. local function equals(tab1, tab2)
  33.     local equal = true
  34.     for k,v in pairs(tab1) do
  35.         if tab2[k] ~= nil then
  36.             if tab1[k] ~= tab2[k] then
  37.                 return false
  38.             end
  39.         end
  40.     end
  41.     return equal
  42. end
  43.  
  44. --- FUNCTIONS ---
  45. -- check output container pressence
  46. local function checkOutputContainer()
  47.     if not interface.canExport(direction) then
  48.         print("\nIl manque le container d'output !")
  49.         print("Ajoutez le sur la face [" .. direction .. "] de l'interface...")
  50.     end
  51.     while not interface.canExport(direction) do
  52.         sleep(1)
  53.     end
  54. end
  55.  
  56. -- returns true if the item is craftable
  57. local function isCraftable(item)
  58.     local allItemsStored = interface.getAvailableItems("ALL")
  59.     for i=1,#allItemsStored do
  60.         if allItemsStored[i].fingerprint.id == item.id then
  61.             if equals(item, allItemsStored[i].fingerprint) then
  62.                 return allItemsStored[i].is_craftable
  63.             end
  64.         end
  65.     end
  66.     return false
  67. end
  68.  
  69. local function processItem(item)
  70.     local details = interface.getItemDetail(item) -- details about the item in the AE
  71.     print("\nEntrez le nombre d'items voulus :")
  72.  
  73.     if details == nil then
  74.         print("\nAucun item en stock et autocraft de " .. " [" .. item.display_name .. "] dans l'AE.")
  75.         print("\nArret !")
  76.         return
  77.     end
  78.  
  79.     local n = tonumber(io.read())           -- quantity of items to move
  80.     local nbItemsInAE = details.all().qty   -- quantity of items in AE
  81.     local toCraft = n - nbItemsInAE         -- quantity of items to craft
  82.  
  83.     -- autocraft if needed
  84.     if toCraft > 0 then
  85.         print("\nNecessite le craft de " .. tostring(toCraft) .. " [" .. item.display_name .. "].")
  86.         if isCraftable(item) then
  87.             print("Autocraft de " .. tostring(toCraft) .. " [" .. item.display_name .. "] en cours...")
  88.             interface.requestCrafting(item, toCraft)
  89.         else
  90.             print("Aucun autocraft autocraft de [" .. item.display_name .. "].")
  91.             print("\nArret !")
  92.             return
  93.         end
  94.     end
  95.  
  96.     -- sending items
  97.     print("\nEnvoie des items :")
  98.     local itemSent = 0
  99.     local currentPercentage = 0
  100.     local maxSize = item.max_size
  101.     while itemSent < n and wantedChest.getStackInSlot(wantedChestSlot) ~= nil do
  102.         checkOutputContainer()
  103.         local exportResult = interface.exportItem(item, direction, math.min(maxSize, (n - itemSent)))
  104.         if exportResult == nil then
  105.             nbExportedItems = 0
  106.         else
  107.             nbExportedItems = exportResult.size
  108.         end
  109.         itemSent = itemSent + nbExportedItems
  110.         currentPercentage = displayPercentage(currentPercentage, n, itemSent)
  111.         sleep(0)
  112.     end
  113.     print("\nFini !")
  114. end
  115.  
  116. --- MAIN CALL ---
  117. local function main()
  118.     local item
  119.     local nbExportedItems
  120.     while true do
  121.         term.clear()
  122.         item = nil
  123.  
  124.         print("Inserez un item dans la cache...")
  125.         while item == nil do
  126.             sleep(2)
  127.             item = wantedChest.getStackInSlot(wantedChestSlot)
  128.         end
  129.  
  130.         processItem(item)
  131.  
  132.         -- finishing
  133.         print("\nRetirez l'item de la cache...")
  134.         while wantedChest.getStackInSlot(wantedChestSlot) ~= nil do
  135.             sleep(1)
  136.         end
  137.  
  138.         sleep(0)
  139.     end
  140. end
  141.  
  142. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement