Class CachedConfig<T extends KamiConfig>

java.lang.Object
com.kamikazejam.kamicommon.configuration.spigot.CachedConfig<T>
Type Parameters:
T - the type of KamiConfig this cached config operates on
All Implemented Interfaces:
ConfigObserver

public abstract class CachedConfig<T extends KamiConfig> extends Object implements ConfigObserver
Abstract base class for configuration caches that automatically reload when the underlying config changes.

This class implements the observer pattern to listen for config changes and provides a framework for caching configuration data in memory. Subclasses should implement loadCache(KamiConfig) to define how their specific configuration data should be cached.

The class still requires registration via register() to start receiving config updates from the backing config.

  • Field Details

    • shutdownOnFailure

      protected boolean shutdownOnFailure
      Whether the server should shut down if loading the config fails. Defaults to true.
  • Constructor Details

    • CachedConfig

      public CachedConfig(@NotNull T config)
      Parameters:
      config - the configuration instance to cache data from
  • Method Details

    • register

      public void register()
      Registers this cached config as an observer of its backing config.
      Shortcut for calling KamiConfig.registerConfigObserver(ConfigObserver) on the backing config.
      i.e. this.config.registerConfigObserver(this);
    • reloadConfig

      public final void reloadConfig()
      Manual reload method. Call this to have onConfigLoaded(KamiConfig) called again and your cache reloaded.
      This method DOES NOT reload the underlying config, it simply calls onConfigLoaded(KamiConfig), triggering your caching logic.

      This CachedConfig class as a member of ConfigObserver is automatically registered to receive all config reloads.
      As such, calling this method is optional and not required. Calls to KamiConfig.reload() will automatically trigger onConfigLoaded(KamiConfig) to be called.

    • onConfigLoaded

      @Internal public final void onConfigLoaded(@NotNull @NotNull KamiConfig config)
      Description copied from interface: ConfigObserver
      Called each time the backing config is loaded or reloaded.
      Specified by:
      onConfigLoaded in interface ConfigObserver
      Parameters:
      config - The updated config object for consumption by this method.
    • loadCache

      public abstract void loadCache(@NotNull T config)
      Should load configuration data into cache from the provided config instance.

      This method is called automatically whenever the underlying configuration changes, and should be implemented to read and cache all necessary configuration data in memory for optimal performance.

      Parameters:
      config - the configuration instance to load data from
    • msg

      @NotNull public @NotNull MessageBuilder msg(@NotNull @NotNull String key)
      Constructs a new MessageBuilder with the provided key, from this config.
    • loadMaterials

      @NotNull public @NotNull List<com.cryptomorin.xseries.XMaterial> loadMaterials(@NotNull @NotNull String key)
      Loads a list of XMaterials from a configuration string list, filtering out invalid materials.

      This method reads a string list from the config, attempts to match each string to an XMaterial, and logs warnings for any invalid materials found. Only valid materials are included in the result.

      Parameters:
      key - the configuration key containing the material list
      Returns:
      a list of valid XMaterials found in the configuration