Package yt.sehrschlecht.classconfig
Class SimpleClassConfig
java.lang.Object
yt.sehrschlecht.classconfig.SimpleClassConfig
A simple class config can be used to write/read data to/from a yaml config file.
- Since:
- 1.0
- Author:
- sehrschlechtYT | https://github.com/sehrschlechtYT
-
Field Summary
Fields -
Constructor Summary
ConstructorsModifierConstructorDescriptionprotected
SimpleClassConfig
(File file) Creates a new instance of the config. -
Method Summary
Modifier and TypeMethodDescriptionprotected Field[]
protected String
getKey
(Field field, ConfigOption option) protected Map<Field,
ConfigOption> protected Object
void
Initializes the config and field values.protected void
migrateOption
(Field field, ConfigOption option, MigrateOption migration) Migrates a config option from an old key to a new key.void
reload()
Reloads the config from the file.protected Object
resetOption
(String key) Resets a config option to its default value.protected void
Sets all config options in the config to their default values.void
save()
Saves the values of all fields to the config file.protected void
Sets the comments of all config options in the config, if they are set via theConfigOption
annotation.protected void
Updates all fields of the config to reflect the values in the config file.protected void
upgrade()
Options are migrated (if necessary) and the comments are set.protected boolean
verifyOption
(Field field, ConfigOption option) Verifies that the type of a field matches the type specified in theConfigOption
annotation.
-
Field Details
-
file
-
document
protected dev.dejvokep.boostedyaml.YamlDocument document -
defaultValues
-
logger
protected final org.slf4j.Logger logger
-
-
Constructor Details
-
SimpleClassConfig
Creates a new instance of the config. Must be called via super() in the constructor of the extending class.- Parameters:
file
- The file to load the config from. Must be a yaml (.yml/.yaml) file.
-
-
Method Details
-
initialize
public void initialize()Initializes the config and field values. This method must be called after the constructor. -
resetOption
Resets a config option to its default value. Default values are retrieved from the values of the option fields.- Parameters:
key
- The key of the option that should be reset- Returns:
- The default value the option was set to, or null
-
resetToDefaultValues
protected void resetToDefaultValues()Sets all config options in the config to their default values. After that, the fields are updated to reflect the changes. -
upgrade
protected void upgrade()Options are migrated (if necessary) and the comments are set. After that, all options that are not yet set in the config are set to their default values. -
getOptions
- Returns:
- A map containing all config options of the config. The key is the field of the option and the value is the config option annotation.
-
getFields
- Returns:
- All fields of the config. This will be handled differently if the config is a Kotlin class.
-
migrateOption
protected void migrateOption(@NotNull Field field, @NotNull ConfigOption option, @NotNull MigrateOption migration) Migrates a config option from an old key to a new key. If the old key is not set or the new key is already set, nothing happens.- Parameters:
field
- The field of the optionoption
- The config option annotationmigration
- The migration annotation
-
verifyOption
Verifies that the type of a field matches the type specified in theConfigOption
annotation.- Parameters:
field
- The field of the optionoption
- The config option annotation- Returns:
- True if the types match, false otherwise
-
getKey
- Parameters:
field
- The field of the optionoption
- The config option annotation- Returns:
- The key of the option. If the key is not set in the annotation, the field name is used.
-
getValue
- Parameters:
field
- An accessible field- Returns:
- The value of the field
-
setComments
protected void setComments()Sets the comments of all config options in the config, if they are set via theConfigOption
annotation. -
reload
public void reload()Reloads the config from the file. After that, the fields are updated to reflect the changes. -
updateFields
protected void updateFields()Updates all fields of the config to reflect the values in the config file. -
save
public void save()Saves the values of all fields to the config file.
-