Class SimpleClassConfig

java.lang.Object
yt.sehrschlecht.classconfig.SimpleClassConfig

public abstract class SimpleClassConfig extends Object
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 Details

    • file

      protected final File file
    • document

      protected dev.dejvokep.boostedyaml.YamlDocument document
    • defaultValues

      protected Map<String,Object> defaultValues
    • logger

      protected final org.slf4j.Logger logger
  • Constructor Details

    • SimpleClassConfig

      protected SimpleClassConfig(@NotNull File file)
      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

      @Nullable protected Object resetOption(@NotNull String key)
      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

      @NotNull protected Map<Field,ConfigOption> 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

      protected Field[] 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 option
      option - The config option annotation
      migration - The migration annotation
    • verifyOption

      protected boolean verifyOption(@NotNull Field field, @NotNull ConfigOption option)
      Verifies that the type of a field matches the type specified in the ConfigOption annotation.
      Parameters:
      field - The field of the option
      option - The config option annotation
      Returns:
      True if the types match, false otherwise
    • getKey

      protected String getKey(@NotNull Field field, @NotNull ConfigOption option)
      Parameters:
      field - The field of the option
      option - 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

      @Nullable protected Object getValue(@NotNull Field field)
      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 the ConfigOption 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.