Class ConfigHandler
java.lang.Object
top.magstar.framework.configs.ConfigHandler
Abstract base class for handling and managing
MagstarConfig instances.
A ConfigHandler provides a mechanism for discovering, registering,
and retrieving configuration objects. It uses the Reflections library to
automatically scan target packages and instantiate all subclasses of
MagstarConfig.
Responsibilities
- Maintain a set of
MagstarConfigimplementations. - Dynamically discover and register config classes from packages.
- Provide access to loaded config instances by class type.
Usage Example
public class ConfigHandlers extends ConfigHandler {
@Override
public Set<MagstarConfig> initSet() {
return new HashSet<>();
}
public ConfigHandlers() {
registerInstance("top.magstar.lib.utils.fileutils");
}
}
- When you want your instance be with automatically reloading config,
- you can use
ConfigSubscriberandSubscribe.
Usage Example
public class MyInstance implements ConfigSubscriber {
@Subscribe(MyConfig.class)
private static FileConfiguration config = Main.getInstance().getConfig(MyConfig.class);
public MyInstance() {
subscribe();
String str = config.getString("example.node");
}
}
- Since:
- 1.0.0
- Author:
- Magstar
-
Constructor Summary
ConstructorsConstructorDescriptionConfigHandler(T plugin) ConfigHandler(MagstarPlugin plugin, org.bukkit.plugin.Plugin instance) -
Method Summary
Modifier and TypeMethodDescriptionfinal <T extends ConfigSubscriber>
voidaddSubscriber(T subscriber) final CommandMessageProviderfinal CommandMessageProvidergetCommandMessageProvider(Class<? extends MessageTemplate<?, ?>> clazz) final <T extends MagstarConfig<?>>
TRetrieve a specific configuration instance by its class type.final MessageTemplate<?, ?> final <T extends MessageTemplate<?,?>>
TgetMessageTemplate(Class<T> clazz) final MagstarPluginprotected final voidregisterInstance(String pack) Automatically discover and registerMagstarConfigimplementations within the given package name.final <T extends MagstarConfig<?>>
voiduploadSubscribers(T config, org.bukkit.configuration.file.FileConfiguration cfg)
-
Constructor Details
-
ConfigHandler
public ConfigHandler(T plugin) -
ConfigHandler
-
-
Method Details
-
getPlugin
-
addSubscriber
-
uploadSubscribers
public final <T extends MagstarConfig<?>> void uploadSubscribers(T config, org.bukkit.configuration.file.FileConfiguration cfg) -
registerInstance
Automatically discover and registerMagstarConfigimplementations within the given package name.This method uses the Reflections library to scan all classes in the target package, find subclasses of
MagstarConfig, and instantiate them via their no-argument constructors.- Parameters:
pack- the base package name to scan for config classes- Throws:
RuntimeException- if instantiation of a configuration class fails
-
getConfig
Retrieve a specific configuration instance by its class type.If multiple configurations of the same type exist, the first matching instance in the registered set will be returned. If none is found,
nullis returned.- Parameters:
clazz- the class type of the configuration to fetch- Returns:
- the matching
MagstarConfiginstance, ornullif not registered
-
getMessageTemplate
-
getMessageTemplate
-
getCommandMessageProvider
-
getCommandMessageProvider
public final CommandMessageProvider getCommandMessageProvider(Class<? extends MessageTemplate<?, ?>> clazz)
-