Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- --=================================================
- -- Recycle Bin by Kadoba
- ---------------------------------------------------
- -- Turns an attached chest into a recycle bin.
- -- Recycle bins only delete stacks that have been
- -- inside of them for more than a specific length
- -- of time. By default this is one minute.
- --=================================================
- ---------------------------------------------------
- -- Config
- ---------------------------------------------------
- -- Delay in seconds between deleting items
- local delay = 5
- -- The side of the computer that the recycle
- -- bin is on. Takes the first program argument
- -- or defaults to "right" if one isn't passed.
- local side = ({...})[1] or "right"
- -- If true then recycled items are logged
- local dolog = true
- -- The log filename
- local logname = "recycle_log"
- ---------------------------------------------------
- -- Setup -
- ---------------------------------------------------
- local timeleft = delay -- Delay time left
- local blink = false -- Cursor blinking
- local deleted = 0 -- Total deleted stacks
- local list = {} -- Buffered deleted list
- list.size = 8 -- Buffer size
- list.i = 0 -- List increment
- -- Returns the buffer index for the list given
- -- the incremental index
- function list.increment_index(n)
- return n % list.size
- end
- -- Returns the buffer index for the list given
- -- the terminal display offset.
- function list.display_index(n)
- return (n + list.i) % list.size
- end
- -- Terminal display lines
- local lines = {}
- lines[1] = "Recycle program has started."
- lines[2] = "Attaching to %s on %s side."
- lines[3] = ""
- lines[4] = "Waiting 0..."
- lines[5] = ""
- lines[6] = "Most recent deleted items:"
- lines[7] = "-------------------------------------"
- -- Buffered list is lines 8 through 15
- lines[16] = "-------------------------------------"
- lines[17] = "Deleted a total of 0 item stacks."
- local log
- if dolog then
- local exists = fs.exists(logname)
- log = fs.open(logname, exists and "a" or "w")
- end
- ---------------------------------------------------
- -- Peripheral Attachment
- ---------------------------------------------------
- local chest = peripheral.wrap(side)
- if not chest then
- error("Could not find chest on side: "..side)
- else
- lines[2] = string.format(lines[2],
- chest.getInventoryName(),
- side
- )
- end
- local prev_stacks = chest.getAllStacks()
- ---------------------------------------------------
- -- Loop Functions
- ---------------------------------------------------
- -- Recycle stacks
- local function recycle()
- local stacks = chest.getAllStacks()
- local prev_info, info, text
- local prev_deleted = deleted
- if not stacks then
- error("Chest appears to have been destroyed.")
- end
- for slot, stack in pairs(prev_stacks) do
- if stacks[slot] then
- prev_info = stack.basic()
- info = stacks[slot].basic()
- if prev_info.id == info.id then
- deleted = deleted + 1
- text = string.format("%s x %d",
- info.display_name,
- info.qty)
- list[ list.increment_index(list.i) ] = text
- list.i = list.i + 1
- if dolog then
- log.writeLine(text)
- end
- chest.destroyStack(slot)
- stacks[slot] = nil
- end
- end
- end
- if deleted > prev_deleted and dolog then
- log.flush()
- end
- prev_stacks = stacks
- end
- -- Draw to terminal
- local function draw()
- local text = "Deleted a total of %d item stacks."
- lines[17] = string.format(text, deleted)
- text = "Waiting %d..."
- lines[4] = string.format(text, timeleft)
- for i=0,list.size-1 do
- lines[8+i] = list[ list.display_index(i) ] or ""
- end
- term.clear()
- for k,v in pairs(lines) do
- term.setCursorPos(1,k)
- term.write(lines[k])
- end
- end
- ---------------------------------------------------
- -- Main Program
- ---------------------------------------------------
- while true do
- timeleft = timeleft - 1
- if timeleft <= 0 then
- recycle()
- timeleft = delay
- end
- draw()
- sleep(1)
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement