Last active
October 7, 2024 18:24
-
-
Save Ale32bit/35817e5a391128f810f7519f33ffac01 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| --[[ | |
| -- List items in Create Vault! | |
| -- AlexDevs.me - MIT License | |
| -- | |
| -- Link Display Link to Target Block, | |
| -- placed on top of Smart Observer facing the vault. | |
| -- | |
| -- Configure Display Link to "List matching items" | |
| -- in "Millibuckets" mode. Set Line to 1. | |
| -- Monitor and target block peripherals can be | |
| -- configured via "vault.monitor" and "vault.target" settings. | |
| -- Set to "auto" to use peripheral.find(). | |
| -- | |
| ]] | |
| settings.define("vault.monitor", { | |
| description = "Monitor to use. Use 'auto' to use the first monitor found.", | |
| type = "string", | |
| default = "auto" | |
| }) | |
| settings.define("vault.target", { | |
| description = "Target block to use. Use 'auto' to use the first target block found.", | |
| type = "string", | |
| default = "auto" | |
| }) | |
| local monitorSide = settings.get("vault.monitor", "auto") | |
| local targetSide = settings.get("vault.target", "auto") | |
| -- Does not need to update as frequently | |
| local delay = 5 | |
| local monitor = monitorSide == "auto" and peripheral.find("monitor") or peripheral.wrap(monitorSide) | |
| local target = targetSide == "auto" and peripheral.find("create_target") or peripheral.wrap(targetSide) | |
| assert(monitor, "Monitor not found!") | |
| assert(target, "Target block not found!") | |
| local function getItems() | |
| local w, h = target.getSize() | |
| local inventory = {} | |
| for i = 1, h do | |
| local line = target.getLine(i) | |
| local count, itemName = line:match("(%w+)%s+([%w%s]*)") | |
| if count and itemName then | |
| table.insert(inventory, { | |
| name = itemName, | |
| count = tonumber(count), | |
| }) | |
| end | |
| end | |
| target.clear() | |
| return inventory | |
| end | |
| local function toSiUnit(n) | |
| local suffixes = { | |
| " ", "k", "M", "G", "T" | |
| } | |
| local i = 1 | |
| while n > 1000 do | |
| n = n / 1000 | |
| i = i + 1 | |
| end | |
| return string.format("%d%s", n, suffixes[i]) | |
| end | |
| local function redraw() | |
| local items = getItems() | |
| local w, h = monitor.getSize() | |
| monitor.setBackgroundColor(colors.black) | |
| monitor.setTextColor(colors.white) | |
| monitor.clear() | |
| monitor.setCursorPos(1, 1) | |
| monitor.write("Vault Content") | |
| for i = 1, math.min(h, #items) do | |
| local item = items[i] | |
| monitor.setCursorPos(1, i + 1) | |
| monitor.write(string.format("%4s %s", toSiUnit(item.count), item.name)) | |
| end | |
| end | |
| while true do | |
| redraw() | |
| sleep(delay) | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment