Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- ------------------------------ Block Breaker ----------------------------- --
- local settingsId = ".settings.breaker." .. os.getComputerID()
- local settingsKey = "blockBreaker"
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.green)
- print("==== Block Breaker ====")
- -- helper functions
- local function readInput(text)
- term.setTextColor(colors.white)
- print(text)
- write("> ")
- return read()
- end
- -- initialize variables
- local digDir, blockName, delay
- local isUsingSettings
- -- load settings
- settings.load(settingsId)
- local savedSettings = settings.get(settingsKey)
- -- handle exising settings
- if savedSettings then
- local function readShouldUseSettings()
- readInput(
- "Settings found. Do you want to reset the settings? If so, press ENTER")
- isUsingSettings = false
- end
- local function settingsCountDown()
- term.setTextColor(colors.orange)
- print("")
- print("The settings will be used automatically")
- for i = 5, 1, -1 do
- local cursorX, cursorY = term.getCursorPos()
- term.clearLine()
- term.setCursorPos(1, cursorY)
- write("in " .. i .. " seconds")
- sleep(1)
- end
- print("")
- isUsingSettings = true
- end
- parallel.waitForAny(readShouldUseSettings, settingsCountDown)
- if isUsingSettings then
- digDir = savedSettings.digDir
- blockName = savedSettings.blockName
- delay = savedSettings.delay
- end
- end
- -- prompt info
- term.setTextColor(colors.blue)
- print(
- "You may place a block at the selected direction first before running the programming so only that block will get broken")
- print("")
- -- select break direction
- local digFunc, inspectFunc
- repeat
- if not digDir then
- digDir = readInput("Input the direction to break (front/up/down, default: front)")
- end
- if (digDir == "front" or digDir == "" or digDir == nil) then
- digDir = 'front'
- digFunc = turtle.dig
- inspectFunc = turtle.inspect
- elseif (digDir == "up") then
- digFunc = turtle.digUp
- inspectFunc = turtle.inspectUp
- elseif (digDir == "down") then
- digFunc = turtle.digDown
- inspectFunc = turtle.inspectDown
- else
- digDir = nil;
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.red)
- print("Invliad value, only 'front', 'up', and 'down' are allowed")
- end
- until digFunc ~= nil
- -- define dig function
- local dig = function()
- local success = digFunc()
- if not success then
- sleep(1)
- end
- end
- -- Read block data at select face
- if not isUsingSettings then
- local hasBlock, blockData = inspectFunc()
- if hasBlock then
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.blue)
- print("Block detected: " .. blockData.name)
- local targetBreakValue = readInput("Should I only break this block? (y/n, default: y)")
- local shouldTargetBreak = targetBreakValue == "y" or targetBreakValue == "Y" or targetBreakValue == "" or
- targetBreakValue == nil
- if shouldTargetBreak then
- blockName = blockData.name
- end
- end
- end
- if blockName then
- if not isUsingSettings then
- local delayInStr = readInput("Input break delay in seconds if any")
- delay = tonumber(delayInStr)
- end
- if delay and delay <= 0 then
- delay = nil
- end
- -- redefine dig
- local tempDig = dig
- dig = function()
- local hasBlock, data = inspectFunc()
- if hasBlock and data.name == blockName then
- if delay then sleep(delay) end
- tempDig()
- end
- end
- end
- -- store settings
- settings.define(settingsKey)
- settings.set(settingsKey, {
- digDir = digDir,
- blockName = blockName,
- delay = delay
- })
- settings.save(settingsId)
- -- info
- term.clear()
- term.setCursorPos(1, 1)
- term.setTextColor(colors.green)
- print("==== Block Breaker ====")
- term.setTextColor(colors.blue)
- print("Direction: " .. digDir)
- if delay then
- print("Delay: " .. delay)
- end
- term.setTextColor(colors.cyan)
- if blockName then
- print("Now breaking: " .. blockName)
- else
- print("Now breaking: everything")
- end
- while true do
- dig()
- end
Advertisement
Add Comment
Please, Sign In to add comment