TheYellowBush

ae2_config.lua

Jul 8th, 2025 (edited)
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.23 KB | None | 0 0
  1. -- AE2 Stockpile Manager - Configuration Module
  2. -- v1
  3.  
  4. local config = {}
  5.  
  6. -- Configuration constants
  7. config.CONFIG_FILE = "ae2_stockpile.txt"
  8. config.CHECK_INTERVAL = 30 -- seconds between checks
  9.  
  10. -- Private variables
  11. local settings = {}
  12. local testMode = false
  13.  
  14. -- Load settings from file
  15. function config.load()
  16.     if fs.exists(config.CONFIG_FILE) then
  17.         local file = fs.open(config.CONFIG_FILE, "r")
  18.         if file then
  19.             local data = file.readAll()
  20.             file.close()
  21.             settings = textutils.unserialize(data) or {}
  22.             print("Loaded " .. #settings .. " configured items")
  23.         end
  24.     else
  25.         settings = {}
  26.         print("No existing configuration found")
  27.     end
  28. end
  29.  
  30. -- Save settings to file
  31. function config.save()
  32.     local file = fs.open(config.CONFIG_FILE, "w")
  33.     if file then
  34.         file.write(textutils.serialize(settings))
  35.         file.close()
  36.         print("Settings saved")
  37.         return true
  38.     else
  39.         print("ERROR: Could not save settings")
  40.         return false
  41.     end
  42. end
  43.  
  44. -- Get current settings
  45. function config.getSettings()
  46.     return settings
  47. end
  48.  
  49. -- Add or update item configuration
  50. function config.addItem(itemName, target, threshold)
  51.     -- Find existing config or create new one
  52.     local found = false
  53.     for i, itemConfig in pairs(settings) do
  54.         if itemConfig.item == itemName then
  55.             itemConfig.target = target
  56.             itemConfig.threshold = threshold
  57.             found = true
  58.             break
  59.         end
  60.     end
  61.    
  62.     if not found then
  63.         table.insert(settings, {
  64.             item = itemName,
  65.             target = target,
  66.             threshold = threshold
  67.         })
  68.     end
  69.    
  70.     return config.save()
  71. end
  72.  
  73. -- Remove item configuration
  74. function config.removeItem(index)
  75.     if index > 0 and index <= #settings then
  76.         local removed = table.remove(settings, index)
  77.         config.save()
  78.         return removed
  79.     end
  80.     return nil
  81. end
  82.  
  83. -- Test mode functions
  84. function config.isTestMode()
  85.     return testMode
  86. end
  87.  
  88. function config.setTestMode(enabled)
  89.     testMode = enabled
  90. end
  91.  
  92. function config.toggleTestMode()
  93.     testMode = not testMode
  94. end
  95.  
  96. return config
Advertisement
Add Comment
Please, Sign In to add comment