2025-09-05 22:16:17 +08:00
|
|
|
|
-- comparator_tester.lua
|
|
|
|
|
|
-- Tool to test comparator connections for Chest Sorter
|
|
|
|
|
|
|
2025-09-05 22:00:07 +08:00
|
|
|
|
local term = require("term")
|
|
|
|
|
|
local colors = require("colors")
|
2025-09-05 22:16:17 +08:00
|
|
|
|
local peripheral = require("peripheral")
|
|
|
|
|
|
local textutils = require("textutils")
|
|
|
|
|
|
|
|
|
|
|
|
-- 保存当前颜色设置
|
|
|
|
|
|
local old_fg = term.getTextColor()
|
|
|
|
|
|
local old_bg = term.getBackgroundColor()
|
2025-09-05 22:00:07 +08:00
|
|
|
|
|
2025-09-05 22:16:17 +08:00
|
|
|
|
-- 设置名称栏颜色并显示
|
|
|
|
|
|
term.setTextColor(colors.white)
|
|
|
|
|
|
term.setBackgroundColor(colors.blue)
|
|
|
|
|
|
term.at(1, 1).clearLine()
|
|
|
|
|
|
term.at(1, 1).write("=== Comparator Tester ===")
|
2025-09-05 22:00:07 +08:00
|
|
|
|
|
2025-09-05 22:16:17 +08:00
|
|
|
|
-- 恢复颜色设置
|
|
|
|
|
|
term.setTextColor(old_fg)
|
|
|
|
|
|
term.setBackgroundColor(old_bg)
|
|
|
|
|
|
term.at(1, 2)
|
|
|
|
|
|
|
|
|
|
|
|
print("This tool will help test your comparator connection.")
|
|
|
|
|
|
print("It uses the same detection logic as the Chest Sorter.")
|
|
|
|
|
|
print("")
|
|
|
|
|
|
|
|
|
|
|
|
-- 检测比较器
|
2025-09-05 22:06:41 +08:00
|
|
|
|
local comparator = nil
|
2025-09-05 22:16:17 +08:00
|
|
|
|
local comparator_name = nil
|
2025-09-05 22:06:41 +08:00
|
|
|
|
local peripherals = peripheral.getNames()
|
2025-09-05 22:00:07 +08:00
|
|
|
|
|
2025-09-05 22:06:41 +08:00
|
|
|
|
print(colors.blue .. "Searching for comparator..." .. colors.white)
|
|
|
|
|
|
|
2025-09-05 22:16:17 +08:00
|
|
|
|
-- 方法1: 尝试将每个外围设备都作为比较器检查
|
2025-09-05 22:06:41 +08:00
|
|
|
|
for _, name in ipairs(peripherals) do
|
2025-09-05 22:16:17 +08:00
|
|
|
|
print("Checking peripheral: " .. name)
|
|
|
|
|
|
local device = peripheral.wrap(name)
|
2025-09-05 22:06:41 +08:00
|
|
|
|
|
2025-09-05 22:16:17 +08:00
|
|
|
|
-- 检查设备是否有getOutputSignal方法
|
|
|
|
|
|
if device and type(device.getOutputSignal) == "function" then
|
|
|
|
|
|
comparator = device
|
|
|
|
|
|
comparator_name = name
|
|
|
|
|
|
print(colors.green .. "Found potential comparator: " .. name .. " (Type: " .. peripheral.getType(name) .. ")" .. colors.white)
|
2025-09-05 22:06:41 +08:00
|
|
|
|
break
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- 方法2: 如果上述方法失败,尝试直接使用peripheral.find
|
2025-09-05 22:16:17 +08:00
|
|
|
|
if not comparator then
|
2025-09-05 22:06:41 +08:00
|
|
|
|
print(colors.yellow .. "Method 1 failed. Trying peripheral.find..." .. colors.white)
|
|
|
|
|
|
comparator = peripheral.find("comparator")
|
|
|
|
|
|
if comparator then
|
2025-09-05 22:16:17 +08:00
|
|
|
|
-- 尝试获取设备名称
|
|
|
|
|
|
for _, name in ipairs(peripherals) do
|
|
|
|
|
|
if peripheral.wrap(name) == comparator then
|
|
|
|
|
|
comparator_name = name
|
|
|
|
|
|
break
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
2025-09-05 22:06:41 +08:00
|
|
|
|
print(colors.green .. "Comparator detected using peripheral.find!" .. colors.white)
|
2025-09-05 22:16:17 +08:00
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
|
|
-- 方法3: 尝试使用redstone比较器的其他可能名称
|
|
|
|
|
|
if not comparator then
|
|
|
|
|
|
print(colors.yellow .. "Method 2 failed. Trying alternative names..." .. colors.white)
|
2025-09-05 22:24:24 +08:00
|
|
|
|
local alternative_names = {"redstone_comparator", "minecraft:comparator", "comparator_block", "comparator"}
|
2025-09-05 22:16:17 +08:00
|
|
|
|
for _, alt_name in ipairs(alternative_names) do
|
|
|
|
|
|
comparator = peripheral.find(alt_name)
|
|
|
|
|
|
if comparator then
|
|
|
|
|
|
-- 尝试获取设备名称
|
|
|
|
|
|
for _, name in ipairs(peripherals) do
|
|
|
|
|
|
if peripheral.wrap(name) == comparator then
|
|
|
|
|
|
comparator_name = name
|
|
|
|
|
|
break
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
print(colors.green .. "Comparator detected using alternative name: " .. alt_name .. "!" .. colors.white)
|
|
|
|
|
|
break
|
|
|
|
|
|
end
|
2025-09-05 22:06:41 +08:00
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2025-09-05 22:24:24 +08:00
|
|
|
|
-- 方法4: 尝试检测redstone接口
|
|
|
|
|
|
if not comparator then
|
|
|
|
|
|
print(colors.yellow .. "Method 3 failed. Trying redstone interface..." .. colors.white)
|
|
|
|
|
|
local redstone = peripheral.find("redstone")
|
|
|
|
|
|
if redstone then
|
|
|
|
|
|
-- 检查redstone接口是否有比较器功能
|
|
|
|
|
|
if type(redstone.getComparatorOutput) == "function" then
|
|
|
|
|
|
comparator = redstone
|
|
|
|
|
|
comparator_name = "redstone_interface"
|
|
|
|
|
|
print(colors.green .. "Found redstone interface with comparator functionality!" .. colors.white)
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2025-09-05 22:06:41 +08:00
|
|
|
|
-- 显示结果
|
2025-09-05 22:16:17 +08:00
|
|
|
|
if not comparator then
|
|
|
|
|
|
print(colors.red .. "No comparator detected!" .. colors.white)
|
|
|
|
|
|
if #peripherals > 0 then
|
|
|
|
|
|
print(colors.yellow .. "Connected peripherals:" .. colors.white)
|
|
|
|
|
|
for _, name in ipairs(peripherals) do
|
|
|
|
|
|
local p_type = peripheral.getType(name)
|
|
|
|
|
|
print("- " .. name .. " (Type: " .. p_type .. ")")
|
|
|
|
|
|
end
|
|
|
|
|
|
else
|
|
|
|
|
|
print(colors.red .. "No peripherals detected at all." .. colors.white)
|
|
|
|
|
|
end
|
|
|
|
|
|
print("")
|
|
|
|
|
|
print(colors.red .. "Troubleshooting steps:" .. colors.white)
|
|
|
|
|
|
print("1. Ensure the comparator is directly connected to the computer")
|
|
|
|
|
|
print("2. Check if the comparator is powered")
|
|
|
|
|
|
print("3. Verify that the comparator is not broken")
|
|
|
|
|
|
print("4. Try reconnecting the comparator")
|
|
|
|
|
|
print("5. Make sure the comparator is placed next to a chest")
|
|
|
|
|
|
else
|
|
|
|
|
|
print(colors.green .. "Comparator detected successfully!" .. colors.white)
|
|
|
|
|
|
print("Comparator name: " .. (comparator_name or "unknown"))
|
|
|
|
|
|
print("Comparator type: " .. peripheral.getType(comparator_name))
|
2025-09-05 22:06:41 +08:00
|
|
|
|
|
2025-09-05 22:16:17 +08:00
|
|
|
|
-- 测试比较器信号
|
|
|
|
|
|
print("")
|
|
|
|
|
|
print(colors.blue .. "Testing comparator signal..." .. colors.white)
|
2025-09-05 22:06:41 +08:00
|
|
|
|
local signal = comparator.getOutputSignal()
|
|
|
|
|
|
print("Output signal level: " .. signal)
|
|
|
|
|
|
|
|
|
|
|
|
if signal > 0 then
|
2025-09-05 22:16:17 +08:00
|
|
|
|
print(colors.green .. "Comparator is detecting items in connected chests." .. colors.white)
|
2025-09-05 22:06:41 +08:00
|
|
|
|
else
|
2025-09-05 22:16:17 +08:00
|
|
|
|
print(colors.yellow .. "Comparator is not detecting any items." .. colors.white)
|
|
|
|
|
|
print("Try adding items to a chest connected to the comparator.")
|
2025-09-05 22:06:41 +08:00
|
|
|
|
end
|
|
|
|
|
|
|
2025-09-05 22:16:17 +08:00
|
|
|
|
-- 持续监控信号
|
|
|
|
|
|
print("")
|
|
|
|
|
|
print(colors.blue .. "Monitoring signal changes (press any key to stop)..." .. colors.white)
|
|
|
|
|
|
while true do
|
|
|
|
|
|
os.sleep(0.5)
|
|
|
|
|
|
local new_signal = comparator.getOutputSignal()
|
|
|
|
|
|
if new_signal ~= signal then
|
|
|
|
|
|
signal = new_signal
|
|
|
|
|
|
print("Signal changed to: " .. signal)
|
|
|
|
|
|
if signal > 0 then
|
|
|
|
|
|
print(colors.green .. "Comparator is now detecting items!" .. colors.white)
|
|
|
|
|
|
else
|
|
|
|
|
|
print(colors.yellow .. "Comparator stopped detecting items." .. colors.white)
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
if term.hasFocus() and term.getKey() ~= nil then
|
|
|
|
|
|
break
|
2025-09-05 22:00:07 +08:00
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
end
|
|
|
|
|
|
|
2025-09-05 22:16:17 +08:00
|
|
|
|
-- 恢复终端设置
|
|
|
|
|
|
term.setTextColor(old_fg)
|
|
|
|
|
|
term.setBackgroundColor(old_bg)
|
|
|
|
|
|
print("")
|
|
|
|
|
|
print("Test completed.")
|