Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --File: /patchtunnel
- dofile("/cattech/turtle.lua")
- local patchItem
- function getItemTypeInSlot2()
- local itemCount = turtle.getItemCount(2) -- Get the count of items in slot 2
- if itemCount > 0 then
- local itemDetail = turtle.getItemDetail(2) -- Get details of the item in slot 2
- return itemDetail.name -- Return the item name
- else
- return nil -- Return nil if slot 2 is empty
- end
- end
- function selectItemFromSlots(itemName)
- for slot = 1, 16 do -- Loop through all 16 slots
- local itemCount = turtle.getItemCount(slot) -- Get the count of items in the current slot
- if itemCount > 0 then
- local itemDetail = turtle.getItemDetail(slot) -- Get details of the item in the current slot
- if itemDetail.name == itemName then
- turtle.select(slot) -- Select the slot if it matches the item name
- return true
- end
- end
- end
- print(patchItem .. "not found.") -- If item is not found
- return false
- end
- function patchUp()
- success, blockData = turtle.inspectUp()
- if success and isLiquid(blockData.name) then
- selectItemFromSlots(patchItem)
- turtle.placeUp()
- end
- end
- function patchDown()
- success, blockData = turtle.inspectDown()
- if success and isLiquid(blockData.name) then
- selectItemFromSlots(patchItem)
- turtle.placeDown()
- end
- end
- function patchForward()
- success, blockData = turtle.inspect()
- if success and isLiquid(blockData.name) then
- selectItemFromSlots(patchItem)
- turtle.place()
- end
- end
- function patchLeft()
- turtle.turnLeft()
- success, blockData = turtle.inspect()
- if success and isLiquid(blockData.name) then
- selectItemFromSlots(patchItem)
- turtle.place()
- end
- turtle.turnRight()
- end
- function patchRight()
- turtle.turnRight()
- success, blockData = turtle.inspect()
- if success and isLiquid(blockData.name) then
- selectItemFromSlots(patchItem)
- turtle.place()
- end
- turtle.turnLeft()
- end
- -- Function to move the turtle back the number of spaces it has traveled
- function moveBack(steps)
- print("Returning back " .. steps .. " spaces.")
- for i = 1, steps do
- refuelIfNeeded(10)
- while not turtle.back() do
- print("Blocked! Pausing for 1 second before retrying.")
- sleep(1) -- Wait for 1 second if it can't move back
- end
- end
- end
- -- Main routine
- function checkAndPatch(directionString)
- for i = 1, string.len(directionString) do
- local char = directionString:sub(i, i)
- if char == "u" then
- patchUp()
- elseif char == "d" then
- patchDown()
- elseif char == "l" then
- patchLeft()
- elseif char == "r" then
- patchRight()
- elseif char == "f" then
- patchForward()
- end
- end
- end
- function isNotAirLavaOrWater(success,blockData)
- if not success then
- return false -- If no block is detected, return false
- end
- -- Check the block type
- local blockName = blockData.name
- if blockName ~= "minecraft:air" and blockName ~= "minecraft:water" and blockName ~= "minecraft:lava" then
- return true -- Return true if the block is not air, lava, or water
- else
- return false -- Return false if the block is air, lava, or water
- end
- end
- function patchFull3by2()
- -- Middle D Facing Forward
- checkAndPatch("d")
- turtle.turnRight()
- turtle.forward()
- -- Right D Facing Right
- checkAndPatch("df")
- turtle.up()
- -- Right U Facing Right
- checkAndPatch("uf")
- turtle.turnLeft()
- turtle.turnLeft()
- turtle.forward()
- -- Middle U Facing Left
- checkAndPatch("u")
- turtle.forward()
- -- Left U Facing Left
- checkAndPatch("uf")
- turtle.down()
- -- Left D Facing Left
- checkAndPatch("df")
- turtle.turnRight()
- turtle.turnRight()
- turtle.forward()
- turtle.turnLeft()
- -- Middle D Facing Forward
- end
- function getTunnelSides()
- local dir = ""
- -- Check right
- turtle.turnRight()
- if isNotAirLavaOrWater(turtle.inspect()) then
- dir = dir .. "r"
- end
- turtle.turnLeft()
- -- Check left
- turtle.turnLeft()
- if isNotAirLavaOrWater(turtle.inspect()) then
- dir = dir .. "l"
- end
- turtle.turnRight()
- -- Check Up
- if isNotAirLavaOrWater(turtle.inspectUp()) then
- dir = dir .. "u"
- end
- -- Check Down
- if isNotAirLavaOrWater(turtle.inspectDown()) then
- dir = dir .. "d"
- end
- print ("Auto-direction : " .. dir)
- return dir
- end
- -- Main program
- local maxSpaces = 50
- if #arg < 1 then
- print("Going " .. maxSpaces .. " default steps.")
- else
- local maxSpaces = tonumber(arg[1])
- end
- local spaces = 0
- patchItem = getItemTypeInSlot2()
- if patchItem == nil then
- print("Item slot 2 empty.")
- else
- while spaces < maxSpaces do
- refuelIfNeeded(30)
- if selectItemFromSlots(patchItem) then
- if turtle.forward() then
- patchFull3by2()
- spaces = spaces + 1
- else
- print("Could not move foward")
- break
- end
- else
- print("Ran out of " .. patchItem)
- break
- end
- end
- end
- print("Returning " .. spaces .. " steps.")
- moveBack(spaces);
Add Comment
Please, Sign In to add comment