xX_Nobody_Xx

oak/birch/spruce Tree Farm

Apr 3rd, 2022 (edited)
789
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.14 KB | None | 0 0
  1. local sleepTime = 0
  2.  
  3. if #arg == 1 then
  4.     sleepTime = tonumber(arg[1])
  5.  
  6.     if sleepTime == 0 then
  7.         print("Please set the sleep time between each check.")
  8.         return
  9.     end
  10. else
  11.     print("There needs to be one correct argument (e.g. logger 5)")
  12.     return
  13. end
  14.  
  15. local WOODS = {
  16.     "minecraft:oak_log",
  17.     "minecraft:birch_log",
  18.     "minecraft:spruce_log"
  19. }
  20.  
  21. function cleanInspectDown()
  22.     local hasBlock, data = turtle.inspectDown()
  23.     local name = textutils.serialize(data.name)
  24.     local name2 = name:gsub("\"", "")
  25.     local val = string.sub(name2, 0, string.len(name) - 2)
  26.     return val
  27. end
  28.  
  29. function cleanInspectUp()
  30.     local hasBlock, data = turtle.inspectUp()
  31.     local name = textutils.serialize(data.name)
  32.     local name2 = name:gsub("\"", "")
  33.     local val = string.sub(name2, 0, string.len(name) - 2)
  34.     return val
  35. end
  36.  
  37. function cleanInspect()
  38.     local hasBlock, data = turtle.inspect()
  39.     local name = textutils.serialize(data.name)
  40.     local name2 = name:gsub("\"", "")
  41.     local val = string.sub(name2, 0, string.len(name) - 2)
  42.     return val
  43. end
  44.  
  45. function down()
  46.     local down = false
  47.     for x = 1, #WOODS do
  48.         if cleanInspectDown() == WOODS[x] then
  49.             down = true
  50.             return down
  51.         end
  52.     end
  53. end
  54.  
  55. function up()
  56.     local down = false
  57.     for x = 1, #WOODS do
  58.         if cleanInspectUp() == WOODS[x] then
  59.             down = true
  60.             return down
  61.         end
  62.     end
  63. end
  64.  
  65. local SAPLINGS = {
  66.     "minecraft:oak_sapling",
  67.     "minecraft:birch_sapling",
  68.     "minecraft:spruce_sapling"
  69. }
  70.  
  71. local INVENTORY_SIZE = 16
  72.  
  73. function plant()
  74.     turtle.select(1)
  75.     for i = 1, INVENTORY_SIZE do
  76.         turtle.select(i)
  77.         local currentItem = turtle.getItemDetail(i)
  78.         if currentItem ~= nil then
  79.             local isAcceptedItem = false
  80.             for x = 1, #SAPLINGS do
  81.                 if currentItem.name == SAPLINGS[x] then
  82.                     turtle.select(i)
  83.                     turtle.place()
  84.                     return
  85.                 end
  86.             end
  87.         end
  88.     end
  89. end
  90.  
  91. function log()
  92.     turtle.dig()
  93.     turtle.forward()
  94.     if down() then
  95.         local continue = true
  96.         local num = 0
  97.         repeat
  98.             turtle.digDown()
  99.             turtle.down()
  100.             if not down() then
  101.                 continue = false
  102.             end
  103.             num = num + 1
  104.         until(continue == false)
  105.         local a = 0
  106.         repeat
  107.             turtle.up()
  108.             a = a + 1
  109.         until(a == num)
  110.     end
  111.     local continue = true
  112.     local num = 0
  113.     repeat
  114.         turtle.digUp()
  115.         turtle.up()
  116.         if not up() then
  117.             continue = false
  118.         end
  119.         num = num + 1
  120.     until(continue == false)
  121.     local a = 0
  122.     repeat
  123.         turtle.down()
  124.         a = a + 1
  125.     until(a == num)
  126.     turtle.back()
  127.     plant()
  128. end
  129.  
  130. function logFront()
  131.     local down = false
  132.     for x = 1, #WOODS do
  133.         if cleanInspect() == WOODS[x] then
  134.             down = true
  135.             return down
  136.         end
  137.     end
  138. end
  139.  
  140. function saplingFront()
  141.     local down = false
  142.     for x = 1, #SAPLINGS do
  143.         if cleanInspect() == SAPLINGS[x] then
  144.             down = true
  145.             return down
  146.         end
  147.     end
  148. end
  149.  
  150. local FUELS = {
  151.     "minecraft:coal_block",
  152.     "minecraft:coal",
  153.     "minecraft:lava_bucket",
  154.     "minecraft:spruce_log",
  155.     "minecraft:oak_log",
  156.     "minecraft:birch_log"
  157. }
  158.  
  159. function refuel(slot_number)
  160.     print("[TURTLE] Refueling... ")
  161.     turtle.select(slot_number)
  162.     turtle.refuel()
  163.     print("[TURTLE] Nom. Nom. Nom.")
  164. end
  165.  
  166. -- Check the current fuel level
  167. function checkFuelLevel()
  168.     local requiredFuelLevel = math.ceil(500)
  169.     local currentFuelLevel = turtle.getFuelLevel()
  170.  
  171.     --print("[TURTLE] Current fuel level is: "..currentFuelLevel.." - Required: "..requiredFuelLevel)
  172.  
  173.     if currentFuelLevel < requiredFuelLevel then
  174.  
  175.         print("[TURTLE] Attempting to locate fuel.")
  176.  
  177.         for i = 1, INVENTORY_SIZE do
  178.             local currentItem = turtle.getItemDetail(i)
  179.             if currentItem ~= nil then
  180.                 for x = 1, #FUELS do
  181.                     if currentItem.name == FUELS[x] then
  182.                         print("[TURTLE] Acceptable fuel found: " ..FUELS[x])
  183.  
  184.                         if currentFuelLevel < requiredFuelLevel then
  185.                             refuel(i)
  186.                         else
  187.                             return true
  188.                         end
  189.                     end
  190.                 end
  191.             end
  192.         end
  193.         print("[TURTLE] No acceptable fuel or not enough found, terminating program...")
  194.         return false
  195.     else
  196.         return true
  197.     end
  198. end
  199.  
  200. function check()
  201.     if logFront() then
  202.         print("[TURTLE] Chopping!")
  203.         log()
  204.         checkFuelLevel()
  205.     elseif not saplingFront() then
  206.         print("[TURTLE] Planting!")
  207.         plant()
  208.         checkFuelLevel()
  209.     else
  210.         print("[TURTLE] Lurking!")
  211.         checkFuelLevel()
  212.     end
  213. end
  214.  
  215. while true do
  216.     print("Checking...")
  217.     check()
  218.     sleep(sleepTime)
  219. end
Add Comment
Please, Sign In to add comment