enderpro100

wood farming

Nov 29th, 2020 (edited)
966
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --- Program : wood farming otimized 3.0
  2. --- Author : LightKnight51
  3. --- Last modification : 01/12/2020
  4.  
  5.  
  6.  
  7. --- Variables
  8.  
  9. local fuel = 0
  10.  
  11. local bExit = false
  12.  
  13. --- Functions
  14.  
  15. -- Get the position in the turtle's inventory
  16. function selecionID(itemID)
  17.     j = nil
  18.     for i = 1, 16 do
  19.  
  20.         data = turtle.getItemDetail(i)
  21.         if data then
  22.             if data.name == itemID then
  23.                 j = i
  24.                 break
  25.             elseif i == 16 and data.name ~= itemID then
  26.                 error(itemID)
  27.                 j = i
  28.             end
  29.         end
  30.     end
  31.     return j
  32. end
  33.  
  34. -- Error handling
  35. function error(typeError)
  36.     if typeError == "minecraft:bedrock" then
  37.         print("Bedrock detectee lors du depot du coffre !")
  38.     elseif typeError == "minecraft:chest" then
  39.         print("Coffre introuvable dans l'inventaire !")
  40.     elseif typeError == "minecraft:coal" then
  41.         print("Charbon introuvable dans l'inventaire !")
  42.     elseif typeError == "minecraft:oak_sapling" then
  43.         print("Pousse de chêne introuvable dans l'inventaire !")
  44.     elseif typeError == "minecraft:bone_meal" then
  45.         print("Poudre d'os introuvable dans l'inventaire !")
  46.     else
  47.         print("L'erreur de type ", typeError, " est inconnue.")
  48.     end
  49.     print("Arret du systeme dans 3sec.")
  50.     os.sleep(3)
  51.     bExit = true
  52. end
  53.  
  54. -- Return the name of a block, or just if it's a block or a fluid
  55. function blockDetection(position, justDetection)
  56.     success, detectionBlock = nil
  57.     block_name = ""
  58.     if position == "right" then
  59.         turtle.turnRight()
  60.         success, detectionBlock = turtle.inspect()
  61.         turtle.turnLeft()
  62.         if success then
  63.             block_name = recognizeLiquidAirBlocks(detectionBlock, justDetection)
  64.         end
  65.     elseif position == "left" then
  66.         turtle.turnLeft()
  67.         success, detectionBlock = turtle.inspect()
  68.         turtle.turnRight()
  69.         if success then
  70.             block_name = recognizeLiquidAirBlocks(detectionBlock, justDetection)
  71.         end
  72.     elseif position == "bottom" then
  73.         turtle.turnRight()
  74.         turtle.turnRight()
  75.         success, detectionBlock = turtle.inspect()
  76.         turtle.turnRight()
  77.         turtle.turnRight()
  78.         if success then
  79.             block_name = recognizeLiquidAirBlocks(detectionBlock, justDetection)
  80.         end
  81.     elseif position == "front" then
  82.         success, detectionBlock = turtle.inspect()
  83.         if success then
  84.             block_name = recognizeLiquidAirBlocks(detectionBlock, justDetection)
  85.         end
  86.     elseif position == "down" then
  87.         success, detectionBlock = turtle.inspectDown()
  88.         if success then
  89.             block_name = recognizeLiquidAirBlocks(detectionBlock, justDetection)
  90.         end
  91.     elseif position == "up" then
  92.         success, detectionBlock = turtle.inspectUp()
  93.         if success then
  94.             block_name = recognizeLiquidAirBlocks(detectionBlock, justDetection)
  95.         end
  96.     else
  97.         block_name = "minecraft:?"
  98.     end
  99.  
  100.     return block_name
  101.     --block_name = recognizeLiquidAirBlocks(detectionBlock, justDetection)
  102. end
  103.  
  104. function recognizeLiquidAirBlocks(detectionBlock, justDetection)
  105.     block_name = ""
  106.     if justDetection == true then
  107.         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
  108.             block_name = true
  109.         else
  110.             block_name = false
  111.         end
  112.     elseif justDetection == false then
  113.         block_name = detectionBlock.name
  114.     end
  115.     return block_name
  116. end
  117.  
  118. function deposeChest()
  119.     if blockDetection("down", false) == "minecraft:bedrock" then
  120.         error("minecraft:bedrock")
  121.     elseif blockDetection("down", false) == "minecraft:chest" then
  122.         print("Coffre deja place !")
  123.         turtle.select(1)
  124.         turtle.digUp()
  125.     else
  126.         turtle.digDown()
  127.         chestId = selecionID("minecraft:chest")
  128.         if chestId ~= nil then
  129.             turtle.select(chestId)
  130.             turtle.placeDown()
  131.             print("Coffre place !")
  132.             turtle.digUp()
  133.         end
  134.         turtle.select(1)
  135.     end
  136. end
  137.  
  138. -- Refuel the turtle properly
  139. function refuel()
  140.     fuel = turtle.getFuelLevel()
  141.     if fuel < 200 then
  142.         fuelId = selecionID("minecraft:coal")
  143.         if fuelId ~= nil then
  144.             turtle.select(fuelId)
  145.             turtle.refuel(1)
  146.         end
  147.     end
  148. end
  149.  
  150. -- Deposit items in the chest
  151. function deposeIntoChest()
  152.     if blockDetection("down", false) == "minecraft:chest" then
  153.         for m = 1, 16  do
  154.             oakSaplingId = selecionID("minecraft:oak_sapling")
  155.             coalId = selecionID("minecraft:coal")
  156.             boneMealId = selecionID("minecraft:bone_meal")
  157.             if oakSaplingId ~= nil and coalId ~= nil and boneMealId ~= nil then
  158.                 if oakSaplingId ~= m and coalId ~= m and boneMealId ~= m then
  159.                     turtle.select(m)
  160.                     turtle.dropDown()
  161.                 end
  162.             end
  163.         end
  164.         print("Le contenu est depose dans le coffre situe sous la turtle.")
  165.         turtle.select(1)
  166.     else
  167.         print("Il n'y a pas de coffre, le contenu reste dans la turtle.")
  168.     end
  169. end
  170.  
  171. -- Deposit items in the hopper
  172. function deposeIntoHopper()
  173.     if blockDetection("down", false) == "minecraft:hopper" then
  174.         for m = 1, 16  do
  175.             oakSaplingId = selecionID("minecraft:oak_sapling")
  176.             coalId = selecionID("minecraft:coal")
  177.             boneMealId = selecionID("minecraft:bone_meal")
  178.             if oakSaplingId ~= nil and coalId ~= nil and boneMealId ~= nil then
  179.                 if oakSaplingId ~= m and coalId ~= m and boneMealId ~= m then
  180.                     turtle.select(m)
  181.                     turtle.dropDown()
  182.                 end
  183.             end
  184.         end
  185.         print("Le contenu est depose dans le hopper situe sous la turtle.")
  186.         turtle.select(1)
  187.     else
  188.         print("Il n'y a pas de hopper, le contenu reste dans la turtle.")
  189.     end
  190. end
  191.  
  192. -- Dig wood, return in init prosition properly and execute deposeIntoChest function
  193. function farmTheWood()
  194.     refuel()
  195.     countHeight = 0
  196.     while blockDetection("front", false) == "minecraft:oak_log" do
  197.         turtle.dig()
  198.         turtle.digUp()
  199.         if turtle.up() ~= true then
  200.             print("error")
  201.             break
  202.         end
  203.         countHeight = countHeight + 1
  204.     end
  205.     if countHeight > 0 then
  206.         for i = 0, countHeight do
  207.             turtle.down()
  208.         end
  209.     end
  210.     deposeIntoChest()
  211.     --deposeIntoHopper()
  212. end
  213.  
  214. function useBoneMeal()
  215.     boneMealId = selecionID("minecraft:bone_meal")
  216.     if boneMealId ~= nil then
  217.         turtle.select(boneMealId)
  218.         turtle.place()
  219.     end
  220. end
  221.  
  222. -- Exit the program
  223. function exit()
  224.     bReturn = true
  225.     return blockDetection("left", false) ~= "minecraft:redstone_block"
  226. end
  227.  
  228. -- Read close broadcast
  229. function readBroadcast()
  230.     while true do
  231.         if testRednet() then
  232.             senderId, message, protocol = rednet.receive("exitTurtleProgram")
  233.             if message == "exit" then
  234.                 print("receive exit from a server !")
  235.                 rednetClose()
  236.                 break
  237.                 --bExit = true
  238.             end
  239.         end
  240.         os.sleep(1)
  241.     end
  242. end
  243.  
  244. -- Close rednet
  245.  
  246. function rednetClose()
  247.     for _, side in pairs(rs.getSides()) do
  248.         if peripheral.isPresent(side) and peripheral.getType(side) == "modem" and peripheral.call(side, "isWireless") then
  249.             if rednet.isOpen(side) then
  250.                 rednet.close()
  251.                 break
  252.             end
  253.         end
  254.     end
  255. end
  256.  
  257. -- Test rednet wireless
  258. function testRednet()
  259.     bOpen = false
  260.     for _, side in pairs(rs.getSides()) do
  261.         if peripheral.isPresent(side) and peripheral.getType(side) == "modem" and peripheral.call(side, "isWireless") then
  262.             if rednet.isOpen(side) ~= true then
  263.                 rednet.open(side)
  264.                 break
  265.             end
  266.             bOpen = true
  267.         end
  268.     end
  269.     return bOpen
  270. end
  271.  
  272. -- Main program function
  273. function mainProgram()
  274.     print("Lancement du programme de farming de bois")
  275.     deposeChest()
  276.     -- if verifyHopper() == false then
  277.     --     break
  278.     -- end
  279.     while bExit ~= true do
  280.         if blockDetection("front", false) == "minecraft:oak_log" then
  281.             -- Let's dig the wood
  282.             farmTheWood()
  283.         elseif blockDetection("front", false) == "minecraft:oak_sapling" then
  284.             useBoneMeal()
  285.         elseif blockDetection("front", true) then
  286.             idSapling = selecionID("minecraft:oak_sapling")
  287.             if idSapling ~= nil then
  288.                 turtle.select(idSapling)
  289.                 turtle.place()
  290.             end
  291.         end
  292.         os.sleep(1)
  293.     end
  294. end
  295.  
  296. --- Program
  297.  
  298. -- Run two funtions at the same time
  299. parallel.waitForAny(mainProgram, readBroadcast)
  300. term.clear()
RAW Paste Data