Advertisement
DabDaddy6223

chicken_sandwich_crafter_turtle

Jun 4th, 2025 (edited)
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 6.86 KB | None | 0 0
  1. ACCEPTABLE_FUEL = {"minecraft:coal", "minecraft:charcoal", "minecraft:oak_planks"}
  2.  
  3. FUEL_COUNT = 0
  4.  
  5. CHICKEN_COUNT = 0
  6. CARROT_COUNT = 0
  7. BREAD_COUNT = 0
  8. CABBAGE_COUNT = 0
  9.  
  10. CHICKEN_SLOT = -1
  11. CARROT_SLOT = -1
  12. BREAD_SLOT = -1
  13. CABBAGE_SLOT = -1
  14.  
  15. TIME_TO_SLEEP = 5
  16.  
  17. function findSlotWithItem(item_name)
  18.     for i=2,16 do
  19.         turtle.select(i)
  20.         local data = turtle.getItemDetail()
  21.         if data ~= nil then
  22.             if data["name"] == item_name then
  23.                 return i
  24.             end
  25.         end
  26.     end
  27.  
  28.     return -1
  29. end
  30.  
  31. function arrHasValue(arr, val)
  32.     for index, value in ipairs(arr) do
  33.         if value == val then
  34.             return true
  35.         end
  36.     end
  37.  
  38.     return false
  39. end
  40.  
  41. function hasFuel()
  42.     return FUEL_COUNT > 0
  43. end
  44.  
  45. function shouldRefuel()
  46.     return turtle.getFuelLevel() <= 0
  47. end
  48.  
  49. function refuel()
  50.     if shouldRefuel() == true then
  51.         if hasFuel() ~= true then
  52.             return false
  53.         end
  54.  
  55.         turtle.select(1)
  56.         turtle.refuel(1)
  57.         FUEL_COUNT = FUEL_COUNT - 1
  58.     end
  59.  
  60.     return true
  61. end
  62.  
  63. function turnAround()
  64.     turtle.turnLeft()
  65.     turtle.turnLeft()
  66. end
  67.  
  68. function main()
  69.     while true do
  70.         -- Prepare
  71.         print("Off we go!")
  72.  
  73.         turtle.select(1)
  74.         local slotData = turtle.getItemDetail()
  75.         if slotData ~= nil then
  76.             if arrHasValue(ACCEPTABLE_FUEL, slotData["name"]) == true then
  77.                 FUEL_COUNT = slotData["count"]
  78.             end
  79.         end
  80.  
  81.         if refuel() ~= true then
  82.             print("No Fuel!")
  83.             return
  84.         end
  85.  
  86.         CHICKEN_COUNT = 0
  87.         CARROT_COUNT = 0
  88.         BREAD_COUNT = 0
  89.         CABBAGE_COUNT = 0
  90.  
  91.         CHICKEN_SLOT = -1
  92.         CARROT_SLOT = -1
  93.         BREAD_SLOT = -1
  94.         CABBAGE_SLOT = -1
  95.  
  96.         -- Collect Bread
  97.         turtle.turnLeft()
  98.        
  99.         if refuel() ~= true then
  100.             print("No Fuel!")
  101.             return
  102.         end
  103.         turtle.forward()
  104.        
  105.         turtle.turnLeft()
  106.  
  107.         local bread_collected = turtle.suck()
  108.         if bread_collected then
  109.             BREAD_SLOT = findSlotWithItem("minecraft:bread")
  110.             turtle.select(BREAD_SLOT)
  111.             local bread_item = turtle.getItemDetail()
  112.             BREAD_COUNT = bread_item["count"]
  113.         end
  114.        
  115.         -- Collect Carrots
  116.         turtle.turnRight()
  117.  
  118.         for i=1,2 do
  119.             if refuel() ~= true then
  120.                 print("No Fuel!")
  121.                 return
  122.             end
  123.             turtle.forward()
  124.         end
  125.        
  126.         turtle.turnRight()
  127.  
  128.         for i=1,6 do
  129.             if refuel() ~= true then
  130.                 print("No Fuel!")
  131.                 return
  132.             end
  133.             turtle.forward()
  134.         end
  135.  
  136.         turtle.turnLeft()
  137.  
  138.         local carrots_collected = turtle.suck()
  139.         if carrots_collected then
  140.             CARROT_SLOT = findSlotWithItem("minecraft:carrot")
  141.             turtle.select(CARROT_SLOT)
  142.             local carrot_item = turtle.getItemDetail()
  143.             CARROT_COUNT = carrot_item["count"]
  144.         end
  145.  
  146.         -- Collect Chicken
  147.         turtle.turnRight()
  148.        
  149.         if refuel() ~= true then
  150.             print("No Fuel!")
  151.             return
  152.         end
  153.         turtle.forward()
  154.  
  155.         turtle.turnRight()
  156.  
  157.         for i=1,3 do
  158.             if refuel() ~= true then
  159.                 print("No Fuel!")
  160.                 return
  161.             end
  162.             turtle.forward()
  163.         end
  164.  
  165.         turtle.turnLeft()
  166.  
  167.         while true do
  168.             local item_removed = turtle.suck()
  169.             if item_removed then
  170.                 CHICKEN_SLOT = findSlotWithItem("minecraft:cooked_chicken")
  171.                 if CHICKEN_SLOT == -1 then
  172.                     local feather_slot = findSlotWithItem("minecraft:feather")
  173.                     turtle.select(feather_slot)
  174.                     turtle.dropDown()
  175.                 else
  176.                     turtle.select(CHICKEN_SLOT)
  177.                     local chicken_item = turtle.getItemDetail()
  178.                     CHICKEN_COUNT = chicken_item["count"]
  179.                     break
  180.                 end
  181.             else
  182.                 break
  183.             end
  184.         end
  185.  
  186.         -- Collect Cabbages
  187.         turtle.turnRight()
  188.  
  189.         for i=1,3 do
  190.             if refuel() ~= true then
  191.                 print("No Fuel!")
  192.                 return
  193.             end
  194.             turtle.forward()
  195.         end
  196.  
  197.         turtle.turnRight()
  198.  
  199.         for i=1,2 do
  200.             if refuel() ~= true then
  201.                 print("No Fuel!")
  202.                 return
  203.             end
  204.             turtle.forward()
  205.         end
  206.  
  207.         turtle.turnLeft()
  208.  
  209.         local cabbages_collected = turtle.suck()
  210.         if cabbages_collected then
  211.             CABBAGE_SLOT = findSlotWithItem("farmersdelight:cabbage")
  212.             turtle.select(CABBAGE_SLOT)
  213.             local cabbage_item = turtle.getItemDetail()
  214.             CABBAGE_COUNT = cabbage_item["count"]
  215.         end
  216.  
  217.         -- Move to crafting position
  218.         turnAround()
  219.  
  220.         for i=1,3 do
  221.             if refuel() ~= true then
  222.                 print("No Fuel!")
  223.                 return
  224.             end
  225.             turtle.forward()
  226.         end
  227.  
  228.         turtle.turnLeft()
  229.  
  230.         if refuel() ~= true then
  231.             print("No Fuel!")
  232.             return
  233.         end
  234.         turtle.forward()
  235.  
  236.         -- Remove Coal for Crafting
  237.         turtle.select(1)
  238.         turtle.dropDown()
  239.  
  240.         -- Craft
  241.         local amount_to_craft = math.min(CHICKEN_COUNT, CARROT_COUNT, BREAD_COUNT, CABBAGE_COUNT)
  242.        
  243.         if amount_to_craft > 0 then
  244.             turtle.select(BREAD_SLOT)
  245.             turtle.transferTo(1)
  246.             turtle.select(CHICKEN_SLOT)
  247.             turtle.transferTo(2)
  248.             turtle.select(CABBAGE_SLOT)
  249.             turtle.transferTo(5)
  250.             turtle.select(CARROT_SLOT)
  251.             turtle.transferTo(6)
  252.  
  253.             turtle.select(16)
  254.             turtle.craft(amount_to_craft)
  255.         end
  256.  
  257.         -- Recollect Coal
  258.         turtle.suckDown(64)
  259.  
  260.         -- Dump Food in Chest
  261.         if refuel() ~= true then
  262.             print("No Fuel!")
  263.             return
  264.         end
  265.         turtle.forward()
  266.  
  267.         for i=1,16 do
  268.             turtle.select(i)
  269.             local item = turtle.getItemDetail()
  270.             if item ~= nil then
  271.             if arrHasValue(ACCEPTABLE_FUEL, item["name"]) ~= true then
  272.                     turtle.dropDown()
  273.             end
  274.             end
  275.         end
  276.         turtle.select(1)
  277.  
  278.         -- Return
  279.         for i=1,3 do
  280.             if refuel() ~= true then
  281.                 print("No Fuel!")
  282.                 return
  283.             end
  284.             turtle.forward()
  285.         end
  286.  
  287.         turnAround()
  288.  
  289.         print("Waiting...")
  290.         os.sleep(TIME_TO_SLEEP)
  291.     end
  292. end
  293.  
  294. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement