Class PluginsLoader

java.lang.Object
net.regsirius06.engine.plugins.loaders.PluginsLoader
All Implemented Interfaces:
PluginLoader<Core>

public final class PluginsLoader extends Object implements PluginLoader<Core>
A concrete implementation of the 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 Details

    • log

      public static final org.slf4j.Logger log
      Logger instance for logging plugin management operations, including loading plugins, validating them, and handling errors such as missing or duplicate ModId annotations.

      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 ModId annotations.
      • 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 ModId is 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

      public static PluginsLoader PLUGINS
      A static instance of PluginsLoader for 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 available Core plugins using the ServiceLoader mechanism.

      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 duplicate ModIds.

      Specified by:
      loadPlugins in interface PluginLoader<Core>
    • getPlugins

      public List<Core> getPlugins()
      Returns the list of all currently loaded Core plugins.
      Specified by:
      getPlugins in interface PluginLoader<Core>
      Returns:
      a list of plugins of type Core
    • getBase

      @NotNull public @NotNull Core getBase()
      Retrieves the base plugin, which is identified by its ModId annotation matching the Base class.

      This method utilizes getPlugin(String) to retrieve the plugin by its ModId value, ensuring that the base plugin is correctly identified by its ModId annotation.

      Returns:
      the base plugin
      Throws:
      RuntimeException - if no base plugin is found
    • getPlugin

      @Nullable public @Nullable Core getPlugin(String id)
      Retrieves a specific plugin by its ModId.

      The plugin is identified by the ModId annotation, which is used as the unique identifier for each plugin. If no plugin with the specified ID is found, null is returned.

      Specified by:
      getPlugin in interface PluginLoader<Core>
      Parameters:
      id - the ModId value to look for
      Returns:
      the plugin associated with the given ID, or null if not found
    • getPluginName

      @Contract(pure=true) public static <C extends Core> String getPluginName(@NotNull C plugin)
      Retrieves the plugin name from the ModId annotation of the provided plugin instance.

      This method extracts the value of the ModId annotation from the plugin class, which is expected to be the unique identifier of the plugin. The plugin name should match the value specified in the ModId annotation 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 extend Core
      Parameters:
      plugin - the plugin instance from which the name is extracted
      Returns:
      the plugin name as a String, which corresponds to the value of the ModId annotation
      Throws:
      NullPointerException - if the provided plugin does not have the ModId annotation
      See Also: