Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- Program : farming
- --- Author : LightKnight51
- --- last modification : 19/04/2023
- --- Variables
- --- Functions
- local arg = { ... }
- -- Put chest up
- function PlaceChest()
- if MarquitoLuaUtils.BlockDetection("up", false) == "minecraft:bedrock" then
- MarquitoLuaUtils.Error("minecraft:bedrock")
- elseif string.find(MarquitoLuaUtils.BlockDetection("up", false),"chest") ~= nil then
- print("Chest already in place")
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Chest already in place")
- turtle.select(1)
- else
- chestId = MarquitoLuaUtils.FindIDContains("chest", false)
- if chestId ~= nil then
- turtle.select(chestId)
- turtle.placeUp()
- print("Chest has been placed")
- turtle.select(1)
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Chest has been placed")
- end
- end
- end
- -- Place water
- function PlaceWater()
- local waterBucketSlot = MarquitoLuaUtils.FindIDContains("water_bucket", false)
- if waterBucketSlot then
- turtle.select(waterBucketSlot)
- turtle.placeDown()
- else
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.ERROR, "Water bucket not found")
- end
- end
- -- Place farmland dirt
- function PlaceFarmDirt()
- local dirtFarmLandIsReady = false
- if not turtle.inspect() then
- local dirtSlot = MarquitoLuaUtils.FindIDContains("dirt", false)
- if dirtSlot then
- turtle.select(dirtSlot)
- turtle.place()
- turtle.dig()
- dirtFarmLandIsReady = true
- else
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.ERROR, "Dirt not found")
- end
- else
- local blockName = MarquitoLuaUtils.BlockDetection("front", false)
- if string.find(blockName, "dirt") then
- turtle.dig()
- dirtFarmLandIsReady = true
- elseif string.find(blockName, "farmland") then
- dirtFarmLandIsReady = true
- else
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.ERROR, "Dirt or farmland not found in front of the turtle")
- end
- end
- return dirtFarmLandIsReady
- end
- -- Use bone meal if we have to grow tree
- function UseBoneMeal()
- boneMealId = MarquitoLuaUtils.FindIDContains("bone_meal", false, false)
- if boneMealId ~= nil then
- turtle.select(boneMealId)
- turtle.place()
- end
- end
- -- Check if we can farm, return true if we can
- function CheckIfWeCanFarm()
- local hasBlock, data = turtle.inspect()
- return hasBlock and data.state.age == 7
- end
- -- Dig food, and get all items in front of the turtle
- function FarmFood()
- turtle.dig()
- turtle.select(1)
- turtle.suck()
- turtle.suck()
- local hasItemsToGet = turtle.suck()
- while hasItemsToGet do
- hasItemsToGet = turtle.suck()
- end
- end
- function LaunchFarming()
- -- When equal to 5, place items in chest
- local putIntoChest = 0
- -- The food we want to have
- local seeds = arg[1]
- if seeds == nil then
- seeds = "wheat_seeds"
- end
- local itemToCheck = seeds
- if string.find(seeds, "_seeds") then
- itemToCheck = MarquitoLuaUtils.Replace(itemToCheck, "_seeds")
- end
- print("Launch farming program")
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.INFO, "Launch farming program")
- sleep(1)
- -- Place Dirt
- if turtle.down() then
- -- Move down of one block
- if PlaceFarmDirt() then
- if turtle.up() then
- -- Place water
- PlaceWater()
- -- Depose the chest
- PlaceChest()
- -- Start use seeds and bone meal
- while true do
- if string.find(MarquitoLuaUtils.BlockDetection("front", false), itemToCheck) and CheckIfWeCanFarm() then
- -- Get food
- FarmFood()
- -- Store items in chest
- local excludingItems = {seeds, "coal", "blaze_rod", "bone_meal"}
- putIntoChest = putIntoChest + 1
- if putIntoChest == 5 then
- putIntoChest = 0
- MarquitoLuaUtils.DropAllItemsInChest(false, excludingItems)
- end
- elseif string.find(MarquitoLuaUtils.BlockDetection("front", false), itemToCheck) then
- UseBoneMeal()
- elseif MarquitoLuaUtils.BlockDetection("front", true) then
- local seedSlot = MarquitoLuaUtils.FindIDContains(seeds, false, false)
- -- Use seed
- if seedSlot ~= nil then
- turtle.select(seedSlot)
- if not turtle.place() then
- -- If we have dirt, use hoe to have farmland
- turtle.dig()
- end
- end
- end
- end
- else
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.ERROR, "Turtle cannot move up")
- end
- end
- else
- MarquitoLuaUtils.Log(MarquitoLuaUtils.LogLevel.ERROR, "Turtle cannot move down")
- end
- MarquitoLuaUtils.EndLog()
- end
- -- Program
- parallel.waitForAny(LaunchFarming, MarquitoLuaUtils.Refuel)
Advertisement
Add Comment
Please, Sign In to add comment