Advertisement
Guest User

Very basic turtle mining program

a guest
Mar 28th, 2015
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.77 KB | None | 0 0
  1. --Very basic turtle mining program.
  2.  
  3. function refuel()
  4. local data
  5. local success = false
  6.     for i = 1, 16, 1 do
  7.         turtle.select(i)
  8.         data = turtle.getItemDetail()
  9.        
  10.         if data then
  11.             if data.name == "minecraft:coal" then --refuel if coal found in internal storage
  12.                 turtle.refuel(turtle.getItemCount())
  13.                 success = true
  14.             end
  15.         end
  16.        
  17.         if turtle.getFuelLevel() == turtle.getFuelLimit() then --if already refueled no need to continue this function
  18.             break
  19.         end
  20.     end
  21.     if not success then
  22.           print("Couldn't refuel!\n")
  23.     end
  24.     turtle.select(1)
  25.     return success
  26. end
  27.  
  28. function isFull()
  29. local data
  30.     for i = 1, 16, 1 do
  31.         turtle.select(i)
  32.         data = turtle.getItemDetail()
  33.        
  34.         if not data then
  35.             do return false end
  36.         end
  37.     end
  38.     turtle.select(1)
  39.     return true
  40. end
  41.  
  42. function isStorage(chestName)
  43.     local success, data = turtle.inspect()
  44.  
  45.     if success then
  46.         if data.name == chestName then
  47.             do return true end
  48.         end
  49.     end
  50.     return false
  51. end
  52.  
  53. function goBackHome(chestName)
  54.     turtle.turnLeft()
  55.     turtle.turnLeft() --180°
  56.    
  57.     while not isStorage(chestName) do
  58.         turtle.forward()
  59.     end
  60. end
  61.  
  62. function store()
  63. local success
  64.     for i = 1, 16, 1 do
  65.         turtle.select(i)
  66.         success = turtle.drop()
  67.  
  68.         if not success then
  69.             do return false end
  70.         end
  71.     end
  72.  
  73.     turtle.turnRight()
  74.     turtle.turnRight()
  75.    
  76.     turtle.select(1)
  77.     return true
  78. end
  79.  
  80. --main
  81. local storageChestName = "minecraft:chest"
  82. local canStillWork= true
  83.  
  84. while true do
  85.     if turtle.getFuelLevel() == 0 then
  86.         canStillWork = refuel()
  87.     elseif isFull() then
  88.         goBackHome(storageChestName)
  89.         canStillWork = store()
  90.     else
  91.         turtle.dig()
  92.         turtle.up()
  93.         turtle.dig()
  94.         turtle.down()
  95.         turtle.forward()
  96.     end
  97.  
  98.     if not canStillWork then
  99.         break
  100.     end
  101. end
  102.  
  103. print("Job finished!")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement