Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --File: /replaceBlocks
- dofile("/cattech/common.lua")
- dofile("/cattech/file.lua")
- continueIfNoMatch = false
- function selectItemFromInventory(itemName)
- for slot = 1, 16 do
- turtle.select(slot)
- local item = turtle.getItemDetail() -- No argument, checks the current slot
- print("Sel S" .. slot .. " - " .. item.name)
- if item and item.name == itemName then
- return true
- end
- end
- return false
- end
- function getReplacementBlock()
- local success
- success = false;
- for slot = 1, 16 do
- turtle.select(slot)
- local item = turtle.getItemDetail()
- print("RB S" .. slot .. " - " .. item.name)
- if item and not turtle.refuel(0) then
- return item.name
- end
- end
- return nil
- end
- function getTargetBlock(direction)
- local success, block
- if direction == "down" then
- success, block = turtle.inspectDown()
- elseif direction == "up" then
- success, block = turtle.inspectUp()
- elseif direction == "right" then
- turtle.turnRight()
- success, block = turtle.inspect()
- turtle.turnLeft() -- Reset orientation
- elseif direction == "left" then
- turtle.turnLeft()
- success, block = turtle.inspect()
- turtle.turnRight() -- Reset orientation
- else
- print("Invalid direction: " .. tostring(direction))
- return nil
- end
- if success then
- return block.name
- end
- return nil
- end
- -- Function to replace a block in a given direction
- function replaceBlock(direction, replaceThis , replaceWith)
- local returnSuccess, block
- returnSuccess = false
- if direction == "right" then
- turtle.turnRight()
- success, block = turtle.inspect()
- if block.name == replaceWith then
- return true
- end
- if success and block.name == replaceThis then
- turtle.dig()
- if selectItemFromInventory(replaceWith) then
- returnSuccess = turtle.place()
- else
- print("No more replacement blocks available!")
- end
- else
- returnSuccess = continueIfNoMatch
- end
- turtle.turnLeft() -- Reset orientation
- end
- if direction == "left" then
- turtle.turnLeft()
- success, block = turtle.inspect()
- if block.name == replaceWith then
- return true
- end
- if success and block.name == replaceThis then
- turtle.dig()
- if selectItemFromInventory(replaceWith) then
- returnSuccess = turtle.place()
- else
- print("No more replacement blocks available!")
- end
- else
- returnSuccess = continueIfNoMatch
- end
- turtle.turnRight() -- Reset orientation
- end
- if direction == "down" then
- success, block = turtle.inspectDown()
- if block.name == replaceWith then
- return true
- end
- if success and block.name == replaceThis then
- turtle.digDown()
- if selectItemFromInventory(replaceWith) then
- returnSuccess = turtle.placeDown()
- else
- print("No more replacement blocks available!")
- end
- else
- returnSuccess = continueIfNoMatch
- end
- end
- if direction == "up" then
- success, block = turtle.inspectUp()
- if block.name == replaceWith then
- return true
- end
- if success and block.name == replaceThis then
- turtle.digUp()
- if selectItemFromInventory(replaceWith) then
- returnSuccess = turtle.placeUp()
- else
- print("No more replacement blocks available!")
- end
- else
- returnSuccess = continueIfNoMatch
- end
- end
- return returnSuccess
- end
- config = loadConfigAndDefaults()
- if #arg >= 1 then
- config["replaceDirection"] = arg[1]
- end
- local replaceWith = getReplacementBlock() -- Find a block to replace with
- local replaceThis = getTargetBlock(config["replaceDirection"])
- while replaceThis == replaceWith do
- refuelIfNeeded(30)
- if not turtle.forward() then
- print("Obstacle ahead, stopping.")
- break
- end
- replaceThis = getTargetBlock(config["replaceDirection"])
- end
- if not replaceThis then
- print("No block to replace found below.")
- return
- else
- print("Replacing : " .. replaceThis);
- end
- if not replaceWith then
- print("No replacement blocks in inventory!")
- return
- else
- print("Replacing with : " .. replaceWith );
- end
- while true do
- if not replaceBlock(config["replaceDirection"], replaceThis, replaceWith ) then
- print("Replace failed, stopping")
- break
- end
- refuelIfNeeded(30)
- if not turtle.forward() then
- print("Obstacle ahead, stopping.")
- break
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement