Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- WirelessMonitor.lua
- -- Version 1.5.0
- -- Author: Modified by AshGrey
- -- Date: 2024-10-16
- -- This script displays missing items or status updates from the AE2 Warehouse on a pocket computer, regular computer, or monitor.
- -- It uses a custom GUI with buttons and scrollable sections.
- -------------------------------------------------------------------------------
- -- INITIALIZATION
- -------------------------------------------------------------------------------
- local function init()
- term.clear()
- term.setCursorPos(1, 1)
- print("Initializing Wireless Monitor...")
- local modem = peripheral.find("modem")
- if not modem then error("Ender Modem not found.") end
- print("Ender Modem initialized.")
- local monitor = peripheral.find("monitor")
- local display = term -- Default display to terminal
- if monitor then
- monitor.setTextScale(0.75) -- Adjust text scale for better readability
- display = monitor
- print("Monitor detected. Output will be displayed on the monitor.")
- else
- print("No monitor detected. Output will be displayed on the terminal.")
- end
- return modem, display
- end
- local modem, display = init()
- local channel = 1
- modem.open(channel)
- -------------------------------------------------------------------------------
- -- FUNCTIONS
- -------------------------------------------------------------------------------
- -- Strips unwanted prefixes from strings
- local function removePrefixes(str)
- return str:gsub("minecraft:", ""):gsub("minecolonies:", ""):gsub("domum_ornamentum:", ""):gsub("Missing items:", "")
- end
- -- Basic GUI drawing helpers
- function pos(...) return term.setCursorPos(...) end
- function cls(...) return term.clear() end
- function tCol(...) return term.setTextColor(...) end
- function bCol(...) return term.setBackgroundColor(...) end
- function box(...) return paintutils.drawFilledBox(...) end
- function line(...) return paintutils.drawLine(...) end
- local x, y = term.getSize()
- -- Draws the main display UI with missing items or status update.
- function drawDisplay()
- cls()
- box(1, 1, x, y, colors.lightBlue) -- Background
- box(2, 2, x - 1, y - 1, colors.white) -- Content Background
- tCol(colors.black)
- bCol(colors.lightBlue)
- pos(2, 2)
- write("Wireless Monitor")
- bCol(colors.white)
- end
- -- Handles the item information section
- function drawItemSection(name, displayName, blocks, missingCount, startY)
- local sectionHeight = 4
- box(2, startY, x - 1, startY + sectionHeight - 1, colors.gray)
- line(2, startY, x - 1, startY, colors.lightGray)
- tCol(colors.black)
- pos(3, startY + 1)
- write(displayName or name)
- pos(3, startY + 2)
- write("Type: " .. name)
- pos(3, startY + 3)
- write("Missing: " .. missingCount)
- if blocks then
- local blockList = {}
- for block in blocks:gmatch("([^,]+)") do
- table.insert(blockList, block)
- end
- for i, block in ipairs(blockList) do
- pos(3, startY + 3 + i)
- write(" " .. block)
- end
- end
- end
- -- Wraps text for monitors
- local function wrapText(text, width)
- local wrappedText = {}
- for line in text:gmatch("[^\n]+") do
- while #line > width do
- local segment = line:sub(1, width):match("^(.-)%s") or line:sub(1, width)
- table.insert(wrappedText, segment)
- line = line:sub(#segment + 1):match("^%s*(.*)") or ""
- end
- if #line > 0 then
- table.insert(wrappedText, line)
- end
- end
- return wrappedText
- end
- -- Scroll logic
- local scrollIndex = 1
- -- Handles scrollable section drawing with dynamic scrolling
- function drawScrollableItems(lines)
- local lineHeight = 3
- local sectionY = 6
- local visibleLines = math.floor((y - sectionY) / lineHeight) -- Number of visible lines
- -- Draw only the visible lines, based on scrollIndex
- for i = scrollIndex, math.min(scrollIndex + visibleLines - 1, #lines) do
- pos(3, sectionY + (i - scrollIndex) * lineHeight)
- write(lines[i])
- end
- end
- -- Scroll logic (ensure you don't go out of bounds)
- local function scrollUp()
- scrollIndex = math.max(1, scrollIndex - 1)
- end
- local function scrollDown()
- scrollIndex = scrollIndex + 1
- end
- -- Process item sections with scrollable content
- local function processItemSection(name, displayName, blocks, missingCount)
- local lines = { (displayName or "") .. "\n", "Type: " .. name, "Missing: " .. missingCount }
- if blocks then
- local blockList = {}
- for block in blocks:gmatch("([^,]+)") do
- table.insert(blockList, block)
- end
- -- Swap block order for "shingle" items
- if name:lower():find("shingle") or (displayName and displayName:lower():find("shingle")) then
- if #blockList >= 2 then
- table.insert(lines, " Shingle: " .. blockList[2])
- table.insert(lines, " Support: " .. blockList[1])
- else
- for _, block in ipairs(blockList) do
- table.insert(lines, " " .. block)
- end
- end
- else
- -- Normal block display
- for _, block in ipairs(blockList) do
- table.insert(lines, " " .. block)
- end
- end
- end
- return lines
- end
- -- Displays missing items (with scrollable content)
- local function displayMissingItems(display, message)
- local lines = { "Missing Items Report:" }
- for itemSection in message:gmatch("([^\n]+:.-Missing Count: %d+)") do
- local name = itemSection:match("^([^:]+):")
- local displayName = itemSection:match("Display Name: ([^\n]+)")
- local blocks = itemSection:match("Blocks Needed: ([^\n]+)")
- local missingCount = itemSection:match("Missing Count: (%d+)")
- local sectionLines = processItemSection(name, displayName, blocks, missingCount)
- for _, line in ipairs(sectionLines) do
- table.insert(lines, line)
- end
- end
- drawDisplay()
- drawScrollableItems(lines)
- end
- -- Displays non-item status updates
- local function displayStatusUpdate(display, message)
- local lines = wrapText("Status Update:\n" .. message, display.getSize())
- drawDisplay()
- drawScrollableItems(lines)
- end
- -------------------------------------------------------------------------------
- -- MAIN LOOP
- -------------------------------------------------------------------------------
- print("Wireless Monitor is now running...")
- while true do
- local event, _, receivedChannel, _, message = os.pullEvent("modem_message")
- if receivedChannel == channel then
- message = removePrefixes(message)
- if message:find("Missing Count:") then
- displayMissingItems(display, message)
- else
- displayStatusUpdate(display, message)
- end
- local event, button, mx, my = os.pullEvent()
- if event == "mouse_click" then
- -- Scroll up/down logic based on mouse position (simplified for now)
- if my <= y - 2 then
- if mx >= x - 2 then
- scrollDown() -- Click to scroll down
- elseif mx <= 2 then
- scrollUp() -- Click to scroll up
- end
- end
- end
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment