Class ConfigHelper
java.lang.Object
tk.airshipcraft.commonlib.configuration.ConfigHelper
Manages the configuration for plugins that extend CommonLib.
This class handles loading, saving, and repairing configuration settings.
It uses reflection to automatically initialize configuration values based on annotated fields in a given class.
Additionally, it maintains a default configuration file to ensure robustness in configuration management.
- Since:
- 2024-1-4
- Version:
- 2.0.0
- Author:
- notzune
-
Constructor Summary
ConstructorsConstructorDescriptionConfigHelper
(CommonLib plugin, Class<?> configClass) Constructs a ConfigHelper for the given plugin and configuration class. -
Method Summary
Modifier and TypeMethodDescriptionboolean
getBoolean
(String key) Gets the value of a configuration option as a short.getBooleanList
(String key) Gets the value of a configuration option as a List of Booleans.getByteList
(String key) Gets the value of a configuration option as a List of Bytes.getCharacterList
(String key) Gets the value of a configuration option as a List of Characters.org.bukkit.Color
Gets the value of a Color configuration option.double
Gets the value of a configuration option as a double.getDoubleList
(String key) Gets the value of a configuration option as a List of Doubles.getFloatList
(String key) Gets the value of a configuration option as a List of Floats.int
Gets the value of a configuration option as an int.getIntegerList
(String key) Gets the value of a configuration option as a List of Integers.org.bukkit.inventory.ItemStack
getItemStack
(String key) Gets the value of an ItemStack configuration option.long
Gets the value of a configuration option as a byte.getLongList
(String key) Gets the value of a configuration option as a List of Longs.getMapList
(String key) Gets the value of a configuration option as a List of Maps.getShortList
(String key) Gets the value of a configuration option as a List of Shorts.Gets the value of a configuration option as a String.getStringList
(String key) Gets the value of a configuration option as a List of Strings.void
Loads the configuration from the config.yml file.void
Repairs the configuration file on plugin restart.void
Saves the current configuration to the config.yml file.
-
Constructor Details
-
ConfigHelper
Constructs a ConfigHelper for the given plugin and configuration class. This will initialize the default configuration by either creating it or loading it if it already exists.- Parameters:
plugin
- The plugin instance. This is used to access plugin-specific configuration and data.configClass
- The class containing configuration fields. Fields in this class should be annotated withConfigOption
.
-
-
Method Details
-
saveConfig
public void saveConfig()Saves the current configuration to the config.yml file. This method iterates through all fields in the provided configClass and saves the corresponding values to the config.yml file. -
loadConfig
public void loadConfig()Loads the configuration from the config.yml file. This method iterates through all fields in the provided configClass and loads the corresponding values from the config.yml file. -
repairConfig
public void repairConfig()Repairs the configuration file on plugin restart. This method ensures that the config file is correctly formatted and contains all necessary values. Existing values are not overwritten unless they are malformed or missing. -
getString
Gets the value of a configuration option as a String. If the option is not present in the config.yml file, the default value is returned.- Parameters:
key
- The key of the configuration option.- Returns:
- The value of the configuration option as a String.
-
getInt
Gets the value of a configuration option as an int. If the option is not present in the config.yml file, the default value is returned.- Parameters:
key
- The key of the configuration option.- Returns:
- The value of the configuration option as an int.
-
getBoolean
Gets the value of a configuration option as a short. If the option is not present in the config.yml file, the default value is returned.- Parameters:
key
- The key of the configuration option.- Returns:
- The value of the configuration option as a short.
-
getLong
Gets the value of a configuration option as a byte. If the option is not present in the config.yml file, the default value is returned.- Parameters:
key
- The key of the configuration option.- Returns:
- The value of the configuration option as a byte.
-
getDouble
Gets the value of a configuration option as a double. If the option is not present in the config.yml file, the default value is returned.- Parameters:
key
- The key of the configuration option.- Returns:
- The value of the configuration option as a double.
-
getFloatList
Gets the value of a configuration option as a List of Floats. If the option is not present in the config.yml file, the default value is returned.- Parameters:
key
- The key of the configuration option.- Returns:
- The value of the configuration option as a List of Floats.
-
getDoubleList
Gets the value of a configuration option as a List of Doubles. If the option is not present in the config.yml file, the default value is returned.- Parameters:
key
- The key of the configuration option.- Returns:
- The value of the configuration option as a List of Doubles.
-
getStringList
Gets the value of a configuration option as a List of Strings. If the option is not present in the config.yml file, the default value is returned.- Parameters:
key
- The key of the configuration option.- Returns:
- The value of the configuration option as a List of Strings.
-
getIntegerList
Gets the value of a configuration option as a List of Integers. If the option is not present in the config.yml file, the default value is returned.- Parameters:
key
- The key of the configuration option.- Returns:
- The value of the configuration option as a List of Integers.
-
getLongList
Gets the value of a configuration option as a List of Longs. If the option is not present in the config.yml file, the default value is returned.- Parameters:
key
- The key of the configuration option.- Returns:
- The value of the configuration option as a List of Longs.
-
getByteList
Gets the value of a configuration option as a List of Bytes. If the option is not present in the config.yml file, the default value is returned.- Parameters:
key
- The key of the configuration option.- Returns:
- The value of the configuration option as a List of Bytes.
-
getCharacterList
Gets the value of a configuration option as a List of Characters. If the option is not present in the config.yml file, the default value is returned.- Parameters:
key
- The key of the configuration option.- Returns:
- The value of the configuration option as a List of Characters.
-
getBooleanList
Gets the value of a configuration option as a List of Booleans. If the option is not present in the config.yml file, the default value is returned.- Parameters:
key
- The key of the configuration option.- Returns:
- The value of the configuration option as a List of Booleans.
-
getShortList
Gets the value of a configuration option as a List of Shorts. If the option is not present in the config.yml file, the default value is returned.- Parameters:
key
- The key of the configuration option.- Returns:
- The value of the configuration option as a List of Shorts.
-
getMapList
Gets the value of a configuration option as a List of Maps. If the option is not present in the config.yml file, the default value is returned. Each map in the list represents a section of the configuration file.- Parameters:
key
- The key of the configuration option.- Returns:
- The value of the configuration option as a List of Maps.
-
getItemStack
Gets the value of an ItemStack configuration option. If the option is not present in the config.yml file, the default value is returned.- Parameters:
key
- The key of the configuration option.- Returns:
- The value of the configuration option as an ItemStack.
-
getColor
Gets the value of a Color configuration option. If the option is not present in the config.yml file, the default value is returned.- Parameters:
key
- The key of the configuration option.- Returns:
- The value of the configuration option as a Color.
-