Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- cc_tweaked_stone_axe.lua
- -- Versión 4: bucle continuo, pausa cuando el cofre de salida está lleno y reanuda al detectarse espacio.
- -- Asume: cofre de materiales a la izquierda, crafter a la derecha, cofre de salida encima (top) — configurable.
- local leftSide = "left" -- cofre con materiales
- local rightSide = "right" -- crafter
- local outputSide = "top" -- cofre/barrel donde el crafter deposita las hachas
- local sleepTime = 0.2
- local names = {
- cobblestone = {"cobblestone", "minecraft:cobblestone"},
- stick = {"stick", "minecraft:stick"}
- }
- local recipe = {
- {item = "cobblestone", count = 1, destSlot = 1},
- {item = "cobblestone", count = 1, destSlot = 2},
- {item = "cobblestone", count = 1, destSlot = 4},
- {item = "stick", count = 1, destSlot = 5},
- {item = "stick", count = 1, destSlot = 8}
- }
- -- UTIL: llama a peripheral.call en modo seguro
- local function pcallPeripheral(side, method, ...)
- local ok, res = pcall(peripheral.call, side, method, ...)
- if not ok then return nil, res end
- return res
- end
- -- Devuelve true si el inventario `side` está completamente lleno (sin huecos ni stacks incompletos).
- -- Estrategia: usa size() para conocer número de ranuras (si está disponible) y getItemDetail(slot)
- -- Si alguna ranura es nil -> hay hueco.
- -- Si alguna ranura tiene count < maxCount -> hay hueco.
- -- Si no es posible consultar size/getItemDetail intentamos deducir a partir de list().
- local function isInventoryFull(side)
- -- intentar size()
- local size = pcallPeripheral(side, "size")
- if size and type(size) == "number" and size > 0 then
- for i = 1, size do
- local stack = pcallPeripheral(side, "getItemDetail", i)
- if not stack then
- -- ranura vacía -> no está lleno
- return false
- else
- local count = stack.count or stack.size or 0
- local maxCount = stack.maxCount or stack.maxSize or 64
- if count < maxCount then
- return false
- end
- end
- end
- return true -- todas las ranuras ocupadas al máximo
- end
- -- fallback: usar list() y ver si existe alguna ranura no mostrada o con espacio
- local list = pcallPeripheral(side, "list")
- if not list then
- -- no se pudo consultar el inventario (no es un periférico de inventario compatible)
- return false -- asumimos que no está 'lleno' para no bloquear el proceso
- end
- -- si list() no devuelve entradas (vacío) -> no está lleno
- local anySlot = false
- for _, stack in pairs(list) do
- anySlot = true
- local count = stack.count or stack.size or 0
- local maxCount = stack.maxCount or stack.maxSize or 64
- if count < maxCount then return false end
- end
- if not anySlot then return false end
- -- si llegamos aquí y list existía, pero no podemos saber el número total de ranuras,
- -- asumimos que está lleno (típico cuando list devuelve sólo ranuras ocupadas y todas al maximo)
- return true
- end
- -- busca slots en el inventario izquierdo que contengan alguno de nameList
- local function findSlotsWith(side, nameList)
- local inv = pcallPeripheral(side, "list") or {}
- local out = {}
- for slot, stack in pairs(inv) do
- local nm = stack.name or stack.displayName or stack.id
- if nm then
- for _, n in ipairs(nameList) do
- if nm == n then out[slot] = (stack.count or stack.size or 0) end
- end
- end
- end
- return out
- end
- local function moveToCrafter(slotFrom, count, destSlot)
- local ok, err = pcall(peripheral.call, leftSide, "pushItems", rightSide, slotFrom, count, destSlot)
- if not ok then return false, err end
- sleep(sleepTime)
- return true
- end
- local function placeRecipe()
- local crafter = pcallPeripheral(rightSide, "list") or {}
- for _, r in ipairs(recipe) do
- local stack = crafter[r.destSlot]
- local has = 0
- if stack then has = stack.count or stack.size or 0 end
- if has < r.count then
- local needed = r.count - has
- local candidates = names[r.item]
- local slots = findSlotsWith(leftSide, candidates)
- for slotFrom, available in pairs(slots) do
- if needed <= 0 then break end
- local toMove = math.min(available, needed)
- local ok, err = moveToCrafter(slotFrom, toMove, r.destSlot)
- if not ok then return false, err end
- needed = needed - toMove
- end
- if needed > 0 then return false, "Faltan materiales para " .. r.item end
- end
- end
- return true
- end
- local function pulseRedstone(side, duration)
- duration = duration or 0.5
- -- si el ordenador no tiene salida en ese lado, setOutput no hará nada, pcall no es necesario
- redstone.setOutput(side, true)
- sleep(duration)
- redstone.setOutput(side, false)
- end
- -- BUCLE PRINCIPAL con pausa automática si output lleno
- print("Sistema automático de fabricación de hachas iniciado.")
- while true do
- -- 1) si el inventario de salida está lleno, esperamos hasta que tenga espacio
- local full = isInventoryFull(outputSide)
- if full then
- print("El inventario de salida ('" .. tostring(outputSide) .. "') está lleno. Pausando hasta que haya espacio...")
- -- bucle de espera: comprobar cada N segundos si ya hay hueco
- repeat
- sleep(2)
- until not isInventoryFull(outputSide)
- print("Espacio detectado en el inventario de salida. Reanudando producción.")
- end
- -- 2) intentamos colocar la receta y activar el crafter
- local ok, err = placeRecipe()
- if ok then
- pulseRedstone(rightSide, 0.3)
- else
- print("Faltan materiales: " .. tostring(err))
- end
- sleep(2)
- end
Add Comment
Please, Sign In to add comment