- startup/client.lua now prompts on first run: asks if a dropper is next to the computer, and if so, which side it's on - Saves config to .dropper_config (JSON with dropperSide, redstoneSide, enabled) - Only launches dropperController if dropper is enabled - dropperController.lua reads .dropper_config for its sides instead of hardcoding 'back' - Delete .client_setup to re-run the setup wizard
76 lines
2.3 KiB
Lua
76 lines
2.3 KiB
Lua
-- Dropper Controller
|
|
-- Watches a dropper peripheral and pulses redstone until it's empty.
|
|
|
|
local DROPPER_SIDE = "back" -- side where the dropper peripheral is
|
|
local REDSTONE_SIDE = "back" -- side facing dropper (for redstone output)
|
|
local PULSE_TIME = 0.3 -- redstone pulse duration in seconds
|
|
local POLL_INTERVAL = 0.5 -- how often to check the dropper
|
|
|
|
-------------------------------------------------
|
|
-- Load config from file if present
|
|
-------------------------------------------------
|
|
|
|
local _baseDir = fs.getDir(shell.getRunningProgram())
|
|
local CONFIG_FILE = fs.combine(_baseDir, ".dropper_config")
|
|
|
|
if fs.exists(CONFIG_FILE) then
|
|
local f = fs.open(CONFIG_FILE, "r")
|
|
local raw = f.readAll()
|
|
f.close()
|
|
local ok, cfg = pcall(textutils.unserialiseJSON, raw)
|
|
if ok and cfg then
|
|
if cfg.dropperSide then DROPPER_SIDE = cfg.dropperSide end
|
|
if cfg.redstoneSide then REDSTONE_SIDE = cfg.redstoneSide end
|
|
if cfg.pulseTime then PULSE_TIME = cfg.pulseTime end
|
|
if cfg.pollInterval then POLL_INTERVAL = cfg.pollInterval end
|
|
end
|
|
end
|
|
|
|
-------------------------------------------------
|
|
-- Helpers
|
|
-------------------------------------------------
|
|
|
|
-- Send a redstone pulse to trigger the dropper
|
|
local function pulseRedstone()
|
|
redstone.setOutput(REDSTONE_SIDE, true)
|
|
sleep(PULSE_TIME)
|
|
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
|
|
-------------------------------------------------
|
|
|
|
print("=================================")
|
|
print(" Dropper Controller (ID " .. os.getComputerID() .. ")")
|
|
print("=================================")
|
|
print("")
|
|
print("Redstone side: " .. REDSTONE_SIDE)
|
|
print("Watching dropper for items...")
|
|
print("")
|
|
|
|
while true do
|
|
if dropperHasItems() then
|
|
print("[DISPENSE] Items detected, firing dropper...")
|
|
|
|
-- Keep pulsing until the dropper is empty
|
|
while dropperHasItems() do
|
|
pulseRedstone()
|
|
sleep(0.1)
|
|
end
|
|
|
|
print("[OK] Dropper empty.")
|
|
end
|
|
|
|
sleep(POLL_INTERVAL)
|
|
end
|