Refactor inventory manager to simplify networking and dispense logic

This commit is contained in:
MayaTheShy
2026-03-15 16:21:58 -04:00
parent 2d53574181
commit 6a88721bad

View File

@@ -1,36 +1,9 @@
-- Inventory Manager: Auto-sort barrel, order & dispense via networked dropper
-- Main computer (networked). Computer 1 sits next to dropper_0 and runs dropperController.lua.
-- Inventory Manager: Auto-sort barrel & order items to dropper
-- Main computer (networked). Computer 1 sits next to dropper_0 and auto-dispenses.
local DROPPER_NAME = "minecraft:dropper_9"
local BARREL_NAME = "minecraft:barrel_0"
local MODEM_SIDE = nil -- auto-detected
local CONTROLLER_ID = 1 -- computer ID of the dropper controller
local POLL_INTERVAL = 2 -- seconds between barrel checks
local PROTOCOL = "inventory"
-------------------------------------------------
-- Networking
-------------------------------------------------
-- Find and open the modem
local function setupModem()
for _, side in ipairs({"top","bottom","left","right","front","back"}) do
if peripheral.getType(side) == "modem" then
rednet.open(side)
MODEM_SIDE = side
return true
end
end
-- Also check for wired modems wrapped as peripherals
for _, name in ipairs(peripheral.getNames()) do
if peripheral.getType(name) == "modem" then
rednet.open(name)
MODEM_SIDE = name
return true
end
end
return false
end
-------------------------------------------------
-- Inventory helpers
@@ -126,16 +99,11 @@ local function sortBarrel()
end
-------------------------------------------------
-- Order & dispense
-- Order
-------------------------------------------------
-- Send redstone trigger to computer 1 via rednet
local function triggerDropper()
rednet.send(CONTROLLER_ID, "dispense", PROTOCOL)
print("[NET] Sent dispense signal to computer " .. CONTROLLER_ID)
end
-- Order items: move from chest(s) to dropper, then trigger dispense
-- Order items: move from chest(s) to dropper
-- Computer 1 will auto-detect and dispense
local function orderItem(itemName, amount)
local catalogue = buildCatalogue()
@@ -174,8 +142,7 @@ local function orderItem(itemName, amount)
print(string.format("[WARN] Only moved %d/%d of %s", amount - remaining, amount, itemName))
end
-- Trigger computer 1 to fire the dropper
triggerDropper()
print("[OK] Items in dropper. Computer 1 will dispense.")
return true
end
@@ -213,13 +180,6 @@ local function main()
print("=================================")
print("")
-- Setup networking
if not setupModem() then
print("[WARN] No modem found. Dispense signals won't work.")
else
print("[OK] Modem opened on: " .. tostring(MODEM_SIDE))
end
-- Check dropper
if peripheral.wrap(DROPPER_NAME) then
print("[OK] Dropper found: " .. DROPPER_NAME)