Advertisement
TheYellowBush

ae2_config.lua

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