Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --- Program : wood farming otimized 3.0
- --- Author : LightKnight51
- --- Last modification : 01/12/2020
- --- Variables
- local fuel = 0
- local bExit = false
- --- Functions
- -- Get the position in the turtle's inventory
- function selecionID(itemID)
- j = nil
- for i = 1, 16 do
- data = turtle.getItemDetail(i)
- if data then
- if data.name == itemID then
- j = i
- break
- elseif i == 16 and data.name ~= itemID then
- error(itemID)
- j = i
- end
- end
- end
- return j
- end
- -- Error handling
- function error(typeError)
- if typeError == "minecraft:bedrock" then
- print("Bedrock detectee lors du depot du coffre !")
- elseif typeError == "minecraft:chest" then
- print("Coffre introuvable dans l'inventaire !")
- elseif typeError == "minecraft:coal" then
- print("Charbon introuvable dans l'inventaire !")
- elseif typeError == "minecraft:oak_sapling" then
- print("Pousse de chêne introuvable dans l'inventaire !")
- elseif typeError == "minecraft:bone_meal" then
- print("Poudre d'os introuvable dans l'inventaire !")
- else
- print("L'erreur de type ", typeError, " est inconnue.")
- end
- print("Arret du systeme dans 3sec.")
- os.sleep(3)
- bExit = true
- end
- -- Return the name of a block, or just if it's a block or a fluid
- function blockDetection(position, justDetection)
- success, detectionBlock = nil
- block_name = ""
- if position == "right" then
- turtle.turnRight()
- success, detectionBlock = turtle.inspect()
- turtle.turnLeft()
- if success then
- block_name = recognizeLiquidAirBlocks(detectionBlock, justDetection)
- end
- elseif position == "left" then
- turtle.turnLeft()
- success, detectionBlock = turtle.inspect()
- turtle.turnRight()
- if success then
- block_name = recognizeLiquidAirBlocks(detectionBlock, justDetection)
- end
- elseif position == "bottom" then
- turtle.turnRight()
- turtle.turnRight()
- success, detectionBlock = turtle.inspect()
- turtle.turnRight()
- turtle.turnRight()
- if success then
- block_name = recognizeLiquidAirBlocks(detectionBlock, justDetection)
- end
- elseif position == "front" then
- success, detectionBlock = turtle.inspect()
- if success then
- block_name = recognizeLiquidAirBlocks(detectionBlock, justDetection)
- end
- elseif position == "down" then
- success, detectionBlock = turtle.inspectDown()
- if success then
- block_name = recognizeLiquidAirBlocks(detectionBlock, justDetection)
- end
- elseif position == "up" then
- success, detectionBlock = turtle.inspectUp()
- if success then
- block_name = recognizeLiquidAirBlocks(detectionBlock, justDetection)
- end
- else
- block_name = "minecraft:?"
- end
- return block_name
- --block_name = recognizeLiquidAirBlocks(detectionBlock, justDetection)
- end
- function recognizeLiquidAirBlocks(detectionBlock, justDetection)
- block_name = ""
- if justDetection == true then
- if detectionBlock.name == "minecraft:air" or detectionBlock.name == "mminecraft:flowing_water" or detectionBlock.name == "minecraft:water" or detectionBlock.name == "mminecraft:flowing_lava" or detectionBlock.name == "minecraft:lava" then
- block_name = true
- else
- block_name = false
- end
- elseif justDetection == false then
- block_name = detectionBlock.name
- end
- return block_name
- end
- function deposeChest()
- if blockDetection("down", false) == "minecraft:bedrock" then
- error("minecraft:bedrock")
- elseif blockDetection("down", false) == "minecraft:chest" then
- print("Coffre deja place !")
- turtle.select(1)
- turtle.digUp()
- else
- turtle.digDown()
- chestId = selecionID("minecraft:chest")
- if chestId ~= nil then
- turtle.select(chestId)
- turtle.placeDown()
- print("Coffre place !")
- turtle.digUp()
- end
- turtle.select(1)
- end
- end
- -- Refuel the turtle properly
- function refuel()
- fuel = turtle.getFuelLevel()
- if fuel < 200 then
- fuelId = selecionID("minecraft:coal")
- if fuelId ~= nil then
- turtle.select(fuelId)
- turtle.refuel(1)
- end
- end
- end
- -- Deposit items in the chest
- function deposeIntoChest()
- if blockDetection("down", false) == "minecraft:chest" then
- for m = 1, 16 do
- oakSaplingId = selecionID("minecraft:oak_sapling")
- coalId = selecionID("minecraft:coal")
- boneMealId = selecionID("minecraft:bone_meal")
- if oakSaplingId ~= nil and coalId ~= nil and boneMealId ~= nil then
- if oakSaplingId ~= m and coalId ~= m and boneMealId ~= m then
- turtle.select(m)
- turtle.dropDown()
- end
- end
- end
- print("Le contenu est depose dans le coffre situe sous la turtle.")
- turtle.select(1)
- else
- print("Il n'y a pas de coffre, le contenu reste dans la turtle.")
- end
- end
- -- Deposit items in the hopper
- function deposeIntoHopper()
- if blockDetection("down", false) == "minecraft:hopper" then
- for m = 1, 16 do
- oakSaplingId = selecionID("minecraft:oak_sapling")
- coalId = selecionID("minecraft:coal")
- boneMealId = selecionID("minecraft:bone_meal")
- if oakSaplingId ~= nil and coalId ~= nil and boneMealId ~= nil then
- if oakSaplingId ~= m and coalId ~= m and boneMealId ~= m then
- turtle.select(m)
- turtle.dropDown()
- end
- end
- end
- print("Le contenu est depose dans le hopper situe sous la turtle.")
- turtle.select(1)
- else
- print("Il n'y a pas de hopper, le contenu reste dans la turtle.")
- end
- end
- -- Dig wood, return in init prosition properly and execute deposeIntoChest function
- function farmTheWood()
- refuel()
- countHeight = 0
- while blockDetection("front", false) == "minecraft:oak_log" do
- turtle.dig()
- turtle.digUp()
- if turtle.up() ~= true then
- print("error")
- break
- end
- countHeight = countHeight + 1
- end
- if countHeight > 0 then
- for i = 0, countHeight do
- turtle.down()
- end
- end
- deposeIntoChest()
- --deposeIntoHopper()
- end
- function useBoneMeal()
- boneMealId = selecionID("minecraft:bone_meal")
- if boneMealId ~= nil then
- turtle.select(boneMealId)
- turtle.place()
- end
- end
- -- Exit the program
- function exit()
- bReturn = true
- return blockDetection("left", false) ~= "minecraft:redstone_block"
- end
- -- Read close broadcast
- function readBroadcast()
- while true do
- if testRednet() then
- senderId, message, protocol = rednet.receive("exitTurtleProgram")
- if message == "exit" then
- print("receive exit from a server !")
- rednetClose()
- break
- --bExit = true
- end
- end
- os.sleep(1)
- end
- end
- -- Close rednet
- function rednetClose()
- for _, side in pairs(rs.getSides()) do
- if peripheral.isPresent(side) and peripheral.getType(side) == "modem" and peripheral.call(side, "isWireless") then
- if rednet.isOpen(side) then
- rednet.close()
- break
- end
- end
- end
- end
- -- Test rednet wireless
- function testRednet()
- bOpen = false
- for _, side in pairs(rs.getSides()) do
- if peripheral.isPresent(side) and peripheral.getType(side) == "modem" and peripheral.call(side, "isWireless") then
- if rednet.isOpen(side) ~= true then
- rednet.open(side)
- break
- end
- bOpen = true
- end
- end
- return bOpen
- end
- -- Main program function
- function mainProgram()
- print("Lancement du programme de farming de bois")
- deposeChest()
- -- if verifyHopper() == false then
- -- break
- -- end
- while bExit ~= true do
- if blockDetection("front", false) == "minecraft:oak_log" then
- -- Let's dig the wood
- farmTheWood()
- elseif blockDetection("front", false) == "minecraft:oak_sapling" then
- useBoneMeal()
- elseif blockDetection("front", true) then
- idSapling = selecionID("minecraft:oak_sapling")
- if idSapling ~= nil then
- turtle.select(idSapling)
- turtle.place()
- end
- end
- os.sleep(1)
- end
- end
- --- Program
- -- Run two funtions at the same time
- parallel.waitForAny(mainProgram, readBroadcast)
- term.clear()
RAW Paste Data