Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- WirelessMonitor.lua
- -- Version 1.4.2
- -- 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.
- -------------------------------------------------------------------------------
- -- 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.5)
- 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
- -- 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
- -- Writes text to the display
- local function writeToDisplay(display, lines)
- display.clear()
- display.setCursorPos(1, 1)
- for _, line in ipairs(lines) do
- display.write(line)
- local _, y = display.getCursorPos()
- display.setCursorPos(1, y + 1)
- end
- end
- -- Processes item sections
- local function processItemSection(name, displayName, blocks, missingCount)
- local lines = { (displayName or "") .. "\n", "Type: " .. name, "Count: " .. missingCount }
- if blocks then
- local blockList = {}
- for block in blocks:gmatch("([^,]+)") do
- table.insert(blockList, block)
- end
- -- Check if the name or display name contains "shingle"
- if name:lower():find("shingle") or (displayName and displayName:lower():find("shingle")) then
- -- Swap block order for "shingle" items
- if #blockList >= 2 then
- table.insert(lines, " Shingle:" .. blockList[2])
- table.insert(lines, " Support:" .. blockList[1])
- else
- -- Less than 2 blocks, show them normally
- 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
- -- Parses and displays missing items
- 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
- writeToDisplay(display, wrapText(table.concat(lines, "\n"), display.getSize()))
- end
- -- Displays non-item status updates
- local function displayStatusUpdate(display, message)
- local lines = wrapText("Status Update:\n" .. message, display.getSize())
- writeToDisplay(display, 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
- end
- end
Advertisement
Add Comment
Please, Sign In to add comment