The Database plugin provides a seamless way to handle database credentials and creates a pooled connection for other plugins to utilize. With this plugin, developers can easily integrate database functionality into their own plugins without having to worry about managing connections or credentials.
To install the Database plugin, follow these steps:
- Download the latest version of the plugin JAR file from the releases section of the repository.
- Place the downloaded Database.jar file into the plugins directory of your Minecraft server.
- Edit the
Database/config.ymlwith the proper credentials
- MySQL
- Redis
# MySQL Databases# You can define as many databases as needed with unique identifiersdatabases:
# Primary databaseprimary:
enabled: trueurl: "jdbc:mysql://localhost:3306/minecraft"username: "user"password: "password"max-connections: 10timezone: "America/Los_Angeles"# Optional: Add more databases as neededstats:
enabled: trueurl: "jdbc:mysql://stats-server:3306/stats"username: "stats_user"password: "stats_pass"max-connections: 5timezone: "UTC"# Redis Configurationredis:
enabled: truehost: "localhost"port: 6379password: ""database: 0timeout: 2000max-connections: 10To utilize the Database API in your own plugin, follow these steps:
Add the pgm.fyi repo to your Maven pom.xml
<repositories>
<repository>
<id>pgm-repo-snapshots</id>
<name>PGM Repository</name>
<url>https://repo.pgm.fyi/snapshots</url>
</repository>
</repositories>Add the Database plugin as a dependency in your Maven pom.xml
<dependency>
<groupId>tc.oc.occ</groupId>
<artifactId>Database</artifactId>
<version>2.0.0-SNAPSHOT</version>
<scope>provided</scope>
</dependency>Below is an example of how to utilize the new Database API:
importjava.sql.Connection;
importjava.sql.PreparedStatement;
importjava.sql.ResultSet;
importjava.sql.SQLException;
importjava.sql.Statement;
importjava.util.Optional;
importjava.util.concurrent.CompletableFuture;
importjava.util.concurrent.ExecutorService;
importjava.util.concurrent.Executors;
importorg.bukkit.plugin.java.JavaPlugin;
importtc.oc.occ.database.Database;
publicclassYourPluginextendsJavaPlugin {
privateExecutorServiceexecutorService;
@OverridepublicvoidonEnable() {
this.executorService = Executors.newFixedThreadPool(5);
createTable("test", "id INT PRIMARY KEY, some_col VARCHAR(65)");
}
@OverridepublicvoidonDisable() {
if (executorService != null) {
executorService.shutdown();
}
}
privatevoidcreateTable(Stringtable, Stringschema) {
CompletableFuture.runAsync(() -> {
// Get connection from default database (first enabled database)Optional<Connection> connOpt = Database.get().getConnection();
if (!connOpt.isPresent()) {
getLogger().warning("Database is not available!");
return;
}
try (Connectionconn = connOpt.get();
Statementstmt = conn.createStatement()) {
Stringsql = "CREATE TABLE IF NOT EXISTS " + table + " (" + schema + ")";
stmt.execute(sql);
getLogger().info("Created table: " + table);
} catch (SQLExceptione) {
getLogger().warning("Error creating table " + table + ": " + e.getMessage());
}
}, executorService);
}
publicCompletableFuture<String> getStringFromTestTable(intid) {
returnCompletableFuture.supplyAsync(() -> {
// Get connection from default databaseOptional<Connection> connOpt = Database.get().getConnection();
if (!connOpt.isPresent()) {
returnnull;
}
finalStringsql = "SELECT * FROM test WHERE id = ?";
try (Connectionconn = connOpt.get();
PreparedStatementstmt = conn.prepareStatement(sql)) {
stmt.setInt(1, id);
try (ResultSetresult = stmt.executeQuery()) {
if (result.next()) {
returnresult.getString("some_col");
}
}
} catch (SQLExceptione) {
getLogger().warning("Database issue: " + e.getMessage());
}
returnnull;
}, executorService);
}
// Example: Using a specific named databasepublicvoidsaveToStatsDatabase(intplayerId, intscore) {
CompletableFuture.runAsync(() -> {
// Get connection from the "stats" databaseOptional<Connection> connOpt = Database.get().getConnection("stats");
if (!connOpt.isPresent()) {
getLogger().warning("Stats database is not available!");
return;
}
try (Connectionconn = connOpt.get();
PreparedStatementstmt = conn.prepareStatement(
"INSERT INTO player_stats (player_id, score) VALUES (?, ?)")) {
stmt.setInt(1, playerId);
stmt.setInt(2, score);
stmt.executeUpdate();
} catch (SQLExceptione) {
getLogger().warning("Failed to save stats: " + e.getMessage());
}
}, executorService);
}
}The old API still works for backwards compatibility but is deprecated:
// Old way (deprecated)Connectionconn = Database.get().getConnectionPool().getPool().getConnection();
// New way (recommended)Optional<Connection> conn = Database.get().getConnection();
// or for a specific database:Optional<Connection> statsConn = Database.get().getConnection("stats");The Database plugin is open source and released under the MIT License. Please review the LICENSE file for more details.