Hsiang_Nianian

farm

Apr 12th, 2025 (edited)
34
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 15.14 KB | None | 0 0
  1. -- ==================================================
  2. -- Thank you for using Beni's Farm Script!
  3. -- ==================================================
  4. --------------------------------------------------
  5. -- CONFIGURATION VARIABLES
  6. --------------------------------------------------
  7. local fuelSuckCount = 10         -- Number of fuel items (e.g., coal) to suck at start.
  8. local lowFuelThreshold = fuelSuckCount * 8
  9.  
  10. --------------------------------------------------
  11. -- GLASS PANE COLOR SETUP
  12. --------------------------------------------------
  13. local args = {...}
  14. local glassColor = args[1] or "green"
  15. local targetGlassPane = "minecraft:" .. glassColor .. "_stained_glass_pane"
  16. --------------------------------------------------
  17. -- CLEAR CONSOLE
  18. term.clear()
  19. term.setCursorPos(1, 1)
  20. --------------------------------------------------
  21. print("Using " .. glassColor .. " stained glass pane for positioning.")
  22.  
  23. --------------------------------------------------
  24. -- POSITIONING ROUTINE
  25. --------------------------------------------------
  26. local function positionTurtle()
  27.   local positioned = false
  28.   local attemptCounter = 0
  29.  
  30.   while not positioned do
  31.     attemptCounter = attemptCounter + 1
  32.  
  33.     local successDown, dataDown = turtle.inspectDown()
  34.     if successDown and dataDown.name == "minecraft:water" then
  35.       local successFront, dataFront = turtle.inspect()
  36.       if successFront and dataFront.name == "minecraft:chest" then
  37.         print("Position OK.")
  38.         positioned = true
  39.       else
  40.         -- If there's water below, but no chest in front, keep trying to move/turn.
  41.         if successFront then
  42.           if dataFront.name == "minecraft:air" then
  43.             turtle.forward()
  44.           elseif dataFront.name == "minecraft:glass" or dataFront.name == "minecraft:glass_pane" then
  45.             turtle.turnLeft()
  46.           elseif dataFront.name == targetGlassPane then
  47.             turtle.down()
  48.           else
  49.             turtle.forward()
  50.           end
  51.         else
  52.           turtle.forward()
  53.         end
  54.       end
  55.     else
  56.       -- No (or wrong) water below; keep searching
  57.       local successFront, dataFront = turtle.inspect()
  58.       if successFront then
  59.         if dataFront.name == "minecraft:air" then
  60.           turtle.forward()
  61.         elseif dataFront.name == "minecraft:glass" or dataFront.name == "minecraft:glass_pane" then
  62.           turtle.turnLeft()
  63.         elseif dataFront.name == targetGlassPane then
  64.           turtle.down()
  65.         else
  66.           turtle.forward()
  67.         end
  68.       else
  69.         turtle.forward()
  70.       end
  71.     end
  72.  
  73.     -- If we’ve been trying for a while, warn the user.
  74.     if attemptCounter > 20 and not positioned then
  75.       print("WARNING: Turtle is not finding water below + chest in front!")
  76.       print("Ensure your farm setup has:")
  77.       print(" - Still water directly beneath the turtle.")
  78.       print(" - A chest directly in front of the turtle.")
  79.       print(" - The correct color stained glass pane above that chest.")
  80.       print(" - A fuel chest to the right of the turtle if it needs refueling.")
  81.       print("Retrying...")
  82.       sleep(10)
  83.       attemptCounter = 0
  84.     end
  85.   end
  86. end
  87.  
  88. --------------------------------------------------
  89. -- FUEL CHECK ROUTINE
  90. --------------------------------------------------
  91. local function fuelCheck()
  92.   local fuel = turtle.getFuelLevel()
  93.   if fuel < lowFuelThreshold then
  94.     print("Fuel low (" .. fuel .. "); refueling...")
  95.     turtle.turnRight()   -- Face refuel Ender Chest.
  96.     turtle.suck(fuelSuckCount)
  97.     for slot = 1, 16 do
  98.       turtle.select(slot)
  99.       turtle.refuel()
  100.     end
  101.     turtle.turnLeft()    -- Restore original facing.
  102.   else
  103.     print("Fuel level sufficient (" .. fuel .. ").")
  104.   end
  105. end
  106.  
  107. --------------------------------------------------
  108. -- DEPOSIT OPERATIONS
  109. --------------------------------------------------
  110. local function depositOperations()
  111.   -- Deposit the entire inventory into the chest in front.
  112.   for slot = 1, 16 do
  113.     turtle.select(slot)
  114.     turtle.drop()
  115.   end
  116.   print("Inventory deposited.")
  117. end
  118.  
  119. --------------------------------------------------
  120. -- INVENTORY MANAGEMENT HELPERS
  121. --------------------------------------------------
  122. local function isInventoryFull()
  123.   for slot = 1, 16 do
  124.     if turtle.getItemDetail(slot) == nil then
  125.       return false
  126.     end
  127.   end
  128.   return true
  129. end
  130.  
  131. local function organizeSeeds(cropBlock)
  132.   -- We only organize seeds for wheat and beetroots (which use dedicated slots)
  133.   local seedType, dedicatedSlot
  134.   if cropBlock == "ars_nouveau:sourceberry_bush" then
  135.     seedType = "ars_nouveau:sourceberry"
  136.     dedicatedSlot = 1
  137.   elseif cropBlock == "fruitsdelight:blueberry_bush" then
  138.     seedType = "fruitsdelight:blueberry"
  139.     dedicatedSlot = 2
  140.   else
  141.     return  -- No organization for others.
  142.   end
  143.  
  144.   turtle.select(dedicatedSlot)
  145.   local dedicatedItem = turtle.getItemDetail(dedicatedSlot)
  146.   local space = 0
  147.   if dedicatedItem then
  148.     space = 64 - dedicatedItem.count  -- Assume a stack size of 64.
  149.   else
  150.     space = 64
  151.   end
  152.  
  153.   for slot = 1, 16 do
  154.     if slot ~= dedicatedSlot then
  155.       turtle.select(slot)
  156.       local detail = turtle.getItemDetail(slot)
  157.       if detail and detail.name == seedType then
  158.         if space > 0 then
  159.           local count = detail.count
  160.           local transferCount = math.min(count, space)
  161.           turtle.transferTo(dedicatedSlot, transferCount)
  162.           turtle.select(dedicatedSlot)
  163.           local newDetail = turtle.getItemDetail(dedicatedSlot)
  164.           if newDetail then
  165.             space = 64 - newDetail.count
  166.           else
  167.             space = 64
  168.           end
  169.         else
  170.           print("Dedicated slot for " .. seedType .. " is full; dropping extra seeds from slot " .. slot)
  171.           turtle.drop()  -- Drop excess seeds.
  172.         end
  173.       end
  174.     end
  175.   end
  176.   turtle.select(dedicatedSlot)
  177. end
  178.  
  179. --------------------------------------------------
  180. -- ATTEMPT TO PLANT A SPECIFIC CROP
  181. --------------------------------------------------
  182. local function attemptToPlant(cropBlock)
  183.   -- Map the crop block to the seed item and dedicated slot.
  184.   local seedType, dedicatedSlot
  185.   if cropBlock == "ars_nouveau:sourceberry_bush" then
  186.     seedType = "ars_nouveau:sourceberry"
  187.     dedicatedSlot = 1
  188.   elseif cropBlock == "fruitsdelight:blueberry_bush" then
  189.     seedType = "fruitsdelight:blueberry"
  190.     dedicatedSlot = 2
  191.   elseif cropBlock == "minecraft:wheat" then
  192.     seedType = "minecraft:wheat_seeds"
  193.     dedicatedSlot = 3
  194.   elseif cropBlock == "minecraft:carrots" then
  195.     seedType = "minecraft:carrot"
  196.     dedicatedSlot = 4
  197.   elseif cropBlock == "minecraft:potatoes" then
  198.     seedType = "minecraft:potato"
  199.     dedicatedSlot = 5
  200.   elseif cropBlock == "minecraft:beetroots" then
  201.     seedType = "minecraft:beetroot_seeds"
  202.     dedicatedSlot = 6
  203.   else
  204.     -- Default to wheat if unknown.
  205.     seedType = "minecraft:wheat_seeds"
  206.     dedicatedSlot = 3
  207.   end
  208.  
  209.   -- Check the dedicated slot.
  210.   turtle.select(dedicatedSlot)
  211.   local slotItem = turtle.getItemDetail(dedicatedSlot)
  212.   if slotItem and slotItem.name ~= seedType then
  213.     -- The dedicated slot contains the wrong item; try to move it.
  214.     local emptySlot = nil
  215.     for s = 1, 16 do
  216.       if s ~= dedicatedSlot and not turtle.getItemDetail(s) then
  217.         emptySlot = s
  218.         break
  219.       end
  220.     end
  221.     if emptySlot then
  222.       turtle.transferTo(emptySlot)
  223.     else
  224.       turtle.drop()
  225.     end
  226.   end
  227.  
  228.   -- If the slot is empty or wrong, search the inventory for the correct seed.
  229.   slotItem = turtle.getItemDetail(dedicatedSlot)
  230.   if not slotItem or slotItem.name ~= seedType then
  231.     local found = false
  232.     for s = 1, 16 do
  233.       if s ~= dedicatedSlot then
  234.         local detail = turtle.getItemDetail(s)
  235.         if detail and detail.name == seedType then
  236.           turtle.select(s)
  237.           turtle.transferTo(dedicatedSlot)
  238.           found = true
  239.           break
  240.         end
  241.       end
  242.     end
  243.     if not found then
  244.       return false
  245.     end
  246.   end
  247.  
  248.   -- Attempt to plant.
  249.   turtle.select(dedicatedSlot)
  250.   local finalItem = turtle.getItemDetail(dedicatedSlot)
  251.   if finalItem and finalItem.name == seedType and finalItem.count > 0 then
  252.     turtle.placeDown()
  253.     return true
  254.   else
  255.     return false
  256.   end
  257. end
  258.  
  259. --------------------------------------------------
  260. -- PLANT SEED WITH FALLBACK
  261. --------------------------------------------------
  262. local function plantSeedWithFallback(requestedBlock)
  263.   -- Try the requested crop first.
  264.   if attemptToPlant(requestedBlock) then
  265.     return
  266.   end
  267.  
  268.   local fallbackOrder = { "ars_nouveau:sourceberry_bush", "fruitsdelight:blueberry_bush", "hexerei:mandrake_flower", "hexerei:mandrake_flower", "hexerei:mugwort_bush", "hexerei:yellow_dock_bush", "minecraft:wheat", "minecraft:carrots", "minecraft:potatoes", "minecraft:beetroots" }
  269.   for _, fallbackBlock in ipairs(fallbackOrder) do
  270.     if fallbackBlock ~= requestedBlock then
  271.       if attemptToPlant(fallbackBlock) then
  272.         print("Planted fallback crop: " .. fallbackBlock)
  273.         return
  274.       end
  275.     end
  276.   end
  277.  
  278.   print("No viable seeds found; skipping planting.")
  279. end
  280.  
  281. --------------------------------------------------
  282. -- HELPER: CHECK IF CROP IS MATURE
  283. --------------------------------------------------
  284. local function isCropMature(blockName, age)
  285.   -- Maturity levels:
  286.   -- ars_nouveau:sourceberry_bush: 3, fruitsdelight:blueberry_bush: 4, hexerei:mandrake_flower: 3, hexerei:mugwort_bush: 3, hexerei:yellow_dock_bush: 3, wheat: 7, carrots: 8, potatoes: 8, beetroots: 3
  287.   if blockName == "ars_nouveau:sourceberry_bush" then
  288.     return age == 3
  289.   elseif blockName == "fruitsdelight:blueberry_bush" then
  290.     return age == 4
  291.   elseif blockName == "hexerei:mandrake_flower" then
  292.     return age == 3
  293.   elseif blockName == "hexerei:mugwort_bush" then
  294.     return age == 3
  295.   elseif blockName == "hexerei:yellow_dock_bush" then
  296.     return age == 3
  297.   elseif blockName == "minecraft:wheat" then
  298.     return age == 7
  299.   elseif blockName == "minecraft:carrots" then
  300.     return age == 7
  301.   elseif blockName == "minecraft:potatoes" then
  302.     return age == 7
  303.   elseif blockName == "minecraft:beetroots" then
  304.     return age == 3
  305.   end
  306.   return false
  307. end
  308.  
  309. --------------------------------------------------
  310. -- PLANT GROWTH CHECK ROUTINE
  311. --------------------------------------------------
  312. local function checkPlantGrowth()
  313.   -- Check two adjacent tiles.
  314.   while true do
  315.     turtle.turnLeft()
  316.     local success1, data1 = turtle.inspect()
  317.     local firstOk = true
  318.     if success1 then
  319.       if data1.name == "ars_nouveau:sourceberry_bush" or data1.name == "fruitsdelight:blueberry_bush" or data1.name == "hexerei:mandrake_flower" or data1.name == "hexeri:mugwort_bush" or data1.name == "hexerei:yellow_dock_bush" or data1.name == "minecraft:wheat" or data1.name == "minecraft:carrots" or data1.name == "minecraft:potatoes" or data1.name == "minecraft:beetroots" then
  320.         if not isCropMature(data1.name, data1.state.age) then
  321.           firstOk = false
  322.         end
  323.       end
  324.     end
  325.  
  326.     if not firstOk then
  327.       print("First adjacent crop not fully grown; waiting 5 minutes.")
  328.       turtle.turnRight()  -- Revert orientation.
  329.       sleep(300)
  330.     else
  331.       turtle.turnLeft()
  332.       local success2, data2 = turtle.inspect()
  333.       local secondOk = true
  334.       if success2 then
  335.       if data1.name == "ars_nouveau:sourceberry_bush" or data1.name == "fruitsdelight:blueberry_bush" or data1.name == "hexerei:mandrake_flower" or data1.name == "hexeri:mugwort_bush" or data1.name == "hexerei:yellow_dock_bush" or data1.name == "minecraft:wheat" or data1.name == "minecraft:carrots" or data1.name == "minecraft:potatoes" or data1.name == "minecraft:beetroots" then
  336.           if not isCropMature(data2.name, data2.state.age) then
  337.             secondOk = false
  338.           end
  339.         end
  340.       end
  341.  
  342.       if not secondOk then
  343.         print("Second adjacent crop not fully grown; waiting 5 minutes.")
  344.         turtle.turnRight()
  345.         turtle.turnRight()  -- Revert to original orientation.
  346.         sleep(300)
  347.       else
  348.         turtle.turnRight()  -- Return to original orientation.
  349.         turtle.turnRight()
  350.         break
  351.       end
  352.     end
  353.   end
  354. end
  355.  
  356. --------------------------------------------------
  357. -- MAIN FARMING PROCESS
  358. --------------------------------------------------
  359. local function mainFarmingProcess()
  360.   print("Starting main farming process.")
  361.   turtle.up()
  362.   turtle.turnLeft()
  363.   turtle.forward()
  364.  
  365.   local row = 1
  366.   local lastPlantedCrop = nil
  367.   while true do
  368.     print("Processing row " .. row)
  369.     while true do
  370.       local successDown, dataDown = turtle.inspectDown()
  371.       if successDown then
  372.         if dataDown.name == "minecraft:torch" then
  373.         elseif dataDown.name == "ars_nouveau:sourceberry_bush" or dataDown.name == "fruitsdelight:blueberry_bush" or dataDown.name == "hexerei:mandrake_flower" or dataDown.name == "hexeri:mugwort_bush" or dataDown.name == "hexerei:yellow_dock_bush" or dataDown.name == "minecraft:wheat" or dataDown.name == "minecraft:carrots" or dataDown.name == "minecraft:potatoes" or dataDown.name == "minecraft:beetroots" then
  374.           if isCropMature(dataDown.name, dataDown.state.age) then
  375.             if isInventoryFull() then
  376.               if dataDown.name == "minecraft:wheat" or dataDown.name == "minecraft:beetroots" or dataDown.name == "ars_nouveau:sourceberry_bush" or dataDown.name == "fruitsdelight:blueberry_bush" then
  377.                 organizeSeeds(dataDown.name)
  378.               end
  379.             end
  380.             turtle.digDown()  -- Harvest the mature crop.
  381.             lastPlantedCrop = dataDown.name
  382.             plantSeedWithFallback(dataDown.name)
  383.           else
  384.           end
  385.         else
  386.         end
  387.       else
  388.         if lastPlantedCrop then
  389.           plantSeedWithFallback(lastPlantedCrop)
  390.         else
  391.           plantSeedWithFallback("minecraft:wheat")
  392.         end
  393.       end
  394.  
  395.       local successFront, dataFront = turtle.inspect()
  396.       if successFront and (dataFront.name == "minecraft:glass" or dataFront.name == "minecraft:glass_pane") then
  397.          break  -- End of current row.
  398.       end
  399.       turtle.forward()
  400.     end
  401.  
  402.     if row % 2 == 1 then
  403.       turtle.turnLeft()
  404.       local successCheck, dataCheck = turtle.inspect()
  405.       if successCheck and (dataCheck.name == "minecraft:glass" or dataCheck.name == "minecraft:glass_pane") then
  406.         break
  407.       else
  408.         turtle.forward()
  409.         turtle.turnLeft()
  410.       end
  411.     else
  412.       turtle.turnRight()
  413.       local successCheck, dataCheck = turtle.inspect()
  414.       if successCheck and (dataCheck.name == "minecraft:glass" or dataCheck.name == "minecraft:glass_pane") then
  415.         break
  416.       else
  417.         turtle.forward()
  418.         turtle.turnRight()
  419.       end
  420.     end
  421.  
  422.     row = row + 1
  423.   end
  424.  
  425.   print("Main farming process complete.")
  426. end
  427.  
  428. --------------------------------------------------
  429. -- MAIN LOOP
  430. --------------------------------------------------
  431. while true do
  432.   print("Thank you for using Beni's Farm Script!")
  433.   positionTurtle()
  434.   fuelCheck()
  435.   depositOperations()
  436.   checkPlantGrowth()
  437.   mainFarmingProcess()
  438.   print("Thank you for using Beni's Farm Script!")
  439.   print("Cycle complete; repositioning...")
  440.   positionTurtle()
  441. end
  442.  
Add Comment
Please, Sign In to add comment