Advertisement
Miki_Tellurium

StripMining-main

Apr 20th, 2024 (edited)
592
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.06 KB | Gaming | 0 0
  1. local stringhelperLib = require("stringhelper")
  2. local exceptionsFile = "stripminingexceptions.txt"
  3.  
  4. local torchPlaceDelay = 12 --How often should place torches
  5. local torchCount = 0
  6. local distance = -1        --Track the distance traveled by the turtle
  7. local running = true       --Track wether the program should keep running
  8.  
  9. --Check if pc is turtle
  10. if not turtle then
  11.   term.setTextColor(colors.red)
  12.   print("Cannot run program. This computer is not a turtle.")
  13.   return
  14. end
  15. --Returns the exceptions list as a table or an empty table if no file is present
  16. local function getExceptionList()
  17.   local file = fs.open(exceptionsFile, "r")
  18.   if not file or stringhelper.isBlank(file.readAll()) then
  19.     shell.run("StripMiningExceptions", "add") --Created the file if it doesn't exists or its empty
  20.   end
  21.   local file = fs.open(exceptionsFile, "r")
  22.   local json = file.readAll()
  23.   file.close()
  24.   return textutils.unserialiseJSON(json)
  25. end
  26. --Drop item present in the exceptions file
  27. local function tryFreeInventorySpace()
  28.   local exceptions = getExceptionList()
  29.   for i = 2,16 do
  30.     turtle.select(i)
  31.     local itemInfo = turtle.getItemDetail()
  32.     if not itemInfo or stringhelper.isInTable(itemInfo.name, exceptions) then
  33.       turtle.dropDown()
  34.     end
  35.   end
  36. end
  37. --Check if inventory is present below turtle
  38. local function checkForInventoryBelow()
  39.   local inventory = peripheral.wrap("bottom")
  40.   if inventory ~= nil and peripheral.hasType(inventory, "inventory") then
  41.     return true
  42.   else
  43.     return false
  44.   end
  45. end
  46.  
  47. local function tryBackToStart()
  48.   for i = 2,16 do --If there is space in inventory do not go back
  49.     turtle.select(i)
  50.     if not turtle.getItemDetail() then
  51.       turtle.select(16)
  52.       turtle.transferTo(i)
  53.       return
  54.     end
  55.   end
  56.  
  57.   for j = 0, distance do
  58.     while not turtle.back() do --Handle falling blocks
  59.       turtle.turnRight()
  60.       turtle.turnRight()
  61.       turtle.dig()
  62.       turtle.turnLeft()
  63.       turtle.turnLeft()
  64.     end
  65.   end
  66.   turtle.down()
  67.   if checkForInventoryBelow() then
  68.     for g = 2, 16 do --Empty inventory
  69.       turtle.select(g)
  70.       turtle.refuel()
  71.       turtle.dropDown()
  72.     end
  73.     turtle.up()
  74.     for k = 0, distance do
  75.       turtle.forward()
  76.     end
  77.     turtle.select(1)
  78.   else
  79.     running = false
  80.     term.setTextColor(colors.red)
  81.     term.setCursorPos(1, 6)
  82.     term.write("No inventory present below!")
  83.     term.setCursorPos(1, 7)
  84.   end
  85. end
  86.  
  87. local function mine()
  88.   turtle.select(1)
  89.   turtle.dig()
  90.   while not turtle.forward() do --Handle falling blocks
  91.     turtle.dig()
  92.   end
  93.   distance = distance + 1
  94.   turtle.digUp()
  95.   turtle.digDown()
  96.   torchCount = torchCount + 1
  97.  
  98.   if turtle.detectDown() then
  99.     turtle.digDown()
  100.   end
  101.  
  102.   if torchCount == torchPlaceDelay then --Place a torch every 10 blocks
  103.     turtle.placeDown()
  104.     torchCount = 0
  105.   end
  106.  
  107.   if turtle.getItemCount(16) ~= 0 then --Go back when inventory is full
  108.     tryFreeInventorySpace()
  109.     tryBackToStart()
  110.   end
  111. end
  112.  
  113. local fuel = turtle.getFuelLevel()
  114. local lenght = ""
  115. --Start program
  116. turtle.select(1)
  117. term.clear()
  118. term.setCursorPos(1, 1)
  119. term.setTextColor(colors.white)
  120. print("Place the turtle on top of a chest")
  121. print("and insert the torches into slot 1.")
  122. term.setCursorPos(1, 3)
  123. term.setTextColor(colors.orange)
  124. term.write("Press ")
  125. term.setTextColor(colors.green)
  126. term.write("Enter ")
  127. term.setTextColor(colors.orange)
  128. term.write("to start or hold ")
  129. term.setTextColor(colors.green)
  130. term.write("Ctrl+T")
  131. term.setCursorPos(1, 4)
  132. term.setTextColor(colors.orange)
  133. term.write("to exit.")
  134. term.setCursorPos(1, 5)
  135. repeat
  136.   local event, key = os.pullEvent("key")
  137. until key == keys.enter
  138. --Check torches in slot 1
  139. if turtle.getItemDetail(1) then
  140.   local data = turtle.getItemDetail(1)
  141.   local torches = { "minecraft:torch", "minecraft:soul_torch" }
  142.  
  143.   if not stringhelper.isInTable(data.name, torches) then
  144.     term.setTextColor(colors.red)
  145.     print("No torches on slot 1.")
  146.     return
  147.   end
  148. else
  149.   term.setTextColor(colors.red)
  150.   print("No torches on slot 1.")
  151.   return
  152. end
  153.  
  154. lenght = turtle.getItemCount(1) * torchPlaceDelay
  155. if fuel - lenght <= 0 then --Fuel check
  156.   print("Not enough fuel.")
  157.   return
  158. else
  159.   term.setCursorPos(1, 5)
  160.   term.setTextColor(colors.white)
  161.   term.write("Tunneling...")
  162.   turtle.digUp()
  163.   turtle.up()
  164.   turtle.digUp()
  165.   repeat
  166.     if running then
  167.       mine()
  168.     else
  169.       return
  170.     end
  171.   until turtle.getItemCount(1) == 0
  172.   tryFreeInventorySpace()
  173.   for h = 0, distance do -- Go back to start
  174.     while not turtle.back() do
  175.       turtle.turnRight()
  176.       turtle.turnRight()
  177.       turtle.dig()
  178.       turtle.turnLeft()
  179.       turtle.turnLeft()
  180.     end
  181.   end
  182.   turtle.down()
  183.   if checkForInventoryBelow() then
  184.     for m = 2, 16 do
  185.       turtle.select(m)
  186.       turtle.dropDown()
  187.     end
  188.   end
  189.   term.setTextColor(colors.green)
  190.   term.setCursorPos(1, 6)
  191.   term.write("Done!")
  192.   term.setCursorPos(1, 7)
  193.   term.setTextColor(colors.white)
  194.   distance = distance + 1
  195.   term.write(distance .. " blocks mined.")
  196.   term.setCursorPos(1, 8)
  197. end
  198.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement