Advertisement
Ni_Jay_Ni

farm.lua

Jul 31st, 2022 (edited)
1,404
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.89 KB | None | 0 0
  1. --[[ FARM program v0.7 ]]--
  2. print("farm [# spaces forward] [# columns]")
  3.  
  4. local args = { ... }
  5. local x, y = 0, 0
  6. local alternating = false
  7. FARM_seedsInvSlotNum = 0
  8.  
  9. local function initFarming()
  10.     if args ~= nil and #args < 1 then
  11.         print("Please input how far forward the turtle should go...")
  12.         y = io.read()
  13.     else
  14.         y = args[1]
  15.     end
  16.  
  17.     if args ~= nil and #args < 2 then
  18.         print("Please input the number of columns there are...")
  19.         x = io.read()
  20.     else
  21.         x = args[2]
  22.     end
  23.  
  24.     if x == nil or y == nil then
  25.         return
  26.     end
  27.  
  28.  
  29.     print("You should place the turtle at the X.")
  30.     print("You should place a chest at the ☐.")
  31.     for rowsSample=0,y-1,1 do
  32.         textutils.slowPrint(string.rep('.',x))
  33.     end
  34.     print("☐X"..string.rep('.',x-2))
  35.     print("Press Y then Enter to continue.")
  36.     local confirmation = io.read()
  37.  
  38.     if confirmation ~= "Y" and confirmation ~= "y" then
  39.         print("Cancelled farming...")
  40.         return
  41.     end
  42. end
  43.  
  44. local function alternateTurn()
  45.     if alternating then
  46.         turtle.turnLeft()
  47.         turtle.forward()
  48.         turtle.turnLeft()
  49.     else
  50.         turtle.turnRight()
  51.         turtle.forward()
  52.         turtle.turnRight()
  53.     end
  54. end
  55.  
  56. local function plantCrop()
  57.     local previousSlot = turtle.getSelectedSlot()
  58.     if FARM_seedsInvSlotNum == 0 then
  59.         for invSlot=1,16,1 do
  60.             local data = turtle.getItemDetail(invSlot)
  61.             if data and data.name == "minecraft:wheat_seeds" then
  62.                 FARM_seedsInvSlotNum = invSlot
  63.                 turtle.select(invSlot)
  64.                 turtle.placeDown()
  65.             end
  66.         end
  67.     elseif FARM_seedsInvSlotNum >= 1 and FARM_seedsInvSlotNum <= 16 then
  68.         turtle.select(FARM_seedsInvSlotNum)
  69.         turtle.placeDown()
  70.     end
  71.     turtle.select(previousSlot)
  72. end
  73.  
  74. local function checkCrop()
  75.     local exists, data = turtle.inspectDown()
  76.     if exists and data.name == "minecraft:wheat" and data.metadata == 7 then
  77.         turtle.digDown()
  78.     end
  79. end
  80.  
  81. local function doFarming()
  82.     for ax=1,x,1 do
  83.         for ay=1,y,1 do
  84.             checkCrop()
  85.             plantCrop()
  86.             turtle.forward()
  87.         end
  88.         checkCrop()
  89.         plantCrop()
  90.         alternateTurn()
  91.         alternating = not alternating
  92.     end
  93. end
  94.  
  95. local function dumpAllInventory()
  96.     for invSlot=1,16,1 do
  97.         turtle.select(invSlot)
  98.         turtle.dropDown()
  99.     end
  100. end
  101.  
  102. local function goHome()
  103.     if alternating then        
  104.         for hy=1,y,1 do
  105.             turtle.forward()
  106.         end
  107.         turtle.turnRight()
  108.         for hx=1,x,1 do
  109.             turtle.forward()
  110.         end
  111.     else
  112.         turtle.turnLeft()
  113.         for hx=1,x,1 do
  114.             turtle.forward()
  115.         end
  116.     end
  117. end
  118.  
  119. initFarming()
  120. doFarming()
  121. goHome()
  122. dumpAllInventory()
  123. turtle.turnRight()
  124.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement