willisshek

BlockBreaker

Aug 30th, 2024 (edited)
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.05 KB | Gaming | 0 0
  1. -- ------------------------------ Block Breaker ----------------------------- --
  2.  
  3. local settingsId = ".settings.breaker." .. os.getComputerID()
  4. local settingsKey = "blockBreaker"
  5.  
  6. term.clear()
  7. term.setCursorPos(1, 1)
  8.  
  9. term.setTextColor(colors.green)
  10. print("==== Block Breaker ====")
  11.  
  12. -- helper functions
  13. local function readInput(text)
  14.   term.setTextColor(colors.white)
  15.   print(text)
  16.   write("> ")
  17.   return read()
  18. end
  19.  
  20. -- initialize variables
  21. local digDir, blockName, delay
  22. local isUsingSettings
  23.  
  24. -- load settings
  25. settings.load(settingsId)
  26. local savedSettings = settings.get(settingsKey)
  27.  
  28. -- handle exising settings
  29. if savedSettings then
  30.   local function readShouldUseSettings()
  31.     readInput(
  32.       "Settings found. Do you want to reset the settings? If so, press ENTER")
  33.  
  34.     isUsingSettings = false
  35.   end
  36.  
  37.   local function settingsCountDown()
  38.     term.setTextColor(colors.orange)
  39.     print("")
  40.     print("The settings will be used automatically")
  41.     for i = 5, 1, -1 do
  42.       local cursorX, cursorY = term.getCursorPos()
  43.       term.clearLine()
  44.       term.setCursorPos(1, cursorY)
  45.       write("in " .. i .. " seconds")
  46.       sleep(1)
  47.     end
  48.     print("")
  49.  
  50.     isUsingSettings = true
  51.   end
  52.  
  53.   parallel.waitForAny(readShouldUseSettings, settingsCountDown)
  54.  
  55.   if isUsingSettings then
  56.     digDir = savedSettings.digDir
  57.     blockName = savedSettings.blockName
  58.     delay = savedSettings.delay
  59.   end
  60. end
  61.  
  62. -- prompt info
  63. term.setTextColor(colors.blue)
  64. print(
  65.   "You may place a block at the selected direction first before running the programming so only that block will get broken")
  66. print("")
  67.  
  68. -- select break direction
  69. local digFunc, inspectFunc
  70. repeat
  71.   if not digDir then
  72.     digDir = readInput("Input the direction to break (front/up/down, default: front)")
  73.   end
  74.  
  75.   if (digDir == "front" or digDir == "" or digDir == nil) then
  76.     digDir = 'front'
  77.     digFunc = turtle.dig
  78.     inspectFunc = turtle.inspect
  79.   elseif (digDir == "up") then
  80.     digFunc = turtle.digUp
  81.     inspectFunc = turtle.inspectUp
  82.   elseif (digDir == "down") then
  83.     digFunc = turtle.digDown
  84.     inspectFunc = turtle.inspectDown
  85.   else
  86.     digDir = nil;
  87.     term.clear()
  88.     term.setCursorPos(1, 1)
  89.     term.setTextColor(colors.red)
  90.     print("Invliad value, only 'front', 'up', and 'down' are allowed")
  91.   end
  92. until digFunc ~= nil
  93.  
  94. -- define dig function
  95. local dig = function()
  96.   local success = digFunc()
  97.   if not success then
  98.     sleep(1)
  99.   end
  100. end
  101.  
  102. -- Read block data at select face
  103. if not isUsingSettings then
  104.   local hasBlock, blockData = inspectFunc()
  105.   if hasBlock then
  106.     term.clear()
  107.     term.setCursorPos(1, 1)
  108.     term.setTextColor(colors.blue)
  109.     print("Block detected: " .. blockData.name)
  110.  
  111.     local targetBreakValue = readInput("Should I only break this block? (y/n, default: y)")
  112.  
  113.     local shouldTargetBreak = targetBreakValue == "y" or targetBreakValue == "Y" or targetBreakValue == "" or
  114.         targetBreakValue == nil
  115.     if shouldTargetBreak then
  116.       blockName = blockData.name
  117.     end
  118.   end
  119. end
  120.  
  121.  
  122. if blockName then
  123.   if not isUsingSettings then
  124.     local delayInStr = readInput("Input break delay in seconds if any")
  125.     delay = tonumber(delayInStr)
  126.   end
  127.  
  128.   if delay and delay <= 0 then
  129.     delay = nil
  130.   end
  131.  
  132.   -- redefine dig
  133.   local tempDig = dig
  134.   dig = function()
  135.     local hasBlock, data = inspectFunc()
  136.     if hasBlock and data.name == blockName then
  137.       if delay then sleep(delay) end
  138.       tempDig()
  139.     end
  140.   end
  141. end
  142.  
  143. -- store settings
  144. settings.define(settingsKey)
  145. settings.set(settingsKey, {
  146.   digDir = digDir,
  147.   blockName = blockName,
  148.   delay = delay
  149. })
  150. settings.save(settingsId)
  151.  
  152. -- info
  153. term.clear()
  154. term.setCursorPos(1, 1)
  155. term.setTextColor(colors.green)
  156. print("==== Block Breaker ====")
  157. term.setTextColor(colors.blue)
  158. print("Direction: " .. digDir)
  159. if delay then
  160.   print("Delay: " .. delay)
  161. end
  162. term.setTextColor(colors.cyan)
  163. if blockName then
  164.   print("Now breaking: " .. blockName)
  165. else
  166.   print("Now breaking: everything")
  167. end
  168.  
  169.  
  170. while true do
  171.   dig()
  172. end
  173.  
Advertisement
Add Comment
Please, Sign In to add comment