Class PluginsLoader
- All Implemented Interfaces:
PluginLoader<Core>
PluginLoader interface that loads Core plugins.
This class is responsible for loading all available plugins of type Core, including ensuring
that plugins are uniquely identified by the ModId annotation. The loader also provides
convenient methods to retrieve plugins by their ModId, validate the loaded plugins for
uniqueness, and fetch a specific base plugin.
The loader can be accessed through the static instance PLUGINS, which is the recommended
way to interact with the plugins system.
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final org.slf4j.LoggerLogger instance for logging plugin management operations, including loading plugins, validating them, and handling errors such as missing or duplicateModIdannotations.static PluginsLoaderA static instance ofPluginsLoaderfor easy access to the loaded plugins. -
Method Summary
Modifier and TypeMethodDescription@NotNull CoregetBase()@Nullable CoreRetrieves a specific plugin by itsModId.getPluginName(C plugin) Retrieves the plugin name from theModIdannotation of the provided plugin instance.Returns the list of all currently loadedCoreplugins.voidLoads all availableCoreplugins using theServiceLoadermechanism.
-
Field Details
-
log
public static final org.slf4j.Logger logLogger instance for logging plugin management operations, including loading plugins, validating them, and handling errors such as missing or duplicateModIdannotations.This logger is used to record key events during the plugin loading process, such as:
- Start and completion of plugin loading.
- Successful loading of individual plugins.
- Warnings about missing or duplicate
ModIdannotations. - Errors encountered during the plugin validation or loading process.
The logger uses the SLF4J framework, allowing flexibility in configuring log levels (e.g., INFO, WARN, ERROR) and directing output to different destinations (console, log files, etc.). This ensures that developers and administrators can track the state of plugin loading and identify issues with the plugins at runtime.
Example log outputs:
- Informational log when a plugin is successfully loaded: "Loaded plugin:
plugin-class-name" - Warning log when a plugin with a specified
ModIdis not found: "Plugin \"id\" not found." - Error log when a plugin fails the validation check for missing or duplicate
ModId: "Duplicate MOD_ID:modId"
By using this logger, developers and administrators can gain insight into the plugin loading process, making it easier to debug issues related to missing or incorrectly annotated plugins.
-
PLUGINS
A static instance ofPluginsLoaderfor easy access to the loaded plugins. This is the recommended object to interact with for plugin management.
-
-
Method Details
-
loadPlugins
public void loadPlugins()Loads all availableCoreplugins using theServiceLoadermechanism.This method clears any previously loaded plugins and then loads plugins from the plugin loader provided by
PluginManager. After loading, it validates that the plugins are properly annotated and that there are no duplicateModIds.- Specified by:
loadPluginsin interfacePluginLoader<Core>
-
getPlugins
Returns the list of all currently loadedCoreplugins.- Specified by:
getPluginsin interfacePluginLoader<Core>- Returns:
- a list of plugins of type
Core
-
getBase
Retrieves the base plugin, which is identified by itsModIdannotation matching theBaseclass.This method utilizes
getPlugin(String)to retrieve the plugin by itsModIdvalue, ensuring that the base plugin is correctly identified by itsModIdannotation.- Returns:
- the base plugin
- Throws:
RuntimeException- if no base plugin is found
-
getPlugin
Retrieves a specific plugin by itsModId.The plugin is identified by the
ModIdannotation, which is used as the unique identifier for each plugin. If no plugin with the specified ID is found,nullis returned.- Specified by:
getPluginin interfacePluginLoader<Core>- Parameters:
id- theModIdvalue to look for- Returns:
- the plugin associated with the given ID, or
nullif not found
-
getPluginName
Retrieves the plugin name from theModIdannotation of the provided plugin instance.This method extracts the
valueof theModIdannotation from the plugin class, which is expected to be the unique identifier of the plugin. The plugin name should match the value specified in theModIdannotation on the plugin class.The method is marked as
pure, meaning it does not have any side effects and always returns the same result for the same input.- Type Parameters:
C- the type of the plugin, which must extendCore- Parameters:
plugin- the plugin instance from which the name is extracted- Returns:
- the plugin name as a
String, which corresponds to thevalueof theModIdannotation - Throws:
NullPointerException- if the providedplugindoes not have theModIdannotation- See Also:
-