Peni2000

SimpleBeni'sFarm

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