Class BaseConfig

java.lang.Object
fr.hytale.loader.config.BaseConfig
All Implemented Interfaces:
Config
Direct Known Subclasses:
JsonConfig, YamlConfig

public abstract class BaseConfig extends Object implements Config
Base implementation for configuration files.

Provides memory management for config data as nested Maps. This class implements the core logic for getting and setting values, handling defaults, and managing nested sections.

Concrete implementations only need to handle the actual loading and saving of data from/to their specific file format (e.g., YAML, JSON).

Since:
1.0.4
Version:
1.0.4
Author:
HytaleLoader
  • Field Details

    • file

      protected final File file
      The file associated with this configuration.
    • data

      protected final Map<String,Object> data
      The data map holding the configuration values.
    • defaults

      protected Map<String,Object> defaults
      The map holding default values.
  • Constructor Details

    • BaseConfig

      public BaseConfig(File file)
      Creates a new BaseConfig instance.
      Parameters:
      file - the configuration file
  • Method Details

    • get

      public Object get(String path)
      Gets a value from the config.
      Specified by:
      get in interface Config
      Parameters:
      path - the path to the value
      Returns:
      the value, or null if not found
    • get

      public Object get(String path, Object def)
      Gets a value from the config with a default fallback.

      If the value is not found in the config data, it attempts to retrieve it from the defaults.

      Specified by:
      get in interface Config
      Parameters:
      path - the path to the value
      def - the default value to return if not found
      Returns:
      the value, or the default if not found
    • getFromDefaults

      protected Object getFromDefaults(String path, Object def)
      Helper method to get a value from the defaults map.
      Parameters:
      path - the path to the value
      def - the fallback value if not found in defaults
      Returns:
      the default value, or def if not found
    • set

      public void set(String path, Object value)
      Sets a value in the configuration at the specified path.

      Creating any necessary nested sections if they do not exist.

      Specified by:
      set in interface Config
      Parameters:
      path - the path to set
      value - the value to set
    • getString

      public String getString(String path)
      Gets a String value from the config.
      Specified by:
      getString in interface Config
      Parameters:
      path - the path to the value
      Returns:
      the string value, or null
    • getString

      public String getString(String path, String def)
      Gets a String value from the config with a default.
      Specified by:
      getString in interface Config
      Parameters:
      path - the path to the value
      def - the default string
      Returns:
      the string value, or default
    • getInt

      public int getInt(String path)
      Gets an int value from the config.
      Specified by:
      getInt in interface Config
      Parameters:
      path - the path to the value
      Returns:
      the int value, or 0
    • getInt

      public int getInt(String path, int def)
      Gets an int value from the config with a default.

      Tries to parse the value as an integer if it is stored as a String.

      Specified by:
      getInt in interface Config
      Parameters:
      path - the path to the value
      def - the default int
      Returns:
      the int value, or default
    • getDouble

      public double getDouble(String path)
      Gets a double value from the config.
      Specified by:
      getDouble in interface Config
      Parameters:
      path - the path to the value
      Returns:
      the double value, or 0.0
    • getDouble

      public double getDouble(String path, double def)
      Gets a double value from the config with a default.

      Tries to parse the value as a double if it is stored as a String.

      Specified by:
      getDouble in interface Config
      Parameters:
      path - the path to the value
      def - the default double
      Returns:
      the double value, or default
    • getBoolean

      public boolean getBoolean(String path)
      Gets a boolean value from the config.
      Specified by:
      getBoolean in interface Config
      Parameters:
      path - the path to the value
      Returns:
      the boolean value, or false
    • getBoolean

      public boolean getBoolean(String path, boolean def)
      Gets a boolean value from the config with a default.

      Tries to parse the value as a boolean if it is stored as a String.

      Specified by:
      getBoolean in interface Config
      Parameters:
      path - the path to the value
      def - the default boolean
      Returns:
      the boolean value, or default
    • getList

      public List<?> getList(String path)
      Gets a List from the config.
      Specified by:
      getList in interface Config
      Parameters:
      path - the path to the value
      Returns:
      the List, or null
    • getList

      public List<?> getList(String path, List<?> def)
      Gets a List from the config with a default.
      Specified by:
      getList in interface Config
      Parameters:
      path - the path to the value
      def - the default List
      Returns:
      the List, or default
    • getStringList

      public List<String> getStringList(String path)
      Gets a List of Strings from the config.

      Converts all elements in the list to String.

      Specified by:
      getStringList in interface Config
      Parameters:
      path - the path to the value
      Returns:
      a List of Strings (never null, empty if not found)
    • getSection

      public ConfigSection getSection(String path)
      Gets a Configuration Section.
      Specified by:
      getSection in interface Config
      Parameters:
      path - the path to the section
      Returns:
      the ConfigSection, or null if not found
    • contains

      public boolean contains(String path)
      Checks if the config contains a value at the specified path.
      Specified by:
      contains in interface Config
      Parameters:
      path - the path to check
      Returns:
      true if found, false otherwise
    • getKeys

      public Set<String> getKeys(boolean deep)
      Gets a set of keys at the root of the config.
      Specified by:
      getKeys in interface Config
      Parameters:
      deep - if true, includes all keys recursively
      Returns:
      a Set of keys
    • getKeysFromMap

      protected Set<String> getKeysFromMap(Map<String,Object> map, String prefix, boolean deep)
      Recursive helper to retrieve keys from a map.
      Parameters:
      map - the map to inspect
      prefix - the key prefix for nested values
      deep - whether to recurse into nested maps
      Returns:
      set of keys
    • getValues

      public Map<String,Object> getValues()
      Gets all values in the config as a Map.
      Specified by:
      getValues in interface Config
      Returns:
      a Map containing all config data
    • getFile

      public File getFile()
      Gets the file associated with this config.
      Specified by:
      getFile in interface Config
      Returns:
      the config File
    • setDefaults

      public void setDefaults(Map<String,Object> defaults)
      Sets the default values for the configuration.
      Specified by:
      setDefaults in interface Config
      Parameters:
      defaults - a Map of default values
    • addDefault

      public void addDefault(String path, Object value)
      Adds a single default value.
      Specified by:
      addDefault in interface Config
      Parameters:
      path - the path for the default
      value - the default value
    • applyDefaults

      protected void applyDefaults()
      Applies default values to the current data map.

      Only adds keys/values that are missing from the current configuration data.