Advertisement
nathmo

Untitled

Apr 3rd, 2024 (edited)
566
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.20 KB | Gaming | 0 0
  1. -- Configuration
  2. local saplingSlot = 1
  3. local fuelSlot = 2 -- (use stick)
  4. local woodSlot = 3
  5. local farmlenght = 10 -- must be even
  6. local farmwidth = 10  
  7. local fuelScannedItem = ""
  8. local woodScannedItem = ""
  9. local sapplingScannedItem = ""
  10.  
  11. local function findItem(itemName)
  12.     for slot = 1, 16 do
  13.         local itemDetail = turtle.getItemDetail(slot)
  14.         if itemDetail and itemDetail.name == itemName then
  15.             return slot
  16.         end
  17.     end
  18.     return -1
  19. end
  20.  
  21. -- Function to refuel the turtle
  22. local function refuel()
  23.     if turtle.getFuelLevel() == "unlimited" or turtle.getFuelLevel() > 100 then
  24.         return
  25.     end
  26.     repeat
  27.         local slotNumber = findItem(fuelScannedItem)
  28.         if slotNumber ~= -1 then
  29.             turtle.select(slotNumber)
  30.             if turtle.refuel(0) then
  31.                 turtle.refuel(1)
  32.             else
  33.                 print("the sample provided is not fuel")
  34.             end
  35.         else
  36.             print("no fuel")
  37.             print("insert required fuel then press any key to start...")
  38.             os.pullEvent("key")
  39.         end
  40.     until slotNumber ~= -1
  41. end
  42.  
  43. -- Function to check if the block in front is wood
  44. local function isWoodAhead()
  45.     local success, data = turtle.inspect()
  46.     if success then
  47.         -- Check if the block in front matches the wood slot item
  48.         if data.name == woodScannedItem then
  49.             return true
  50.         end
  51.     end
  52.     return false
  53. end
  54.  
  55.  
  56. -- Function to check if the block on top is wood
  57. local function isWoodUp()
  58.     local success, data = turtle.inspectUp()
  59.     if success then
  60.         -- Check if the block in front matches the wood slot item
  61.         if data.name == woodScannedItem then
  62.             return true
  63.         end
  64.     end
  65.     return false
  66. end
  67.  
  68. -- Function to go up until there is no more wood
  69. local function goUpUntilNoWood()
  70.     while isWoodUp() do
  71.         turtle.digUp()
  72.         turtle.up()
  73.         refuel()
  74.     end
  75.     while not turtle.detectDown() do
  76.         turtle.down()
  77.     end
  78.     turtle.digDown()
  79. end
  80.  
  81. -- Function to plant sapling
  82. local function plantSapling()
  83.     local slotNumber = findItem(sapplingScannedItem)
  84.     if slotNumber ~= -1 then
  85.         turtle.select(slotNumber)
  86.         turtle.placeDown()
  87.     else
  88.         print("no sappling, skipping")
  89.     end
  90. end
  91.  
  92. -- Function to unload inventory
  93. local function unloadInventory()
  94.     for i = 4, 16 do
  95.         turtle.select(i)
  96.         turtle.drop()
  97.     end
  98. end
  99.  
  100. -- Function to ensure the path is clear of wood or leaves
  101. local function clearpath()
  102.     if isWoodAhead() then
  103.         turtle.dig()
  104.         turtle.forward()
  105.         goUpUntilNoWood()
  106.         plantSapling()
  107.     else
  108.         turtle.dig()
  109.         turtle.forward()
  110.     end
  111.     turtle.suckDown()
  112. end
  113.  
  114. -- Main function
  115. local function main()
  116.     print("Slot    | 1              | 2           | 3           |  4   |")
  117.     print("Content | Sapling sample | Fuel sample | Wood sample | Fuel |")
  118.     print("Press any key to start...")
  119.     os.pullEvent("key")
  120.     fuelScannedItem = turtle.getItemDetail(fuelSlot).name
  121.     woodScannedItem = turtle.getItemDetail(woodSlot).name
  122.     sapplingScannedItem = turtle.getItemDetail(saplingSlot).name
  123.     print("fuel : "..fuelScannedItem)
  124.     print("wood : "..woodScannedItem)
  125.     print("sappling : "..sapplingScannedItem)
  126.     print("Running")
  127.     while true do
  128.         -- Start zigzag pattern
  129.         for i = 1, (farmlenght) do
  130.             for j = 1, (farmwidth-1) do
  131.                 refuel()
  132.                 clearpath()
  133.             end
  134.             -- Turn at end of row
  135.             if i < farmlenght then
  136.                 if i % 2 == 1 then
  137.                     turtle.turnRight()
  138.                     clearpath()
  139.                     turtle.turnRight()
  140.                 else
  141.                     turtle.turnLeft()
  142.                     clearpath()
  143.                     turtle.turnLeft()
  144.                 end
  145.             end
  146.         end
  147.         -- Return to start
  148.         turtle.turnRight()
  149.         for i = 1, (farmlenght-1) do
  150.             clearpath()
  151.         end
  152.         -- Unload inventory
  153.         unloadInventory() -- face the hopper
  154.         turtle.turnRight()
  155.     end
  156. end
  157.  
  158. main()
  159.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement