enderpro100

Farming

Apr 19th, 2023 (edited)
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.41 KB | Gaming | 0 0
  1. --- Program : farming
  2. --- Author : LightKnight51
  3. --- last modification : 19/04/2023
  4.  
  5.  
  6. --- Variables
  7.    
  8.  
  9. --- Functions
  10.  
  11. local arg = { ... }
  12.  
  13.  
  14. -- Put chest up
  15. function PlaceChest()
  16.     if MarquitoLuaUtils.BlockDetection("up", false) == "minecraft:bedrock" then
  17.         MarquitoLuaUtils.Error("minecraft:bedrock")
  18.     elseif string.find(MarquitoLuaUtils.BlockDetection("up", false),"chest") ~= nil then
  19.         print("Chest already in place")
  20.         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Chest already in place")
  21.         turtle.select(1)
  22.     else
  23.         chestId = MarquitoLuaUtils.FindIDContains("chest", false)
  24.         if chestId ~= nil then
  25.             turtle.select(chestId)
  26.             turtle.placeUp()
  27.             print("Chest has been placed")
  28.             turtle.select(1)
  29.             MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Chest has been placed")
  30.         end
  31.     end
  32. end
  33.  
  34. -- Place water
  35. function PlaceWater()
  36.     local waterBucketSlot = MarquitoLuaUtils.FindIDContains("water_bucket", false)
  37.  
  38.     if waterBucketSlot then
  39.         turtle.select(waterBucketSlot)
  40.         turtle.placeDown()
  41.     else
  42.         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.ERROR, "Water bucket not found")
  43.     end
  44. end
  45.  
  46. -- Place farmland dirt
  47. function PlaceFarmDirt()
  48.     local dirtFarmLandIsReady = false
  49.  
  50.     if not turtle.inspect() then
  51.         local dirtSlot = MarquitoLuaUtils.FindIDContains("dirt", false)
  52.         if dirtSlot then
  53.             turtle.select(dirtSlot)
  54.             turtle.place()
  55.             turtle.dig()
  56.             dirtFarmLandIsReady = true
  57.         else
  58.             MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.ERROR, "Dirt not found")
  59.         end
  60.     else
  61.         local blockName = MarquitoLuaUtils.BlockDetection("front", false)
  62.         if string.find(blockName, "dirt") then
  63.             turtle.dig()
  64.             dirtFarmLandIsReady = true
  65.         elseif string.find(blockName, "farmland") then
  66.             dirtFarmLandIsReady = true
  67.         else
  68.             MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.ERROR, "Dirt or farmland not found in front of the turtle")
  69.         end
  70.     end
  71.  
  72.     return dirtFarmLandIsReady
  73. end
  74.  
  75. -- Use bone meal if we have to grow tree
  76. function UseBoneMeal()
  77.     boneMealId = MarquitoLuaUtils.FindIDContains("bone_meal", false, false)
  78.     if boneMealId ~= nil then
  79.         turtle.select(boneMealId)
  80.         turtle.place()
  81.     end
  82. end
  83.  
  84. -- Check if we can farm, return true if we can
  85. function CheckIfWeCanFarm()
  86.     local hasBlock, data = turtle.inspect()
  87.  
  88.     return hasBlock and data.state.age == 7
  89. end
  90.  
  91. -- Dig food, and get all items in front of the turtle
  92. function FarmFood()
  93.     turtle.dig()
  94.     turtle.select(1)
  95.     turtle.suck()
  96.     turtle.suck()
  97.     local hasItemsToGet = turtle.suck()
  98.  
  99.     while hasItemsToGet do
  100.         hasItemsToGet = turtle.suck()
  101.     end
  102. end
  103.  
  104. function LaunchFarming()
  105.     -- When equal to 5, place items in chest
  106.     local putIntoChest = 0
  107.     -- The food we want to have
  108.     local seeds = arg[1]
  109.  
  110.     if seeds == nil then
  111.         seeds = "wheat_seeds"
  112.     end
  113.  
  114.     local itemToCheck = seeds
  115.  
  116.     if string.find(seeds, "_seeds") then
  117.         itemToCheck = MarquitoLuaUtils.Replace(itemToCheck, "_seeds")
  118.     end
  119.  
  120.     print("Launch farming program")
  121.     MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Launch farming program")
  122.     sleep(1)
  123.     -- Place Dirt
  124.     if turtle.down() then
  125.         -- Move down of one block
  126.         if PlaceFarmDirt() then
  127.             if turtle.up() then
  128.                 -- Place water
  129.                 PlaceWater()
  130.                 -- Depose the chest
  131.                 PlaceChest()
  132.                 -- Start use seeds and bone meal
  133.                 while true do
  134.                     if string.find(MarquitoLuaUtils.BlockDetection("front", false), itemToCheck) and CheckIfWeCanFarm() then
  135.                         -- Get food
  136.                         FarmFood()
  137.                         -- Store items in chest
  138.                         local excludingItems = {seeds, "coal", "blaze_rod", "bone_meal"}
  139.                         putIntoChest = putIntoChest + 1
  140.                         if putIntoChest == 5 then
  141.                             putIntoChest = 0
  142.                             MarquitoLuaUtils.DropAllItemsInChest(false, excludingItems)
  143.                         end
  144.                     elseif string.find(MarquitoLuaUtils.BlockDetection("front", false), itemToCheck) then
  145.                         UseBoneMeal()
  146.                     elseif MarquitoLuaUtils.BlockDetection("front", true) then
  147.                         local seedSlot = MarquitoLuaUtils.FindIDContains(seeds, false, false)
  148.                         -- Use seed
  149.                         if seedSlot ~= nil then
  150.                             turtle.select(seedSlot)
  151.                             if not turtle.place() then
  152.                                 -- If we have dirt, use hoe to have farmland
  153.                                 turtle.dig()
  154.                             end
  155.                         end
  156.                     end
  157.                 end
  158.             else
  159.                 MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.ERROR, "Turtle cannot move up")
  160.             end
  161.         end
  162.     else
  163.         MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.ERROR, "Turtle cannot move down")
  164.     end
  165.     MarquitoLuaUtils.EndLog()
  166. end
  167.  
  168. -- Program
  169. parallel.waitForAny(LaunchFarming, MarquitoLuaUtils.Refuel)
Advertisement
Add Comment
Please, Sign In to add comment