### Complete Libby Plugin Example with Dependencies and Relocations (Java) Source: https://context7.com/alessiodp/libby/llms.txt A comprehensive example of integrating Libby into a Bukkit plugin. It demonstrates loading multiple dependencies, including HikariCP and its SLF4J logging libraries into an isolated class loader, and applying relocations to prevent namespace conflicts. Error handling for dependency loading is included. ```java import net.byteflux.libby.BukkitLibraryManager; import net.byteflux.libby.Library; import net.byteflux.libby.logging.LogLevel; import org.bukkit.plugin.java.JavaPlugin; public class DatabasePlugin extends JavaPlugin { @Override public void onEnable() { // Initialize library manager BukkitLibraryManager libraryManager = new BukkitLibraryManager(this); libraryManager.setLogLevel(LogLevel.INFO); // Add repositories libraryManager.addMavenCentral(); // HikariCP connection pool with relocation Library hikariCP = Library.builder() .groupId("com{}zaxxer") .artifactId("HikariCP") .version("5.1.0") .relocate("com{}zaxxer{}hikari", "dbplugin{}libs{}hikari") .checksum("ABC123...==") // Optional SHA-256 checksum .build(); // SLF4J (HikariCP dependency) in isolated loader Library slf4jApi = Library.builder() .groupId("org{}slf4j") .artifactId("slf4j-api") .version("2.0.9") .id("hikari-logging") .isolatedLoad(true) .build(); Library slf4jSimple = Library.builder() .groupId("org{}slf4j") .artifactId("slf4j-simple") .version("2.0.9") .id("hikari-logging") .isolatedLoad(true) .build(); // MySQL connector Library mysqlConnector = Library.builder() .groupId("com{}mysql") .artifactId("mysql-connector-j") .version("8.2.0") .relocate("com{}mysql", "dbplugin{}libs{}mysql") .build(); // Load all libraries (downloads if not cached) try { libraryManager.loadLibrary(slf4jApi); libraryManager.loadLibrary(slf4jSimple); libraryManager.loadLibrary(hikariCP); libraryManager.loadLibrary(mysqlConnector); getLogger().info("All dependencies loaded successfully!"); // Now use HikariCP (relocated to dbplugin.libs.hikari.*) // HikariConfig config = new HikariConfig(); // config.setJdbcUrl("jdbc:mysql://localhost:3306/database"); // HikariDataSource dataSource = new HikariDataSource(config); } catch (RuntimeException e) { getLogger().severe("Failed to load dependencies: " + e.getMessage()); getServer().getPluginManager().disablePlugin(this); } } } ``` -------------------------------- ### Load Library using LibraryManager Source: https://github.com/alessiodp/libby/blob/master/README.md This complete Java example shows the final steps of using Libby: initializing a `BukkitLibraryManager`, building a `Library` object with specified details, adding Maven Central as a repository, and then loading the library using `libraryManager.loadLibrary(lib)`. This method handles downloading and loading the dependency into the plugin's classpath. ```java BukkitLibraryManager libraryManager = new BukkitLibraryManager(plugin); Library lib = Library.builder() .groupId("your{}dependency{}groupId") // "{}" is replaced with ".", useful to avoid unwanted changes made by maven-shade-plugin .artifactId("artifactId") .version("version") // The following are optional // Sets an id for the library .id("my-lib") // Relocation is applied to the downloaded jar before loading it .relocate("package{}to{}relocate", "the{}relocated{}package") // "{}" is replaced with ".", useful to avoid unwanted changes made by maven-shade-plugin // The library is loaded into an IsolatedClassLoader, which is in common between every library with the same id .isolatedLoad(true) .classifier("customClassifier") .checksum("Base64-encoded SHA-256 checksum") .build(); libraryManager.addMavenCentral(); libraryManager.loadLibrary(lib); ``` -------------------------------- ### Initialize Bukkit and Bungee Library Managers Source: https://github.com/alessiodp/libby/blob/master/README.md This Java code demonstrates how to create instances of `LibraryManager` for different Minecraft server platforms. It shows the initialization for a Bukkit/Spigot plugin using `BukkitLibraryManager` and a Bungee plugin using `BungeeLibraryManager`. The `plugin` object (presumably your main plugin class instance) is passed as an argument. ```java // Create a library manager for a Bukkit/Spigot plugin BukkitLibraryManager bukkitLibraryManager = new BukkitLibraryManager(plugin); // Create a library manager for a Bungee plugin BungeeLibraryManager bungeeLibraryManager = new BungeeLibraryManager(plugin); // Also Nukkit, Sponge, and Velocity are supported ``` -------------------------------- ### Build a Library Instance with Builder API Source: https://github.com/alessiodp/libby/blob/master/README.md This Java snippet illustrates how to construct a `Library` object using its builder pattern. Key configurations include `groupId`, `artifactId`, `version`, an optional `id` for caching, `relocate` patterns for package renaming, `isolatedLoad` for classloader isolation, `classifier`, and `checksum` for integrity verification. The `{}` notation is used to automatically replace with `.` characters, preventing conflicts with the `maven-shade-plugin`. ```java Library lib = Library.builder() .groupId("your{}dependency{}groupId") // "{}" is replaced with ".", useful to avoid unwanted changes made by maven-shade-plugin .artifactId("artifactId") .version("version") // The following are optional // Sets an id for the library .id("my-lib") // Relocation is applied to the downloaded jar before loading it .relocate("package{}to{}relocate", "the{}relocated{}package") // "{}" is replaced with ".", useful to avoid unwanted changes made by maven-shade-plugin // The library is loaded into an IsolatedClassLoader, which is in common between every library with the same id .isolatedLoad(true) .classifier("customClassifier") .checksum("Base64-encoded SHA-256 checksum") .build(); ``` -------------------------------- ### Configure Library Download Log Levels (Java) Source: https://context7.com/alessiodp/libby/llms.txt Shows how to set the logging level for Libby's `LibraryManager`. This allows control over the verbosity of messages generated during library downloads, relocations, and other operations. The available levels range from `DEBUG` to `ERROR`. ```java import net.byteflux.libby.BukkitLibraryManager; import net.byteflux.libby.logging.LogLevel; BukkitLibraryManager libraryManager = new BukkitLibraryManager(plugin); // Get current log level LogLevel currentLevel = libraryManager.getLogLevel(); // Set log level to suppress informational messages libraryManager.setLogLevel(LogLevel.WARN); // Only warnings and errors // Available log levels: // LogLevel.DEBUG - Detailed debugging information // LogLevel.INFO - General informational messages (default) // LogLevel.WARN - Warnings and non-fatal errors // LogLevel.ERROR - Only critical errors ``` -------------------------------- ### Configure Library Relocations Source: https://context7.com/alessiodp/libby/llms.txt Relocation allows renaming packages within downloaded JARs to prevent classpath conflicts between different plugin versions. This can be done simply via the `Library.builder().relocate()` method or more advanced with `Relocation.builder()` for includes and excludes. ```java import net.byteflux.libby.Library; import net.byteflux.libby.relocation.Relocation; // Simple relocation using Library builder Library simpleLib = Library.builder() .groupId("org{}yaml") .artifactId("snakeyaml") .version("2.2") .relocate("org{}yaml{}snakeyaml", "myplugin{}libs{}snakeyaml") .build(); // Advanced relocation with includes/excludes using Relocation builder Relocation advancedRelocation = Relocation.builder() .pattern("org.yaml.snakeyaml") .relocatedPattern("myplugin.libs.snakeyaml") .include("org.yaml.snakeyaml.Yaml") // Only relocate specific classes .exclude("org.yaml.snakeyaml.introspector.*") // Exclude certain packages .build(); Library advancedLib = Library.builder() .groupId("org{}yaml") .artifactId("snakeyaml") .version("2.2") .relocate(advancedRelocation) .build(); ``` -------------------------------- ### Configure Repositories for Dependency Downloads Source: https://context7.com/alessiodp/libby/llms.txt Libby's `LibraryManager` supports various built-in Maven repositories like MavenCentral and Sonatype, as well as custom repository URLs. Repositories are queried in the order they are added, prioritizing library-specific ones. ```java import net.byteflux.libby.BukkitLibraryManager; import net.byteflux.libby.Repositories; BukkitLibraryManager libraryManager = new BukkitLibraryManager(plugin); // Built-in repository helpers libraryManager.addMavenCentral(); // https://repo1.maven.org/maven2/ libraryManager.addSonatype(); // https://oss.sonatype.org/content/groups/public/ libraryManager.addJCenter(); // https://jcenter.bintray.com/ libraryManager.addJitPack(); // https://jitpack.io/ libraryManager.addMavenLocal(); // ~/.m2/repository/ // Custom repositories libraryManager.addRepository("https://repo.alessiodp.com/releases/"); libraryManager.addRepository("https://repo.papermc.io/repository/maven-public/"); // Access repository constants directly String mavenCentral = Repositories.MAVEN_CENTRAL; String sonatype = Repositories.SONATYPE; String jcenter = Repositories.JCENTER; String jitpack = Repositories.JITPACK; ``` -------------------------------- ### Configure Java Libraries with Libby Builder Source: https://context7.com/alessiodp/libby/llms.txt The `Library.builder()` in Libby allows configuring Maven artifacts for runtime dependency management. It supports Maven coordinates, direct URLs, checksums, package relocations, and isolated class loading. Use the fluent API to specify all necessary properties for a library. ```java import net.byteflux.libby.Library; // Basic library with Maven coordinates Library basicLib = Library.builder() .groupId("com{}google{}code{}gson") // "{}" replaced with "." to avoid shade plugin issues .artifactId("gson") .version("2.10.1") .build(); // Library with all optional features Library fullLib = Library.builder() .groupId("org{}apache{}commons") .artifactId("commons-lang3") .version("3.14.0") .id("commons-lang") // ID for isolated class loader sharing .classifier("sources") // Optional Maven classifier .checksum("Base64EncodedSHA256ChecksumHere==") // SHA-256 checksum verification .isolatedLoad(true) // Load in isolated class loader .relocate("org{}apache{}commons", "myplugin{}libs{}commons") // Package relocation .repository("https://repo1.maven.org/maven2/") // Per-library repository .url("https://example.com/direct-download.jar") // Direct download URL fallback .build(); // Snapshot library (always re-downloaded) Library snapshotLib = Library.builder() .groupId("com{}example") .artifactId("my-lib") .version("1.0.0-SNAPSHOT") .build(); ``` -------------------------------- ### Configure Maven Shade Plugin for Relocation Source: https://github.com/alessiodp/libby/blob/master/README.md This XML configuration for the `maven-shade-plugin` is crucial for relocating Libby's package to prevent conflicts with other plugins. It specifies the original package of Libby (`net.byteflux.libby`) and the desired `shadedPattern` which should be unique to your plugin (e.g., `yourPackage.libs.net.byteflux.libby`). ```xml org.apache.maven.plugins maven-shade-plugin net.byteflux.libby yourPackage.libs.net.byteflux.libby ``` -------------------------------- ### Manage BungeeCord Plugin Dependencies with BungeeLibraryManager Source: https://context7.com/alessiodp/libby/llms.txt The `BungeeLibraryManager` offers runtime dependency management for BungeeCord proxy plugins, mirroring the API of the Bukkit variant. It facilitates the loading of external libraries, allowing for cleaner plugin JARs and simplified dependency handling on the proxy. ```java import net.byteflux.libby.BungeeLibraryManager; import net.byteflux.libby.Library; import net.md_5.bungee.api.plugin.Plugin; public class MyBungeePlugin extends Plugin { @Override public void onEnable() { BungeeLibraryManager libraryManager = new BungeeLibraryManager(this); libraryManager.addMavenCentral(); Library jedis = Library.builder() .groupId("redis{}clients") .artifactId("jedis") .version("5.1.0") .relocate("redis{}clients{}jedis", "myplugin{}libs{}jedis") .build(); libraryManager.loadLibrary(jedis); } } ``` -------------------------------- ### Manage Dependencies with PaperLibraryManager Source: https://context7.com/alessiodp/libby/llms.txt The `PaperLibraryManager` is designed for Paper 1.19.3+ servers, leveraging Paper's internal loader via `paper-plugin.yml`. It simplifies adding repositories and loading libraries, including support for package relocation to avoid conflicts. ```java import net.byteflux.libby.Library; import net.byteflux.libby.PaperLibraryManager; import org.bukkit.plugin.java.JavaPlugin; public class MyPaperPlugin extends JavaPlugin { @Override public void onEnable() { // Requires paper-plugin.yml, not plugin.yml PaperLibraryManager libraryManager = new PaperLibraryManager(this); libraryManager.addMavenCentral(); Library mongoDriver = Library.builder() .groupId("org{}mongodb") .artifactId("mongodb-driver-sync") .version("4.11.1") .relocate("com{}mongodb", "myplugin{}libs{}mongodb") .relocate("org{}bson", "myplugin{}libs{}bson") .build(); libraryManager.loadLibrary(mongoDriver); } } ``` -------------------------------- ### Load Libraries into Isolated ClassLoader (Java) Source: https://context7.com/alessiodp/libby/llms.txt Demonstrates how to use Libby's `IsolatedClassLoader` to load libraries with a shared ID into an isolated classpath. This prevents conflicts with server-provided libraries and other plugins. Dependencies are loaded via a `BukkitLibraryManager`. ```java import net.byteflux.libby.BukkitLibraryManager; import net.byteflux.libby.Library; import net.byteflux.libby.classloader.IsolatedClassLoader; BukkitLibraryManager libraryManager = new BukkitLibraryManager(plugin); libraryManager.addMavenCentral(); // Load libraries into isolated class loader with shared ID Library lib1 = Library.builder() .groupId("org{}slf4j") .artifactId("slf4j-api") .version("2.0.9") .id("logging-libs") .isolatedLoad(true) .build(); Library lib2 = Library.builder() .groupId("org{}slf4j") .artifactId("slf4j-simple") .version("2.0.9") .id("logging-libs") // Same ID shares isolated class loader with lib1 .isolatedLoad(true) .build(); libraryManager.loadLibrary(lib1); libraryManager.loadLibrary(lib2); // Access the isolated class loader IsolatedClassLoader isolatedLoader = libraryManager.getIsolatedClassLoaderOf("logging-libs"); // Load classes from isolated environment Class loggerClass = isolatedLoader.loadClass("org.slf4j.Logger"); Class loggerFactoryClass = isolatedLoader.loadClass("org.slf4j.LoggerFactory"); ``` -------------------------------- ### Add Libby Maven Dependency and Repository Source: https://github.com/alessiodp/libby/blob/master/README.md This snippet shows how to add the Libby Maven artifact to your project's `pom.xml` file. It includes configuring the custom repository where Libby is hosted and declaring the `libby-bukkit` dependency. Replace `bukkit` with the appropriate artifact for other platforms like BungeeCord. ```xml AlessioDP https://repo.alessiodp.com/releases/ net.byteflux libby-bukkit 1.3.1 ``` -------------------------------- ### Configure Maven pom.xml for Libby Dependency and Relocation Source: https://context7.com/alessiodp/libby/llms.txt This snippet shows how to add Libby as a dependency in your Maven project's pom.xml. It also configures the Maven Shade plugin to relocate Libby and its dependencies, preventing potential conflicts with other plugins on the server. Ensure you choose the correct artifactId for your target platform. ```xml alessiodp-repo https://repo.alessiodp.com/releases/ net.byteflux libby-bukkit 1.3.1 org.apache.maven.plugins maven-shade-plugin 3.5.1 package shade net.byteflux.libby your.plugin.package.libs.libby ``` -------------------------------- ### Manage Bukkit Plugin Dependencies with BukkitLibraryManager Source: https://context7.com/alessiodp/libby/llms.txt The `BukkitLibraryManager` simplifies runtime dependency management for Bukkit/Spigot plugins. It automatically handles downloading, caching, relocating, and loading libraries into the plugin's classpath. You can specify a custom download directory for dependencies. ```java import net.byteflux.libby.BukkitLibraryManager; import net.byteflux.libby.Library; import org.bukkit.plugin.java.JavaPlugin; public class MyPlugin extends JavaPlugin { @Override public void onEnable() { // Create library manager (downloads to plugins/MyPlugin/lib/) BukkitLibraryManager libraryManager = new BukkitLibraryManager(this); // Or with custom directory name (downloads to plugins/MyPlugin/dependencies/) BukkitLibraryManager customManager = new BukkitLibraryManager(this, "dependencies"); // Add repositories libraryManager.addMavenCentral(); libraryManager.addRepository("https://repo.alessiodp.com/releases/"); // Create and load a library Library hikariCP = Library.builder() .groupId("com{}zaxxer") .artifactId("HikariCP") .version("5.1.0") .relocate("com{}zaxxer{}hikari", "myplugin{}libs{}hikari") .build(); libraryManager.loadLibrary(hikariCP); // Library is now available in classpath // com.zaxxer.hikari.* classes are relocated to myplugin.libs.hikari.* } } ``` -------------------------------- ### Manage Dependencies with VelocityLibraryManager Source: https://context7.com/alessiodp/libby/llms.txt The `VelocityLibraryManager` class integrates with Velocity's plugin manager to handle plugin dependencies. It allows adding Maven Central and loading specific libraries defined by GAV coordinates, supporting relocation rules. ```java import com.velocitypowered.api.event.Subscribe; import com.velocitypowered.api.event.proxy.ProxyInitializeEvent; import com.velocitypowered.api.plugin.Plugin; import com.velocitypowered.api.plugin.PluginManager; import com.velocitypowered.api.plugin.annotation.DataDirectory; import net.byteflux.libby.Library; import net.byteflux.libby.VelocityLibraryManager; import org.slf4j.Logger; import javax.inject.Inject; import java.nio.file.Path; @Plugin(id = "myplugin", name = "MyPlugin", version = "1.0.0") public class MyVelocityPlugin { private final Logger logger; private final Path dataDirectory; private final PluginManager pluginManager; @Inject public MyVelocityPlugin(Logger logger, @DataDirectory Path dataDirectory, PluginManager pluginManager) { this.logger = logger; this.dataDirectory = dataDirectory; this.pluginManager = pluginManager; } @Subscribe public void onProxyInitialization(ProxyInitializeEvent event) { VelocityLibraryManager libraryManager = new VelocityLibraryManager<>(logger, dataDirectory, pluginManager, this); libraryManager.addMavenCentral(); Library lib = Library.builder() .groupId("com{}google{}guava") .artifactId("guava") .version("33.0.0-jre") .build(); libraryManager.loadLibrary(lib); } } ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.