CCCPaste

me_dashboard

-- me_dashboard.lua
-- CC:Tweaked + Advanced Peripherals: ME system dashboard (uses documented API functions)

-- find peripherals
local me = peripheral.find("me_bridge")
local mon = peripheral.find("monitor")

if not me then
  print("ME Bridge not found")
  return
end
if not mon then
  print("Monitor not found")
  return
end

-- safe wrappers (check existence to avoid nil calls)
local has_getCraftingTasks = type(me.getCraftingTasks) == "function"
local has_getCraftingJob = type(me.getCraftingJob) == "function"

-- monitor setup
local function setupMonitor()
  -- try a readable text scale; if monitor too small adjust manually
  if type(mon.setTextScale) == "function" then
    pcall(mon.setTextScale, 0.5)
  end
  if type(mon.setBackgroundColor) == "function" then
    mon.setBackgroundColor(colors.black)
  end
  if type(mon.setTextColor) == "function" then
    mon.setTextColor(colors.white)
  end
  mon.clear()
end
setupMonitor()

local function centerText(y, txt)
  local w, h = mon.getSize()
  local x = math.max(1, math.floor((w - #txt) / 2) + 1)
  mon.setCursorPos(x, y)
  mon.write(txt)
end

local function safeCall(fn, ...)
  if type(fn) ~= "function" then return nil end
  local ok, res = pcall(fn, ...)
  if not ok then return nil end
  return res
end

-- render loop
while true do
  mon.clear()

  -- header
  centerText(1, "=== ME System Dashboard ===")

  -- connection / online
  local connected = safeCall(me.isConnected)
  local online = safeCall(me.isOnline)
  mon.setCursorPos(2, 3)
  mon.write("Bridge Connected: ")
  mon.setTextColor((connected and colors.lime) or colors.red)
  mon.write(tostring(connected))
  mon.setTextColor(colors.white)

  mon.setCursorPos(2, 4)
  mon.write("Network Online: ")
  mon.setTextColor((online and colors.lime) or colors.red)
  mon.write(tostring(online))
  mon.setTextColor(colors.white)

  -- storage summary (example using listItems/listCraftableItems)
  local items = safeCall(me.listItems) or {}
  local craftables = safeCall(me.listCraftableItems) or {}
  mon.setCursorPos(2, 6)
  mon.write("Stored item types: " .. tostring(#items))
  mon.setCursorPos(2, 7)
  mon.write("Craftable item types: " .. tostring(#craftables))

  -- crafting tasks (use documented getCraftingTasks())
  mon.setCursorPos(2, 9)
  if has_getCraftingTasks then
    local tasks = safeCall(me.getCraftingTasks) or {}
    mon.write("Active crafting tasks: " .. tostring(#tasks))
    -- show up to first 8 tasks
    for i = 1, math.min(8, #tasks) do
      local t = tasks[i]
      local y = 9 + i
      mon.setCursorPos(4, y)
      -- tasks from ME Bridge may include fields like .output or .id or .displayName depending on implementation
      local name = t.output and (t.output.displayName or t.output.name) or (t.displayName or ("job#" .. (t.id or "?")))
      mon.write("- " .. tostring(name))
    end
  else
    mon.write("Crafting API not available on this bridge")
  end

  -- optional: energy
  local energy = safeCall(me.getStoredEnergy)
  if energy then
    mon.setCursorPos(2, 18)
    mon.write("Grid Energy: " .. tostring(energy))
  end

  -- reset color
  mon.setTextColor(colors.white)

  sleep(3) -- refresh interval
end