Advertisement
posicat

/replaceBlocks

Mar 11th, 2025 (edited)
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. --File: /replaceBlocks
  2.  
  3. dofile("/cattech/common.lua")
  4. dofile("/cattech/file.lua")
  5.  
  6. continueIfNoMatch = false
  7.  
  8. function selectItemFromInventory(itemName)
  9.     for slot = 1, 16 do
  10.         turtle.select(slot)
  11.         local item = turtle.getItemDetail()  -- No argument, checks the current slot
  12.         print("Sel S" .. slot .. " - " .. item.name)
  13.         if item and item.name == itemName then
  14.             return true
  15.         end
  16.     end
  17.     return false
  18. end
  19.  
  20. function getReplacementBlock()
  21.     local success
  22.  
  23.     success = false;
  24.  
  25.     for slot = 1, 16 do
  26.         turtle.select(slot)
  27.         local item = turtle.getItemDetail()
  28.         print("RB S" .. slot .. " - " .. item.name)
  29.         if item and not turtle.refuel(0) then
  30.             return item.name
  31.         end
  32.     end
  33.     return nil
  34. end
  35.  
  36. function getTargetBlock(direction)
  37.     local success, block
  38.  
  39.     if direction == "down" then
  40.         success, block = turtle.inspectDown()
  41.     elseif direction == "up" then
  42.         success, block = turtle.inspectUp()
  43.     elseif direction == "right" then
  44.         turtle.turnRight()
  45.         success, block = turtle.inspect()
  46.         turtle.turnLeft() -- Reset orientation
  47.     elseif direction == "left" then
  48.         turtle.turnLeft()
  49.         success, block = turtle.inspect()
  50.         turtle.turnRight() -- Reset orientation
  51.     else
  52.         print("Invalid direction: " .. tostring(direction))
  53.         return nil
  54.     end
  55.  
  56.     if success then
  57.         return block.name
  58.     end
  59.     return nil
  60. end
  61.  
  62.  
  63. -- Function to replace a block in a given direction
  64. function replaceBlock(direction, replaceThis , replaceWith)
  65.     local returnSuccess, block
  66.  
  67.     returnSuccess = false
  68.  
  69.     if direction == "right" then
  70.         turtle.turnRight()
  71.         success, block = turtle.inspect()
  72.  
  73.         if block.name == replaceWith then
  74.             return true
  75.         end
  76.  
  77.         if success and block.name == replaceThis then
  78.             turtle.dig()
  79.             if selectItemFromInventory(replaceWith) then
  80.                 returnSuccess = turtle.place()
  81.             else
  82.                 print("No more replacement blocks available!")
  83.             end
  84.         else
  85.             returnSuccess = continueIfNoMatch
  86.         end
  87.         turtle.turnLeft() -- Reset orientation
  88.     end
  89.  
  90.     if direction == "left" then
  91.         turtle.turnLeft()
  92.         success, block = turtle.inspect()
  93.  
  94.         if block.name == replaceWith then
  95.             return true
  96.         end
  97.  
  98.         if success and block.name == replaceThis then
  99.             turtle.dig()
  100.             if selectItemFromInventory(replaceWith) then
  101.                 returnSuccess = turtle.place()
  102.             else
  103.                 print("No more replacement blocks available!")
  104.             end
  105.         else
  106.             returnSuccess = continueIfNoMatch
  107.         end
  108.         turtle.turnRight() -- Reset orientation
  109.     end
  110.  
  111.     if direction == "down" then
  112.         success, block = turtle.inspectDown()
  113.  
  114.         if block.name == replaceWith then
  115.             return true
  116.         end
  117.  
  118.         if success and block.name == replaceThis then
  119.             turtle.digDown()
  120.             if selectItemFromInventory(replaceWith) then
  121.                 returnSuccess = turtle.placeDown()
  122.             else
  123.                 print("No more replacement blocks available!")
  124.             end
  125.         else
  126.             returnSuccess = continueIfNoMatch
  127.         end
  128.     end
  129.  
  130.     if direction == "up" then
  131.         success, block = turtle.inspectUp()
  132.  
  133.         if block.name == replaceWith then
  134.             return true
  135.         end
  136.  
  137.         if success and block.name == replaceThis then
  138.             turtle.digUp()
  139.             if selectItemFromInventory(replaceWith) then
  140.                 returnSuccess = turtle.placeUp()
  141.             else
  142.                 print("No more replacement blocks available!")
  143.             end
  144.         else
  145.             returnSuccess = continueIfNoMatch
  146.         end
  147.     end
  148.  
  149.     return returnSuccess
  150. end
  151.  
  152. config = loadConfigAndDefaults()
  153.  
  154. if #arg >= 1 then
  155.     config["replaceDirection"] = arg[1]
  156. end
  157.  
  158. local replaceWith = getReplacementBlock() -- Find a block to replace with
  159. local replaceThis = getTargetBlock(config["replaceDirection"])
  160.  
  161. while replaceThis == replaceWith do
  162.     refuelIfNeeded(30)
  163.  
  164.     if not turtle.forward() then
  165.         print("Obstacle ahead, stopping.")
  166.         break
  167.     end
  168.     replaceThis = getTargetBlock(config["replaceDirection"])
  169. end
  170.  
  171.  
  172. if not replaceThis then
  173.     print("No block to replace found below.")
  174.     return
  175. else
  176.     print("Replacing : " .. replaceThis);
  177. end
  178.  
  179. if not replaceWith then
  180.     print("No replacement blocks in inventory!")
  181.     return
  182. else
  183.     print("Replacing with : " .. replaceWith );
  184. end
  185.  
  186. while true do
  187.     if not replaceBlock(config["replaceDirection"], replaceThis, replaceWith ) then
  188.         print("Replace failed, stopping")
  189.         break
  190.     end
  191.  
  192.     refuelIfNeeded(30)
  193.  
  194.     if not turtle.forward() then
  195.         print("Obstacle ahead, stopping.")
  196.         break
  197.     end
  198. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement