Advertisement
DabDaddy6223

carrot_farm_turtle

Jun 3rd, 2025 (edited)
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.64 KB | None | 0 0
  1. ACCEPTABLE_FUEL = {"minecraft:coal", "minecraft:charcoal", "minecraft:oak_planks"}
  2.  
  3. FUEL_COUNT = 0
  4.  
  5. CROP = "minecraft:carrots"
  6. CROP_ITEM = "minecraft:carrot"
  7. HARVEST_AGE = 7
  8.  
  9. function arrHasValue(arr, val)
  10.     for index, value in ipairs(arr) do
  11.         if value == val then
  12.             return true
  13.         end
  14.     end
  15.  
  16.     return false
  17. end
  18.  
  19. function hasFuel()
  20.     return FUEL_COUNT > 0
  21. end
  22.  
  23. function shouldRefuel()
  24.     return turtle.getFuelLevel() <= 0
  25. end
  26.  
  27. function refuel()
  28.     if shouldRefuel() == true then
  29.         if hasFuel() ~= true then
  30.             return false
  31.         end
  32.  
  33.         turtle.select(1)
  34.         turtle.refuel(1)
  35.         FUEL_COUNT = FUEL_COUNT - 1
  36.     end
  37.  
  38.     return true
  39. end
  40.  
  41. function turnAround()
  42.     turtle.turnLeft()
  43.     turtle.turnLeft()
  44. end
  45.  
  46. function plant()
  47.     for i=2,16 do
  48.         turtle.select(i)
  49.         local slotData = turtle.getItemDetail()
  50.         if slotData ~= nil then
  51.             if slotData["name"] == CROP_ITEM then
  52.                 turtle.placeDown()
  53.                 return
  54.             end
  55.         end
  56.     end
  57. end
  58.  
  59. function handleCrop()
  60.     local found, block = turtle.inspectDown()
  61.     if found == true then
  62.         if block.name == CROP and block.state.age == HARVEST_AGE then
  63.             turtle.digDown()
  64.             plant()
  65.         end
  66.     else
  67.         plant()
  68.     end
  69. end
  70.  
  71. function doStrip()
  72.     for i=1, 8 do
  73.         if refuel() ~= true then
  74.             print("No Fuel!")
  75.             return
  76.         end
  77.  
  78.         turtle.forward()
  79.         handleCrop()
  80.     end
  81. end
  82.  
  83. function turnLeft()
  84.     if refuel() ~= true then
  85.         print("No Fuel!")
  86.         return
  87.     end
  88.  
  89.     turtle.turnLeft()
  90.     turtle.forward()
  91.     turtle.turnLeft()
  92.     handleCrop()
  93. end
  94.  
  95. function turnRight()
  96.     if refuel() ~= true then
  97.         print("No Fuel!")
  98.         return
  99.     end
  100.  
  101.     turtle.turnRight()
  102.     turtle.forward()
  103.     turtle.turnRight()
  104.     handleCrop()
  105. end
  106.  
  107. function main()
  108.     -- Prepare
  109.     turtle.select(1)
  110.     local slotData = turtle.getItemDetail()
  111.     if slotData ~= nil then
  112.         if arrHasValue(ACCEPTABLE_FUEL, slotData["name"]) == true then
  113.             FUEL_COUNT = slotData["count"]
  114.         end
  115.     end
  116.  
  117.     if refuel() ~= true then
  118.         print("No Fuel!")
  119.         return
  120.     end
  121.  
  122.     -- Move to beginning spot
  123.     if refuel() ~= true then
  124.         print("No Fuel!")
  125.         return
  126.     end
  127.     turtle.forward()
  128.  
  129.     turtle.turnLeft()
  130.  
  131.     for i=1,4 do
  132.         if refuel() ~= true then
  133.             print("No Fuel!")
  134.             return
  135.         end
  136.         turtle.forward()
  137.     end
  138.  
  139.     turtle.turnRight()
  140.  
  141.     if refuel() ~= true then
  142.         print("No Fuel!")
  143.         return
  144.     end
  145.  
  146.     -- Harvest Farm
  147.     handleCrop()
  148.     for i=1,4 do
  149.         doStrip()
  150.         turnRight()
  151.         doStrip()
  152.         turnLeft()
  153.     end
  154.  
  155.     doStrip()
  156.  
  157.     if refuel() ~= true then
  158.         print("No Fuel!")
  159.         return
  160.     end
  161.  
  162.     -- Move to Chests
  163.     turnAround()
  164.  
  165.     for i=1,9 do
  166.         if refuel() ~= true then
  167.             print("No Fuel!")
  168.             return
  169.         end
  170.         turtle.forward()
  171.     end
  172.  
  173.     turtle.turnRight()
  174.  
  175.     for i=1,4 do
  176.         if refuel() ~= true then
  177.             print("No Fuel!")
  178.             return
  179.         end
  180.         turtle.forward()
  181.     end
  182.  
  183.     -- Dump Carrots
  184.     for i=2,16 do
  185.         turtle.select(i)
  186.         local slotData = turtle.getItemDetail()
  187.         if slotData ~= nil then
  188.             if slotData["name"] == CROP_ITEM then
  189.                 turtle.dropDown(slotData["count"])
  190.             end
  191.         end
  192.     end
  193.  
  194.     turtle.turnRight()
  195. end
  196.  
  197. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement