Compare commits

...

2 Commits

2 changed files with 28 additions and 77 deletions

View File

@@ -1,27 +1,15 @@
-- Dropper Controller: Runs on Computer 1 (next to dropper_0)
-- Listens for rednet "dispense" messages and fires redstone to the back.
-- Watches the dropper and pulses redstone until it's empty.
local REDSTONE_SIDE = "back" -- side facing dropper_0
local PULSE_TIME = 0.3 -- redstone pulse duration in seconds
local PROTOCOL = "inventory"
local POLL_INTERVAL = 0.5 -- how often to check the dropper
local DROPPER_SIDE = "back" -- side where the dropper is (as peripheral)
-------------------------------------------------
-- Setup
-- Helpers
-------------------------------------------------
-- Open any available modem
local function setupModem()
for _, side in ipairs({"top","bottom","left","right","front","back"}) do
if peripheral.getType(side) == "modem" then
rednet.open(side)
print("[OK] Modem opened on: " .. side)
return true
end
end
print("[ERR] No modem found!")
return false
end
-- Send a redstone pulse to trigger the dropper
local function pulseRedstone()
redstone.setOutput(REDSTONE_SIDE, true)
@@ -29,6 +17,15 @@ local function pulseRedstone()
redstone.setOutput(REDSTONE_SIDE, false)
end
-- Check if the dropper has any items
local function dropperHasItems()
local dropper = peripheral.wrap(DROPPER_SIDE)
if not dropper then return false end
local contents = dropper.list()
if not contents then return false end
return next(contents) ~= nil
end
-------------------------------------------------
-- Main
-------------------------------------------------
@@ -37,28 +34,22 @@ print("=================================")
print(" Dropper Controller (ID " .. os.getComputerID() .. ")")
print("=================================")
print("")
print("Dropper side: " .. REDSTONE_SIDE)
print("Listening for dispense commands...")
print("Redstone side: " .. REDSTONE_SIDE)
print("Watching dropper for items...")
print("")
if not setupModem() then
print("Cannot continue without a modem.")
return
end
while true do
local senderID, message, protocol = rednet.receive(PROTOCOL)
if dropperHasItems() then
print("[DISPENSE] Items detected, firing dropper...")
if message == "dispense" then
print(string.format("[RX] Dispense from computer %d", senderID))
-- Pulse once per item slot in the dropper to eject everything
-- A dropper has 9 slots; pulse enough times to empty it
for i = 1, 9 do
-- Keep pulsing until the dropper is empty
while dropperHasItems() do
pulseRedstone()
sleep(0.1)
end
print("[OK] Dropper fired (9 pulses)")
print("[OK] Dropper empty.")
end
sleep(POLL_INTERVAL)
end

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)