feat(chest_sorter): 改进箱子分类器程序并添加比较器检测功能

添加比较器检测逻辑,优化物品分类流程,并增强错误处理和用户指导:
1. 使用比较器信号检测输入箱物品变化
2. 改进外围设备检测和分类逻辑
3. 添加详细的错误信息和教程内容
4. 新增测试比较器程序
5. 更新安装程序版本号
This commit is contained in:
2025-09-05 22:00:07 +08:00
parent bd524aabf1
commit 4474238da1
3 changed files with 115 additions and 42 deletions

View File

@@ -26,12 +26,30 @@ term.setTextColor(old_fg)
term.setBackgroundColor(old_bg) term.setBackgroundColor(old_bg)
term.at(1, 2) term.at(1, 2)
-- 检查是否有调解器(comparator)连接 -- 检查是否有比较器(comparator)连接
local comparators = peripheral.find("comparator") local comparator = peripheral.find("comparator")
if not comparators then local peripherals = peripheral.getNames()
error("No comparator detected. Please connect a comparator to the computer.", 0)
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
error("Please connect a comparator to the computer.", 0)
else
print(colors.green .. "Comparator detected!" .. colors.white)
end end
-- 存储所有连接的外围设备名称
local connected_peripherals = peripherals
-- 定义配置变量 -- 定义配置变量
local config = { local config = {
input_chest = nil, input_chest = nil,
@@ -63,8 +81,16 @@ local function setupWizard()
print("This will guide you through setting up your chest sorter.") print("This will guide you through setting up your chest sorter.")
print("") print("")
-- 列出所有连接的箱子 -- 列出所有连接的箱子类型外围设备
local chests = peripheral.getNames() local chests = {}
for _, name in ipairs(connected_peripherals) do
local peripheral_type = peripheral.getType(name)
-- 检查是否为箱子类型的外围设备
if peripheral_type == "chest" or peripheral_type == "trapped_chest" or peripheral_type == "barrel" then
table.insert(chests, name)
end
end
if #chests == 0 then if #chests == 0 then
error("No chests detected. Please connect at least one chest.", 0) error("No chests detected. Please connect at least one chest.", 0)
end end
@@ -145,52 +171,63 @@ local function autoSort()
print(colors.green .. "Starting auto-sorting..." .. colors.white) print(colors.green .. "Starting auto-sorting..." .. colors.white)
print("Press Ctrl+T to stop.") print("Press Ctrl+T to stop.")
-- 获取比较器对象
local comparator = peripheral.wrap(peripheral.find("comparator"))
if not comparator then
print(colors.red .. "Failed to wrap comparator." .. colors.white)
return
end
-- 捕获Ctrl+T中断 -- 捕获Ctrl+T中断
local success, error = pcall(function() local success, error = pcall(function()
while true do while true do
-- 检查输入箱中的物品 -- 使用比较器检测输入箱是否有物品变化
local input_inventory = peripheral.wrap(config.input_chest) local input_chest = peripheral.wrap(config.input_chest)
if not input_inventory then if not input_chest then
print(colors.red .. "Failed to connect to input chest." .. colors.white) print(colors.red .. "Failed to connect to input chest." .. colors.white)
break break
end end
-- 遍历输入箱的所有槽位 -- 检查比较器信号
local items_found = false local comparator_level = comparator.getOutputSignal()
for slot = 1, 16 do if comparator_level > 0 then
local item = input_inventory.getItemDetail(slot) print(colors.blue .. "Comparator detected items in input chest." .. colors.white)
if item then
items_found = true
print("Found item: " .. item.name)
-- 尝试找到匹配的类别 -- 遍历输入箱的所有槽位
local category_found = false local items_found = false
for category, chest_name in pairs(config.output_chests) do for slot = 1, 16 do
if string.find(item.name, category) or string.find(item.displayName, category) then local item = input_chest.getItemDetail(slot)
local output_inventory = peripheral.wrap(chest_name) if item then
if output_inventory then items_found = true
print("Moving to '" .. category .. "' chest...") print("Found item: " .. item.name)
input_inventory.pushItems(chest_name, slot)
category_found = true -- 尝试找到匹配的类别
break local category_found = false
else for category, chest_name in pairs(config.output_chests) do
print(colors.red .. "Failed to connect to '" .. category .. "' chest." .. colors.white) if string.find(item.name, category) or string.find(item.displayName, category) then
local output_inventory = peripheral.wrap(chest_name)
if output_inventory then
print("Moving to '" .. category .. "' chest...")
input_chest.pushItems(chest_name, slot)
category_found = true
break
else
print(colors.red .. "Failed to connect to '" .. category .. "' chest." .. colors.white)
end
end end
end end
end
if not category_found then if not category_found then
print(colors.yellow .. "No matching category found for '" .. item.name .. "'." .. colors.white) print(colors.yellow .. "No matching category found for '" .. item.name .. "'." .. colors.white)
end end
-- 短暂延迟避免过快处理 -- 短暂延迟避免过快处理
os.sleep(0.1) os.sleep(0.1)
end
end end
end else
-- 如果比较器没有检测到物品,稍作等待
-- 如果没有找到物品,稍作等待 os.sleep(0.5)
if not items_found then
os.sleep(1)
end end
end end
end) end)
@@ -223,8 +260,14 @@ local function showTutorial()
print(" a. Select an input chest (where you'll place items to sort)") print(" a. Select an input chest (where you'll place items to sort)")
print(" b. Assign output chests to categories (e.g., 'coal', 'tools')") print(" b. Assign output chests to categories (e.g., 'coal', 'tools')")
print("5. After setup, the program will automatically sort items:") print("5. After setup, the program will automatically sort items:")
print(" - Items in the input chest will be moved to the appropriate output chest") print(" - The comparator will detect when items are added to the input chest")
print(" - Based on the category names you assigned") print(" - Items will be moved to the appropriate output chest based on category names")
print("")
print("Troubleshooting:")
print("- If you get a 'No comparator detected' error:")
print(" 1. Check that the comparator is properly connected to the computer")
print(" 2. Make sure the comparator is receiving a redstone signal from the chests")
print(" 3. Verify that the comparator is not broken")
print("") print("")
print("Tips:") print("Tips:")
print("- Use specific category names for better sorting (e.g., 'iron_ore' instead of 'ore')") print("- Use specific category names for better sorting (e.g., 'iron_ore' instead of 'ore')")

View File

@@ -0,0 +1,30 @@
-- Simple comparator test program
local peripheral = require("peripheral")
local term = require("term")
local colors = require("colors")
print(colors.green .. "=== Comparator Test ===" .. colors.white)
-- 查找比较器
local comparator = peripheral.find("comparator")
if not comparator then
print(colors.red .. "No comparator detected!" .. colors.white)
-- 列出所有连接的外围设备
local peripherals = peripheral.getNames()
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
else
print(colors.green .. "Comparator detected!" .. colors.white)
print("Comparator type: " .. peripheral.getType(peripheral.find("comparator")))
print("Output signal level: " .. comparator.getOutputSignal())
end
print(colors.blue .. "Test completed." .. colors.white)

View File

@@ -1,5 +1,5 @@
-- LeonOS installer -- LeonOS installer
local INSTALLER_VERSION = "0.3.8 Beta 19" local INSTALLER_VERSION = "0.3.8 Beta 20"
local DEFAULT_ROM_DIR = "/leonos" local DEFAULT_ROM_DIR = "/leonos"
print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...") print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...")