firex20

crafter_redstone_timer

Nov 7th, 2025 (edited)
19
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.16 KB | Gaming | 0 0
  1. -- redstone_crafter_timer.lua
  2. -- Versión 2: ahora comprueba que haya al menos N items EN CADA slot monitorizado
  3. -- y activa un timer (pulsos de redstone) mientras **todos** los slots tengan >= N items.
  4. --
  5. -- Uso (parámetros opcionales):
  6. --   shell.run("redstone_crafter_timer", <crafterSide>, <redstoneSide>, <slots>, <minPerSlot>, <pulseDur>, <interval>)
  7. -- Ejemplo:
  8. --   shell.run("redstone_crafter_timer", "right", "right", "1,2,4,5,8", "2", "0.25", "1")
  9. --   - vigila los slots 1,2,4,5,8 y empezará a pulsar sólo cuando CADA uno tenga al menos 2 items.
  10. --
  11. -- Valores por defecto si no se pasan parámetros:
  12. --   crafterSide = "right"
  13. --   redstoneSide = "right"
  14. --   slots = {1,2,4,5,8}
  15. --   minPerSlot = 2
  16. --   pulseDur = 0.25
  17. --   interval = 1.0
  18. --
  19. -- El programa corre en bucle infinito. Para detenerlo: Ctrl+T (o cerrar el ordenador).
  20.  
  21. local args = {...}
  22. local crafterSide  = args[1] or "right"
  23. local redstoneSide = args[2] or crafterSide
  24. local slotsArg     = args[3] or "1,2,4,5,8"
  25. local minPerSlot   = tonumber(args[4]) or 2
  26. local pulseDur     = tonumber(args[5]) or 0.25
  27. local interval     = tonumber(args[6]) or 1.0
  28. local pollDelay    = 0.3 -- tiempo entre comprobaciones cuando no está pulsando
  29.  
  30. -- parse slots
  31. local function parseSlots(s)
  32.   local t = {}
  33.   for part in string.gmatch(s, "[^,]+") do
  34.     local n = tonumber((part:gsub("%s+","")))
  35.     if n and n > 0 then table.insert(t, n) end
  36.   end
  37.   return t
  38. end
  39. local slots = parseSlots(slotsArg)
  40.  
  41. -- safe peripheral call
  42. local function tryCall(side, method, ...)
  43.   local ok, res = pcall(peripheral.call, side, method, ...)
  44.   if not ok then return nil, res end
  45.   return res
  46. end
  47.  
  48. -- Comprueba si cada slot monitorizado tiene AL MENOS minPerSlot items.
  49. -- Devuelve true, totalItems si la condición se cumple; false, info si no.
  50. local function eachSlotAtLeast(side, slotsToCheck, minCount)
  51.   local inv = tryCall(side, "list")
  52.   if not inv then return nil, "No se pudo leer inventario en '"..tostring(side).."'" end
  53.   local total = 0
  54.   for _, slot in ipairs(slotsToCheck) do
  55.     local stack = inv[slot]
  56.     local cnt = 0
  57.     if stack then cnt = (stack.count or stack.size or 0) end
  58.     if cnt < minCount then
  59.       return false, {slot=slot, count=cnt}
  60.     end
  61.     total = total + cnt
  62.   end
  63.   return true, total
  64. end
  65.  
  66. -- genera un pulso de redstone (setOutput) en el lado redstoneSide
  67. local function pulse(side, dur)
  68.   redstone.setOutput(side, true)
  69.   sleep(dur)
  70.   redstone.setOutput(side, false)
  71. end
  72.  
  73. -- imprime configuración inicial
  74. print("== Redstone Crafter Timer (each-slot mode) ==")
  75. print("Crafter side: " .. tostring(crafterSide))
  76. print("Redstone side: " .. tostring(redstoneSide))
  77. print("Slots monitoreados: " .. table.concat(slots, ","))
  78. print("Min per slot: " .. tostring(minPerSlot) .. " items -> empezará a pulsar cuando CADA slot tenga >= minPerSlot")
  79. print("Pulse dur: " .. tostring(pulseDur) .. "s, Interval: " .. tostring(interval) .. "s")
  80. print("Presiona Ctrl+T para detener.")
  81.  
  82. -- BUCLE PRINCIPAL
  83. local pulsing = false
  84. while true do
  85.   local ok, info = eachSlotAtLeast(crafterSide, slots, minPerSlot)
  86.   if ok == nil then
  87.     print("Error leyendo inventario: " .. tostring(info))
  88.     sleep(2)
  89.   else
  90.     if ok then
  91.       if not pulsing then
  92.         print("Condición alcanzada: todos los slots tienen >= "..minPerSlot..". Iniciando pulsos...")
  93.         pulsing = true
  94.       end
  95.       -- mientras la condición se mantenga, pulsamos periódicamente
  96.       while true do
  97.         local curOk, curInfo = eachSlotAtLeast(crafterSide, slots, minPerSlot)
  98.         if curOk == nil then
  99.           print("Error leyendo inventario durante pulso: "..tostring(curInfo))
  100.           pulsing = false
  101.           break
  102.         end
  103.         if not curOk then
  104.           print("Un slot ya no cumple (slot "..tostring(curInfo.slot).." tiene "..tostring(curInfo.count).." items). Deteniendo pulsos.")
  105.           pulsing = false
  106.           break
  107.         end
  108.         pulse(redstoneSide, pulseDur)
  109.         sleep(interval)
  110.       end
  111.     else
  112.       if pulsing then
  113.         print("Condición ha cesado. Dejando de pulsar.")
  114.         pulsing = false
  115.       end
  116.       sleep(pollDelay)
  117.     end
  118.   end
  119. end
  120.  
Add Comment
Please, Sign In to add comment