HamsterFurtif

replaceBattery

Jul 15th, 2019
277
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.66 KB | None | 0 0
  1. -- shell.run("pastebin run GFVEwzSn")
  2.  
  3. local scan = peripheral.wrap("left")
  4. term.clear()
  5. term.setCursorPos(1, 1)
  6.  
  7. local x = 0
  8. local y = 0
  9.  
  10. print("\nEmpty batteries chest on the top, full batteries one on the right\n")
  11.  
  12.  
  13. function getBatteryEnergy(x, z, y)
  14.     local battery = scan.getBlockMeta(x, z, y)
  15.     local energy = battery.energy
  16.     if battery.displayName == "Turtle Charger (RF)" then
  17.         energy = nil
  18.     end
  19.     return energy
  20. end
  21.  
  22. function isBatteryEmpty(energy)
  23.     if energy ~= nil and energy.stored <= energy.capacity * 0.05 then
  24.         return true
  25.     else
  26.         return false
  27.     end
  28. end
  29.  
  30. function throwEmptyBattery()
  31.     turtle.dig()
  32.     turtle.dropUp()
  33. end
  34.  
  35. function putNewBattery()
  36.     turtle.turnLeft()
  37.     while not turtle.suck() do
  38.     end
  39.     turtle.turnRight()
  40.     while not turtle.place() do
  41.     end
  42. end
  43.  
  44. function setDirection()
  45.     local data = scan.getBlockMeta(0, 0, 0)
  46.     local turtleDirection = data.state.facing
  47.     if turtleDirection == "west" then
  48.         x = -1
  49.     elseif turtleDirection == "east" then
  50.         x = 1
  51.     elseif turtleDirection == "north" then
  52.         y = -1
  53.     elseif turtleDirection == "south" then
  54.         y = 1
  55.     end
  56. end
  57.  
  58. function start()
  59.     local _, data  = turtle.inspect()
  60.     while data.displayName ~= "tile.ender_storage.name" do
  61.         turtle.turnLeft()
  62.         _, data = turtle.inspect()
  63.     end
  64.     turtle.turnRight()
  65.     setDirection()
  66. end
  67.  
  68. start()
  69.  
  70. while true do
  71.     if not turtle.detect() or isBatteryEmpty(getBatteryEnergy(x, 0, y)) then
  72.         if turtle.detect() then
  73.             throwEmptyBattery()
  74.         end
  75.         putNewBattery()
  76.     end
  77. end
Add Comment
Please, Sign In to add comment