Class ConfigService

java.lang.Object
com.zetaplugins.zetacore.config.ConfigService

public class ConfigService extends Object
Service for managing plugin configuration files with caching support.

Config classes annotated with ConfigFile are managed: getConfig(Class) always returns the same instance per class, and reload() updates those instances in place. Code holding a reference to a config object (e.g. an injected one) therefore always sees the current values.

All methods are thread-safe. Updating a managed instance is not atomic, though: a thread that reads its fields while reload() runs on another thread may see a mix of old and new values. Code that reads config values off the main thread and needs them to be consistent should use getSnapshot(Class).

  • Constructor Details

    • ConfigService

      public ConfigService(JavaPlugin plugin)
      Construct a new ConfigService.
      Parameters:
      plugin - The JavaPlugin instance.
  • Method Details

    • getConfig

      public FileConfiguration getConfig(PluginConfigFile config)
      Get a configuration file based on the provided PluginConfig enum.
      Parameters:
      config - The PluginConfig enum representing the desired configuration file.
      Returns:
      The FileConfiguration object for the specified configuration file.
    • getConfig

      public FileConfiguration getConfig(PluginConfigFile config, boolean useCache)
      Get a configuration file based on the provided PluginConfig enum.
      Parameters:
      config - The PluginConfig enum representing the desired configuration file.
      useCache - Whether to use the cached version if available.
      Returns:
      The FileConfiguration object for the specified configuration file.
    • getConfig

      public FileConfiguration getConfig(String fileName)
      Get a configuration file based on the provided file name.
      Parameters:
      fileName - The name of the configuration file (without the .yml extension).
      Returns:
      The FileConfiguration object for the specified configuration file.
    • getConfig

      public FileConfiguration getConfig(String fileName, boolean useCache)
      Get a configuration file based on the provided file name.
      Parameters:
      fileName - The name of the configuration file (without the .yml extension).
      useCache - Whether to use the cached version if available.
      Returns:
      The FileConfiguration object for the specified configuration file.
    • getConfig

      public <T> T getConfig(Class<T> configClass)
      Get the managed instance of a configuration class.

      The file is mapped on the first call; later calls return the same instance.

      Type Parameters:
      T - The type of the configuration class.
      Parameters:
      configClass - The configuration class annotated with ConfigFile.
      Returns:
      The managed instance of the configuration class.
    • getConfig

      public <T> T getConfig(Class<T> configClass, boolean useCache)
      Get the managed instance of a configuration class.
      Type Parameters:
      T - The type of the configuration class.
      Parameters:
      configClass - The configuration class annotated with ConfigFile.
      useCache - If false, the file is re-read from disk and the managed instance is updated in place before it is returned.
      Returns:
      The managed instance of the configuration class.
    • getSnapshot

      public <T> T getSnapshot(Class<T> configClass)
      Creates a new, unmanaged instance of a configuration class with the values currently loaded.

      Unlike the managed instance, a snapshot is never changed by reload(), so all of its values belong to the same version of the file. Use it to read config values consistently off the main thread.

      Type Parameters:
      T - The type of the configuration class.
      Parameters:
      configClass - The configuration class annotated with ConfigFile.
      Returns:
      A new instance of the configuration class.
    • reload

      public void reload()
      Reload all configuration files from disk.

      Clears the file cache, reloads the plugin's default config and updates every managed config instance in place. Values missing from a file fall back to the field defaults of the config class. If any config fails to map, no managed instance is changed.

      Throws:
      ConfigMappingException - If a configuration file cannot be mapped.
    • asResolver

      public InstanceResolver asResolver()
      Creates a resolver that lets a ServiceRegistry inject managed config instances. ZetaCorePlugin registers it automatically.
       registry.addResolver(configService.asResolver());
       
      Returns:
      A resolver for classes annotated with ConfigFile.
    • saveConfig

      public void saveConfig(PluginConfigFile config, FileConfiguration fileConfig)
      Save a configuration file based on the provided PluginConfig enum.
      Parameters:
      config - The PluginConfig enum representing the configuration file to save.
      fileConfig - The FileConfiguration object to save.
    • saveConfig

      public void saveConfig(String fileName, FileConfiguration fileConfig)
      Save a configuration file based on the provided file name.
      Parameters:
      fileName - The name of the configuration file (without the .yml extension).
      fileConfig - The FileConfiguration object to save.
    • clearCache

      public void clearCache()
      Clear the configuration cache.