Class ConfigService
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionCreates a resolver that lets aServiceRegistryinject managed config instances.voidClear the configuration cache.getConfig(PluginConfigFile config) Get a configuration file based on the provided PluginConfig enum.getConfig(PluginConfigFile config, boolean useCache) Get a configuration file based on the provided PluginConfig enum.<T> TGet the managed instance of a configuration class.<T> TGet the managed instance of a configuration class.Get a configuration file based on the provided file name.Get a configuration file based on the provided file name.<T> TgetSnapshot(Class<T> configClass) Creates a new, unmanaged instance of a configuration class with the values currently loaded.voidreload()Reload all configuration files from disk.voidsaveConfig(PluginConfigFile config, FileConfiguration fileConfig) Save a configuration file based on the provided PluginConfig enum.voidsaveConfig(String fileName, FileConfiguration fileConfig) Save a configuration file based on the provided file name.
-
Constructor Details
-
ConfigService
Construct a new ConfigService.- Parameters:
plugin- The JavaPlugin instance.
-
-
Method Details
-
getConfig
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
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
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
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
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 withConfigFile.- Returns:
- The managed instance of the configuration class.
-
getConfig
Get the managed instance of a configuration class.- Type Parameters:
T- The type of the configuration class.- Parameters:
configClass- The configuration class annotated withConfigFile.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
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 withConfigFile.- 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
Creates a resolver that lets aServiceRegistryinject managed config instances.ZetaCorePluginregisters it automatically.registry.addResolver(configService.asResolver());
- Returns:
- A resolver for classes annotated with
ConfigFile.
-
saveConfig
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
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.
-