Implemented ME-bridge readout for pages

This commit is contained in:
Max
2026-06-01 04:14:20 +02:00
parent 1d3a507a06
commit 81aca559c9
+100 -13
View File
@@ -12,6 +12,7 @@ local mf = require("morefonts-pe")
local frame = Pine3D.newFrame()
frame:setBackgroundColor(colors.black)
local meSystem = peripheral.find("meBridge") or peripheral.find("rsBridge")
local chainItemsByBaseId
local baseItemIds
@@ -118,6 +119,78 @@ local function getItemIcon(item)
return item.icon or nil
end
local function getMeItemCount(itemId)
if not meSystem or not itemId then
return 0
end
if type(meSystem.getItem) == "function" then
local ok, item = pcall(meSystem.getItem, { name = itemId })
if ok and item then
return tonumber(item.amount or item.count or item.qty) or 0
end
end
return 0
end
local function getMeItemCounts(itemIds)
local counts = {}
for i = 1, #itemIds do
counts[itemIds[i]] = 0
end
if not meSystem then
return counts
end
local unresolved = {}
local unresolvedCount = 0
for i = 1, #itemIds do
local itemId = itemIds[i]
local count = getMeItemCount(itemId)
counts[itemId] = count
if count == 0 then
unresolved[itemId] = true
unresolvedCount = unresolvedCount + 1
end
end
if unresolvedCount == 0 then
return counts
end
local listItems = meSystem.listItems or meSystem.getItems or meSystem.listAvailableItems
if not listItems then
return counts
end
local ok, items = pcall(listItems)
if not ok or type(items) ~= "table" then
return counts
end
for i = 1, #items do
local item = items[i]
local itemId = item and (item.name or item.id or item.item_id)
if itemId and unresolved[itemId] then
counts[itemId] = tonumber(item.amount or item.count or item.qty) or 0
unresolved[itemId] = nil
unresolvedCount = unresolvedCount - 1
if unresolvedCount == 0 then
break
end
end
end
return counts
end
local function getFallbackIcon()
if fallbackIcon == false then
local ok, image = pcall(paintutils.loadImage, "/icons16/diamond16.nfp")
@@ -285,25 +358,39 @@ local function drawPage(base_id)
local backIcon = getBackButton()
local backWidth, backHeight = imageSize(backIcon)
local backX = SCREEN_WIDTH - backWidth + 1
local pageItemIds = {}
local visibleItemCount = math.min(math.max(#pageItems, 9), 9)
frame.buffer:clear()
drawNfpScaled(frame.buffer, backIcon, (backX - 1) * 2 + 1, 1)
-- TODO LATER get item counts from me-system using item ids. For now, just use random numbers.
for i = 1, math.min(math.max(#pageItems, 9), 9) do
local item = pageItems[i]
local icon = getItemIcon(item) or defaultIcon
drawItem(icon, 4+(8+4)*((i-1)%3), 2+(5+1)*math.floor((i-1)/3), math.random(0, 1000000000000))
for i = 1, visibleItemCount do
pageItemIds[i] = pageItems[i].id
end
assertBufferValid(frame)
frame:drawBuffer()
local function renderPage()
local itemCounts = getMeItemCounts(pageItemIds)
frame.buffer:clear()
drawNfpScaled(frame.buffer, backIcon, (backX - 1) * 2 + 1, 1)
for i = 1, visibleItemCount do
local item = pageItems[i]
local icon = getItemIcon(item) or defaultIcon
drawItem(icon, 4+(8+4)*((i-1)%3), 2+(5+1)*math.floor((i-1)/3), itemCounts[item.id] or 0)
end
assertBufferValid(frame)
frame:drawBuffer()
end
renderPage()
local refreshTimer = os.startTimer(5)
while true do
local _, side, x, y = os.pullEvent("monitor_touch")
local event, p1, x, y = os.pullEvent()
if side == monName and x >= backX and x < backX + backWidth and y >= 1 and y <= backHeight then
if event == "timer" and p1 == refreshTimer then
renderPage()
refreshTimer = os.startTimer(5)
elseif event == "monitor_touch" and p1 == monName and x >= backX and x < backX + backWidth and y >= 1 and y <= backHeight then
drawOverview()
return
end