kabluey

Farming Script

Apr 7th, 2021 (edited)
819
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --[[
  2.  
  3. Original Script Created by CEEBS
  4. Modified by Kabluey, only change was to work if no fuel required and reduce loop interval
  5. YouTube: https://www.youtube.com/channel/UCQvB8QBknoS1jYgwpYGQrQg
  6. Twitter: https://twitter.com/OnlyCeebs
  7.  
  8. Really appreciate all the support you have given me, so thank you!
  9.  
  10. ]]--
  11.  
  12. local INVENTORY_SIZE = 16
  13. local MAIN_LOOP_INTERVAL = 30
  14. local FUEL_CROP_CHEST_INTERVAL = 5
  15. local length = 0
  16. local rows = 0
  17.  
  18. -- Receive arguments and validate user input
  19. if #arg == 2 then
  20.     length = tonumber(arg[1])
  21.     rows = tonumber(arg[2])
  22.  
  23.     if length <= 1 or rows <= 1 then
  24.         print("Please enter values bigger than 1")
  25.         return
  26.     end
  27. else
  28.     print("Please enter both the length and with of the farm area. (e.g. scriptName 10 10)")
  29.     return
  30. end
  31.  
  32. -- List of accepted fuels
  33. local ACCEPTED_FUELS = {
  34.     "minecraft:coal_block",
  35.     "minecraft:coal"
  36. }
  37.  
  38. -- List of accepted seeds
  39. local SEEDS = {
  40.     "minecraft:carrot",
  41.     "minecraft:potato",
  42.     "minecraft:wheat_seeds"
  43. }
  44.  
  45. -- List of mature crops
  46. local CROPS = {
  47.     "minecraft:carrots",
  48.     "minecraft:potatoes",
  49.     "minecraft:wheat"
  50. }
  51.  
  52. -- Refuel using the found fuel
  53. function refuel(slot_number)
  54.     turtle.select(slot_number)
  55.     turtle.refuel()
  56. end
  57.  
  58. -- Check the current fuel level
  59. function checkFuelLevel()
  60.    
  61.     local currentFuelLevel = turtle.getFuelLevel()
  62.  
  63.     if (currentFuelLevel ~= "unlimited") then
  64.         if currentFuelLevel <= 0 then
  65.             print("Out of fuel, waiting for fuel to be added. Checking for fuel every ".. FUEL_CROP_CHEST_INTERVAL .." seconds.")
  66.             while currentFuelLevel <= 0 do
  67.                 for i = 1, INVENTORY_SIZE do
  68.                     local currentItem = turtle.getItemDetail(i)
  69.                     if currentItem ~= nil then
  70.                         for x = 1, #ACCEPTED_FUELS do
  71.                             if currentItem.name == ACCEPTED_FUELS[x] then
  72.                                 refuel(i)
  73.                             end
  74.                         end
  75.                     end
  76.                 end
  77.                 sleep(FUEL_CROP_CHEST_INTERVAL)
  78.                 currentFuelLevel = turtle.getFuelLevel()
  79.             end
  80.             print("Fuel added successfully, continuing...")
  81.         end
  82.     end
  83. end
  84.  
  85. -- Get the amount of seeds
  86. function getSeedsCount()
  87.     local seedsCount = 0
  88.  
  89.     for i = 1, INVENTORY_SIZE do
  90.         local currentItem = turtle.getItemDetail(i)
  91.         if currentItem ~= nil then
  92.             for x = 1, #SEEDS do
  93.                 if currentItem.name == SEEDS[x] then
  94.                     seedsCount = seedsCount + turtle.getItemCount(i)
  95.                 end
  96.             end
  97.         end
  98.     end
  99.  
  100.     return seedsCount
  101. end
  102.  
  103. -- Checking for seeds
  104. function checkSeedsCount()
  105.    
  106.     local seedsCount = getSeedsCount()
  107.  
  108.     if seedsCount <= 0 then
  109.         print("Out of seeds, waiting for seeds to be added. Checking for seeds every ".. FUEL_CROP_CHEST_INTERVAL .." seconds.")
  110.         while seedsCount <= 0 do
  111.             seedsCount = getSeedsCount()
  112.             sleep(FUEL_CROP_CHEST_INTERVAL)
  113.         end
  114.         print("Seeds added successfully, continuing...")
  115.     end
  116.  
  117. end
  118.  
  119. -- Movement helper functions
  120. function moveForward(times)
  121.  
  122.     checkFuelLevel()
  123.     checkSeedsCount()
  124.    
  125.     if times then
  126.         for i = 1, times do
  127.             turtle.forward()
  128.             turtle.suckDown()
  129.         end
  130.     else
  131.         turtle.forward()
  132.         turtle.suckDown()
  133.     end
  134. end
  135.  
  136. function turnLeft(times)
  137.     if times then
  138.         for i = 1, times do
  139.             turtle.turnLeft()
  140.         end
  141.     else
  142.         turtle.turnLeft()
  143.     end
  144. end
  145.  
  146. function turnRight(times)
  147.     if times then
  148.         for i = 1, times do
  149.             turtle.turnRight()
  150.         end
  151.     else
  152.         turtle.turnRight()
  153.     end
  154. end
  155.  
  156. -- Plant our crops!
  157. function plantCrops()
  158.     for i = 1, INVENTORY_SIZE do
  159.         local currentItem = turtle.getItemDetail(i)
  160.         if currentItem ~= nil then
  161.             for x = 1, #SEEDS do
  162.                 if currentItem.name == SEEDS[x] then
  163.                     turtle.select(i)
  164.                     turtle.placeDown()
  165.                     break
  166.                 end
  167.             end
  168.         end
  169.     end
  170. end
  171.  
  172. -- Harvest our crops!
  173. function harvestCrops()
  174.     local isBlock, block = turtle.inspectDown()
  175.  
  176.     if isBlock ~= false then
  177.         local isCrop = false
  178.  
  179.         for x = 1, #CROPS do
  180.             if block.name == CROPS[x] then
  181.                 isCrop = true
  182.             end
  183.         end
  184.  
  185.         if isCrop then
  186.             if block.state.age == 7 then
  187.                 turtle.digDown()
  188.                 turtle.suckDown()
  189.                 plantCrops()
  190.             end
  191.         else
  192.             if block.name == "minecraft:grass" then
  193.                 turtle.digDown()
  194.                 turtle.digDown()
  195.                 turtle.suckDown()
  196.                 plantCrops()
  197.             end
  198.         end
  199.                
  200.     else
  201.         turtle.digDown()
  202.         turtle.suckDown()
  203.         plantCrops()
  204.     end
  205. end
  206.  
  207. -- Inventory sorting
  208. function inventorySort()
  209.     for j = 1, INVENTORY_SIZE do
  210.         local currentItem = turtle.getItemDetail(j)
  211.  
  212.         if currentItem ~= nil then
  213.             turtle.select(j)
  214.             for k = j, INVENTORY_SIZE do
  215.                 if turtle.compareTo(k) then
  216.                     turtle.select(k)
  217.                     turtle.transferTo(j)
  218.                     turtle.select(j)
  219.                 end
  220.             end
  221.         end
  222.     end
  223. end
  224.  
  225. -- Dump inventory
  226. function dumpInventory()
  227.  
  228.     local cropCount = 0
  229.     local requiredSeedsCount = math.ceil(length * rows)
  230.  
  231.     for i = 1, INVENTORY_SIZE do
  232.  
  233.         local currentItem = turtle.getItemDetail(i)
  234.  
  235.         if currentItem ~= nil then
  236.  
  237.             local isFuel = false
  238.             local isCrop = false
  239.  
  240.             for x = 1, #ACCEPTED_FUELS do
  241.                 if currentItem.name == ACCEPTED_FUELS[x] then
  242.                     isFuel = true
  243.                     break
  244.                 end
  245.             end
  246.  
  247.             if not isFuel then
  248.                 for x = 1, #SEEDS do
  249.                     if currentItem.name == SEEDS[x] then
  250.                         if cropCount < requiredSeedsCount then
  251.                             cropCount = cropCount + currentItem.count
  252.                             isCrop = true
  253.                         else
  254.                             isCrop = false
  255.                         end
  256.                     end
  257.                 end
  258.             end
  259.  
  260.             if not isCrop and not isFuel then
  261.                 turtle.select(i)
  262.                 while turtle.dropDown() == false do
  263.                     print("No room in target inventory, please clear some space. Checking target inventory every ".. FUEL_CROP_CHEST_INTERVAL .." seconds.")
  264.                     sleep(FUEL_CROP_CHEST_INTERVAL)
  265.                 end
  266.             end
  267.         end
  268.     end
  269.  
  270.     inventorySort()
  271. end
  272.  
  273. -- Farming loop
  274. function farm()
  275.  
  276.     print("Starting farm loop.")
  277.  
  278.     for i = 1, rows do
  279.  
  280.         if i ~= 1 then
  281.             for j = 1, length - 1 do
  282.                 moveForward()
  283.                 harvestCrops()
  284.             end  
  285.         else
  286.             for j = 1, length do
  287.                 moveForward()
  288.                 harvestCrops()
  289.             end  
  290.         end  
  291.  
  292.         if i == rows then
  293.             if i % 2 == 0 then
  294.                 moveForward()
  295.                 turnRight()
  296.                 moveForward(rows - 1)
  297.                 turnRight()
  298.             else
  299.                 turnRight()
  300.                 moveForward()
  301.                 turnRight()
  302.                 moveForward(length)
  303.                 turnRight()
  304.                 moveForward(rows)
  305.                 turnRight()
  306.             end
  307.         else
  308.             if i % 2 == 0 then
  309.                 turnLeft()
  310.                 moveForward()
  311.                 harvestCrops()
  312.                 turnLeft()
  313.             else
  314.                 turnRight()
  315.                 moveForward()
  316.                 harvestCrops()
  317.                 turnRight()
  318.             end
  319.         end
  320.        
  321.     end
  322.  
  323.     dumpInventory()
  324. end
  325.  
  326. while true do
  327.     farm()
  328.     print("Completed loop, waiting " .. MAIN_LOOP_INTERVAL .. " seconds to start next loop.")
  329.     sleep(MAIN_LOOP_INTERVAL)
  330. end
  331.  
  332.  
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×