BombBloke

Crafter (OpenPeripherals)

Feb 8th, 2019 (edited)
537
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 20.12 KB | None | 0 0
  1. -- Makes stuff out of whatever's in a chest above a turtle, according to a list of recipes in the "make" table.
  2. -- Pulls new materials out of a container in front of the turtle and adds them to the chest as it goes.
  3.  
  4. -- Doesn't yet account too well for item types that don't stack up to 64 ('cause I'm lazy).
  5.  
  6. local sleepSeconds = 60  -- Lower this if you have a super-high ingredient flow.
  7.  
  8. local chest = peripheral.wrap("top")
  9.  
  10. local slots, items = chest.getInventorySize()
  11.  
  12. local make = {
  13.     {
  14.         ["name"] = "Glowstone",
  15.         ["id"] = "minecraft:glowstone:0",
  16.         ["recipe"] = {
  17.             "minecraft:glowstone_dust:0", "minecraft:glowstone_dust:0", nil,
  18.             "minecraft:glowstone_dust:0", "minecraft:glowstone_dust:0"
  19.         }
  20.     },
  21.    
  22.     {
  23.         ["name"] = "Clay",
  24.         ["id"] = "minecraft:clay:0",
  25.         ["recipe"] = {
  26.             "minecraft:clay_ball:0", "minecraft:clay_ball:0", nil,
  27.             "minecraft:clay_ball:0", "minecraft:clay_ball:0"
  28.         }
  29.     },
  30.    
  31.     {
  32.         ["name"] = "Block of Quartz",
  33.         ["id"] = "minecraft:quartz_block:0",
  34.         ["recipe"] = {
  35.             "minecraft:quartz:0", "minecraft:quartz:0", nil,
  36.             "minecraft:quartz:0", "minecraft:quartz:0"
  37.         }
  38.     },
  39.  
  40.     {
  41.         ["name"] = "Yellorium Dust",
  42.         ["id"] = "BigReactors:BRIngot:4",
  43.         ["recipe"] = {
  44.             "ExtraBees:misc:27", "ExtraBees:misc:27", nil,
  45.             "ExtraBees:misc:27", "ExtraBees:misc:27"
  46.         }
  47.     },
  48.  
  49.     {
  50.         ["name"] = "Ferrous Ingot",
  51.         ["id"] = "ThermalFoundation:material:68",
  52.         ["recipe"] = {
  53.             "ThermalFoundation:material:100", "ThermalFoundation:material:100", "ThermalFoundation:material:100",
  54.             "ThermalFoundation:material:100", "ThermalFoundation:material:100", "ThermalFoundation:material:100",
  55.             "ThermalFoundation:material:100", "ThermalFoundation:material:100", "ThermalFoundation:material:100"
  56.         }
  57.     },
  58.    
  59.     {
  60.         ["name"] = "Ferrous Block",
  61.         ["id"] = "ThermalFoundation:Storage:4",
  62.         ["recipe"] = {
  63.             "ThermalFoundation:material:68", "ThermalFoundation:material:68", "ThermalFoundation:material:68",
  64.             "ThermalFoundation:material:68", "ThermalFoundation:material:68", "ThermalFoundation:material:68",
  65.             "ThermalFoundation:material:68", "ThermalFoundation:material:68", "ThermalFoundation:material:68"
  66.         }
  67.     },
  68.    
  69.     {
  70.         ["name"] = "Shiny Ingot",
  71.         ["id"] = "ThermalFoundation:material:69",
  72.         ["recipe"] = {
  73.             "ThermalFoundation:material:101", "ThermalFoundation:material:101", "ThermalFoundation:material:101",
  74.             "ThermalFoundation:material:101", "ThermalFoundation:material:101", "ThermalFoundation:material:101",
  75.             "ThermalFoundation:material:101", "ThermalFoundation:material:101", "ThermalFoundation:material:101"
  76.         }
  77.     },
  78.    
  79.     {
  80.         ["name"] = "Shiny Block",
  81.         ["id"] = "ThermalFoundation:Storage:5",
  82.         ["recipe"] = {
  83.             "ThermalFoundation:material:69", "ThermalFoundation:material:69", "ThermalFoundation:material:69",
  84.             "ThermalFoundation:material:69", "ThermalFoundation:material:69", "ThermalFoundation:material:69",
  85.             "ThermalFoundation:material:69", "ThermalFoundation:material:69", "ThermalFoundation:material:69"
  86.         }
  87.     },
  88.    
  89.     {
  90.         ["name"] = "Block of Redstone",
  91.         ["id"] = "minecraft:redstone_block:0",
  92.         ["recipe"] = {
  93.             "minecraft:redstone:0", "minecraft:redstone:0", "minecraft:redstone:0",
  94.             "minecraft:redstone:0", "minecraft:redstone:0", "minecraft:redstone:0",
  95.             "minecraft:redstone:0", "minecraft:redstone:0", "minecraft:redstone:0"
  96.         }
  97.     },
  98.  
  99.     {
  100.         ["name"] = "Seeds",
  101.         ["id"] = "minecraft:wheat_seeds:0",
  102.         ["output"] = 2,
  103.         ["recipe"] = {
  104.             "minecraft:wheat:0", "minecraft:wheat:0"
  105.         }
  106.     },
  107.  
  108.     {
  109.         ["name"] = "Gold Ingot",
  110.         ["id"] = "minecraft:gold_ingot:0",
  111.         ["recipe"] = {
  112.             "minecraft:gold_nugget:0", "minecraft:gold_nugget:0", "minecraft:gold_nugget:0",
  113.             "minecraft:gold_nugget:0", "minecraft:gold_nugget:0", "minecraft:gold_nugget:0",
  114.             "minecraft:gold_nugget:0", "minecraft:gold_nugget:0", "minecraft:gold_nugget:0"
  115.         }
  116.     },
  117.  
  118.     {
  119.         ["name"] = "Block of Gold",
  120.         ["id"] = "minecraft:gold_block:0",
  121.         ["recipe"] = {
  122.             "minecraft:gold_ingot:0", "minecraft:gold_ingot:0", "minecraft:gold_ingot:0",
  123.             "minecraft:gold_ingot:0", "minecraft:gold_ingot:0", "minecraft:gold_ingot:0",
  124.             "minecraft:gold_ingot:0", "minecraft:gold_ingot:0", "minecraft:gold_ingot:0"
  125.         }
  126.     },
  127.  
  128.     {
  129.         ["name"] = "Diamond",
  130.         ["id"] = "minecraft:diamond:0",
  131.         ["recipe"] = {
  132.             "MagicBees:beeNugget:5", "MagicBees:beeNugget:5", "MagicBees:beeNugget:5",
  133.             "MagicBees:beeNugget:5", "MagicBees:beeNugget:5", "MagicBees:beeNugget:5",
  134.             "MagicBees:beeNugget:5", "MagicBees:beeNugget:5", "MagicBees:beeNugget:5"
  135.         }
  136.     },
  137.  
  138.     {
  139.         ["name"] = "Block of Diamond",
  140.         ["id"] = "minecraft:diamond_block:0",
  141.         ["recipe"] = {
  142.             "minecraft:diamond:0", "minecraft:diamond:0", "minecraft:diamond:0",
  143.             "minecraft:diamond:0", "minecraft:diamond:0", "minecraft:diamond:0",
  144.             "minecraft:diamond:0", "minecraft:diamond:0", "minecraft:diamond:0"
  145.         }
  146.     },
  147.  
  148.     {
  149.         ["name"] = "Lapis Lazuli Block",
  150.         ["id"] = "minecraft:lapis_block:0",
  151.         ["recipe"] = {
  152.             "minecraft:dye:4", "minecraft:dye:4", "minecraft:dye:4",
  153.             "minecraft:dye:4", "minecraft:dye:4", "minecraft:dye:4",
  154.             "minecraft:dye:4", "minecraft:dye:4", "minecraft:dye:4"
  155.         }
  156.     },
  157.  
  158.     {
  159.         ["name"] = "Block of Coal",
  160.         ["id"] = "minecraft:coal_block:0",
  161.         ["recipe"] = {
  162.             "minecraft:coal:0", "minecraft:coal:0", "minecraft:coal:0",
  163.             "minecraft:coal:0", "minecraft:coal:0", "minecraft:coal:0",
  164.             "minecraft:coal:0", "minecraft:coal:0", "minecraft:coal:0"
  165.         }
  166.     },
  167.  
  168.     {
  169.         ["name"] = "Alveary",
  170.         ["id"] = "Forestry:alveary:0",
  171.         ["recipe"] = {
  172.             "Forestry:craftingMaterial:6", "Forestry:craftingMaterial:6", "Forestry:craftingMaterial:6",
  173.             "Forestry:craftingMaterial:6", "Forestry:impregnatedCasing:0", "Forestry:craftingMaterial:6",
  174.             "Forestry:craftingMaterial:6", "Forestry:craftingMaterial:6", "Forestry:craftingMaterial:6"
  175.         }
  176.     },
  177.  
  178.     {
  179.         ["name"] = "Oak Wood Planks",
  180.         ["id"] = "minecraft:planks:0",
  181.         ["output"] = 4,
  182.         ["recipe"] = {
  183.             "minecraft:log:0"
  184.         }
  185.     },
  186.  
  187.     {
  188.         ["name"] = "Dark Oak Wood Planks",
  189.         ["id"] = "minecraft:planks:5",
  190.         ["output"] = 4,
  191.         ["recipe"] = {
  192.             "minecraft:log2:1"
  193.         }
  194.     },
  195.  
  196.     {
  197.         ["name"] = "Crafting Table",
  198.         ["id"] = "minecraft:crafting_table:0",
  199.         ["recipe"] = {
  200.             "minecraft:planks:0", "minecraft:planks:0", nil,
  201.             "minecraft:planks:0", "minecraft:planks:0"
  202.         }
  203.     },
  204.  
  205.     {
  206.         ["name"] = "Crafting Table",
  207.         ["id"] = "minecraft:crafting_table:0",
  208.         ["recipe"] = {
  209.             "minecraft:planks:5", "minecraft:planks:5", nil,
  210.             "minecraft:planks:5", "minecraft:planks:5"
  211.         }
  212.     },
  213.    
  214.     {
  215.         ["name"] = "Compressed Cobblestone",
  216.         ["id"] = "ExtraUtilities:cobblestone_compressed:0",
  217.         ["recipe"] = {
  218.             "minecraft:cobblestone:0", "minecraft:cobblestone:0", "minecraft:cobblestone:0",
  219.             "minecraft:cobblestone:0", "minecraft:cobblestone:0", "minecraft:cobblestone:0",
  220.             "minecraft:cobblestone:0", "minecraft:cobblestone:0", "minecraft:cobblestone:0"
  221.         }
  222.     },
  223.    
  224.     {
  225.         ["name"] = "Double Compressed Cobblestone",
  226.         ["id"] = "ExtraUtilities:cobblestone_compressed:1",
  227.         ["recipe"] = {
  228.             "ExtraUtilities:cobblestone_compressed:0", "ExtraUtilities:cobblestone_compressed:0", "ExtraUtilities:cobblestone_compressed:0",
  229.             "ExtraUtilities:cobblestone_compressed:0", "ExtraUtilities:cobblestone_compressed:0", "ExtraUtilities:cobblestone_compressed:0",
  230.             "ExtraUtilities:cobblestone_compressed:0", "ExtraUtilities:cobblestone_compressed:0", "ExtraUtilities:cobblestone_compressed:0"
  231.         }
  232.     },
  233.    
  234.     {
  235.         ["name"] = "Triple Compressed Cobblestone",
  236.         ["id"] = "ExtraUtilities:cobblestone_compressed:2",
  237.         ["recipe"] = {
  238.             "ExtraUtilities:cobblestone_compressed:1", "ExtraUtilities:cobblestone_compressed:1", "ExtraUtilities:cobblestone_compressed:1",
  239.             "ExtraUtilities:cobblestone_compressed:1", "ExtraUtilities:cobblestone_compressed:1", "ExtraUtilities:cobblestone_compressed:1",
  240.             "ExtraUtilities:cobblestone_compressed:1", "ExtraUtilities:cobblestone_compressed:1", "ExtraUtilities:cobblestone_compressed:1"
  241.         }
  242.     },
  243.    
  244.     {
  245.         ["name"] = "Quadruple Compressed Cobblestone",
  246.         ["id"] = "ExtraUtilities:cobblestone_compressed:3",
  247.         ["recipe"] = {
  248.             "ExtraUtilities:cobblestone_compressed:2", "ExtraUtilities:cobblestone_compressed:2", "ExtraUtilities:cobblestone_compressed:2",
  249.             "ExtraUtilities:cobblestone_compressed:2", "ExtraUtilities:cobblestone_compressed:2", "ExtraUtilities:cobblestone_compressed:2",
  250.             "ExtraUtilities:cobblestone_compressed:2", "ExtraUtilities:cobblestone_compressed:2", "ExtraUtilities:cobblestone_compressed:2"
  251.         }
  252.     },
  253.    
  254.     {
  255.         ["name"] = "Quintuple Compressed Cobblestone",
  256.         ["id"] = "ExtraUtilities:cobblestone_compressed:4",
  257.         ["recipe"] = {
  258.             "ExtraUtilities:cobblestone_compressed:3", "ExtraUtilities:cobblestone_compressed:3", "ExtraUtilities:cobblestone_compressed:3",
  259.             "ExtraUtilities:cobblestone_compressed:3", "ExtraUtilities:cobblestone_compressed:3", "ExtraUtilities:cobblestone_compressed:3",
  260.             "ExtraUtilities:cobblestone_compressed:3", "ExtraUtilities:cobblestone_compressed:3", "ExtraUtilities:cobblestone_compressed:3"
  261.         }
  262.     },
  263.    
  264.     {
  265.         ["name"] = "Sextuple Compressed Cobblestone",
  266.         ["id"] = "ExtraUtilities:cobblestone_compressed:5",
  267.         ["recipe"] = {
  268.             "ExtraUtilities:cobblestone_compressed:4", "ExtraUtilities:cobblestone_compressed:4", "ExtraUtilities:cobblestone_compressed:4",
  269.             "ExtraUtilities:cobblestone_compressed:4", "ExtraUtilities:cobblestone_compressed:4", "ExtraUtilities:cobblestone_compressed:4",
  270.             "ExtraUtilities:cobblestone_compressed:4", "ExtraUtilities:cobblestone_compressed:4", "ExtraUtilities:cobblestone_compressed:4"
  271.         }
  272.     },
  273.    
  274.     {
  275.         ["name"] = "Septuple Compressed Cobblestone",
  276.         ["id"] = "ExtraUtilities:cobblestone_compressed:6",
  277.         ["recipe"] = {
  278.             "ExtraUtilities:cobblestone_compressed:5", "ExtraUtilities:cobblestone_compressed:5", "ExtraUtilities:cobblestone_compressed:5",
  279.             "ExtraUtilities:cobblestone_compressed:5", "ExtraUtilities:cobblestone_compressed:5", "ExtraUtilities:cobblestone_compressed:5",
  280.             "ExtraUtilities:cobblestone_compressed:5", "ExtraUtilities:cobblestone_compressed:5", "ExtraUtilities:cobblestone_compressed:5"
  281.         }
  282.     },
  283.    
  284.     {
  285.         ["name"] = "Octuple Compressed Cobblestone",
  286.         ["id"] = "ExtraUtilities:cobblestone_compressed:7",
  287.         ["recipe"] = {
  288.             "ExtraUtilities:cobblestone_compressed:6", "ExtraUtilities:cobblestone_compressed:6", "ExtraUtilities:cobblestone_compressed:6",
  289.             "ExtraUtilities:cobblestone_compressed:6", "ExtraUtilities:cobblestone_compressed:6", "ExtraUtilities:cobblestone_compressed:6",
  290.             "ExtraUtilities:cobblestone_compressed:6", "ExtraUtilities:cobblestone_compressed:6", "ExtraUtilities:cobblestone_compressed:6"
  291.         }
  292.     },
  293.    
  294.     {
  295.         ["name"] = "White Petal Block",
  296.         ["id"] = "Botania:petalBlock:0",
  297.         ["recipe"] = {
  298.             "Botania:petal:0", "Botania:petal:0", "Botania:petal:0",
  299.             "Botania:petal:0", "Botania:petal:0", "Botania:petal:0",
  300.             "Botania:petal:0", "Botania:petal:0", "Botania:petal:0"
  301.         }
  302.     },
  303.    
  304.     {
  305.         ["name"] = "Orange Petal Block",
  306.         ["id"] = "Botania:petalBlock:1",
  307.         ["recipe"] = {
  308.             "Botania:petal:1", "Botania:petal:1", "Botania:petal:1",
  309.             "Botania:petal:1", "Botania:petal:1", "Botania:petal:1",
  310.             "Botania:petal:1", "Botania:petal:1", "Botania:petal:1"
  311.         }
  312.     },
  313.    
  314.     {
  315.         ["name"] = "Magenta Petal Block",
  316.         ["id"] = "Botania:petalBlock:2",
  317.         ["recipe"] = {
  318.             "Botania:petal:2", "Botania:petal:2", "Botania:petal:2",
  319.             "Botania:petal:2", "Botania:petal:2", "Botania:petal:2",
  320.             "Botania:petal:2", "Botania:petal:2", "Botania:petal:2"
  321.         }
  322.     },
  323.    
  324.     {
  325.         ["name"] = "Light Blue Petal Block",
  326.         ["id"] = "Botania:petalBlock:3",
  327.         ["recipe"] = {
  328.             "Botania:petal:3", "Botania:petal:3", "Botania:petal:3",
  329.             "Botania:petal:3", "Botania:petal:3", "Botania:petal:3",
  330.             "Botania:petal:3", "Botania:petal:3", "Botania:petal:3"
  331.         }
  332.     },
  333.    
  334.     {
  335.         ["name"] = "Yellow Petal Block",
  336.         ["id"] = "Botania:petalBlock:4",
  337.         ["recipe"] = {
  338.             "Botania:petal:4", "Botania:petal:4", "Botania:petal:4",
  339.             "Botania:petal:4", "Botania:petal:4", "Botania:petal:4",
  340.             "Botania:petal:4", "Botania:petal:4", "Botania:petal:4"
  341.         }
  342.     },
  343.    
  344.     {
  345.         ["name"] = "Lime Petal Block",
  346.         ["id"] = "Botania:petalBlock:5",
  347.         ["recipe"] = {
  348.             "Botania:petal:5", "Botania:petal:5", "Botania:petal:5",
  349.             "Botania:petal:5", "Botania:petal:5", "Botania:petal:5",
  350.             "Botania:petal:5", "Botania:petal:5", "Botania:petal:5"
  351.         }
  352.     },
  353.    
  354.     {
  355.         ["name"] = "Pink Petal Block",
  356.         ["id"] = "Botania:petalBlock:6",
  357.         ["recipe"] = {
  358.             "Botania:petal:6", "Botania:petal:6", "Botania:petal:6",
  359.             "Botania:petal:6", "Botania:petal:6", "Botania:petal:6",
  360.             "Botania:petal:6", "Botania:petal:6", "Botania:petal:6"
  361.         }
  362.     },
  363.    
  364.     {
  365.         ["name"] = "Gray Petal Block",
  366.         ["id"] = "Botania:petalBlock:7",
  367.         ["recipe"] = {
  368.             "Botania:petal:7", "Botania:petal:7", "Botania:petal:7",
  369.             "Botania:petal:7", "Botania:petal:7", "Botania:petal:7",
  370.             "Botania:petal:7", "Botania:petal:7", "Botania:petal:7"
  371.         }
  372.     },
  373.    
  374.     {
  375.         ["name"] = "Light Gray Petal Block",
  376.         ["id"] = "Botania:petalBlock:8",
  377.         ["recipe"] = {
  378.             "Botania:petal:8", "Botania:petal:8", "Botania:petal:8",
  379.             "Botania:petal:8", "Botania:petal:8", "Botania:petal:8",
  380.             "Botania:petal:8", "Botania:petal:8", "Botania:petal:8"
  381.         }
  382.     },
  383.    
  384.     {
  385.         ["name"] = "Cyan Petal Block",
  386.         ["id"] = "Botania:petalBlock:9",
  387.         ["recipe"] = {
  388.             "Botania:petal:9", "Botania:petal:9", "Botania:petal:9",
  389.             "Botania:petal:9", "Botania:petal:9", "Botania:petal:9",
  390.             "Botania:petal:9", "Botania:petal:9", "Botania:petal:9"
  391.         }
  392.     },
  393.    
  394.     {
  395.         ["name"] = "Purple Petal Block",
  396.         ["id"] = "Botania:petalBlock:10",
  397.         ["recipe"] = {
  398.             "Botania:petal:10", "Botania:petal:10", "Botania:petal:10",
  399.             "Botania:petal:10", "Botania:petal:10", "Botania:petal:10",
  400.             "Botania:petal:10", "Botania:petal:10", "Botania:petal:10"
  401.         }
  402.     },
  403.    
  404.     {
  405.         ["name"] = "Blue Petal Block",
  406.         ["id"] = "Botania:petalBlock:11",
  407.         ["recipe"] = {
  408.             "Botania:petal:11", "Botania:petal:11", "Botania:petal:11",
  409.             "Botania:petal:11", "Botania:petal:11", "Botania:petal:11",
  410.             "Botania:petal:11", "Botania:petal:11", "Botania:petal:11"
  411.         }
  412.     },
  413.    
  414.     {
  415.         ["name"] = "Brown Petal Block",
  416.         ["id"] = "Botania:petalBlock:12",
  417.         ["recipe"] = {
  418.             "Botania:petal:12", "Botania:petal:12", "Botania:petal:12",
  419.             "Botania:petal:12", "Botania:petal:12", "Botania:petal:12",
  420.             "Botania:petal:12", "Botania:petal:12", "Botania:petal:12"
  421.         }
  422.     },
  423.    
  424.     {
  425.         ["name"] = "Green Petal Block",
  426.         ["id"] = "Botania:petalBlock:13",
  427.         ["recipe"] = {
  428.             "Botania:petal:13", "Botania:petal:13", "Botania:petal:13",
  429.             "Botania:petal:13", "Botania:petal:13", "Botania:petal:13",
  430.             "Botania:petal:13", "Botania:petal:13", "Botania:petal:13"
  431.         }
  432.     },
  433.    
  434.     {
  435.         ["name"] = "Red Petal Block",
  436.         ["id"] = "Botania:petalBlock:14",
  437.         ["recipe"] = {
  438.             "Botania:petal:14", "Botania:petal:14", "Botania:petal:14",
  439.             "Botania:petal:14", "Botania:petal:14", "Botania:petal:14",
  440.             "Botania:petal:14", "Botania:petal:14", "Botania:petal:14"
  441.         }
  442.     },
  443.    
  444.     {
  445.         ["name"] = "Black Petal Block",
  446.         ["id"] = "Botania:petalBlock:15",
  447.         ["recipe"] = {
  448.             "Botania:petal:15", "Botania:petal:15", "Botania:petal:15",
  449.             "Botania:petal:15", "Botania:petal:15", "Botania:petal:15",
  450.             "Botania:petal:15", "Botania:petal:15", "Botania:petal:15"
  451.         }
  452.     },
  453. }
  454.  
  455. for i = 1, #make do
  456.     make[i].output = make[i].output or 1
  457.     make[i].slotMax = make[i].slotMax or 64
  458. end
  459.  
  460. -- Translation of recipe slots to turtle inventory slots.
  461. local bench = {1, 2, 3, 5, 6, 7, 9, 10 ,11}
  462.  
  463. local function registerItem(slot, id, amount)
  464.     if items[slot] then
  465.         items[slot] = items[slot] + amount
  466.        
  467.         local ref = items[id]
  468.        
  469.         if ref then
  470.             ref.amount = ref.amount + amount
  471.            
  472.             for i = 1, #ref do
  473.                 if ref[i] == slot then
  474.                     return
  475.                 end
  476.             end
  477.         end
  478.        
  479.         error("Attempt to register "..amount.." "..id.." in chest slot "..slot..", but it holds another item type.")
  480.     else
  481.         items[slot] = amount
  482.        
  483.         items.freeSlots = items.freeSlots - 1
  484.        
  485.         local ref = items[id]
  486.  
  487.         if ref then
  488.             ref[#ref + 1] = slot
  489.             ref.amount = ref.amount + amount
  490.         else
  491.             items[id] = {slot, ["amount"] = amount}
  492.         end
  493.  
  494.         if slot == items.freeSlot then
  495.             items.freeSlot = nil
  496.  
  497.             for i = 1, slots do
  498.                 if not items[i] then
  499.                     items.freeSlot = i
  500.                     break
  501.                 end
  502.             end
  503.         end
  504.     end
  505. end
  506.  
  507. local function readChestSlots()
  508.     items = {["freeSlots"] = 0}
  509.    
  510.     for i = slots, 1, -1 do
  511.         local stack = chest.getStackInSlot(i)
  512.        
  513.         if stack then
  514.             registerItem(i, stack.id .. ":" .. stack.dmg, stack.qty)
  515.         else
  516.             items.freeSlot, items.freeSlots = i, items.freeSlots + 1
  517.         end
  518.     end
  519. end
  520.  
  521. turtle.select(1)
  522.  
  523. while true do
  524.     readChestSlots()
  525.    
  526.     -- Loop through all recipes in the database and attempt to craft them.
  527.     for i = 1, #make do
  528.         local crafted = true
  529.        
  530.         while crafted do
  531.             crafted = false
  532.        
  533.             -- Eject anything the turtle's holding into the chest above.
  534.             for j = 1, 16 do
  535.                 local item = turtle.getItemDetail(j)
  536.  
  537.                 if item then
  538.                     local id, amount = item.name .. ":" .. item.damage, item.count
  539.  
  540.                     -- Merge into whatever slots we can.
  541.                     if items[id] then
  542.                         local ref = items[id]
  543.  
  544.                         for k = 1, #ref do
  545.                             if items[ref[k]] < 64 then
  546.                                 local toPull = math.min(64 - items[ref[k]], amount)
  547.  
  548.                                 local pulled = chest.pullItemIntoSlot("down", j, toPull, ref[k])
  549.                                 amount = amount - pulled
  550.                                 if pulled > 0 then registerItem(ref[k], id, pulled) end
  551.  
  552.                                 if amount == 0 then break end
  553.                             end
  554.                         end
  555.                     end
  556.  
  557.                     -- Use a free slot if we have to.
  558.                     while amount > 0 do
  559.                         if not items.freeSlot then error("Attempted to stash "..id..", but ran out of room in the chest!") end
  560.  
  561.                         local pulled = chest.pullItemIntoSlot("down", j, amount, items.freeSlot)
  562.                         amount = amount - pulled
  563.                         if pulled > 0 then registerItem(items.freeSlot, id, pulled) end
  564.                        
  565.                         if amount > 0 then readChestSlots() end
  566.                     end
  567.                 end
  568.             end
  569.  
  570.             -- Confirm whether we can craft the current recipe.
  571.             -- First, how much of the recipe's output can we store?
  572.             local item, maxCanStore, targetSlot = make[i], 0
  573.  
  574.             -- Is there a spare slot to stick things in?
  575.             if items.freeSlot then
  576.                 maxCanStore, targetSlot = item.slotMax, items.freeSlot
  577.  
  578.             -- If not, will we be able to merge a stack?
  579.             elseif items[item.id] then
  580.                 local list, lowest = items[item.id], item.slotMax
  581.  
  582.                 for j = 1, #list do
  583.                     if items[list[j]] < lowest then
  584.                         lowest, targetSlot = items[list[j]], list[j]
  585.                     end
  586.                 end
  587.  
  588.                 maxCanStore = item.slotMax - lowest
  589.             end
  590.  
  591.             if maxCanStore > 0 then
  592.                 -- Have storage, but do we have ingrediants?
  593.                 local maxCanMake, resources = item.slotMax, {}
  594.  
  595.                 -- Which ones do we need, and in what amounts?
  596.                 for j = 1, 9 do
  597.                     local part = item.recipe[j]
  598.  
  599.                     if part then
  600.                         if not items[part] then
  601.                             maxCanMake = 0
  602.                             break
  603.                         end
  604.  
  605.                         if resources[part] then
  606.                             resources[resources[part]] = resources[resources[part]] + 1
  607.                         else
  608.                             resources[part] = #resources + 1
  609.                             resources[resources[part]] = 1
  610.                         end
  611.  
  612.                         if math.floor(items[part].amount / resources[resources[part]]) < maxCanMake then
  613.                             maxCanMake = math.floor(items[part].amount / resources[resources[part]])
  614.                         end
  615.                     end
  616.                 end
  617.  
  618.                 local amount = math.min(math.floor(maxCanStore / item.output), maxCanMake)
  619.  
  620.                 if amount > 0 then
  621.                     -- Let's craft some stuff!
  622.                     print("Crafting "..amount*item.output.." "..item.name..".")
  623.  
  624.                     -- Gather the needed materials into the turtle's inventory.
  625.                     for j = 1, 9 do
  626.                         local part = item.recipe[j]
  627.  
  628.                         if part then
  629.                             local needed = amount
  630.  
  631.                             local slots = items[part]
  632.  
  633.                             for k = #slots, 1, -1 do
  634.                                 if needed < items[slots[k]] then
  635.                                     chest.pushItemIntoSlot("down", slots[k], needed, bench[j])
  636.  
  637.                                     items[slots[k]] = items[slots[k]] - needed
  638.                                     break
  639.                                 else
  640.                                     chest.pushItemIntoSlot("down", slots[k], items[slots[k]], bench[j])
  641.  
  642.                                     needed = needed - items[slots[k]]
  643.  
  644.                                     items[table.remove(slots, k)] = nil
  645.                                     items.freeSlots = items.freeSlots + 1
  646.  
  647.                                     if needed == 0 then break end
  648.                                 end
  649.                             end
  650.  
  651.                             slots.amount = slots.amount - amount
  652.                         end
  653.                     end
  654.  
  655.                     -- And then mash them together.
  656.                     crafted = turtle.craft()
  657.                 end
  658.             end
  659.         end
  660.     end
  661.    
  662.     sleep(sleepSeconds)
  663. end
Add Comment
Please, Sign In to add comment