ame824

Minecolonies and ME integration (advanced peripherals)

Apr 23rd, 2026
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.45 KB | Gaming | 0 0
  1. -- ==================== INIT ====================
  2. local integrator = peripheral.find("colonyIntegrator")
  3. local bridge = peripheral.find("meBridge")
  4.  
  5. local monitors = { peripheral.find("monitor") }
  6.  
  7. local monitorOrders   -- für Work Orders
  8. local monitorRequests -- für Requests
  9.  
  10. if #monitors == 0 then
  11.     error("Kein Monitor gefunden!")
  12. elseif #monitors == 1 then
  13.     monitorOrders = monitors[1]
  14.     monitorRequests = monitors[1]
  15.     print("Nur 1 Monitor gefunden → alles auf einen Monitor")
  16. else
  17.     monitorOrders = monitors[1]     -- Erster Monitor = Work Orders
  18.     monitorRequests = monitors[2]   -- Zweiter Monitor = Requests
  19.     print("2 Monitore gefunden → SPLIT_MODE aktiviert")
  20. end
  21.  
  22. if not integrator then error("No colonyIntegrator!") end
  23. if not bridge then error("No ME Bridge!") end
  24.  
  25. -- ==================== SETTINGS ====================
  26. local CONFIG = {
  27.     EXPORT_DIRECTION = "down",
  28.     LOOP_INTERVAL = 300,
  29.     AUTO_EXPORT = true,
  30.     AUTO_CRAFT = true,
  31.     CLEAR_MONITOR = true,
  32.     SPLIT_MODE = (#monitors >= 2)   -- automatisch aktiv, wenn 2 Monitore da sind
  33. }
  34.  
  35. -- ==================== DEBUG ====================
  36. local function dbg(msg)
  37.     print("[DEBUG] " .. tostring(msg))
  38. end
  39.  
  40. -- ==================== UI ====================
  41. local UI = {}
  42.  
  43. function UI.init(monitor)
  44.     if CONFIG.CLEAR_MONITOR then monitor.clear() end
  45.     monitor.setCursorPos(1, 1)
  46.     monitor.setTextScale(0.5)        -- Gut für die meisten Monitore
  47.     return { monitor = monitor, line = 1 }
  48. end
  49.  
  50. function UI.write(ui, text, color)
  51.     if not ui or not ui.monitor then return end
  52.     ui.monitor.setCursorPos(1, ui.line)
  53.     ui.monitor.setTextColor(color or colors.white)
  54.     ui.monitor.write(tostring(text or ""):sub(1, 60))
  55.     ui.line = ui.line + 1
  56. end
  57.  
  58. function UI.hr(ui)
  59.     UI.write(ui, string.rep("-", 52), colors.gray)
  60. end
  61.  
  62. -- ==================== ME SYSTEM ====================
  63. local ME = {}
  64.  
  65. function ME.getCount(r)
  66.     if not r then return 0 end
  67.     local item = r.item or {}
  68.     if item.name then
  69.         local res = bridge.getItem({ name = item.name })
  70.         return res and res.amount or 0
  71.     end
  72.     return 0
  73. end
  74.  
  75. function ME.export(r, amount)
  76.     if not CONFIG.AUTO_EXPORT then return false end
  77.     local item = r.item
  78.     if not item or not item.name then return false end
  79.    
  80.     dbg("Export: " .. item.name .. " x" .. amount)
  81.    
  82.     local exported = 0
  83.     local retries = 0
  84.     while exported < amount and retries < 15 do
  85.         local ok, result = pcall(function()
  86.             return bridge.exportItem({ name = item.name, count = 1 }, CONFIG.EXPORT_DIRECTION)
  87.         end)
  88.         if ok and result and result > 0 then
  89.             exported = exported + 1
  90.             retries = 0
  91.         else
  92.             retries = retries + 1
  93.             sleep(0.5)
  94.         end
  95.         sleep(0.1)
  96.     end
  97.     return exported > 0
  98. end
  99.  
  100. function ME.isCraftable(r)
  101.     if not r then return false end
  102.     local item = r.item
  103.     if not item or not item.name then return false end
  104.     local ok, result = pcall(function()
  105.         return bridge.isItemCraftable({ name = item.name })
  106.     end)
  107.     return ok and result == true
  108. end
  109.  
  110. function ME.craft(r, amount)
  111.     if not CONFIG.AUTO_CRAFT then return false end
  112.     local item = r.item
  113.     if not item or not item.name then return false end
  114.     local ok = pcall(function()
  115.         bridge.craftItem({ name = item.name, count = amount })
  116.     end)
  117.     if ok then
  118.         dbg("Craft started: " .. item.name)
  119.     end
  120.     return ok
  121. end
  122.  
  123. -- ==================== COLONY ====================
  124. local Colony = {}
  125.  
  126. function Colony.getOrders()
  127.     local orders = integrator.getWorkOrders() or {}
  128.     dbg("Orders: " .. #orders)
  129.     return orders
  130. end
  131.  
  132. function Colony.getRequests()
  133.     local requests = integrator.getRequests() or {}
  134.     dbg("Requests: " .. #requests)
  135.     return requests
  136. end
  137.  
  138. function Colony.getRequestInfo(req)
  139.     if not req then return "Unknown Request" end
  140.    
  141.     local name = req.name or req.desc or "Unbekannter Request"
  142.     local count = req.count or 1
  143.     local state = req.state or "UNKNOWN"
  144.    
  145.     -- Wenn es Items gibt, den ersten Item-Namen anzeigen
  146.     local itemName = "?"
  147.     if req.items and req.items[1] then
  148.         local item = req.items[1]
  149.         itemName = (item.displayName or item.name or "?"):gsub("^.*:", ""):gsub("_", " ")
  150.     end
  151.    
  152.     return string.format("%s x%d  [%s]  → %s", name, count, state, itemName)
  153. end
  154.  
  155. function Colony.getResources(order)
  156.     local builder = order.builder
  157.     local ok, res = pcall(function()
  158.         return integrator.getBuilderResources(builder)
  159.     end)
  160.     if ok then
  161.         return res or {}
  162.     end
  163.     return {}
  164. end
  165.  
  166. function Colony.getRequestItem(req)
  167.     if not req or not req.items or not req.items[1] then
  168.         return nil
  169.     end
  170.     return req.items[1]   -- das eigentliche Item-Objekt
  171. end
  172.  
  173. function Colony.getBuilderName(builder)
  174.     if type(builder) == "string" then return builder end
  175.     if type(builder) == "table" then return builder.name or "Builder" end
  176.     return "Unknown"
  177. end
  178.  
  179. -- ==================== LOGIC ====================
  180. local Logic = {}
  181.  
  182. function Logic.handleResource(r, ui)
  183.     local name = (r.displayName or "?"):gsub("^.*:", ""):gsub("_"," ")
  184.     local need = r.needed or 0
  185.     local delivered = r.delivered or 0
  186.     local status = r.status or "UNKNOWN"
  187.     local missing = need - delivered
  188.  
  189.     if status ~= "DONT_HAVE" then
  190.         UI.write(ui, " " .. name .. " [" .. status .. "]", colors.gray)
  191.         return 0, 0
  192.     end
  193.  
  194.     local haveME = ME.getCount(r)
  195.  
  196.     if haveME >= missing then
  197.         UI.write(ui, " " .. name .. " missing " .. missing, colors.red)
  198.         if ME.export(r, missing) then
  199.             UI.write(ui, " -> Export OK", colors.lime)
  200.             return 1, 0
  201.         else
  202.             UI.write(ui, " -> Export FAIL", colors.orange)
  203.         end
  204.     else
  205.         UI.write(ui, " " .. name .. " missing " .. missing .. " (ME:" .. haveME .. ")", colors.orange)
  206.         local craftable = ME.isCraftable(r)
  207.         if craftable then
  208.             UI.write(ui, " [CRAFTABLE]", colors.cyan)
  209.             if ME.craft(r, missing) then
  210.                 UI.write(ui, " -> Craft started", colors.yellow)
  211.                 return 0, 1
  212.             else
  213.                 UI.write(ui, " -> Craft FAIL", colors.red)
  214.             end
  215.         else
  216.             UI.write(ui, " [NOT CRAFTABLE]", colors.red)
  217.         end
  218.     end
  219.     return 0, 0
  220. end
  221.  
  222. function Logic.handleWorkOrder(order, ui)
  223.     local builderName = Colony.getBuilderName(order.builder)
  224.     UI.write(ui, builderName, colors.white)
  225.  
  226.     local resources = Colony.getResources(order)
  227.     if #resources == 0 then
  228.         UI.write(ui, " (keine Resourcen-Daten)", colors.gray)
  229.         return 0, 0
  230.     end
  231.  
  232.     local exp, craft = 0, 0
  233.     for _, r in ipairs(resources) do
  234.         local e, c = Logic.handleResource(r, ui)
  235.         exp = exp + e
  236.         craft = craft + c
  237.     end
  238.     UI.write(ui, " ")
  239.     return exp, craft
  240. end
  241.  
  242. function Logic.handleRequest(req, ui)
  243.     if not req then return 0, 0 end
  244.  
  245.     local state = req.state or "UNKNOWN"
  246.     local reqName = req.name or "Request"
  247.     local count = req.count or 1
  248.  
  249.     -- Zeile 1: Allgemeine Request-Info
  250.     UI.write(ui, reqName .. " x" .. count .. "  [" .. state .. "]", colors.lightBlue)
  251.  
  252.     if state ~= "IN_PROGRESS" and state ~= "OPEN" then
  253.         UI.write(ui, "  (Status übersprungen)", colors.gray)
  254.         UI.write(ui, " ")
  255.         return 0, 0
  256.     end
  257.  
  258.     local possibleItems = req.items or {}
  259.     if #possibleItems == 0 then
  260.         UI.write(ui, "  Keine Items im Request", colors.red)
  261.         UI.write(ui, " ")
  262.         return 0, 0
  263.     end
  264.  
  265.     local exp, craft = 0, 0
  266.     local fulfilled = false
  267.  
  268.     -- Wir gehen alle Alternativen durch und nehmen die erste, die im ME verfügbar oder craftbar ist
  269.     for i, item in ipairs(possibleItems) do
  270.         if not item or not item.name then goto continue end
  271.  
  272.         local displayName = (item.displayName or item.name):gsub("^.*:", ""):gsub("_", " ")
  273.         local haveME = ME.getCount({item = item})
  274.  
  275.         UI.write(ui, "  Variante " .. i .. ": " .. displayName .. " (ME: " .. haveME .. ")", colors.white)
  276.  
  277.         if haveME >= count then
  278.             UI.write(ui, "    -> Genug im ME! Exportiere...", colors.red)
  279.             if ME.export({item = item}, count) then
  280.                 UI.write(ui, "    -> Export ERFOLGREICH", colors.lime)
  281.                 exp = exp + 1
  282.                 fulfilled = true
  283.                 break
  284.             else
  285.                 UI.write(ui, "    -> Export fehlgeschlagen", colors.orange)
  286.             end
  287.  
  288.         elseif ME.isCraftable({item = item}) then
  289.             UI.write(ui, "    -> Craftbar! Starte Craft...", colors.cyan)
  290.             if ME.craft({item = item}, count) then
  291.                 UI.write(ui, "    -> Craft gestartet", colors.yellow)
  292.                 craft = craft + 1
  293.                 fulfilled = true
  294.                 break
  295.             else
  296.                 UI.write(ui, "    -> Craft fehlgeschlagen", colors.red)
  297.             end
  298.         else
  299.             UI.write(ui, "    -> Weder vorhanden noch craftbar", colors.gray)
  300.         end
  301.  
  302.         ::continue::
  303.     end
  304.  
  305.     if not fulfilled then
  306.         UI.write(ui, "  Keine Variante konnte erfüllt werden", colors.orange)
  307.     end
  308.  
  309.     UI.write(ui, " ")
  310.     return exp, craft
  311. end
  312.  
  313. -- ==================== MAIN UPDATE ====================
  314. local function update()
  315.     local uiOrders = UI.init(monitorOrders)
  316.     local uiRequests = CONFIG.SPLIT_MODE and UI.init(monitorRequests) or uiOrders
  317.  
  318.     -- === WORK ORDERS ===
  319.     UI.write(uiOrders, "=== WORK ORDERS ===", colors.cyan)
  320.     UI.hr(uiOrders)
  321.  
  322.     local orders = Colony.getOrders()
  323.     local totalExp = 0
  324.     local totalCraft = 0
  325.  
  326.     if #orders == 0 then
  327.         UI.write(uiOrders, "Keine Work Orders", colors.lime)
  328.     else
  329.         UI.write(uiOrders, "WorkOrders: " .. #orders, colors.cyan)
  330.         for _, order in ipairs(orders) do
  331.             local e, c = Logic.handleWorkOrder(order, uiOrders)
  332.             totalExp = totalExp + e
  333.             totalCraft = totalCraft + c
  334.         end
  335.     end
  336.     UI.hr(uiOrders)
  337.  
  338.     -- === REQUESTS ===
  339.     if CONFIG.SPLIT_MODE then
  340.         UI.write(uiRequests, "=== REQUESTS ===", colors.cyan)
  341.         UI.hr(uiRequests)
  342.     else
  343.         UI.write(uiOrders, "=== REQUESTS ===", colors.cyan)
  344.         UI.hr(uiOrders)
  345.     end
  346.  
  347.     local requests = Colony.getRequests()
  348.  
  349.     if #requests == 0 then
  350.         UI.write(CONFIG.SPLIT_MODE and uiRequests or uiOrders, "Keine Requests", colors.lime)
  351.     else
  352.         UI.write(CONFIG.SPLIT_MODE and uiRequests or uiOrders, "Requests: " .. #requests, colors.cyan)
  353.         for _, req in ipairs(requests) do
  354.             Logic.handleRequest(req, CONFIG.SPLIT_MODE and uiRequests or uiOrders)
  355.         end
  356.     end
  357.  
  358.     -- Summary
  359.     UI.hr(uiOrders)
  360.     UI.write(uiOrders, "Export: " .. totalExp .. " | Craft: " .. totalCraft, colors.lime)
  361. end
  362.  
  363. -- ==================== LOOP ====================
  364. while true do
  365.     local ok, err = pcall(update)
  366.     if not ok then
  367.         print("ERROR: " .. tostring(err))
  368.         if monitorOrders then
  369.             monitorOrders.clear()
  370.             monitorOrders.setCursorPos(1,1)
  371.             monitorOrders.setTextColor(colors.red)
  372.             monitorOrders.write("ERROR!")
  373.             monitorOrders.setTextColor(colors.white)
  374.         end
  375.         sleep(5)
  376.     end
  377.     sleep(CONFIG.LOOP_INTERVAL)
  378. end
Advertisement
Add Comment
Please, Sign In to add comment