firex20

craft_stone_axe

Nov 7th, 2025 (edited)
17
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 5.53 KB | Gaming | 0 0
  1. -- cc_tweaked_stone_axe.lua
  2. -- Versión 4: bucle continuo, pausa cuando el cofre de salida está lleno y reanuda al detectarse espacio.
  3. -- Asume: cofre de materiales a la izquierda, crafter a la derecha, cofre de salida encima (top) — configurable.
  4.  
  5. local leftSide   = "left"   -- cofre con materiales
  6. local rightSide  = "right"  -- crafter
  7. local outputSide = "top"    -- cofre/barrel donde el crafter deposita las hachas
  8. local sleepTime  = 0.2
  9.  
  10. local names = {
  11.   cobblestone = {"cobblestone", "minecraft:cobblestone"},
  12.   stick       = {"stick", "minecraft:stick"}
  13. }
  14.  
  15. local recipe = {
  16.   {item = "cobblestone", count = 1, destSlot = 1},
  17.   {item = "cobblestone", count = 1, destSlot = 2},
  18.   {item = "cobblestone", count = 1, destSlot = 4},
  19.   {item = "stick",       count = 1, destSlot = 5},
  20.   {item = "stick",       count = 1, destSlot = 8}
  21. }
  22.  
  23. -- UTIL: llama a peripheral.call en modo seguro
  24. local function pcallPeripheral(side, method, ...)
  25.   local ok, res = pcall(peripheral.call, side, method, ...)
  26.   if not ok then return nil, res end
  27.   return res
  28. end
  29.  
  30. -- Devuelve true si el inventario `side` está completamente lleno (sin huecos ni stacks incompletos).
  31. -- Estrategia: usa size() para conocer número de ranuras (si está disponible) y getItemDetail(slot)
  32. -- Si alguna ranura es nil -> hay hueco.
  33. -- Si alguna ranura tiene count < maxCount -> hay hueco.
  34. -- Si no es posible consultar size/getItemDetail intentamos deducir a partir de list().
  35. local function isInventoryFull(side)
  36.   -- intentar size()
  37.   local size = pcallPeripheral(side, "size")
  38.   if size and type(size) == "number" and size > 0 then
  39.     for i = 1, size do
  40.       local stack = pcallPeripheral(side, "getItemDetail", i)
  41.       if not stack then
  42.         -- ranura vacía -> no está lleno
  43.         return false
  44.       else
  45.         local count = stack.count or stack.size or 0
  46.         local maxCount = stack.maxCount or stack.maxSize or 64
  47.         if count < maxCount then
  48.           return false
  49.         end
  50.       end
  51.     end
  52.     return true -- todas las ranuras ocupadas al máximo
  53.   end
  54.  
  55.   -- fallback: usar list() y ver si existe alguna ranura no mostrada o con espacio
  56.   local list = pcallPeripheral(side, "list")
  57.   if not list then
  58.     -- no se pudo consultar el inventario (no es un periférico de inventario compatible)
  59.     return false -- asumimos que no está 'lleno' para no bloquear el proceso
  60.   end
  61.  
  62.   -- si list() no devuelve entradas (vacío) -> no está lleno
  63.   local anySlot = false
  64.   for _, stack in pairs(list) do
  65.     anySlot = true
  66.     local count = stack.count or stack.size or 0
  67.     local maxCount = stack.maxCount or stack.maxSize or 64
  68.     if count < maxCount then return false end
  69.   end
  70.   if not anySlot then return false end
  71.  
  72.   -- si llegamos aquí y list existía, pero no podemos saber el número total de ranuras,
  73.   -- asumimos que está lleno (típico cuando list devuelve sólo ranuras ocupadas y todas al maximo)
  74.   return true
  75. end
  76.  
  77. -- busca slots en el inventario izquierdo que contengan alguno de nameList
  78. local function findSlotsWith(side, nameList)
  79.   local inv = pcallPeripheral(side, "list") or {}
  80.   local out = {}
  81.   for slot, stack in pairs(inv) do
  82.     local nm = stack.name or stack.displayName or stack.id
  83.     if nm then
  84.       for _, n in ipairs(nameList) do
  85.         if nm == n then out[slot] = (stack.count or stack.size or 0) end
  86.       end
  87.     end
  88.   end
  89.   return out
  90. end
  91.  
  92. local function moveToCrafter(slotFrom, count, destSlot)
  93.   local ok, err = pcall(peripheral.call, leftSide, "pushItems", rightSide, slotFrom, count, destSlot)
  94.   if not ok then return false, err end
  95.   sleep(sleepTime)
  96.   return true
  97. end
  98.  
  99. local function placeRecipe()
  100.   local crafter = pcallPeripheral(rightSide, "list") or {}
  101.   for _, r in ipairs(recipe) do
  102.     local stack = crafter[r.destSlot]
  103.     local has = 0
  104.     if stack then has = stack.count or stack.size or 0 end
  105.     if has < r.count then
  106.       local needed = r.count - has
  107.       local candidates = names[r.item]
  108.       local slots = findSlotsWith(leftSide, candidates)
  109.       for slotFrom, available in pairs(slots) do
  110.         if needed <= 0 then break end
  111.         local toMove = math.min(available, needed)
  112.         local ok, err = moveToCrafter(slotFrom, toMove, r.destSlot)
  113.         if not ok then return false, err end
  114.         needed = needed - toMove
  115.       end
  116.       if needed > 0 then return false, "Faltan materiales para " .. r.item end
  117.     end
  118.   end
  119.   return true
  120. end
  121.  
  122. local function pulseRedstone(side, duration)
  123.   duration = duration or 0.5
  124.   -- si el ordenador no tiene salida en ese lado, setOutput no hará nada, pcall no es necesario
  125.   redstone.setOutput(side, true)
  126.   sleep(duration)
  127.   redstone.setOutput(side, false)
  128. end
  129.  
  130. -- BUCLE PRINCIPAL con pausa automática si output lleno
  131. print("Sistema automático de fabricación de hachas iniciado.")
  132. while true do
  133.   -- 1) si el inventario de salida está lleno, esperamos hasta que tenga espacio
  134.   local full = isInventoryFull(outputSide)
  135.   if full then
  136.     print("El inventario de salida ('" .. tostring(outputSide) .. "') está lleno. Pausando hasta que haya espacio...")
  137.     -- bucle de espera: comprobar cada N segundos si ya hay hueco
  138.     repeat
  139.       sleep(2)
  140.     until not isInventoryFull(outputSide)
  141.     print("Espacio detectado en el inventario de salida. Reanudando producción.")
  142.   end
  143.  
  144.   -- 2) intentamos colocar la receta y activar el crafter
  145.   local ok, err = placeRecipe()
  146.   if ok then
  147.     pulseRedstone(rightSide, 0.3)
  148.   else
  149.     print("Faltan materiales: " .. tostring(err))
  150.   end
  151.  
  152.   sleep(2)
  153. end
  154.  
Add Comment
Please, Sign In to add comment