ConfigAPI
Overview
The ConfigAPI is a utility class designed to simplify the management of YAML configuration files in a Spigot plugin. It provides functionality for creating, loading, saving, and modifying configuration files, including player-specific configurations and inline comments.
Initialization
Constructors
ConfigAPI(String path, String fileName, Plugin plugin)Creates or loads a configuration file at the specified path. If the file does not exist, it is created with default settings.
Parameters:
path(String): The directory path where the configuration file is located.fileName(String): The name of the configuration file.plugin(Plugin): The plugin instance.
ConfigAPI(String path, String fileName, Runnable callback, Plugin plugin)Same as above, but with an additional callback that runs after creating a new configuration file.
Parameters:
path(String): The directory path where the configuration file is located.fileName(String): The name of the configuration file.callback(Runnable): Code to execute after creating a new configuration file.plugin(Plugin): The plugin instance.
Static Methods
getConig(String path, String fileName, Plugin plugin)
getConig(String path, String fileName, Plugin plugin)Initializes and retrieves a singleton instance of the ConfigAPI. Subsequent calls return the existing instance.
Parameters:
path(String): The configuration directory.fileName(String): The configuration file name.plugin(Plugin): The plugin instance.
Returns:
ConfigAPI: The singleton instance of the class.
getConfig()
getConfig()Retrieves the existing instance of ConfigAPI. Throws an exception if not initialized.
Returns:
ConfigAPI: The singleton instance of the class.
Throws:
IllegalStateException: If theConfigAPIhas not been initialized.
Core Methods
File Management
save(): Writes the in-memory configuration to the file, preserving inline comments.reloadConfig(): Reloads the configuration from the file.exists(): Checks if the configuration file exists.Returns:
boolean:trueif the file exists,falseotherwise.
delete(): Deletes the configuration file if it exists.
Access and Modification
get(): Retrieves theFileConfigurationobject for the main configuration file.setInlineComments(String path, List<String> comments): Adds inline comments for a specified configuration path.Parameters:
path(String): The configuration path.comments(List): The list of comments to add.
setStringList(String path, List<String> list): Sets a list of strings at the specified path.getStringList(String path): Retrieves a list of strings from the specified path.Returns:
List<String>: The string list from the specified path.
addDefaultStringList(String path, List<String> defaultList): Sets a default string list if the path does not already exist.
Version Management
checkVersion(String currentVersion): Logs a warning if the configuration version does not match the provided version.Parameters:
currentVersion(String): The expected version of the configuration.
Player-Specific Configuration
Loading and Saving
getPlayerConfig(Player player): Retrieves or creates a YAML configuration file for a specific player.Parameters:
player(Player): The player instance.
Returns:
YamlConfiguration: The player's configuration file.
saveModifiedConfigs(): Saves only the configurations marked as modified.setPlayerConfigModified(Player player): Marks a player's configuration as modified for saving.
Access and Modification
setPlayerConfigValue(Player player, String path, Object value): Sets a value in a player's configuration and marks it as modified.Parameters:
player(Player): The player instance.path(String): The configuration path.value(Object): The value to set.
getPlayerConfigValue(Player player, String path, T defaultValue): Retrieves a value from a player's configuration with a fallback default.Parameters:
player(Player): The player instance.path(String): The configuration path.defaultValue(T): The default value to return if the path does not exist.
Returns:
T: The retrieved value.
Locations
setPlayerLocation(Player player, String path, Location location): Stores a player's location in their configuration file.getPlayerLocation(Player player, String path): Retrieves a player's location from their configuration file.Returns:
Location: The retrieved location ornullif not set.
Location Management
setLocation(String path, Location location): Sets a location in the main configuration file.getLocation(String path): Retrieves a location from the main configuration file.Returns:
Location: The retrieved location ornullif not set.
Utility Methods
checkVersion(String currentVersion): Checks the configuration file's version against the provided version string. Logs a warning if they do not match.setStringList(String path, List<String> list): Sets a list of strings in the configuration at the specified path.getStringList(String path): Retrieves a list of strings from the configuration file.
Notes
Ensure
ConfigAPIis initialized usinggetConigbefore calling other methods.Supports inline comments, player-specific configurations, and location handling for better organization and usability.
Automatically creates missing files and directories when saving configurations.
Examples
Initialize and Use
Inline Comments
Player-Specific Configuration
Handling Locations
How to hook into ConfigAPI
Configuration in Main Class
How to use ConfigAPI in your classes
Last updated