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.at(1, 2)
-- 检查是否有调解器(comparator)连接
local comparators = peripheral.find("comparator")
if not comparators then
error("No comparator detected. Please connect a comparator to the computer.", 0)
-- 检查是否有比较器(comparator)连接
local comparator = peripheral.find("comparator")
local peripherals = peripheral.getNames()
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
-- 存储所有连接的外围设备名称
local connected_peripherals = peripherals
-- 定义配置变量
local config = {
input_chest = nil,
@@ -63,8 +81,16 @@ local function setupWizard()
print("This will guide you through setting up your chest sorter.")
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
error("No chests detected. Please connect at least one chest.", 0)
end
@@ -145,20 +171,32 @@ local function autoSort()
print(colors.green .. "Starting auto-sorting..." .. colors.white)
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中断
local success, error = pcall(function()
while true do
-- 检查输入箱中的物品
local input_inventory = peripheral.wrap(config.input_chest)
if not input_inventory then
-- 使用比较器检测输入箱是否有物品变化
local input_chest = peripheral.wrap(config.input_chest)
if not input_chest then
print(colors.red .. "Failed to connect to input chest." .. colors.white)
break
end
-- 检查比较器信号
local comparator_level = comparator.getOutputSignal()
if comparator_level > 0 then
print(colors.blue .. "Comparator detected items in input chest." .. colors.white)
-- 遍历输入箱的所有槽位
local items_found = false
for slot = 1, 16 do
local item = input_inventory.getItemDetail(slot)
local item = input_chest.getItemDetail(slot)
if item then
items_found = true
print("Found item: " .. item.name)
@@ -170,7 +208,7 @@ local function autoSort()
local output_inventory = peripheral.wrap(chest_name)
if output_inventory then
print("Moving to '" .. category .. "' chest...")
input_inventory.pushItems(chest_name, slot)
input_chest.pushItems(chest_name, slot)
category_found = true
break
else
@@ -187,10 +225,9 @@ local function autoSort()
os.sleep(0.1)
end
end
-- 如果没有找到物品,稍作等待
if not items_found then
os.sleep(1)
else
-- 如果比较器没有检测到物品,稍作等待
os.sleep(0.5)
end
end
end)
@@ -223,8 +260,14 @@ local function showTutorial()
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("5. After setup, the program will automatically sort items:")
print(" - Items in the input chest will be moved to the appropriate output chest")
print(" - Based on the category names you assigned")
print(" - The comparator will detect when items are added to the input chest")
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("Tips:")
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
local INSTALLER_VERSION = "0.3.8 Beta 19"
local INSTALLER_VERSION = "0.3.8 Beta 20"
local DEFAULT_ROM_DIR = "/leonos"
print("Start loading LeonOS installer ("..INSTALLER_VERSION..")...")