hhhzzzsss

cropfarm.lua

Aug 19th, 2023 (edited)
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 8.68 KB | None | 0 0
  1. local seedMap = {}
  2. seedMap["minecraft:wheat"] = "minecraft:wheat_seeds"
  3. seedMap["minecraft:beetroots"] = "minecraft:beetroot_seeds"
  4. seedMap["minecraft:carrots"] = "minecraft:carrot"
  5. seedMap["minecraft:potatoes"] = "minecraft:potato"
  6. seedMap["minecraft:potatoes"] = "minecraft:potato"
  7. seedMap["farmersdelight:cabbages"] = "farmersdelight:cabbage_seeds"
  8. seedMap["farmersdelight:onions"] = "farmersdelight:onion"
  9.  
  10. local maxAgeMap = {}
  11. maxAgeMap["minecraft:wheat"] = 7
  12. maxAgeMap["minecraft:beetroots"] = 3
  13. maxAgeMap["minecraft:carrots"] = 7
  14. maxAgeMap["minecraft:potatoes"] = 7
  15. maxAgeMap["farmersdelight:cabbages"] = 7
  16. maxAgeMap["farmersdelight:onions"] = 7
  17.  
  18. local function fastFail(success)
  19.     if not success then
  20.         print("An unexpected error has occured! Aborting.")
  21.         exit()
  22.     end
  23. end
  24.  
  25. local function refuel(amount)
  26.     local fuelLevel = turtle.getFuelLevel()
  27.     if fuelLevel == "unlimited" then
  28.         return true
  29.     end
  30.  
  31.     turtle.select(16)
  32.     while turtle.getItemCount(16) > 0 and turtle.getFuelLevel() < amount do
  33.         if not turtle.refuel(1) then
  34.             return false
  35.         end
  36.     end
  37.  
  38.     if turtle.getFuelLevel() < amount then
  39.         return false
  40.     else
  41.         return true
  42.     end
  43. end
  44.  
  45. local function waitForFuel(amount)
  46.     if not refuel(amount) then
  47.         print("Please put fuel in slot 16")
  48.         while not refuel(amount) do
  49.             sleep(0.5)
  50.         end
  51.         print("Received sufficient fuel. Continuing...")
  52.     end
  53. end
  54.  
  55. local function selectItem(name)
  56.     for i=15, 1, -1 do
  57.         itemDetail = turtle.getItemDetail(i)
  58.         if itemDetail and itemDetail.name == name then
  59.             turtle.select(i)
  60.             return true
  61.         end
  62.     end
  63.     return false
  64. end
  65.  
  66. local function countItem(name)
  67.     num = 0
  68.     for i=1, 15 do
  69.         itemDetail = turtle.getItemDetail(i)
  70.         if itemDetail and itemDetail.name == name then
  71.             num = num + itemDetail.count
  72.         end
  73.     end
  74.     return num
  75. end
  76.  
  77. local function waitForItem(name, amount)
  78.     if countItem(name) < amount then
  79.         print("Please provide more " .. name)
  80.         while countItem(name) < amount do
  81.             sleep(0.5)
  82.         end
  83.         print("Received sufficient " .. name .. ". Continuing...")
  84.     end
  85.     selectItem(name)
  86. end
  87.  
  88. local function waitForDrop()
  89.     if turtle.getItemCount() > 0 and not turtle.drop() then
  90.         print("Please make room for turtle to drop")
  91.         while turtle.getItemCount() > 0 and not turtle.drop() do
  92.             sleep(0.5)
  93.         end
  94.         print("Turtle successfully dropped. Continuing...")
  95.     end
  96. end
  97.  
  98. local function waitForDropDown()
  99.     if turtle.getItemCount() > 0 and not turtle.dropDown() then
  100.         print("Please make room for turtle to drop")
  101.         while turtle.getItemCount() > 0 and not turtle.dropDown() do
  102.             sleep(0.5)
  103.         end
  104.         print("Turtle successfully dropped. Continuing...")
  105.     end
  106. end
  107.  
  108. local function waitForDropUp()
  109.     if turtle.getItemCount() > 0 and not turtle.dropUp() then
  110.         print("Please make room for turtle to drop")
  111.         while turtle.getItemCount() > 0 and not turtle.dropUp() do
  112.             sleep(0.5)
  113.         end
  114.         print("Turtle successfully dropped. Continuing...")
  115.     end
  116. end
  117.  
  118. local function containerRefuel(amount)
  119.     turtle.select(16)
  120.     waitForDropDown() -- Edit this line to when changing refuel direction
  121.     turtle.suckDown() -- Edit this line to when changing refuel direction
  122.     if not refuel(amount) then
  123.         print("Waiting for fuel...")
  124.         sleep(0.5)
  125.         while not refuel(amount) do
  126.             sleep(0.5)
  127.             waitForDropDown() -- Edit this line to when changing refuel direction
  128.             turtle.suckDown() -- Edit this line to when changing refuel direction
  129.         end
  130.     end
  131. end
  132.  
  133. local function getBlock()
  134.     local hasBlock, data = turtle.inspect()
  135.     if hasBlock then
  136.         return data.name
  137.     else
  138.         return "minecraft:air"
  139.     end
  140. end
  141.  
  142. local function getBlockDown()
  143.     local hasBlock, data = turtle.inspectDown()
  144.     if hasBlock then
  145.         return data.name
  146.     else
  147.         return "minecraft:air"
  148.     end
  149. end
  150.  
  151. local function getBlockUp()
  152.     local hasBlock, data = turtle.inspectUp()
  153.     if hasBlock then
  154.         return data.name
  155.     else
  156.         return "minecraft:air"
  157.     end
  158. end
  159.  
  160. local function tryForward()
  161.     waitForFuel(1)
  162.     return turtle.forward()
  163. end
  164.  
  165. local function tryBack()
  166.     waitForFuel(1)
  167.     return turtle.back()
  168. end
  169.  
  170. local function tryDown()
  171.     waitForFuel(1)
  172.     return turtle.down()
  173. end
  174.  
  175. local function tryUp()
  176.     waitForFuel(1)
  177.     return turtle.up()
  178. end
  179.  
  180. local function repeatForward(amount)
  181.     for i=1, amount do
  182.         fastFail(tryForward())
  183.     end
  184. end
  185.  
  186. local function repeatBack(amount)
  187.     for i=1, amount do
  188.         fastFail(tryBack())
  189.     end
  190. end
  191.  
  192. local function repeatDown(amount)
  193.  
  194.     for i=1, amount do
  195.         fastFail(tryDown())
  196.     end
  197. end
  198.  
  199. local function repeatUp(amount)
  200.     for i=1, amount do
  201.         fastFail(tryUp())
  202.     end
  203. end
  204.  
  205. local function isInTransportColumn()
  206.     local hasBlock, data = turtle.inspectUp()
  207.     if not hasBlock or data.name == "minecraft:cobblestone" then
  208.         return true
  209.     else
  210.         return false
  211.     end
  212. end
  213.  
  214. local function recenter()
  215.     while not isInTransportColumn() do
  216.         if not tryForward() then
  217.             turtle.turnRight()
  218.         end
  219.     end
  220.     while getBlock() ~= "minecraft:chest" and getBlock() ~= "minecraft:hopper" do
  221.         turtle.turnLeft()
  222.     end
  223.     while getBlock() ~= "minecraft:chest" do
  224.         tryUp()
  225.     end
  226. end
  227.  
  228. local function tryPlant(name)
  229.     if countItem(name) > 0 then
  230.         selectItem(name)
  231.         turtle.placeDown()
  232.         return true
  233.     else
  234.         return false
  235.     end
  236. end
  237.  
  238. local function doRow()
  239.     local rowSeed = nil
  240.     local hasMissing = false
  241.     local hasBlock, data = turtle.inspectDown()
  242.     if hasBlock then
  243.         local seed = seedMap[data.name]
  244.         if seed then
  245.             rowSeed = seed
  246.             if data.state.age == maxAgeMap[data.name] then
  247.                 turtle.digDown()
  248.                 tryPlant(seed)
  249.             end
  250.         end
  251.     elseif rowSeed then
  252.         if not tryPlant(rowSeed) then
  253.             hasMissing = true
  254.         end
  255.     else
  256.         hasMissing = true
  257.     end
  258.     for i=1, 13 do
  259.         fastFail(tryForward())
  260.         local hasBlock, data = turtle.inspectDown()
  261.         if hasBlock then
  262.             local seed = seedMap[data.name]
  263.             if seed then
  264.                 rowSeed = seed
  265.                 if data.state.age == maxAgeMap[data.name] then
  266.                     turtle.digDown()
  267.                     tryPlant(seed)
  268.                 end
  269.             end
  270.         elseif rowSeed then
  271.             if not tryPlant(rowSeed) then
  272.                 hasMissing = true
  273.             end
  274.         else
  275.             hasMissing = true
  276.         end
  277.     end
  278.  
  279.     if hasMissing and rowSeed then
  280.         for i=1, 13 do
  281.             fastFail(tryBack())
  282.             if not turtle.detectDown() then
  283.                 tryPlant(rowSeed)
  284.             end
  285.         end
  286.         repeatForward(13)
  287.     end
  288. end
  289.  
  290. recenter()
  291.  
  292. if not fs.isDir("/stateFiles") then
  293.     fs.delete("/stateFiles")
  294.     fs.makeDir("/stateFiles")
  295. end
  296.  
  297. if fs.isDir("/stateFiles/farmResetDown") then
  298.     fs.delete("/stateFiles/farmResetDown")
  299. end
  300.  
  301. if fs.exists("/stateFiles/farmResetDown") then
  302.     while not turtle.detectDown() do
  303.         tryDown()
  304.     end
  305.     fs.delete("/stateFiles/farmResetDown")
  306. end
  307.  
  308. while true do
  309.     if getBlockDown() == "minecraft:barrel" then
  310.         containerRefuel(2048)
  311.     end
  312.  
  313.     sleep(3*60) -- adjust based on the number of layers, make it around 10 minutes per full cycle
  314.  
  315.     turtle.turnRight()
  316.     fastFail(tryForward())
  317.     doRow()
  318.     turtle.turnRight()
  319.     fastFail(tryForward())
  320.     turtle.turnRight()
  321.     doRow()
  322.     for i=1, 6 do
  323.         turtle.turnLeft()
  324.         fastFail(tryForward())
  325.         turtle.turnLeft()
  326.         doRow()
  327.         turtle.turnRight()
  328.         fastFail(tryForward())
  329.         turtle.turnRight()
  330.         doRow()
  331.     end
  332.  
  333.     recenter()
  334.  
  335.     for i=1, 15 do
  336.         if turtle.getItemCount(i) > 0 then
  337.             turtle.select(i)
  338.             waitForDrop()
  339.         end
  340.     end
  341.  
  342.     turtle.select(16)
  343.     if not turtle.refuel(0) then
  344.         waitForDrop()
  345.     end
  346.  
  347.     if getBlockUp() == "minecraft:air" then
  348.         repeatUp(3)
  349.     else
  350.         local file = fs.open("/stateFiles/farmResetDown", "w")
  351.         file.close()
  352.         while not turtle.detectDown() do
  353.             tryDown()
  354.         end
  355.         fs.delete("/stateFiles/farmResetDown")
  356.     end
  357. end
Add Comment
Please, Sign In to add comment