Advertisement
MajorVictory

GraviChestplate AutoCharger

Apr 28th, 2013
243
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.25 KB | None | 0 0
  1.  
  2. -- number of seconds before rebooting the machine, use -1 to disable
  3. local timeToReboot = 300
  4.  
  5. --time in seconds between updates
  6. local sleepTime = 0.5
  7.  
  8. -- the itemid of item to charge
  9. local chargeItemID = 30473
  10.  
  11. sorter = peripheral.wrap("right")
  12.  
  13. function SecondsToClock(sSeconds)
  14.     local nSeconds = tonumber(sSeconds)
  15.     if nSeconds == 0 then
  16.         return "00:00:00";
  17.     else
  18.         nHours = string.format("%02d", math.floor(nSeconds/3600));
  19.         nMins = string.format("%02d", math.floor(nSeconds/60 - (nHours*60)));
  20.         nSecs = string.format("%02d", math.floor(nSeconds - nHours*3600 - nMins *60));
  21.         return nHours..":"..nMins..":"..nSecs
  22.     end
  23. end
  24.  
  25. local w,h = term.getSize()
  26.  
  27. function printStatus()
  28.     term.clear()
  29.  
  30.     local clockTime = "Time: "..SecondsToClock(seconds)
  31.     term.setCursorPos(w - #clockTime+1, 1)
  32.     term.setBackgroundColor(colors.white)
  33.     term.setTextColor(colors.black)
  34.     term.write(clockTime)
  35.  
  36.     if timeToReboot > 0 then
  37.         local rebootDisplay = "Reboot in: "..string.format("%d",(timeToReboot - seconds))
  38.         term.setCursorPos(w - #rebootDisplay+1, 2)
  39.         term.write(rebootDisplay)
  40.     end
  41.  
  42.  
  43.     term.setBackgroundColor(colors.black)
  44.     term.setTextColor(colors.white)
  45. end
  46.  
  47. --[[ Original code by NeverCast, revised for v3.0 by lost_RD
  48.      This should be loaded like a typical api
  49.      Feel free to name it what you like
  50.      This paste created for http://ftbwiki.org/Interactive_Sorter
  51. --]]
  52. local directions = { [0]=0,[1]=1,[2]=2,[3]=3,[4]=4,[5]=5,["down"] = 0, ["up"] = 1, ["-Z"] = 2, ["+Z"] = 3, ["-X"] = 4, ["+X"] = 5, ["+Y"] = 1, ["-Y"] = 0}
  53. directions.south = directions["+Z"]
  54. directions.east = directions["+X"]
  55. directions.north = directions["-Z"]
  56. directions.west = directions["-X"]
  57.  
  58. -- Gets the Unique ID based on the ID and Meta
  59. function getUUID(id, meta)
  60.   uuid = id + meta * 32768
  61.   return uuid
  62. end
  63.  
  64. -- Get a stack table from a single uuid and amount
  65. -- Valid for version 3.0
  66. function getID(uuid)
  67.   id = uuid
  68.   meta = 0
  69.   if uuid > 32768 then
  70.     meta = uuid%32768
  71.     id = id - (meta * 32768)
  72.   end
  73. end
  74.  
  75. function getStack(uuid, c)
  76.     local stack = {}
  77.  
  78.     stack.id = uuid
  79.     stack.meta = 0
  80.     if uuid > 32768 then
  81.         stack.meta = uuid%32768
  82.         stack.id = stack.id - (stack.meta * 32768)
  83.     end
  84.    
  85.     stack.count = c
  86.     stack.uuid = uuid
  87.  
  88.     return stack
  89. end
  90.  
  91.  
  92. -- Get stacks from an Interactive Sorter
  93. -- direction   : the direction of the Interactive Sorter Peripheral
  94. -- invDirection: the direction of the inventory from the peripheral
  95. -- valid options for invDirection are 0,1,2,3,4,5 ( original values),
  96. -- north, south, east, west, up, down, and the +/-X,Y,Z strings.
  97. -- (see directions variable)
  98. function getStacks(direction, invDirection)
  99.   if not peripheral.isPresent(direction) then
  100.     return false, "No Peripheral"
  101.   end
  102.   if peripheral.getType(direction) ~= "interactiveSorter" then
  103.     return false, "Not a sorter"
  104.   end
  105.   local stacks = {}
  106.   for uuid,count in pairs(peripheral.call(direction, "list", directions[invDirection])) do
  107.     table.insert(stacks, getStack(uuid, count))
  108.   end
  109.   return true, stacks
  110. end
  111.  
  112. local isRunning = true
  113. local reboot = false
  114. local charging = false
  115.  
  116. while isRunning do
  117.     printStatus()
  118.  
  119.     local isSorter, stacks = getStacks("right", "up")
  120.  
  121.  
  122.     if not charging and isSorter then
  123.  
  124.         for key, value in pairs(stacks) do
  125.             if not charging and stack.id == chargeItemID then
  126.  
  127.                 local stack = stacks[key]
  128.  
  129.                 sorter.extract(directions.up, stack.uuid, directions.down, 1)
  130.  
  131.  
  132.                 term.setCursorPos(1,1)
  133.                 term.write("Charging...")
  134.                 sleep(1)
  135.  
  136.                 charging = true
  137.  
  138.             end
  139.         end
  140.  
  141.     else if charging and isSorter then
  142.  
  143.         isSorter, stacks = getStacks("right", "down")
  144.  
  145.         for key, value in pairs(stacks) do
  146.             if stack.id == chargeItemID and stack.meta == 0 then
  147.  
  148.                 local stack = stacks[key]
  149.  
  150.                 sorter.extract(directions.down, stack.uuid, directions.up, 1)
  151.  
  152.                 term.setCursorPos(1,1)
  153.                 term.write("Done charging, Moving item up")
  154.                 sleep(1)
  155.  
  156.                 charging = false
  157.             end
  158.         end
  159.  
  160.     end
  161.  
  162.     if not charging and timeToReboot > 0 and seconds >= timeToReboot then
  163.         term.clear()
  164.         term.setCursorPos(1,1)
  165.         term.write("Rebooting...")
  166.         reboot = true
  167.         isRunning = false
  168.         sleep(2)
  169.     else
  170.         seconds = seconds + sleepTime
  171.         sleep(sleepTime)
  172.     end
  173. end
  174.  
  175. if reboot then os.reboot() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement