Advertisement
k2green

Replacer

Jul 31st, 2024
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.43 KB | Software | 0 0
  1. local args = { ... }
  2. local outputName = args[1]
  3. local replaceName = args[2]
  4. local replaceWith = args[3]
  5. local outputDirection = args[4]
  6. local tempDirection = args[5]
  7.  
  8. local function turn(direction, inverted)
  9.     if direction == "left" then
  10.         if inverted then
  11.             turtle.turnRight()
  12.         else
  13.             turtle.turnLeft()
  14.         end
  15.     elseif direction == "right" then
  16.         if inverted then
  17.             turtle.turnLeft()
  18.         else
  19.             turtle.turnRight()
  20.         end
  21.     end
  22. end
  23.  
  24. local function findFlawed(inventory)
  25.     for i=1, inventory.size() do
  26.         local details = inventory.getItemDetail(i)
  27.        
  28.         if details ~= nil and details.name == replaceWith then
  29.             return true, i
  30.         end
  31.     end
  32.    
  33.     return false, nil
  34. end
  35.  
  36. while true do
  37.     local exists, details = turtle.inspect()
  38.    
  39.     if exists and details.name == outputName then
  40.         break
  41.     end
  42.    
  43.     turtle.turnLeft()
  44. end
  45.  
  46. for i=1, 16 do
  47.     turtle.select(i)
  48.     turtle.drop()
  49. end
  50.  
  51. turtle.select(1)
  52. turn(outputDirection, true)
  53.  
  54. while true do
  55.     if turtle.detect() then
  56.         local _, details = turtle.inspect()
  57.        
  58.         if details.name == replaceName then
  59.             turtle.dig()
  60.             turn(outputDirection, false)
  61.             turtle.drop()
  62.             turn(outputDirection, true)
  63.         end
  64.     else
  65.         local inout = peripheral.wrap(outputDirection)
  66.         local found, slot = findFlawed(inout)
  67.        
  68.         if found then
  69.             inout.pushItems(tempDirection, slot, 1, 1)
  70.             turn(tempDirection, false)
  71.             turtle.suck()
  72.             turn(tempDirection, true)
  73.             turtle.place()
  74.         end
  75.     end
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement