Advertisement
superjaja05

Untitled

Sep 13th, 2021
1,074
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 10.15 KB | None | 0 0
  1. local fuel = turtle.getFuelLevel()
  2. local fuelmax = turtle.getFuelLimit()
  3.  
  4. local args = {...}
  5.  
  6. if args[1] == "reset" then
  7.     fs.delete("/mconfig/hook.txt")
  8.     fs.delete("/mconfig/id.txt")
  9.     print("Successfully deleted config!")
  10.     os.sleep(1)
  11. end
  12. enablehook = false
  13. if fs.exists("/DiscordHook.lua") then
  14.     DH = require("DiscordHook")
  15. else
  16.     shell.run("wget https://raw.githubusercontent.com/Wendelstein7/DiscordHook-CC/master/DiscordHook.lua DiscordHook.lua")
  17.     DH = require("DiscordHook")
  18. end
  19.  
  20. if not fs.exists("/mconfig") then
  21.     fs.makeDir("/mconfig")
  22. end
  23.  
  24. if not fs.exists("/mconfig/hook.txt") then
  25.     config1 = fs.open("/mconfig/hook.txt", "w")
  26.     print("Please enter WEBHOOK url:\n(or 'false' to disable)")
  27.     input1 = io.read()
  28.     config1.write(input1)
  29.     config1.close()
  30. end
  31. if not fs.exists("/mconfig/id.txt") then
  32.     config2 = fs.open("/mconfig/id.txt", "w")
  33.     print("Please enter your discord ID:")
  34.     input2 = io.read()
  35.     config2.write(input2)
  36.     config2.close()
  37. end
  38.  
  39. if fs.exists("/mconfig/hook.txt") then
  40.     config1 = fs.open("/mconfig/hook.txt", "r")
  41.     hookURL = config1.readAll()
  42.     if hookURL == "false" then
  43.         enablehook = false
  44.     else
  45.         success, hook = DH.createWebhook(hookURL)
  46.         if not success then
  47.             error("Webhook connection failed! Reason: " .. hook)
  48.         end
  49.         hook.send("**Turtle Miner Hook successfully connected! ID:** "..os.getComputerID())
  50.         enablehook = true
  51.     end
  52. end
  53.  
  54. if fs.exists("/mconfig/id.txt") then
  55.     config2 = fs.open("/mconfig/id.txt", "r")
  56.     dcID = config2.readAll()
  57. end
  58.  
  59. logN = 1
  60.  
  61. if not fs.exists("/log") then
  62.     fs.makeDir("/log")
  63. end
  64.  
  65. if fs.exists("/log/old.txt") and fs.exists("/log/latest.txt") then
  66.     fs.delete("/log/old.txt")
  67.     fs.move("/log/latest.txt", "/log/old.txt")
  68. end
  69.  
  70. if fs.exists("/log/latest.txt") and not fs.exists("/log/old.txt") then
  71.     fs.move("/log/latest.txt", "/log/old.txt")
  72. end
  73.  
  74. if not fs.exists("/log/latest.txt") then
  75.     logfile = fs.open("/log/latest.txt", "w")
  76.     logfile.write("Log Files! \n")
  77.     logfile.close()
  78. end
  79.  
  80.  
  81. function log(x)
  82.     print("["..logN.."] "..x)
  83.     logfile = fs.open("/log/latest.txt", "a")
  84.     logfile.write("["..logN.."] "..x.."\n")
  85.     logfile.close()
  86.     if enablehook then
  87.         hook.send("["..logN.."] "..x, "Mining Turtle "..os.getComputerID())
  88.     end
  89.     logN = logN+1
  90. end
  91. function logcd(x)
  92.     print("["..logN.."] "..x)
  93.     logfile = fs.open("/log/latest.txt", "a")
  94.     logfile.write("["..logN.."] "..x.."\n")
  95.     logfile.close()
  96.     logN = logN+1
  97. end
  98.  
  99. function mineFront(x1)
  100.     i = 0
  101.     logcd("Mining forward: "..x1-i.." "..i1.."/8 Done")
  102.     repeat
  103.         turtle.dig()
  104.         turtle.digUp()
  105.         turtle.forward()
  106.         i = i+1
  107.         RX = RX+1
  108.         logcd("X:"..RX.." Y:"..RY.." Z:"..RZ)
  109.         os.sleep(0.25)
  110.     until i == x1
  111. end
  112.  
  113. function mineFront3(x1)
  114.     i = 0
  115.     logcd("Mining forward: "..x1-i.." "..i1.."/8 Done")
  116.     repeat
  117.         turtle.dig()
  118.         turtle.digUp()
  119.         turtle.forward()
  120.         i = i+1
  121.         RZ = RZ-1
  122.         logcd("X:"..RX.." Y:"..RY.." Z:"..RZ)
  123.         os.sleep(0.25)
  124.     until i == x1
  125. end
  126.  
  127. function mineFront2(x1)
  128.     i = 0
  129.     logcd("Mining forward (return): "..x1-i.." "..i1.."/8 Done")
  130.     repeat
  131.         turtle.dig()
  132.         turtle.digUp()
  133.         turtle.forward()
  134.         i = i+1
  135.         RX = RX-1
  136.         logcd("X:"..RX.." Y:"..RY.." Z:"..RZ)
  137.         os.sleep(0.25)
  138.     until i == x1
  139. end
  140.  
  141. function changeRow(x1)
  142.     logcd("Changing Row (1)")
  143.     turtle.turnRight()
  144.     turtle.dig()
  145.     turtle.digUp()
  146.     turtle.forward()
  147.     turtle.turnRight()
  148.     RZ = RZ+1
  149.     logcd("X:"..RX.." Y:"..RY.." Z:"..RZ)
  150. end
  151. function changeRow2(x1)
  152.     logcd("Changing Row (2)")
  153.     turtle.turnLeft()
  154.     turtle.dig()
  155.     turtle.digUp()
  156.     turtle.forward()
  157.     turtle.turnLeft()
  158.     RZ = RZ+1
  159.     logcd("X:"..RX.." Y:"..RY.." Z:"..RZ)
  160. end
  161. function emptyItems()
  162.     local iz = 0
  163.     if RZ ~= 0 then
  164.         log("Moving Z..")
  165.         turtle.turnLeft()
  166.         repeat
  167.             turtle.forward()
  168.             iz = iz+1
  169.             logcd(iz.."/"..RZ)
  170.         until iz == RZ
  171.         turtle.turnRight()
  172.     end
  173.     local ix = 0
  174.     if RX ~= 0 then
  175.         log("Moving X..")
  176.         repeat
  177.             turtle.back()
  178.             ix = ix+1
  179.             logcd(ix.."/"..RX)
  180.         until ix == RX
  181.     end
  182.     local iy = 0
  183.     if RY ~= 0 then
  184.         log("Moving Y..")
  185.         repeat
  186.             turtle.up()
  187.             iy = iy-1
  188.             logcd(iy.."/"..RY)
  189.         until iy == RY
  190.     end
  191.     turtle.turnLeft()
  192.     i3 = 1
  193.     repeat
  194.         turtle.select(i3)
  195.         turtle.drop()
  196.         i3 = i3+1
  197.     until i3 == 16
  198.     if turtle.getItemDetail(13) ~= nil then
  199.         log("---- Failed to empty inventory.. Stopping! ----")
  200.         os.reboot()
  201.     end
  202.  
  203.     turtle.turnLeft()
  204.     turtle.select(16)
  205.     turtle.suck()
  206.     turtle.refuel()
  207.     fuelslot = turtle.getItemDetail(16)
  208.     if fuelslot["name"] == "minecraft:bucket" then
  209.         turtle.turnRight()
  210.         turtle.drop()
  211.         turtle.turnLeft()
  212.     end
  213.     turtle.suck()
  214.     turtle.refuel()
  215.     fuelslot = turtle.getItemDetail(16)
  216.     if fuelslot["name"] == "minecraft:bucket" then
  217.         turtle.turnRight()
  218.         turtle.drop()
  219.         turtle.turnLeft()
  220.     end
  221.     turtle.turnRight()
  222.     turtle.turnRight()
  223.     turtle.select(1)
  224. end
  225. function returnHome()
  226.     local iz = 0
  227.     if RZ ~= 0 then
  228.         log("Moving Z..")
  229.         turtle.turnLeft()
  230.         repeat
  231.             turtle.forward()
  232.             iz = iz+1
  233.             logcd(iz.."/"..RZ)
  234.         until iz == RZ
  235.         turtle.turnRight()
  236.     end
  237.     local ix = 0
  238.     if RX ~= 0 then
  239.         log("Moving X..")
  240.         repeat
  241.             turtle.back()
  242.             ix = ix+1
  243.             logcd(ix.."/"..RX)
  244.         until ix == RX
  245.     end
  246.     local iy = 0
  247.     if RY ~= 0 then
  248.         log("Moving Y..")
  249.         repeat
  250.             turtle.up()
  251.             iy = iy-1
  252.             logcd(iy.."/"..RY)
  253.         until iy == RY
  254.     end
  255.     os.reboot()
  256. end
  257.  
  258. function returnWork()
  259.     local iy2 = 0
  260.     local ix2 = 0
  261.     local iz2 = 0
  262.     local RY2 = RY
  263.     if RY ~= 0 then
  264.         log("Moving Y..")
  265.         repeat
  266.             turtle.down()
  267.             iy2 = iy2-1
  268.             logcd(iy2.."/"..RY)
  269.         until iy2 == RY
  270.     end
  271.     if RX ~= 0 then
  272.         log("Moving X..")
  273.         repeat
  274.             turtle.forward()
  275.             ix2 = ix2+1
  276.             logcd(ix2.."/"..RX)
  277.         until ix2 == RX
  278.     end
  279.     if RZ ~= 0 then
  280.         log("Moving Z..")
  281.         turtle.turnRight()
  282.         repeat
  283.             turtle.forward()
  284.             iz2 = iz2+1
  285.             logcd(iz2.."/"..RZ)
  286.         until iz2 == RZ
  287.         turtle.turnLeft()
  288.     end
  289. end
  290.  
  291. RX = 0
  292. RY = 0
  293. RZ = 0
  294.  
  295. log("Turtle fuel is "..fuel.."/"..fuelmax.." (Refuel threshold is "..(fuelmax/16)..")")
  296. os.sleep(0.25)
  297.  
  298. if fuel < (fuelmax/1) then
  299.     turtle.select(16)
  300.     local fuelslot = turtle.getItemDetail(16)
  301.     if fuelslot ~= nil then
  302.     log("Item in Fuel Slot! : "..fuelslot["name"].." Attempting to refuel..")
  303.     turtle.refuel()
  304.     os.sleep(0.25)
  305.     local fuel = turtle.getFuelLevel()
  306.     log("Turtle fuel is now "..fuel.."/"..fuelmax)
  307.     end
  308. end
  309.  
  310. turtle.select(1)
  311.  
  312. os.sleep(0.25)
  313.  
  314. log("Engage Miner? (y/n)")
  315. result1 = io.read()
  316. log("User Input: "..result1)
  317. if result1 == "y" then
  318.     log("Starting!")
  319.     log("Y Offset: ")
  320.     result2 = io.read()
  321.     log("User Input: "..result2)
  322.     turtle.digDown()
  323.     turtle.down()
  324.     RY = RY-1
  325.     logcd("X:"..RX.." Y:"..RY.." Z:"..RZ)
  326.     offset1 = 0
  327.     if result2 ~= nil and tonumber(result2) ~= 0 then
  328.         log("Offsetting..")
  329.         if tonumber(result2) > 1 then
  330.             repeat
  331.                 turtle.digDown()
  332.                 turtle.down()
  333.                 offset1 = offset1+1
  334.                 RY = RY-1
  335.                 logcd("X:"..RX.." Y:"..RY.." Z:"..RZ)
  336.             until offset1 == tonumber(result2)-1
  337.         else
  338.             turtle.digDown()
  339.             turtle.down()
  340.         end
  341.         log("Offsetting Done!")
  342.     end
  343.     repeat
  344.         i1 = 0
  345.         repeat
  346.             if turtle.getItemDetail(13) ~= nil then
  347.                 log("-- Full inventory! returning home.. --")
  348.                 emptyItems()
  349.                 log("Return & Drop done! Returning to work..")
  350.                 returnWork()
  351.             end
  352.             if turtle.getFuelLevel() < fuelmax/16 then
  353.                 log("-- Low Fuel! Refueling enabled. --")
  354.                 emptyItems()
  355.                 fuel1 = turtle.getFuelLevel()
  356.                 log("Refueled! new fuel level: "..fuel1)
  357.                 os.sleep(1)
  358.                 if turtle.getFuelLevel() < fuelmax/16 then
  359.                     turtle.turnRight()
  360.                     if enablehook then
  361.                     hook.send("<@"..dcID.."> **Your turtle with ID:** __"..os.getComputerID().."__ **Ran out of fuel.**")
  362.                     end
  363.                     os.reboot()
  364.                 end
  365.                 returnWork()
  366.             end
  367.             mineFront(15)
  368.             changeRow(1)
  369.             mineFront2(15)
  370.             changeRow2()
  371.             i1 = i1+1
  372.             local inspect1, inspect2 = turtle.inspect()
  373.             if inspect2["name"] == "minecraft:bedrock" then
  374.                 log("--- Bedrock Detected! Returning Home ---")
  375.                 breakloop = true
  376.                 breakloop2 = true
  377.                 emptyItems()
  378.                 break
  379.             end
  380.         until i1 == 8 or breakloop == true
  381.         local inspect1, inspect2 = turtle.inspectDown()
  382.         if inspect2["name"] == "minecraft:bedrock" then
  383.             log("--- Bedrock Detected! Returning Home ---")
  384.             breakloop = true
  385.             breakloop2 = true
  386.             emptyItems()
  387.             break
  388.         end
  389.         log("---- Done one layer! Lowering.. ----".." Current Coords: X:"..RX.." Y:"..RY.." Z:"..RZ)
  390.         turtle.turnLeft()
  391.         mineFront3(16)
  392.         turtle.turnRight()
  393.         turtle.digDown()
  394.         turtle.down()
  395.         RY = RY-1
  396.         logcd("X:"..RX.." Y:"..RY.." Z:"..RZ)
  397.     until breakloop2 == true
  398. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement