Refactor inventory manager to simplify networking and dispense logic
This commit is contained in:
@@ -1,36 +1,9 @@
|
|||||||
-- Inventory Manager: Auto-sort barrel, order & dispense via networked dropper
|
-- Inventory Manager: Auto-sort barrel & order items to dropper
|
||||||
-- Main computer (networked). Computer 1 sits next to dropper_0 and runs dropperController.lua.
|
-- Main computer (networked). Computer 1 sits next to dropper_0 and auto-dispenses.
|
||||||
|
|
||||||
local DROPPER_NAME = "minecraft:dropper_9"
|
local DROPPER_NAME = "minecraft:dropper_9"
|
||||||
local BARREL_NAME = "minecraft:barrel_0"
|
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 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
|
-- Inventory helpers
|
||||||
@@ -126,16 +99,11 @@ local function sortBarrel()
|
|||||||
end
|
end
|
||||||
|
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
-- Order & dispense
|
-- Order
|
||||||
-------------------------------------------------
|
-------------------------------------------------
|
||||||
|
|
||||||
-- Send redstone trigger to computer 1 via rednet
|
-- Order items: move from chest(s) to dropper
|
||||||
local function triggerDropper()
|
-- Computer 1 will auto-detect and dispense
|
||||||
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
|
|
||||||
local function orderItem(itemName, amount)
|
local function orderItem(itemName, amount)
|
||||||
local catalogue = buildCatalogue()
|
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))
|
print(string.format("[WARN] Only moved %d/%d of %s", amount - remaining, amount, itemName))
|
||||||
end
|
end
|
||||||
|
|
||||||
-- Trigger computer 1 to fire the dropper
|
print("[OK] Items in dropper. Computer 1 will dispense.")
|
||||||
triggerDropper()
|
|
||||||
return true
|
return true
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -213,13 +180,6 @@ local function main()
|
|||||||
print("=================================")
|
print("=================================")
|
||||||
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
|
-- Check dropper
|
||||||
if peripheral.wrap(DROPPER_NAME) then
|
if peripheral.wrap(DROPPER_NAME) then
|
||||||
print("[OK] Dropper found: " .. DROPPER_NAME)
|
print("[OK] Dropper found: " .. DROPPER_NAME)
|
||||||
|
|||||||
Reference in New Issue
Block a user