fix: enhance monitor detection to handle adjacent peripherals correctly

This commit is contained in:
MayaTheShy
2026-03-28 23:41:20 -04:00
parent ec1a681924
commit 9396fbd81a

View File

@@ -120,7 +120,24 @@ end
-- Monitor setup
-------------------------------------------------
local function findMonitor(side, excludeSide)
--- Check whether two peripheral names refer to the same physical block.
-- Needed because a monitor placed adjacent to the computer AND connected
-- via wired modem appears under both its side name (e.g. "left") and its
-- network name (e.g. "monitor_0"). A simple string comparison misses this.
local function sameMonitor(a, b)
if not a or not b then return false end
if a == b then return true end
-- Write a distinctive cursor position via one name and read it back
-- via the other; if they share state, they are the same peripheral.
local ok1 = pcall(peripheral.call, a, "setCursorPos", 9876, 5432)
if not ok1 then return false end
local ok2, x, y = pcall(peripheral.call, b, "getCursorPos")
pcall(peripheral.call, a, "setCursorPos", 1, 1)
return ok2 and x == 9876 and y == 5432
end
local function findMonitor(side, excludeNames)
excludeNames = excludeNames or {}
local mon = peripheral.wrap(side)
local monName
if mon and mon.setTextScale then
@@ -130,18 +147,24 @@ local function findMonitor(side, excludeSide)
end
if not mon then
for _, name in ipairs(peripheral.getNames()) do
if peripheral.getType(name) == "monitor" and name ~= excludeSide then
if peripheral.getType(name) == "monitor" then
local dominated = false
for _, ex in ipairs(excludeNames) do
if sameMonitor(name, ex) then dominated = true; break end
end
if not dominated then
mon = peripheral.wrap(name)
monName = name
break
end
end
end
end
return mon, monName
end
function D.setupMonitor()
D.mon, D.monName = findMonitor(cfg.MONITOR_SIDE, cfg.SMELTER_MONITOR_SIDE)
D.mon, D.monName = findMonitor(cfg.MONITOR_SIDE, { cfg.SMELTER_MONITOR_SIDE })
if not D.mon then return false end
mainDevice = UI.Device({
@@ -152,7 +175,7 @@ function D.setupMonitor()
end
function D.setupSmelterMonitor()
D.smelterMon, D.smelterMonName = findMonitor(cfg.SMELTER_MONITOR_SIDE, D.monName)
D.smelterMon, D.smelterMonName = findMonitor(cfg.SMELTER_MONITOR_SIDE, { D.monName })
if not D.smelterMon then return false end
smelterDevice = UI.Device({
@@ -178,8 +201,8 @@ function D.setupBillboardMonitor()
-- Auto-detect: find any monitor not already used by main/smelter
for _, name in ipairs(peripheral.getNames()) do
if peripheral.getType(name) == "monitor"
and name ~= D.monName
and name ~= D.smelterMonName then
and not sameMonitor(name, D.monName)
and not sameMonitor(name, D.smelterMonName) then
local mon = peripheral.wrap(name)
if mon and mon.setTextScale then
D.billboardMon = mon