Advertisement
k2green

CCTweaked AE2 certus crafting

Jul 30th, 2024
161
1
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.82 KB | Software | 1 0
  1. local args = {...}
  2.  
  3. local function addOrInsert(tab, ident, value)
  4.     if tab[ident] == nil then
  5.         tab[ident] = { value }
  6.     else
  7.         table.insert(tab[ident], value)
  8.     end
  9. end
  10.  
  11. local function findRecipes(side)
  12.     local chest = peripheral.wrap(side)
  13.     local data = {
  14.         ["totals"] = {
  15.             ["ccq"] = 0,
  16.             ["block"] = 0,
  17.             ["damaged"] = 0,
  18.             ["chipped"] = 0
  19.         }
  20.     }
  21.    
  22.     for i=1, chest.size() do
  23.         local details = chest.getItemDetail(i)
  24.        
  25.         if details ~= nil then
  26.             if details.name == "ae2:charged_certus_quartz_crystal" then
  27.                 addOrInsert(data, "ccq", {["slot"] = i, ["count"] = details.count})
  28.             elseif details.name == "ae2:quartz_block" then
  29.                 addOrInsert(data, "block", {["slot"] = i, ["count"] = details.count})
  30.             elseif details.name == "ae2:damaged_budding_quartz" then
  31.                 addOrInsert(data, "damaged", {["slot"] = i, ["count"] = details.count})
  32.             elseif details.name == "ae2:chipped_budding_quartz" then
  33.                 addOrInsert(data, "chipped", {["slot"] = i, ["count"] = details.count})
  34.             end
  35.         end
  36.     end
  37.    
  38.     if data.ccq ~= nil and #data.ccq > 0 then
  39.         for k,v in ipairs(data.ccq) do
  40.             data.totals.ccq = data.totals.ccq + v.count
  41.         end
  42.     end
  43.    
  44.     if data.block ~= nil and #data.block > 0 then
  45.         for k,v in ipairs(data.block) do
  46.             data.totals.block = data.totals.block + v.count
  47.         end
  48.     end
  49.    
  50.     if data.damaged ~= nil and #data.damaged > 0 then
  51.         for k,v in ipairs(data.damaged) do
  52.             data.totals.damaged = data.totals.damaged + v.count
  53.         end
  54.     end
  55.    
  56.     if data.chipped ~= nil and #data.chipped > 0 then
  57.         for k,v in ipairs(data.chipped) do
  58.             data.totals.chipped = data.totals.chipped + v.count
  59.         end
  60.     end
  61.    
  62.     return data
  63. end
  64.  
  65. local function suckFromSlot(fromSide, toSide, slot, count)
  66.     local chest = peripheral.wrap(fromSide)
  67.     chest.pushItems(toSide, slot, count, 1)
  68.    
  69.     if toSide == "top" then
  70.         turtle.suckUp(count)
  71.     elseif toSide == "bottom" then
  72.         turtle.suckDown(count)
  73.     else
  74.         turtle.suck(count)
  75.     end
  76. end
  77.  
  78. local function grabResources(data, level, fromSide, toSide)
  79.     local amount = math.min(data.totals.ccq, data.totals[level], 64)
  80.    
  81.     turtle.select(1)
  82.     local currentCcqAmount = amount
  83.     for k,v in ipairs(data.ccq) do
  84.         if currentCcqAmount <= 0 then
  85.             break
  86.         end
  87.        
  88.         local suckAmount = math.min(v.count, currentCcqAmount)
  89.         suckFromSlot(fromSide, toSide, v.slot, suckAmount)
  90.         currentCcqAmount = currentCcqAmount - suckAmount
  91.     end
  92.    
  93.     turtle.select(2)
  94.     local currentAmount = amount
  95.     for k,v in ipairs(data[level]) do
  96.         if currentAmount <= 0 then
  97.             break
  98.         end
  99.        
  100.         local suckAmount = math.min(v.count, currentAmount)
  101.         suckFromSlot(fromSide, toSide, v.slot, suckAmount)
  102.         currentAmount = currentAmount - suckAmount
  103.     end
  104.    
  105.     return amount
  106. end
  107.  
  108. local function turn(direction, inverted)
  109.     if direction == "left" then
  110.         if inverted then
  111.             turtle.turnRight()
  112.         else
  113.             turtle.turnLeft()
  114.         end
  115.     elseif direction == "right" then
  116.         if inverted then
  117.             turtle.turnLeft()
  118.         else
  119.             turtle.turnRight()
  120.         end
  121.     end
  122. end
  123.  
  124. local function craft(amount, direction)
  125.     if amount > 0 then
  126.         turn(direction, false)
  127.        
  128.         for i=1, amount do
  129.             turtle.select(1)
  130.             turtle.drop(1)
  131.             turtle.select(2)
  132.             turtle.drop(1)
  133.             turtle.select(3)
  134.            
  135.             sleep(4)
  136.            
  137.             turtle.suck(1)
  138.         end
  139.        
  140.         turn(direction, true)
  141.         turtle.select(3)
  142.         turtle.drop(amount)
  143.     end
  144. end
  145.  
  146. local function run(fromSide, toSide, direction)
  147.     local data = findRecipes("front")
  148.     local amount = 0
  149.    
  150.     if data.totals.ccq > 0 then
  151.         if data.totals.chipped > 0 then
  152.             amount = grabResources(data, "chipped", fromSide, toSide)
  153.         elseif data.totals.damaged > 0 then
  154.             amount = grabResources(data, "damaged", fromSide, toSide)
  155.         elseif data.totals.block > 0 then
  156.             amount = grabResources(data, "block", fromSide, toSide)
  157.         end
  158.        
  159.         craft(amount, direction)
  160.     end
  161. end
  162.  
  163. while true do
  164.     run(args[1], args[2], args[3])
  165.     sleep(5)
  166. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement