largeNumberGoeshere

replaceBlock

May 31st, 2021 (edited)
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.99 KB | None | 0 0
  1. --- replace block turtle program
  2. replaceTheseBlocks = {"minecraft:stripped_birch_log","minecraft:dirt",""}
  3. replaceBlocksWith = {"minecraft:birch_log"}
  4.  
  5.  
  6. function getName(inspectionDirection)
  7.     local isBlock,details = inspectionDirection()  
  8.    
  9.     local name = "undefined"
  10.     if isBlock then
  11.         name = details.name
  12.     else
  13.         name = ""
  14.     end
  15.     return name
  16. end
  17.  
  18. function getNameForward()
  19.     local name = getName(turtle.inspect)
  20.     return name
  21. end
  22.  
  23.  
  24. function isNameForward(tab_names)
  25.     local blockName = getNameForward()
  26.     for i,v in pairs(tab_names) do
  27.         if blockName == v then
  28.             return true
  29.         end
  30.     end
  31.    
  32.     return false
  33. end
  34.  
  35. function isSlotOccupied(slot)
  36.     local cnt = turtle.getItemCount(slot)
  37.     return cnt > 0
  38. end
  39.  
  40.  
  41. function getSlotItemName(slot)
  42.     local detail = turtle.getItemDetail(slot)
  43.  
  44.     if detail ~= nil then
  45.         return detail.name
  46.     else
  47.         return ""
  48.     end
  49. end
  50.  
  51. function findSlotWith(tab_names)
  52.     for i=1,16 do
  53.         if isSlotOccupied(i) then
  54.             --turtle.select(i)
  55.             local name = getSlotItemName(i)
  56.            
  57.             for i2,v in pairs(tab_names) do
  58.                 if name == v then
  59.                     return tonumber(i)
  60.                 end
  61.             end
  62.            
  63.            
  64.         end
  65.     end
  66.     --no match
  67.     return "no match"
  68. end
  69.  
  70. function replaceWithSlot()
  71.  
  72.     local slot = findSlotWith(replaceBlocksWith)
  73.     --print(slot)
  74.     if slot == "no match" then
  75.         print("None of item found. Please add more items")
  76.        
  77.         while findSlotWith(replaceBlocksWith) == "no match" do
  78.             sleep(1)
  79.             write(".")
  80.         end
  81.        
  82.     else
  83.         return slot
  84.     end
  85.    
  86.     --print(slot)
  87.  
  88.    
  89.     --return 2
  90. end
  91.  
  92.  
  93.  
  94. function main()
  95.     if isNameForward(replaceTheseBlocks) then
  96.         print("replacable block detected")
  97.         local a,b= turtle.dig()
  98.             if not a then
  99.                 print(tostring(a)..": "..b)
  100.             end
  101.         turtle.suck()
  102.         turtle.select(replaceWithSlot())
  103.         turtle.place()
  104.     end
  105. end
  106.  
  107. function info()
  108.    
  109. end
  110.  
  111. ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
  112. ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ---- ----
  113. info()
  114. while true do
  115.     main()
  116. end
  117.  
  118.  
  119.    
  120.    
  121.    
  122.    
  123.    
  124.  
  125.  
  126.  
Add Comment
Please, Sign In to add comment