gur111

infinite_break_loop

Jun 21st, 2025 (edited)
285
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.36 KB | None | 0 0
  1. -- Infinite loop for breaking blocks in front of the turtle
  2. -- Usage: break_loop [delay] [--rclick] [--block <blockname>] [--exclude <blockname>]
  3. -- delay: optional delay in seconds between break attempts (default: 0.1)
  4. -- --rclick: perform right-click (turtle.place()) instead of breaking blocks
  5. -- --block <blockname>: only break blocks that contain the specified name (e.g., "log" matches all log types)
  6. -- --exclude <blockname>: break everything except blocks that contain the specified name
  7.  
  8. local args = {...}
  9. local delay = 0.1
  10. local rightClick = false
  11. local targetBlock = nil
  12. local excludeBlock = nil
  13.  
  14. -- Parse arguments
  15. local i = 1
  16. while i <= #args do
  17. local arg = args[i]
  18. if arg == "--rclick" then
  19. rightClick = true
  20. elseif arg == "--block" then
  21. i = i + 1
  22. if i <= #args then
  23. targetBlock = string.lower(args[i])
  24. else
  25. print("Error: --block requires a block name")
  26. return
  27. end
  28. elseif arg == "--exclude" then
  29. i = i + 1
  30. if i <= #args then
  31. excludeBlock = string.lower(args[i])
  32. else
  33. print("Error: --exclude requires a block name")
  34. return
  35. end
  36. elseif tonumber(arg) then
  37. delay = tonumber(arg)
  38. end
  39. i = i + 1
  40. end
  41.  
  42. -- Validate arguments
  43. if targetBlock and excludeBlock then
  44. print("Error: Cannot use both --block and --exclude at the same time")
  45. return
  46. end
  47.  
  48. if rightClick then
  49. print("Starting infinite right-click loop...")
  50. print("Delay between right-clicks: " .. delay .. " seconds")
  51. else
  52. print("Starting infinite block breaking loop...")
  53. if targetBlock then
  54. print("Only breaking blocks containing: " .. targetBlock)
  55. elseif excludeBlock then
  56. print("Breaking all blocks except those containing: " .. excludeBlock)
  57. end
  58. print("Delay between breaks: " .. delay .. " seconds")
  59. end
  60. print("Press Ctrl+T to stop")
  61.  
  62. -- Function to safely break a block in front
  63. local function breakBlockInFront()
  64. if turtle.detect() then
  65. local success, data = turtle.inspect()
  66. if success and data then
  67. local blockName = data.name or "unknown block"
  68. local lowerBlockName = string.lower(blockName)
  69.  
  70. -- If targetBlock is specified, check if the block matches
  71. if targetBlock then
  72. if not string.find(lowerBlockName, targetBlock) then
  73. print("Skipping: " .. blockName .. " (not matching '" .. targetBlock .. "')")
  74. return false
  75. end
  76. end
  77.  
  78. -- If excludeBlock is specified, check if the block should be excluded
  79. if excludeBlock then
  80. if string.find(lowerBlockName, excludeBlock) then
  81. print("Skipping: " .. blockName .. " (excluded by '" .. excludeBlock .. "')")
  82. return false
  83. end
  84. end
  85.  
  86. print("Breaking: " .. blockName)
  87. turtle.dig()
  88. return true
  89. else
  90. -- If inspection fails but filters are specified, don't dig
  91. if targetBlock or excludeBlock then
  92. print("Cannot inspect block - skipping due to block filter")
  93. return false
  94. end
  95. turtle.dig() -- Try to dig anyway if inspection fails and no filter
  96. return true
  97. end
  98. end
  99. return false
  100. end
  101.  
  102. -- Function to perform right-click (place block)
  103. local function rightClickInFront()
  104. local success = turtle.place()
  105. if success then
  106. print("Right-clicked (placed block)")
  107. return true
  108. else
  109. print("Right-click failed (no item or space blocked)")
  110. return false
  111. end
  112. end
  113.  
  114. -- Main infinite loop
  115. local actionCount = 0
  116. while true do
  117. local success = false
  118.  
  119. if rightClick then
  120. success = rightClickInFront()
  121. if success then
  122. actionCount = actionCount + 1
  123. print("Right-clicks performed: " .. actionCount)
  124. end
  125. else
  126. success = breakBlockInFront()
  127. if success then
  128. actionCount = actionCount + 1
  129. print("Blocks broken: " .. actionCount)
  130. else
  131. print("No block detected in front")
  132. end
  133. end
  134.  
  135. -- Wait for the specified delay
  136. sleep(delay)
  137. end
  138.  
Advertisement
Add Comment
Please, Sign In to add comment