Advertisement
Greyman27

farm.lua

Apr 9th, 2024 (edited)
5
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.24 KB | None | 0 0
  1. local length = 18
  2. local width = 18
  3.  
  4. local fuel_slot = 1
  5. local fuel_minimum = length*width+length+width+10
  6. local seed_slot = 2
  7. local seconds_to_wait = 600
  8. local wait_first = false
  9. local deposit = false
  10. local deposit_direction = 3
  11.  
  12. local b = false
  13. local block = nil
  14. local farming = true
  15. local turn_adjust = 1
  16. os.loadAPI("grey_api/move.lua")
  17. os.loadAPI("grey_api/util.lua")
  18.  
  19. function farm_wheat(length,width)
  20.     if length==nil then length=9 end--length is the forward size of the wheat plot
  21.     if width==nil then width=9 end--width is the right size of the wheat plot
  22.     turn_adjust = 1-- 1=+x, -1=-x
  23.    
  24.     while farming do
  25.         harvest_wheat()
  26.         if move.get_x() >= length-1 and turn_adjust == 1 then
  27.             if move.get_z() >= width-1 then
  28.                 move.return_to_home()
  29.                 end_run()
  30.             else
  31.                 move.turn("right")
  32.                 move.go()
  33.                 harvest_wheat()
  34.                 move.turn("right")
  35.                 move.go()
  36.                 turn_adjust = turn_adjust * -1
  37.             end
  38.         elseif move.get_x() <= 0 and turn_adjust == -1 then
  39.             if move.get_z() >= width-1 then
  40.                 move.return_to_home()
  41.                 end_run()
  42.             else
  43.                 move.turn("left")
  44.                 move.go()
  45.                 harvest_wheat()
  46.                 move.turn("left")
  47.                 move.go()
  48.                 turn_adjust = turn_adjust * -1
  49.             end
  50.         else
  51.             if not move.go() then
  52.                 return false
  53.             end
  54.         end
  55.     end--end while farming    
  56. end
  57.  
  58. function end_run()
  59.     if deposit then deposit() end
  60.     util.wait(seconds_to_wait)
  61.     turn_adjust = 1
  62.     print("wait done")
  63. end
  64.  
  65. function deposit()
  66.     local item = nil
  67.     move.face(deposit_direction)
  68.     for i=1,16 do
  69.         item = turtle.getItemDetail(i)
  70.         if item ~= nil and (item["name"]=="minecraft:wheat_seeds" or item["name"]=="minecraft:wheat") then
  71.             --print("dropped "..item["name"].. " from slot "..i)
  72.             turtle.select(i)
  73.             turtle.drop()
  74.         end
  75.     end
  76.     move.face(0)
  77. end
  78.  
  79. function harvest_wheat()
  80.     b,block = turtle.inspectDown()
  81.     if b == false then
  82.         --print("No block at ("..move.get_x()..","..move.get_z()..")")
  83.         find_seed_slot()
  84.         turtle.select(seed_slot)
  85.         if not turtle.placeDown() then
  86.             print("unplantable block at ("..move.get_x()..","..move.get_z()..")")
  87.         end
  88.     elseif block['state']['age']==7 then
  89.         turtle.digDown()
  90.         --print("Harvested at ("..move.get_x()..","..move.get_z()..")")
  91.         find_seed_slot()
  92.         turtle.select(seed_slot)
  93.         turtle.placeDown()
  94.     else
  95.         --print("age!=7 at ("..move.get_x()..","..move.get_z()..")")
  96.     end
  97.    
  98.     if turtle.getFuelLevel() <= fuel_minimum then
  99.         util.fuel(1)
  100.     end
  101. end
  102.  
  103. function find_seed_slot()
  104.     for i=1,16 do
  105.         local item = turtle.getItemDetail(i)
  106.         if item ~= nil and item["name"] == "minecraft:wheat_seeds" then
  107.             seed_slot = i
  108.             return i
  109.         end
  110.     end
  111.     return 0
  112. end
  113.  
  114. if wait_first then
  115.     util.wait(seconds_to_wait)
  116. end
  117. farm_wheat(length,width)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement