Class DependencyLoader

java.lang.Object
me.croabeast.common.DependencyLoader

public class DependencyLoader extends Object
The DependencyLoader class provides functionality to dynamically download and load external JAR dependencies at runtime for Bukkit/Spigot/Paper plugins.

It supports downloading files from remote repositories (such as Maven or a custom repository) and loading them into the plugin’s classpath by using low-level reflection (via Unsafe and Reflector) to modify the internal structures of the class loader.

There are two main modes for storing the downloaded libraries:

  • Complex structure: Uses a Maven-like directory layout (e.g., /libraries/com/google/code/gson/gson/2.8.9/gson-2.8.9.jar).
  • Flat structure: Places the JAR files directly inside the libraries folder (e.g., /libraries/gson-2.8.9.jar).
The mode is determined by the complexStructure flag.

Preconfigured instances:

Example usage:


 // In your plugin's onLoad or onEnable method:
 DependencyLoader loader = DependencyLoader.BUKKIT_LOADER;
 loader.load("com.example", "my-library", "${version}", DependencyLoader.MAVEN_REPO_URLS[0], false);

 // Alternatively, load dependencies from a YAML configuration file:
 loader.loadFromYAML(new File(getDataFolder(), "dependencies.yml"));
 

See Also:
  • YAMLFile
  • YamlConfiguration
  • Field Details

    • BUKKIT_LOADER

      public static final DependencyLoader BUKKIT_LOADER
      A preconfigured DependencyLoader that uses Bukkit's world container and a subfolder named "libraries".

      Note: The folder structure for this loader is fixed; its complexStructure flag cannot be changed.

    • MAVEN_REPO_URLS

      public static final String[] MAVEN_REPO_URLS
      An array of default Maven repository URLs.
  • Method Details

    • load

      public final boolean load(String group, String artifact, String version, String repoUrl, boolean replace)
      Downloads and loads a dependency JAR file into the plugin's runtime classpath.

      The method constructs the dependency path and file name from the group, artifact, and version. It downloads the JAR if it does not exist or if replace is true, and then loads it dynamically via reflection.

      Parameters:
      group - the dependency group ID.
      artifact - the dependency artifact ID.
      version - the dependency version (this can be parameterized via release tags).
      repoUrl - the base URL of the repository to download the dependency from; if null, the first URL from MAVEN_REPO_URLS is used.
      replace - if true, the dependency will be re-downloaded even if it exists.
      Returns:
      true if the dependency was successfully loaded; false otherwise.
    • load

      public final boolean load(String group, String artifact, String version, boolean replace)
      Overloaded variant of the main load method that uses the default repository URL.
      Parameters:
      group - the dependency group ID.
      artifact - the dependency artifact ID.
      version - the dependency version.
      Returns:
      true if the dependency was successfully loaded; false otherwise.
    • load

      public final boolean load(String group, String artifact, String version, String repoUrl)
      Overloaded variant of the main load method that does not replace existing files and uses the default repository URL.
      Parameters:
      group - the dependency group ID.
      artifact - the dependency artifact ID.
      version - the dependency version.
      Returns:
      true if the dependency was successfully loaded; false otherwise.
    • load

      public final boolean load(String group, String artifact, String version)
      Overloaded variant of the main load method that does not replace existing files and uses the default repository URL.
      Parameters:
      group - the dependency group ID.
      artifact - the dependency artifact ID.
      version - the dependency version.
      Returns:
      true if the dependency was successfully loaded; false otherwise.
    • loadFromConfiguration

      public boolean loadFromConfiguration(org.bukkit.configuration.file.FileConfiguration c)
      Loads dependencies from a given FileConfiguration.

      The configuration should contain a list of dependency maps under the key dependencies.

      Parameters:
      c - the FileConfiguration containing dependency definitions.
      Returns:
      true if at least one dependency was loaded successfully; false otherwise.
    • loadFromFile

      public boolean loadFromFile(File file)
      Loads dependencies specified in a YAML file.

      The YAML file must have a .yml extension and contain dependency definitions.

      Parameters:
      file - the YAML file containing dependency definitions.
      Returns:
      true if dependencies were loaded successfully; false otherwise.
    • loadFromYAML

      public boolean loadFromYAML(me.croabeast.file.YAMLFile file)
      Loads dependencies specified in a YAMLFile instance.
      Parameters:
      file - a YAMLFile containing dependency definitions.
      Returns:
      true if dependencies were loaded successfully; false otherwise.
    • fromFolder

      public static DependencyLoader fromFolder(File librariesFolder, String folderName)
      Creates a new DependencyLoader instance for the specified folder and subfolder.
      Parameters:
      librariesFolder - the base folder containing dependency libraries.
      folderName - the subfolder name to use (may be null).
      Returns:
      a new DependencyLoader instance.
    • fromFolder

      public static DependencyLoader fromFolder(File librariesFolder)
      Creates a new DependencyLoader instance for the specified folder without a subfolder.
      Parameters:
      librariesFolder - the base folder containing dependency libraries.
      Returns:
      a new DependencyLoader instance.