Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- number of seconds before rebooting the machine, use -1 to disable
- local timeToReboot = 300
- --time in seconds between updates
- local sleepTime = 0.5
- -- how long to wait whe nthe item is nearly fully charged before removing it
- local waitForFullChargeTime = 20
- -- the itemid of item to charge
- local chargeItemID = 30473
- sorter = peripheral.wrap("right")
- local seconds = 0
- function SecondsToClock(sSeconds)
- local nSeconds = tonumber(sSeconds)
- if nSeconds == 0 then
- return "00:00:00";
- else
- nHours = string.format("%02d", math.floor(nSeconds/3600));
- nMins = string.format("%02d", math.floor(nSeconds/60 - (nHours*60)));
- nSecs = string.format("%02d", math.floor(nSeconds - nHours*3600 - nMins *60));
- return nHours..":"..nMins..":"..nSecs
- end
- end
- --[[ Original code by NeverCast, revised for v3.0 by lost_RD
- This should be loaded like a typical api
- Feel free to name it what you like
- This paste created for http://ftbwiki.org/Interactive_Sorter
- Fixed by MajorVictory (April 28, 2013)
- --]]
- 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}
- directions.south = directions["+Z"]
- directions.east = directions["+X"]
- directions.north = directions["-Z"]
- directions.west = directions["-X"]
- -- Gets the Unique ID based on the ID and Meta
- function getUUID(id, meta)
- uuid = id + meta * 32768
- return uuid
- end
- -- Get a stack table from a single uuid and amount
- -- Valid for version 3.0
- function getID(uuid)
- local durability, id = 0, uuid
- if uuid > 32768 then
- id = uuid % 32768
- durability = (uuid - id) / 32768
- end
- return id, durability
- end
- function getStack(uuid, c)
- local stack = {}
- stack.id, stack.meta = getID(uuid)
- stack.count = c
- stack.uuid = uuid
- return stack
- end
- -- Get stacks from an Interactive Sorter
- -- direction : the direction of the Interactive Sorter Peripheral
- -- invDirection: the direction of the inventory from the peripheral
- -- valid options for invDirection are 0,1,2,3,4,5 ( original values),
- -- north, south, east, west, up, down, and the +/-X,Y,Z strings.
- -- (see directions variable)
- function getStacks(direction, invDirection)
- if not peripheral.isPresent(direction) then
- return false, {}
- end
- if peripheral.getType(direction) ~= "interactiveSorter" then
- return false, {}
- end
- local stacks = {}
- local lowerinv = peripheral.call(direction, "list", directions[invDirection])
- if lowerinv ~= nil then
- for uuid,count in pairs(peripheral.call(direction, "list", directions[invDirection])) do
- table.insert(stacks, getStack(uuid, count))
- end
- return true, stacks
- else
- return false, {}
- end
- end
- function table.contains(table, element)
- for _, value in pairs(table) do
- if value == element then
- return true
- end
- end
- return false
- end
- local unchargedIDs = {}
- for n=0,25 do table.insert(unchargedIDs, getUUID(chargeItemID, n)) end
- local isRunning = true
- local reboot = false
- local charging = false
- local status1,status2,status3 = "","",""
- local w,h = term.getSize()
- function printStatus()
- term.clear()
- local clockTime = "Time: "..SecondsToClock(seconds)
- term.setCursorPos(w - #clockTime+1, 1)
- term.setBackgroundColor(colors.white)
- term.setTextColor(colors.black)
- term.write(clockTime)
- if timeToReboot > 0 then
- local rebootDisplay = "Reboot in: "..string.format("%d",(timeToReboot - seconds))
- term.setCursorPos(w - #rebootDisplay+1, 2)
- term.write(rebootDisplay)
- end
- term.setBackgroundColor(colors.black)
- term.setTextColor(colors.white)
- term.setCursorPos(1,1)
- term.write(status1)
- term.setCursorPos(1,2)
- term.write(status2)
- -- uncomment for debug infos
- --term.setCursorPos(1,3)
- --term.write(status3)
- --term.setCursorPos(1,4)
- --term.write(status4)
- --term.setCursorPos(1,5)
- --term.write(status5)
- end
- while isRunning do
- printStatus()
- local _, lowerInv = getStacks("right","down")
- local lowerInvSize = #lowerInv
- status5 = lowerInvSize
- if not charging and lowerInvSize > 0 then
- charging = true
- status1 = "Charging item..."
- status2 = "Waiting for item to finish..."
- end
- printStatus()
- if not charging then
- for uuid,count in pairs(sorter.list(directions.up)) do
- local id, meta = getID(uuid)
- meta = (27 - meta)
- status1 = "Looking for item to charge..."
- status2 = ""
- status3 = "Item: "..id.." ("..meta.."/27)"
- status4 = "uuid: "..uuid
- printStatus()
- seconds = seconds + 1
- sleep(1)
- if not charging and id == chargeItemID and meta < 26 then
- if lowerInvSize < 1 then
- status1 = "Charging item..."
- printStatus()
- sorter.extract(directions.up, uuid, directions.down, 1)
- charging = true
- end
- end
- end
- elseif lowerInvSize > 0 then
- for uuid,count in pairs(sorter.list(directions.down)) do
- local id, meta = getID(uuid)
- meta = (27.0 - meta)
- status1 = "Charging item..."
- status3 = "Charging Item: "..id.." ("..meta.."/27)"
- status4 = "uuid: "..uuid
- if id == chargeItemID and meta == 26 then
- local waitleft = waitForFullChargeTime
- while waitleft > 0 do
- status1 = "Charging finishing in "..waitleft.." seconds..."
- printStatus()
- waitleft = waitleft - 1
- seconds = seconds + 1
- sleep(1)
- end
- status1 = "Finished charging, moving item back up."
- printStatus()
- sorter.extract(directions.down, uuid, directions.up, 1)
- charging = false
- else
- status2 = "Waiting for item to finish...("..meta.."/27)"
- printStatus()
- end
- end
- end
- if not charging and timeToReboot > 0 and seconds >= timeToReboot then
- term.clear()
- term.setCursorPos(1,1)
- term.write("Rebooting...")
- reboot = true
- isRunning = false
- sleep(2)
- else
- seconds = seconds + sleepTime
- sleep(sleepTime)
- end
- end
- if reboot then os.reboot() end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement