Jayex_Designs

harvest

Jun 9th, 2021 (edited)
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.81 KB | None | 0 0
  1. PotatoesCollected = 0
  2. SLOT_COUNT = 16
  3. NoFuel = false
  4.  
  5.  
  6.  
  7. function CheckFuel()
  8.     if(turtle.getFuelLevel() < 50) then
  9.         for slot = 1, SLOT_COUNT, 1 do
  10.             turtle.select(slot)
  11.             if(turtle.refuel()) then
  12.                 NoFuel = false
  13.                 return true
  14.             end
  15.         end
  16.         return false
  17.     else
  18.         NoFuel = false
  19.         return true
  20.     end
  21. end
  22.  
  23. function GetItemIndex(itemName)
  24.     for slot = 1, SLOT_COUNT, 1 do
  25.         local item = turtle.getItemDetail(slot)
  26.         if(item ~= nil) then
  27.             if(item["name"] == itemName) then
  28.                 return slot
  29.             end
  30.         end
  31.     end
  32. end
  33.  
  34.  
  35.  
  36. function DropPotatoes(potatoIndex)
  37.     local numPotatoes = turtle.getItemCount(potatoIndex) - 1
  38.  
  39.     if numPotatoes > 0 then
  40.         turtle.select(potatoIndex)
  41.         turtle.dropDown(numPotatoes)
  42.     end
  43.  
  44.     potatoIndex = GetItemIndex("minecraft:poisonous_potato")
  45.     if potatoIndex ~= nil then
  46.         turtle.select(potatoIndex)
  47.         turtle.dropDown()
  48.     end
  49.  
  50.     return numPotatoes
  51. end
  52.  
  53.  
  54.  
  55. function Succ()
  56.     for i = 1, 6, 1 do
  57.         turtle.suck()
  58.     end
  59. end
  60.  
  61.  
  62.  
  63. function CheckGrowth()
  64.     local isBlock, data = turtle.inspect()
  65.     if(isBlock) then
  66.         if (data['state']['age'] == 7) then
  67.             return true
  68.         end
  69.     else
  70.         return false
  71.     end
  72. end
  73.  
  74.  
  75.  
  76. function Harvest()
  77.     local isBlock, data = turtle.inspect()
  78.     if(CheckGrowth()) then
  79.         if (data['state']['age'] == 7) then
  80.             local potatoIndex = GetItemIndex("minecraft:potato")
  81.             turtle.select(potatoIndex)
  82.             turtle.dig()
  83.             turtle.place()
  84.             Succ()
  85.  
  86.             local numPotatoes = DropPotatoes(potatoIndex)
  87.             PotatoesCollected = PotatoesCollected + numPotatoes
  88.         end
  89.     else
  90.         Succ()
  91.         local potatoIndex = GetItemIndex("minecraft:potato")
  92.         turtle.select(potatoIndex)
  93.         turtle.place()
  94.     end
  95.  
  96.     Succ()
  97. end
  98.  
  99. function HarvestRow()
  100.     while true do
  101.         Harvest()
  102.         turtle.turnLeft()
  103.         if (turtle.detect()) then
  104.             turtle.turnLeft()
  105.             return
  106.         else
  107.             turtle.turnLeft()
  108.             Harvest()
  109.             turtle.turnRight()
  110.             turtle.forward()
  111.             turtle.turnRight()
  112.         end
  113.     end
  114. end
  115.  
  116.  
  117.  
  118. function WaitForGrowth()
  119.     while not CheckGrowth() do
  120.         sleep(5)
  121.     end
  122. end
  123.  
  124.  
  125.  
  126.  
  127.  
  128. print('Beginning Harvest...')
  129.  
  130. while true do
  131.     if (not CheckFuel()) then
  132.         if (not NoFuel) then
  133.             print("There is no fuel, I can't continue")
  134.             NoFuel = true
  135.         end
  136.         sleep(2)
  137.     else
  138.         WaitForGrowth()
  139.         HarvestRow()
  140.         print(PotatoesCollected .. ' potatoes harvested')
  141.     end
  142. end
Add Comment
Please, Sign In to add comment