GLaDOS446

FARMCONTROL OC v2

Jun 10th, 2026 (edited)
26
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 11.57 KB | Source Code | 0 0
  1. local component = require("component")
  2. local event = require("event")
  3. local term = require("term")
  4. local serialization = require("serialization")
  5.  
  6. local gpu = component.gpu
  7.  
  8. -- AUTOMATISCHE AE2-SUCHE
  9. local ae2 = nil
  10. local ae2_type = nil
  11. for addr, ctype in component.list() do
  12.   if ctype and ctype:sub(1, 3) == "me_" then
  13.     ae2 = component.proxy(addr)
  14.     ae2_type = ctype
  15.     break
  16.   end
  17. end
  18.  
  19. -- CONFIG
  20. local PASSWORD = "admin"
  21. local SAVE_FILE = "/home/farm_config_v3.dat"
  22.  
  23. -- ECHTE SEITENNAMEN STATT HIMMELSRICHTUNGEN
  24. local sides = { [0]="Unten", [1]="Oben", [2]="Vorne", [3]="Hinten", [4]="Links", [5]="Rechts" }
  25. local farms = {}
  26. local selected_idx = 1
  27. local current_tab = "farms"
  28.  
  29. local function save_data()
  30.   local f = io.open(SAVE_FILE, "w")
  31.   if f then f:write(serialization.serialize(farms)); f:close() end
  32. end
  33.  
  34. local function load_data()
  35.   local f = io.open(SAVE_FILE, "r")
  36.   if f then
  37.     local content = f:read("*a")
  38.     f:close()
  39.     farms = serialization.unserialize(content) or {}
  40.   else
  41.     -- Standard-Dummy falls keine Datei existiert
  42.     farms = {{name="Feld 1", redstone_uuid="HIER_UUID_EINTRAGEN", sower_side=2, gath_side=3, sower_on=false, gath_on=false}}
  43.     save_data()
  44.   end
  45. end
  46.  
  47. local function update_redstone()
  48.   for _, farm in ipairs(farms) do
  49.     if farm.redstone_uuid and farm.redstone_uuid ~= "KEIN_BLOCK_GEFUNDEN" then
  50.       if component.type(farm.redstone_uuid) == "redstone" then
  51.         local proxy = component.proxy(farm.redstone_uuid)
  52.         if proxy then
  53.           proxy.setOutput(farm.sower_side, farm.sower_on and 15 or 0)
  54.           proxy.setOutput(farm.gath_side, farm.gath_on and 15 or 0)
  55.         end
  56.       end
  57.     end
  58.   end
  59. end
  60.  
  61. local function draw_text(x, y, text, fg, bg)
  62.   if fg then gpu.setForeground(fg) end
  63.   if bg then gpu.setBackground(bg) end
  64.   gpu.set(x, y, tostring(text))
  65. end
  66.  
  67. local function safe_read(mask)
  68.   local status, result = pcall(term.read, nil, nil, nil, mask)
  69.   if status and result then return result:gsub("\n", "") end
  70.   return ""
  71. end
  72.  
  73. local function do_login()
  74.   while true do
  75.     term.clear()
  76.     draw_text(20, 5, "=== SECURITY LOGIN ===", 0x00FFFF, 0x000000)
  77.     draw_text(15, 8, "Bitte Passwort eingeben:", 0xFFFFFF)
  78.     gpu.set(15, 10, "[                    ]")
  79.     term.setCursor(16, 10)
  80.     local input = safe_read("*")
  81.     if input == PASSWORD then break end
  82.     draw_text(17, 13, "FALSCHES PASSWORT!", 0xFF0000)
  83.     os.sleep(1.5)
  84.   end
  85. end
  86.  
  87. local function draw_navigation()
  88.   gpu.setResolution(75, 22)
  89.   gpu.setBackground(0x222222)
  90.   gpu.fill(1, 1, 75, 1, " ")
  91.  
  92.   local farms_bg = (current_tab == "farms") and 0x00E5FF or 0x222222
  93.   local farms_fg = (current_tab == "farms") and 0x000000 or 0xFFFFFF
  94.   draw_text(2, 1, " [1] FARMS & CONTROLLER ", farms_fg, farms_bg)
  95.  
  96.   local ae2_bg = (current_tab == "ae2") and 0x00E5FF or 0x222222
  97.   local ae2_fg = (current_tab == "ae2") and 0x000000 or 0xFFFFFF
  98.   draw_text(28, 1, " [2] AE2 SPEICHERANZEIGE ", ae2_fg, ae2_bg)
  99.  
  100.   gpu.setBackground(0x000000)
  101.   draw_text(1, 2, string.rep("-", 75), 0x555555)
  102. end
  103.  
  104. local function draw_dashboard()
  105.   draw_text(2, 4, "FARMLISTE:", 0xFFFF00)
  106.   for i, farm in ipairs(farms) do
  107.     local y = 4 + i
  108.     if i == selected_idx then
  109.       draw_text(2, y, "> " .. farm.name, 0x00FF00)
  110.     else
  111.       draw_text(4, y, farm.name, 0xFFFFFF)
  112.     end
  113.   end
  114.  
  115.   draw_text(2, 19, "[+] NEUE FARM ANLEGEN", 0x000000, 0x00FF00)
  116.   for y = 3, 20 do gpu.set(26, y, "|") end
  117.  
  118.   local f = farms[selected_idx]
  119.   if f then
  120.     draw_text(29, 4, "DETAILS: " .. f.name:upper(), 0x00E5FF, 0x000000)
  121.     local short_uuid = tostring(f.redstone_uuid):sub(1, 18) .. "..."
  122.     draw_text(29, 5, "Block-UUID: " .. short_uuid, 0x00FF00)
  123.     draw_text(29, 6, "Sower: " .. sides[f.sower_side] .. " | Ernter: " .. sides[f.gath_side], 0x888888)
  124.    
  125.     if f.sower_on then
  126.       draw_text(29, 9, "  SOWER: AKTIV   ", 0x000000, 0x00FF00)
  127.     else
  128.       draw_text(29, 9, "  SOWER: INAKTIV ", 0xFFFFFF, 0xFF0000)
  129.     end
  130.    
  131.     if f.gath_on then
  132.       draw_text(51, 9, " ERNTER: AKTIV   ", 0x000000, 0x00FF00)
  133.     else
  134.       draw_text(51, 9, " ERNTER: INAKTIV ", 0xFFFFFF, 0xFF0000)
  135.     end
  136.    
  137.     draw_text(29, 13, "[ BEIDE AN ]", 0x000000, 0xAAAAAA)
  138.     draw_text(46, 13, "[ BEIDE AUS ]", 0x000000, 0xAAAAAA)
  139.     draw_text(29, 17, "[ FARM COMPLETE LOESCHEN ]", 0xFFFFFF, 0x993333)
  140.   else
  141.     draw_text(29, 9, "Keine Farm aktiv. Klicke links auf [+].", 0x888888, 0x000000)
  142.   end
  143. end
  144.  
  145. local function draw_ae2_storage()
  146.   draw_text(2, 3, "AE2 NETZWERK-STATUS (Auto-Update: 5s)", 0x00E5FF, 0x000000)
  147.  
  148.   if not ae2 then
  149.     draw_text(2, 5, "FEHLER: Kein AE2-Bauteil am Adapter erkannt!", 0xFF0000)
  150.     return
  151.   end
  152.  
  153.   local usage_ok, usage = pcall(ae2.getAvgPowerUsage)
  154.   local stored_ok, stored = pcall(ae2.getStoredPower)
  155.   local max_ok, max_p = pcall(ae2.getMaxPowerStorage)
  156.  
  157.   local p_str = "Stromverbrauch: " .. (usage_ok and string.format("%.1f RF/t", usage * 2) or "N/A")
  158.   if stored_ok and max_ok then
  159.     p_str = p_str .. string.format(" | Speicher-Puffer: %d/%d RF", stored * 2, max_p * 2)
  160.   end
  161.   draw_text(2, 4, p_str, 0xFFFF00)
  162.   draw_text(2, 5, "Schnittstelle: " .. ae2_type, 0x888888)
  163.  
  164.   draw_text(1, 6, string.rep("-", 75), 0x555555)
  165.  
  166.   local item_ok, items = pcall(ae2.getItemsInNetwork)
  167.   if not item_ok or not items then item_ok, items = pcall(ae2.getAvailableItems) end
  168.   if not item_ok or not items then item_ok, items = pcall(ae2.getStoredItems) end
  169.  
  170.   local fluid_ok, fluids = pcall(ae2.getFluidsInNetwork)
  171.   if not fluid_ok or not fluids then fluid_ok, fluids = pcall(ae2.getAvailableFluids) end
  172.   if not fluid_ok or not fluids then fluid_ok, fluids = pcall(ae2.getStoredFluids) end
  173.  
  174.   for y = 7, 21 do gpu.set(38, y, "|") end
  175.  
  176.   -- Spalte 1: Items
  177.   draw_text(2, 7, "[ GEGENSTAENDE / ITEMS ]", 0x00E5FF)
  178.   draw_text(2, 8, "Name                    Menge", 0x555555)
  179.  
  180.   local item_row = 9
  181.   local item_count = 0
  182.   if item_ok and items then
  183.     for _, item in pairs(items) do
  184.       if item and (item.label or item.name) and item_count < 13 then
  185.         local label = item.label or item.name
  186.         local amount = item.size or item.amount or 0
  187.         if amount > 0 then
  188.           if #label > 22 then label = label:sub(1, 19) .. "..." end
  189.           draw_text(2, item_row, label, 0xFFFFFF)
  190.           draw_text(26, item_row, string.format("%d", amount), 0x00FF00)
  191.           item_row = item_row + 1
  192.           item_count = item_count + 1
  193.         end
  194.       end
  195.     end
  196.     if item_count == 0 then draw_text(2, 9, "Keine Items im System.", 0x888888) end
  197.   else
  198.     draw_text(2, 9, "[Fehler bei Item-Abfrage]", 0xFF0000)
  199.   end
  200.  
  201.   -- Spalte 2: Fluids
  202.   draw_text(40, 7, "[ FLUESSIGKEITEN / FLUIDS ]", 0x00E5FF)
  203.   draw_text(40, 8, "Name                    Menge", 0x555555)
  204.  
  205.   local fluid_row = 9
  206.   local fluid_count = 0
  207.   if fluid_ok and fluids then
  208.     for _, fluid in pairs(fluids) do
  209.       if fluid and (fluid.label or fluid.name) and fluid_count < 13 then
  210.         local label = fluid.label or fluid.name
  211.         local amount = fluid.amount or fluid.size or 0
  212.         if amount > 0 then
  213.           if #label > 22 then label = label:sub(1, 19) .. "..." end
  214.          
  215.           local amt_str = ""
  216.           if amount >= 1000 then
  217.             amt_str = string.format("%.1f B", amount / 1000)
  218.           else
  219.             amt_str = amount .. " mB"
  220.           end
  221.          
  222.           draw_text(40, fluid_row, label, 0xFFFFFF)
  223.           draw_text(64, fluid_row, amt_str, 0x00FFFF)
  224.           fluid_row = fluid_row + 1
  225.           fluid_count = fluid_count + 1
  226.         end
  227.       end
  228.     end
  229.     if fluid_count == 0 then draw_text(40, 9, "Keine Fluids im System.", 0x888888) end
  230.   else
  231.     draw_text(40, 9, "[Fehler bei Fluid-Abfrage]", 0xFF0000)
  232.   end
  233. end
  234.  
  235. local function refresh_screen()
  236.   term.clear()
  237.   draw_navigation()
  238.   if current_tab == "farms" then draw_dashboard()
  239.   elseif current_tab == "ae2" then draw_ae2_storage() end
  240. end
  241.  
  242. -- NEUES ANLEGE-MENÜ MIT MANUELLER UUID EINGABE
  243. local function add_farm_menu()
  244.   term.clear()
  245.   draw_text(5, 2, "=== NEUE FARM ANLEGEN ===", 0xFFFF00, 0x000000)
  246.  
  247.   draw_text(5, 5, "1. Name der Farm eingeben:")
  248.   term.setCursor(5, 6)
  249.   local name = safe_read()
  250.   if name == "" then name = "Feld " .. (#farms + 1) end
  251.  
  252.   draw_text(5, 9, "2. Redstone-Block UUID eingeben:")
  253.   term.setCursor(5, 10)
  254.   local uuid = safe_read()
  255.   if uuid == "" then uuid = "KEIN_BLOCK_GEFUNDEN" end
  256.  
  257.   local new_farm = {name=name, redstone_uuid=uuid, sower_side=2, gath_side=3, sower_on=false, gath_on=false}
  258.  
  259.   while true do
  260.     term.clear()
  261.     draw_text(5, 2, "=== SEITEN-KONFIGURATION ===", 0xFFFF00, 0x000000)
  262.     draw_text(5, 4, "Farm: " .. name, 0x00FF00)
  263.     draw_text(5, 5, "UUID: " .. uuid:sub(1, 20) .. "...", 0x888888)
  264.    
  265.     -- Strukturierte Klick-Flächen mit [ - ] und [ + ]
  266.     draw_text(5, 8,  "Sower-Seite :  [ - ]  " .. string.format("%-7s", sides[new_farm.sower_side]) .. "  [ + ]", 0xFFFFFF)
  267.     draw_text(5, 11, "Ernter-Seite:  [ - ]  " .. string.format("%-7s", sides[new_farm.gath_side]) .. "  [ + ]", 0xFFFFFF)
  268.    
  269.     draw_text(5, 16, "[ SPEICHERN ]", 0x000000, 0x00FF00)
  270.     draw_text(22, 16, "[ ABBRECHEN ]", 0xFFFFFF, 0xFF0000)
  271.    
  272.     local _, _, x, y = event.pull("touch")
  273.    
  274.     -- Klick-Erkennung für die Buttons
  275.     if y == 8 then
  276.       if x >= 20 and x <= 25 then new_farm.sower_side = (new_farm.sower_side - 1) % 6 end
  277.       if x >= 37 and x <= 44 then new_farm.sower_side = (new_farm.sower_side + 1) % 6 end
  278.     elseif y == 11 then
  279.       if x >= 20 and x <= 25 then new_farm.gath_side = (new_farm.gath_side - 1) % 6 end
  280.       if x >= 37 and x <= 44 then new_farm.gath_side = (new_farm.gath_side + 1) % 6 end
  281.     elseif y == 16 and x >= 5 and x <= 17 then
  282.       table.insert(farms, new_farm)
  283.       save_data()
  284.       selected_idx = #farms
  285.       break
  286.     elseif y == 16 and x >= 22 and x <= 34 then
  287.       break
  288.     end
  289.   end
  290.   update_redstone()
  291. end
  292.  
  293. load_data()
  294. do_login()
  295. update_redstone()
  296. refresh_screen()
  297.  
  298. while true do
  299.   local id, _, x, y = event.pull(5, "touch")
  300.  
  301.   if id == "touch" then
  302.     if y == 1 then
  303.       if x >= 2 and x <= 25 then current_tab = "farms"; refresh_screen()
  304.       elseif x >= 28 and x <= 52 then current_tab = "ae2"; refresh_screen() end
  305.     end
  306.    
  307.     if current_tab == "farms" then
  308.       if x >= 2 and x <= 24 and y >= 5 and y <= (4 + #farms) then
  309.         selected_idx = y - 4
  310.         refresh_screen()
  311.       end
  312.       if x >= 2 and x <= 24 and y == 19 then
  313.         add_farm_menu()
  314.         refresh_screen()
  315.       end
  316.      
  317.       local f = farms[selected_idx]
  318.       if f then
  319.         if x >= 29 and x <= 45 and y == 9 then
  320.           f.sower_on = not f.sower_on; update_redstone(); refresh_screen()
  321.         elseif x >= 51 and x <= 67 and y == 9 then
  322.           f.gath_on = not f.gath_on; update_redstone(); refresh_screen()
  323.         elseif x >= 29 and x <= 40 and y == 13 then
  324.           f.sower_on = true; f.gath_on = true; update_redstone(); refresh_screen()
  325.         elseif x >= 46 and x <= 58 and y == 13 then
  326.           f.sower_on = false; f.gath_on = false; update_redstone(); refresh_screen()
  327.         elseif x >= 29 and x <= 55 and y == 17 then
  328.           table.remove(farms, selected_idx)
  329.           if selected_idx > #farms then selected_idx = #farms end
  330.           if selected_idx == 0 then selected_idx = 1 end
  331.           save_data(); update_redstone(); refresh_screen()
  332.         end
  333.       end
  334.     elseif current_tab == "ae2" then
  335.       refresh_screen()
  336.     end
  337.   elseif id == nil then
  338.     if current_tab == "ae2" then
  339.       refresh_screen()
  340.     end
  341.   end
  342. end
Advertisement
Add Comment
Please, Sign In to add comment