Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- redstone_crafter_timer.lua
- -- Versión 2: ahora comprueba que haya al menos N items EN CADA slot monitorizado
- -- y activa un timer (pulsos de redstone) mientras **todos** los slots tengan >= N items.
- --
- -- Uso (parámetros opcionales):
- -- shell.run("redstone_crafter_timer", <crafterSide>, <redstoneSide>, <slots>, <minPerSlot>, <pulseDur>, <interval>)
- -- Ejemplo:
- -- shell.run("redstone_crafter_timer", "right", "right", "1,2,4,5,8", "2", "0.25", "1")
- -- - vigila los slots 1,2,4,5,8 y empezará a pulsar sólo cuando CADA uno tenga al menos 2 items.
- --
- -- Valores por defecto si no se pasan parámetros:
- -- crafterSide = "right"
- -- redstoneSide = "right"
- -- slots = {1,2,4,5,8}
- -- minPerSlot = 2
- -- pulseDur = 0.25
- -- interval = 1.0
- --
- -- El programa corre en bucle infinito. Para detenerlo: Ctrl+T (o cerrar el ordenador).
- local args = {...}
- local crafterSide = args[1] or "right"
- local redstoneSide = args[2] or crafterSide
- local slotsArg = args[3] or "1,2,4,5,8"
- local minPerSlot = tonumber(args[4]) or 2
- local pulseDur = tonumber(args[5]) or 0.25
- local interval = tonumber(args[6]) or 1.0
- local pollDelay = 0.3 -- tiempo entre comprobaciones cuando no está pulsando
- -- parse slots
- local function parseSlots(s)
- local t = {}
- for part in string.gmatch(s, "[^,]+") do
- local n = tonumber((part:gsub("%s+","")))
- if n and n > 0 then table.insert(t, n) end
- end
- return t
- end
- local slots = parseSlots(slotsArg)
- -- safe peripheral call
- local function tryCall(side, method, ...)
- local ok, res = pcall(peripheral.call, side, method, ...)
- if not ok then return nil, res end
- return res
- end
- -- Comprueba si cada slot monitorizado tiene AL MENOS minPerSlot items.
- -- Devuelve true, totalItems si la condición se cumple; false, info si no.
- local function eachSlotAtLeast(side, slotsToCheck, minCount)
- local inv = tryCall(side, "list")
- if not inv then return nil, "No se pudo leer inventario en '"..tostring(side).."'" end
- local total = 0
- for _, slot in ipairs(slotsToCheck) do
- local stack = inv[slot]
- local cnt = 0
- if stack then cnt = (stack.count or stack.size or 0) end
- if cnt < minCount then
- return false, {slot=slot, count=cnt}
- end
- total = total + cnt
- end
- return true, total
- end
- -- genera un pulso de redstone (setOutput) en el lado redstoneSide
- local function pulse(side, dur)
- redstone.setOutput(side, true)
- sleep(dur)
- redstone.setOutput(side, false)
- end
- -- imprime configuración inicial
- print("== Redstone Crafter Timer (each-slot mode) ==")
- print("Crafter side: " .. tostring(crafterSide))
- print("Redstone side: " .. tostring(redstoneSide))
- print("Slots monitoreados: " .. table.concat(slots, ","))
- print("Min per slot: " .. tostring(minPerSlot) .. " items -> empezará a pulsar cuando CADA slot tenga >= minPerSlot")
- print("Pulse dur: " .. tostring(pulseDur) .. "s, Interval: " .. tostring(interval) .. "s")
- print("Presiona Ctrl+T para detener.")
- -- BUCLE PRINCIPAL
- local pulsing = false
- while true do
- local ok, info = eachSlotAtLeast(crafterSide, slots, minPerSlot)
- if ok == nil then
- print("Error leyendo inventario: " .. tostring(info))
- sleep(2)
- else
- if ok then
- if not pulsing then
- print("Condición alcanzada: todos los slots tienen >= "..minPerSlot..". Iniciando pulsos...")
- pulsing = true
- end
- -- mientras la condición se mantenga, pulsamos periódicamente
- while true do
- local curOk, curInfo = eachSlotAtLeast(crafterSide, slots, minPerSlot)
- if curOk == nil then
- print("Error leyendo inventario durante pulso: "..tostring(curInfo))
- pulsing = false
- break
- end
- if not curOk then
- print("Un slot ya no cumple (slot "..tostring(curInfo.slot).." tiene "..tostring(curInfo.count).." items). Deteniendo pulsos.")
- pulsing = false
- break
- end
- pulse(redstoneSide, pulseDur)
- sleep(interval)
- end
- else
- if pulsing then
- print("Condición ha cesado. Dejando de pulsar.")
- pulsing = false
- end
- sleep(pollDelay)
- end
- end
- end
Add Comment
Please, Sign In to add comment