From 82a9fec46d792a8511afaa3de31309d87fb4aea1 Mon Sep 17 00:00:00 2001 From: SikongJueluo <1822250894@qq.com> Date: Fri, 21 Nov 2025 14:56:56 +0800 Subject: [PATCH] refactor(autocraft): improve peripheral initialization with retry logic --- src/autocraft/main.ts | 54 +++++++++++++++++++++++++++++-------------- 1 file changed, 37 insertions(+), 17 deletions(-) diff --git a/src/autocraft/main.ts b/src/autocraft/main.ts index e797b97..275e04e 100644 --- a/src/autocraft/main.ts +++ b/src/autocraft/main.ts @@ -15,7 +15,7 @@ import { const logger = new Logger({ processors: [ processor.filterByLevel(LogLevel.Info), - processor.addFullTimestamp(), + processor.addTimestamp(), ], renderer: textRenderer, streams: [new ConsoleStream()], @@ -33,22 +33,12 @@ const peripheralsNames = { packageExtractor: "create:packager_0", }; -const packsInventory = peripheral.wrap( - peripheralsNames.packsInventory, -) as InventoryPeripheral; -const itemsInventory = peripheral.wrap( - peripheralsNames.itemsInventory, -) as InventoryPeripheral; -const packageExtractor = peripheral.wrap( - peripheralsNames.packageExtractor, -) as InventoryPeripheral; -const blockReader = peripheral.wrap( - peripheralsNames.blockReader, -) as BlockReaderPeripheral; -const wiredModem = peripheral.wrap( - peripheralsNames.wiredModem, -) as WiredModemPeripheral; -const turtleLocalName = wiredModem.getNameLocal(); +let packsInventory: InventoryPeripheral; +let itemsInventory: InventoryPeripheral; +let packageExtractor: InventoryPeripheral; +let blockReader: BlockReaderPeripheral; +let wiredModem: WiredModemPeripheral; +let turtleLocalName: string; enum State { IDLE, @@ -57,9 +47,39 @@ enum State { } function main() { + while (true) { + try { + packsInventory = peripheral.wrap( + peripheralsNames.packsInventory, + ) as InventoryPeripheral; + itemsInventory = peripheral.wrap( + peripheralsNames.itemsInventory, + ) as InventoryPeripheral; + packageExtractor = peripheral.wrap( + peripheralsNames.packageExtractor, + ) as InventoryPeripheral; + blockReader = peripheral.wrap( + peripheralsNames.blockReader, + ) as BlockReaderPeripheral; + wiredModem = peripheral.wrap( + peripheralsNames.wiredModem, + ) as WiredModemPeripheral; + turtleLocalName = wiredModem.getNameLocal(); + + logger.info("Peripheral initialization complete..."); + break; + } catch (error) { + logger.warn( + `Peripheral initialization failed for ${String(error)}, try again...`, + ); + sleep(1); + } + } + const craftManager = new CraftManager(turtleLocalName, itemsInventory); const recipesQueue = new Queue(); const recipesWaitingMap = new Map(); + let currentState = State.IDLE; let nextState = State.IDLE; let hasPackage = redstone.getInput(peripheralsNames.redstone);