Advertisement
DabDaddy6223

chicken_sandwich_crafter_turtle

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