chopstyix

mine_v2

Jan 24th, 2022 (edited)
506
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.50 KB | None | 0 0
  1. local arg = {...}
  2.  
  3. -- Variables
  4. local userInput_distance = tonumber(arg[1])
  5. local userInput_nether = tonumber(arg[2])
  6. local userInput_debug = arg[3]
  7.  
  8. local torchSpacing = 7
  9. local ignoreFirstBlock = 1 -- Prevents turtle from attempting to place a torch at program launch
  10. local currentIteration = tonumber(userInput_distance)
  11. local direction = 1 -- 0: Left // 1: Forward // 2: Right // 3: Rear
  12. local height = 0 -- 0: Down // 1: Up
  13.  
  14. local readyToLaunch = false
  15.  
  16. local flag_Nether = false
  17.  
  18. function clearScreen()
  19.   term.clear()
  20.   term.setCursorPos(1,1)
  21. end
  22.  
  23. function lavaProtection()
  24.  local success, data = turtle.inspect()
  25.  if data.name == "minecraft:lava" then
  26.     turtle.select(13)
  27.     turtle.place()
  28.   end
  29. end
  30.  
  31. function findSortItem(item,slot)
  32.   local itemDetect
  33.   for i=1,16 do
  34.     itemDetect = turtle.getItemDetail(i)
  35.     if itemDetect then
  36.       print(itemDetect)
  37.       if(itemDetect.name == item) then
  38.         print("Item found in slot",i)
  39.         turtle.select(i)
  40.         turtle.transferTo(slot)
  41.       end
  42.     end
  43.   end
  44. end
  45.  
  46. function inventoryCheck() -- TO DO ORGANIZE THIS SHIT, ADD CHECKS FOR BLOCK AND SWAP
  47.     local torch = turtle.getItemDetail(16)
  48.     local chest = turtle.getItemDetail(15)
  49.     local block = turtle.getItemDetail(13)
  50.    
  51.     if (torch == nil) or (chest == nil) then
  52.       clearScreen()
  53.       print("I'm not prepared to mine!")
  54.       print("I need an Ender Chest in slot 15 and a Torch on slot 16.")
  55.       print("Press any key to continue.")
  56.       os.pullEvent("key")
  57.     elseif (torch.name == "minecraft:torch") and (chest.name == "enderstorage:ender_chest") then
  58.       readyToLaunch = true
  59.     else
  60.       clearScreen()  
  61.       print("I'm not prepared to mine!")
  62.       print("I need an Ender Chest in slot 15 and a Torch on slot 16.")
  63.       print("Press any key to continue.")
  64.       os.pullEvent("key")
  65.     end
  66.    
  67.     -- if flag_Nether == true) then
  68.       -- if blocks == nil then
  69.         -- clearScreen()
  70.         -- print("I'm not prepared to mine!")
  71.         -- print("I need Cobblestone on slot 13.")
  72.         -- print("Press any key to continue.")
  73.         -- os.pullEvent("key")
  74.         -- readyToLaunch = false
  75.       -- elseif (block.name) == "minecraft:cobblestone"
  76. end
  77.  
  78. function netherBlocks()
  79.     if (flag_Nether == true) then
  80.       if (height == 0) and (turtle.detectDown() == false) then
  81.         turtle.select(13)
  82.         turtle.placeDown()
  83.       elseif (height == 1) and (turtle.detectUp() == false) then
  84.         turtle.select(13)
  85.         turtle.placeUp()
  86.       end
  87.     end  
  88. end
  89.  
  90. function getBlockName()
  91.   local success,data = turtle.inspect()
  92.   return data.name
  93. end
  94.  
  95. function dig()
  96.   while turtle.detect() do
  97.     turtle.dig()
  98.   end
  99.   if (flag_Nether == true) and (direction ~= 1) then
  100.     sleep(.6)
  101.     lavaProtection()
  102.   end  
  103. end
  104.  
  105. function digUp()
  106.   while turtle.detectUp() do
  107.     turtle.digUp()
  108.   end
  109.   if (flag_Nether == true) and (direction ~= 1) then
  110.     sleep(.6)
  111.     lavaProtection()
  112.   end  
  113. end
  114.  
  115. function torchCheck()
  116.   if (ignoreFirstBlock == 1) and (height == 1) then
  117.     -- Do Nothing :)
  118.     ignoreFirstBlock = 0
  119.   elseif (currentIteration >= torchSpacing) and (height == 1) then
  120.     turtle.select(16)
  121.     turtle.place()
  122.     currentIteration = 1
  123.   end
  124. end
  125.  
  126. function switchHeight()
  127.   if height == 0 then
  128.     turtle.up()
  129.     height = 1
  130.   elseif height == 1 then
  131.     turtle.down()
  132.     height = 0
  133.   else
  134.     print("switchHeight error!")
  135.   end
  136. end
  137.  
  138. function unloadItems()
  139.   -- Rotate turtle to Rear
  140.   local originalDirection = direction
  141.   if (direction == 0) then
  142.     turtle.turnLeft()
  143.   elseif (direction == 1) then
  144.     turtle.turnLeft()
  145.     turtle.turnLeft()    
  146.   elseif (direction == 2) then
  147.     turtle.turnRight()
  148.   else
  149.     print("unloadItems error!")
  150.   end
  151.   direction = 3
  152.   -- turtle selects ender chest
  153.   turtle.select(15)
  154.   -- turtle place ender chest
  155.   turtle.place()
  156.   -- turtle loops placing items into chest
  157.   for i=1,12 do
  158.     turtle.select(i)
  159.     turtle.drop()
  160.   end
  161.   -- turtle selects inv for ender chest
  162.   turtle.select(15)
  163.   -- turtle mines
  164.   turtle.dig()
  165.   -- turtle faces original direction
  166.   if (originalDirection == 0) then
  167.     turtle.turnRight()
  168.     direction = 0
  169.   elseif (originalDirection == 1) then
  170.     turtle.turnRight()
  171.     turtle.turnRight()
  172.     direction = 1
  173.   elseif (originalDirection == 2) then
  174.     turtle.turnLeft()
  175.     direction = 2
  176.   else
  177.     print("unloadItems error in originalDirection!")
  178.   end
  179. end
  180.  
  181. function slice()
  182.   if (direction == 0) then
  183.     dig()
  184.     torchCheck()
  185.     turtle.turnRight()
  186.     direction = 1
  187.     dig()
  188.     turtle.turnRight()
  189.     direction = 2
  190.     dig()
  191.   elseif (direction == 1) then
  192.     turtle.turnLeft()
  193.     direction = 0
  194.     dig()
  195.     torchCheck()
  196.     turtle.turnRight()
  197.     direction = 1
  198.     dig()
  199.     turtle.turnRight()
  200.     dig()
  201.     direction = 2
  202.   elseif (direction == 2) then
  203.     dig()
  204.     turtle.turnLeft()
  205.     direction = 1
  206.     dig()
  207.     turtle.turnLeft()
  208.     direction = 0
  209.     dig()
  210.     torchCheck()
  211.   else
  212.     print("bruh")
  213.   end
  214.   if turtle.getItemCount(10) >= 1 then
  215.     unloadItems()
  216.   end
  217. end
  218.  
  219. -- END OF FUNCTIONS
  220.  
  221. -- if loopInput == nil then
  222.  -- loopInput = 1
  223. -- end
  224.  
  225. ----------------------
  226. -- MAIN CODE BEGINS --
  227. ----------------------
  228.  
  229. if userInput_nether == nil or userInput_nether == 0 then
  230.   flag_Nether = false
  231. elseif userInput_nether == 1 then
  232.   flag_Nether = true
  233. else
  234.   print("Invalid second argument. Please use either 0 or 1!")
  235. end
  236.  
  237. findSortItem("minecraft:cobblestone",13)
  238. findSortItem("minecraft:diamond_pickaxe",14)
  239. findSortItem("enderstorage:ender_chest",15)
  240. findSortItem("minecraft:torch",16)
  241.  
  242. -- Inventory Check on Launch
  243. while readyToLaunch == false do
  244.   inventoryCheck()
  245. end
  246.  
  247. if torchSpacing * turtle.getItemCount(16) < userInput_distance then
  248.   clearScreen()
  249.   print("I don't have enough torches to go that far!")
  250.   print("OVERRIDE: Distance value set to", torchSpacing * turtle.getItemCount(16), "blocks")
  251.   print("Press any key to continue")
  252.   os.pullEvent("key")
  253. end
  254.  
  255. -- Catch currentIteration if nil
  256. if userInput_distance == nil then
  257.   clearScreen()
  258.   print("Please input an additional argument for distance!")
  259. else
  260.   local pickaxe = turtle.getItemDetail(14)
  261.   if (pickaxe.name == "minecraft:diamond_pickaxe") then
  262.     shell.run("equip 14 right")
  263.   end
  264.   for i=1, userInput_distance do
  265.     clearScreen()
  266.     print("Fuel Level: ", turtle.getFuelLevel())
  267.     if (flag_Nether == true) then
  268.       term.setTextColor(colors.green)
  269.       print("Nether Mode Enabled")
  270.       term.setTextColor(colors.white)
  271.     else
  272.       term.setTextColor(colors.red)
  273.       print("Nether Mode Disabled")
  274.       term.setTextColor(colors.white)
  275.     end
  276.     -- Check if a torch is present in inventory
  277.     local torchSlot = turtle.getItemDetail(16)
  278.     if torchSlot == nil or torchSlot.name ~= "minecraft:torch" then
  279.       print("I'm out of torches! Exiting program...")
  280.       break
  281.     end  
  282.     -- dig left to right
  283.     slice()
  284.     -- move up
  285.     switchHeight()
  286.     netherBlocks()
  287.     -- dig right to left
  288.     slice()
  289.     -- turn towards front, move forward
  290.     if direction == 0 then
  291.       turtle.turnRight()
  292.       turtle.forward()
  293.     elseif direction == 1 then
  294.       turtle.turnLeft()
  295.       turtle.forward()
  296.     else
  297.       print("End of loop error!")
  298.     end
  299.     direction = 1
  300.     -- if in the nether do this thingy
  301.     netherBlocks()
  302.     currentIteration = currentIteration + 1
  303.   end
  304. end
  305. -- swap back
  306. shell.run("equip 14 right")
  307. print("Done!")
Advertisement
Add Comment
Please, Sign In to add comment