Filmos

Inventory Sorter

Dec 4th, 2021 (edited)
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.21 KB | None | 0 0
  1. -- Config
  2. local chests = {peripheral.wrap("front")}
  3. local steps = {15}
  4. local dropoff = peripheral.wrap("back")
  5.  
  6. local items = {{
  7.   {'minecraft:obsidian'},
  8.   {'byg:lignite', 'byg:lignite_block'},
  9.   {'minecraft:coal', 'minecraft:coal_block'},
  10.   {'byg:anthracite', 'byg:anthracite_block'},
  11.   {'minecraft:lapis_lazuli', 'minecraft:lapis_block'},
  12.   {'minecraft:redstone', 'minecraft:redstone_block'},
  13.   {'create:copper_nugget', 'create:copper_ingot', 'create:copper_block'},
  14.   {'create:zinc_nugget', 'create:zinc_ingot', 'create:zinc_block'},
  15.   {'minecraft:quartz', 'minecraft:quartz_block'},
  16.   {'minecraft:iron_nugget', 'minecraft:iron_ingot', 'minecraft:iron_block'},
  17.   {'minecraft:gold_nugget', 'minecraft:gold_ingot', 'minecraft:gold_block'},
  18.   {'minecraft:emerald', 'minecraft:emerald_block'},
  19.   {'minecraft:diamond', 'minecraft:diamond_block'},
  20.   {'tconstruct:cobalt_nugget', 'tconstruct:cobalt_ingot', 'tconstruct:cobalt_block'},
  21.   {'minecraft:ancient_debris'}
  22. }}
  23.  
  24. -- Preprocessing
  25. local mapTo = {}
  26. for _, chest in ipairs(chests) do
  27.   for col=1, steps[_] do
  28.     local last = nil
  29.     for i=1, math.floor(chest.size()/steps[_]), 2 do
  30.       if items[_] and items[_][col] and items[_][col][i] then
  31.          last = items[_][col][i]
  32.          mapTo[items[_][col][i]] = {_=_, c=col, i=i, s=2}
  33.          table.insert(items[_][col], i, items[_][col][i])
  34.       end
  35.     end
  36.    
  37.     if last then
  38.       start, e = string.find(last, ":")
  39.       last = string.sub(last, start+1)
  40.       last = 'overloaded_cb:compressed_'..last
  41.       for i=1, math.floor(chest.size()/steps[_])-#items[_][col] do
  42.          table.insert(items[_][col], last..'_'..i)
  43.          mapTo[last..'_'..i] = {_=_, c=col, i=#items[_][col], s=1}
  44.       end
  45.     end
  46.   end
  47. end
  48.  
  49.  
  50. while(true) do
  51.   -- Item extraction
  52.   for _, chest in ipairs(chests) do
  53.     for col=1, steps[_] do
  54.       for i=1,math.floor(chest.size()/steps[_]) do
  55.        
  56.         local slot = (i-1)*steps[_]+col
  57.         local item = chest.getItemDetail(slot)
  58.        
  59.         local pattern = "-"
  60.         if items[_] and items[_][col] and items[_][col][i] then
  61.            pattern = items[_][col][i]
  62.         end
  63.              
  64.         if item and item.name ~= pattern then
  65.            dropoff.pullItems(peripheral.getName(chest), slot)
  66.         end
  67.        
  68.       end
  69.     end
  70.   end
  71.  
  72.   -- Item insertion
  73.   for slot, item in pairs(dropoff.list()) do
  74.     if mapTo[item.name] then
  75.       local p = mapTo[item.name]
  76.       for s=1, p.s do
  77.          chests[p._].pullItems(peripheral.getName(dropoff), slot, 64, (p.i+s-2)*steps[p._]+p.c)
  78.       end
  79.          
  80.       -- Up-crafting
  81.       local leftover = dropoff.getItemDetail(slot)
  82.       if leftover and leftover.count >= 9 then
  83.          local portion = math.floor(leftover.count/9)
  84.          dropoff.pushItems("bottom", slot, portion*9)
  85.          turtle.select(1)
  86.          turtle.suckDown(portion*9)
  87.          local craftPos = {2, 3, 5, 6, 7, 9, 10, 11}
  88.          for j, cp in ipairs(craftPos) do
  89.             turtle.transferTo(cp, portion)
  90.          end
  91.          turtle.craft()
  92.          turtle.dropDown()
  93.          dropoff.pullItems("bottom", 1)
  94.       end
  95.       dropoff.pullItems(peripheral.getName(dropoff), slot)
  96.     end
  97.   end
  98. end
Advertisement
Add Comment
Please, Sign In to add comment