Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- local stringhelperLib = require("stringhelper")
- local exceptionsFile = "stripminingexceptions.txt"
- local torchPlaceDelay = 12 --How often should place torches
- local torchCount = 0
- local distance = -1 --Track the distance traveled by the turtle
- local running = true --Track wether the program should keep running
- --Check if pc is turtle
- if not turtle then
- term.setTextColor(colors.red)
- print("Cannot run program. This computer is not a turtle.")
- return
- end
- --Returns the exceptions list as a table or an empty table if no file is present
- local function getExceptionList()
- local file = fs.open(exceptionsFile, "r")
- if not file or stringhelper.isBlank(file.readAll()) then
- shell.run("StripMiningExceptions", "add") --Created the file if it doesn't exists or its empty
- end
- local file = fs.open(exceptionsFile, "r")
- local json = file.readAll()
- file.close()
- return textutils.unserialiseJSON(json)
- end
- --Drop item present in the exceptions file
- local function tryFreeInventorySpace()
- local exceptions = getExceptionList()
- for i = 2,16 do
- turtle.select(i)
- local itemInfo = turtle.getItemDetail()
- if not itemInfo or stringhelper.isInTable(itemInfo.name, exceptions) then
- turtle.dropDown()
- end
- end
- end
- --Check if inventory is present below turtle
- local function checkForInventoryBelow()
- local inventory = peripheral.wrap("bottom")
- if inventory ~= nil and peripheral.hasType(inventory, "inventory") then
- return true
- else
- return false
- end
- end
- local function tryBackToStart()
- for i = 2,16 do --If there is space in inventory do not go back
- turtle.select(i)
- if not turtle.getItemDetail() then
- turtle.select(16)
- turtle.transferTo(i)
- return
- end
- end
- for j = 0, distance do
- while not turtle.back() do --Handle falling blocks
- turtle.turnRight()
- turtle.turnRight()
- turtle.dig()
- turtle.turnLeft()
- turtle.turnLeft()
- end
- end
- turtle.down()
- if checkForInventoryBelow() then
- for g = 2, 16 do --Empty inventory
- turtle.select(g)
- turtle.refuel()
- turtle.dropDown()
- end
- turtle.up()
- for k = 0, distance do
- turtle.forward()
- end
- turtle.select(1)
- else
- running = false
- term.setTextColor(colors.red)
- term.setCursorPos(1, 6)
- term.write("No inventory present below!")
- term.setCursorPos(1, 7)
- end
- end
- local function mine()
- turtle.select(1)
- turtle.dig()
- while not turtle.forward() do --Handle falling blocks
- turtle.dig()
- end
- distance = distance + 1
- turtle.digUp()
- turtle.digDown()
- torchCount = torchCount + 1
- if turtle.detectDown() then
- turtle.digDown()
- end
- if torchCount == torchPlaceDelay then --Place a torch every 10 blocks
- turtle.placeDown()
- torchCount = 0
- end
- if turtle.getItemCount(16) ~= 0 then --Go back when inventory is full
- tryFreeInventorySpace()
- tryBackToStart()
- end
- end
- local fuel = turtle.getFuelLevel()
- local lenght = ""
- --Start program
- turtle.select(1)
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.white)
- print("Place the turtle on top of a chest")
- print("and insert the torches into slot 1.")
- term.setCursorPos(1, 3)
- term.setTextColor(colors.orange)
- term.write("Press ")
- term.setTextColor(colors.green)
- term.write("Enter ")
- term.setTextColor(colors.orange)
- term.write("to start or hold ")
- term.setTextColor(colors.green)
- term.write("Ctrl+T")
- term.setCursorPos(1, 4)
- term.setTextColor(colors.orange)
- term.write("to exit.")
- term.setCursorPos(1, 5)
- repeat
- local event, key = os.pullEvent("key")
- until key == keys.enter
- --Check torches in slot 1
- if turtle.getItemDetail(1) then
- local data = turtle.getItemDetail(1)
- local torches = { "minecraft:torch", "minecraft:soul_torch" }
- if not stringhelper.isInTable(data.name, torches) then
- term.setTextColor(colors.red)
- print("No torches on slot 1.")
- return
- end
- else
- term.setTextColor(colors.red)
- print("No torches on slot 1.")
- return
- end
- lenght = turtle.getItemCount(1) * torchPlaceDelay
- if fuel - lenght <= 0 then --Fuel check
- print("Not enough fuel.")
- return
- else
- term.setCursorPos(1, 5)
- term.setTextColor(colors.white)
- term.write("Tunneling...")
- turtle.digUp()
- turtle.up()
- turtle.digUp()
- repeat
- if running then
- mine()
- else
- return
- end
- until turtle.getItemCount(1) == 0
- tryFreeInventorySpace()
- for h = 0, distance do -- Go back to start
- while not turtle.back() do
- turtle.turnRight()
- turtle.turnRight()
- turtle.dig()
- turtle.turnLeft()
- turtle.turnLeft()
- end
- end
- turtle.down()
- if checkForInventoryBelow() then
- for m = 2, 16 do
- turtle.select(m)
- turtle.dropDown()
- end
- end
- term.setTextColor(colors.green)
- term.setCursorPos(1, 6)
- term.write("Done!")
- term.setCursorPos(1, 7)
- term.setTextColor(colors.white)
- distance = distance + 1
- term.write(distance .. " blocks mined.")
- term.setCursorPos(1, 8)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement