Class CachedConfig<T extends KamiConfig>
- Type Parameters:
T
- the type ofKamiConfig
this cached config operates on
- All Implemented Interfaces:
ConfigObserver
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 Summary
FieldsModifier and TypeFieldDescriptionprotected boolean
Whether the server should shut down if loading the config fails. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionabstract void
Should load configuration data into cache from the provided config instance.@NotNull List
<com.cryptomorin.xseries.XMaterial> loadMaterials
(@NotNull String key) Loads a list of XMaterials from a configuration string list, filtering out invalid materials.@NotNull MessageBuilder
Constructs a newMessageBuilder
with the provided key, from this config.final void
onConfigLoaded
(@NotNull KamiConfig config) Called each time the backing config is loaded or reloaded.void
register()
Registers this cached config as an observer of its backing config.
Shortcut for callingKamiConfig.registerConfigObserver(ConfigObserver)
on the backing config.
i.e.final void
Manual reload method.
-
Field Details
-
shutdownOnFailure
protected boolean shutdownOnFailureWhether the server should shut down if loading the config fails. Defaults totrue
.
-
-
Constructor Details
-
CachedConfig
- 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 callingKamiConfig.registerConfigObserver(ConfigObserver)
on the backing config.
i.e.this.config.registerConfigObserver(this);
-
reloadConfig
public final void reloadConfig()Manual reload method. Call this to haveonConfigLoaded(KamiConfig)
called again and your cache reloaded.
This method DOES NOT reload the underlying config, it simply callsonConfigLoaded(KamiConfig)
, triggering your caching logic.This
CachedConfig
class as a member ofConfigObserver
is automatically registered to receive all config reloads.
As such, calling this method is optional and not required. Calls toKamiConfig.reload()
will automatically triggeronConfigLoaded(KamiConfig)
to be called. -
onConfigLoaded
Description copied from interface:ConfigObserver
Called each time the backing config is loaded or reloaded.- Specified by:
onConfigLoaded
in interfaceConfigObserver
- Parameters:
config
- The updated config object for consumption by this method.
-
loadCache
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
Constructs a newMessageBuilder
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
-