From 0c11c52f3795bf08ba5f70fd3ff2cd3af1e5b24f Mon Sep 17 00:00:00 2001 From: Joseph Date: Wed, 29 Jul 2020 01:20:34 +0100 Subject: [PATCH] Add support for mariadb driver --- .../endervaults/api/storage/DataStorage.java | 2 +- .../dig/endervaults/api/storage/Storage.java | 3 +- bukkit/build.gradle | 2 ++ .../endervaults/bukkit/EVBukkitPlugin.java | 7 +++-- ...riMySQLStorage.java => HikariStorage.java} | 14 ++++++--- .../bukkit/storage/YamlStorage.java | 3 +- bukkit/src/main/resources/config.yml | 30 ++++++++++++++++++- 7 files changed, 50 insertions(+), 11 deletions(-) rename bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/{HikariMySQLStorage.java => HikariStorage.java} (95%) diff --git a/api/src/main/java/com/github/dig/endervaults/api/storage/DataStorage.java b/api/src/main/java/com/github/dig/endervaults/api/storage/DataStorage.java index 19b2769..ab8e61e 100644 --- a/api/src/main/java/com/github/dig/endervaults/api/storage/DataStorage.java +++ b/api/src/main/java/com/github/dig/endervaults/api/storage/DataStorage.java @@ -9,7 +9,7 @@ public interface DataStorage { - boolean init(); + boolean init(Storage storage); void close(); diff --git a/api/src/main/java/com/github/dig/endervaults/api/storage/Storage.java b/api/src/main/java/com/github/dig/endervaults/api/storage/Storage.java index 1c4f0bf..59fc01a 100644 --- a/api/src/main/java/com/github/dig/endervaults/api/storage/Storage.java +++ b/api/src/main/java/com/github/dig/endervaults/api/storage/Storage.java @@ -3,6 +3,7 @@ public enum Storage { FLATFILE, - MYSQL + MYSQL, + MARIADB } diff --git a/bukkit/build.gradle b/bukkit/build.gradle index cbb2137..14f771a 100644 --- a/bukkit/build.gradle +++ b/bukkit/build.gradle @@ -12,6 +12,7 @@ dependencies { compileOnly 'org.spigotmc:spigot-api:1.16.1-R0.1-SNAPSHOT' compileOnly 'org.bukkit:craftbukkit:1.16.1-R0.1-SNAPSHOT' compile 'com.zaxxer:HikariCP:3.4.5' + compile 'org.mariadb.jdbc:mariadb-java-client:2.6.2' compile 'de.tr7zw:item-nbt-api:2.4.1' } @@ -20,5 +21,6 @@ shadowJar { relocate 'de.tr7zw.changeme.nbtapi', 'com.github.dig.endervaults.lib.nbt' relocate 'com.zaxxer.hikari', 'com.github.dig.endervaults.lib.hikari' + relocate 'org.mariadb.jdbc', 'com.github.dig.endervaults.lib.mariadb' relocate 'org.javatuples', 'com.github.dig.endervaults.lib.javatuples' } diff --git a/bukkit/src/main/java/com/github/dig/endervaults/bukkit/EVBukkitPlugin.java b/bukkit/src/main/java/com/github/dig/endervaults/bukkit/EVBukkitPlugin.java index 247f691..58e9c7e 100644 --- a/bukkit/src/main/java/com/github/dig/endervaults/bukkit/EVBukkitPlugin.java +++ b/bukkit/src/main/java/com/github/dig/endervaults/bukkit/EVBukkitPlugin.java @@ -17,7 +17,7 @@ import com.github.dig.endervaults.bukkit.ui.icon.SelectIconListener; import com.github.dig.endervaults.bukkit.ui.selector.SelectorListener; import com.github.dig.endervaults.bukkit.storage.YamlStorage; -import com.github.dig.endervaults.bukkit.storage.HikariMySQLStorage; +import com.github.dig.endervaults.bukkit.storage.HikariStorage; import com.github.dig.endervaults.bukkit.vault.BukkitVaultAutoSave; import com.github.dig.endervaults.bukkit.vault.BukkitVaultPersister; import com.github.dig.endervaults.bukkit.vault.metadata.BukkitVaultMetadataRegistry; @@ -201,13 +201,14 @@ private boolean setupDataStorage() { case FLATFILE: dataStorage = new YamlStorage(); break; + case MARIADB: case MYSQL: - dataStorage = new HikariMySQLStorage(); + dataStorage = new HikariStorage(); break; } log.log(Level.INFO, "[EnderVaults] Using data storage: " + storage.toString() + "."); - if (!dataStorage.init()) { + if (!dataStorage.init(storage)) { log.log(Level.SEVERE, "[EnderVaults] Error with data storage, disabling..."); Bukkit.getPluginManager().disablePlugin(this); return false; diff --git a/bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/HikariMySQLStorage.java b/bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/HikariStorage.java similarity index 95% rename from bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/HikariMySQLStorage.java rename to bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/HikariStorage.java index 395aa04..58d88e3 100644 --- a/bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/HikariMySQLStorage.java +++ b/bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/HikariStorage.java @@ -3,6 +3,7 @@ import com.github.dig.endervaults.api.PluginProvider; import com.github.dig.endervaults.api.lang.Lang; import com.github.dig.endervaults.api.storage.DataStorage; +import com.github.dig.endervaults.api.storage.Storage; import com.github.dig.endervaults.api.util.VaultSerializable; import com.github.dig.endervaults.api.vault.Vault; import com.github.dig.endervaults.api.vault.metadata.MetadataConverter; @@ -24,7 +25,7 @@ import java.util.logging.Level; @Log -public class HikariMySQLStorage implements DataStorage { +public class HikariStorage implements DataStorage { private final EVBukkitPlugin plugin = (EVBukkitPlugin) PluginProvider.getPlugin(); @@ -33,9 +34,9 @@ public class HikariMySQLStorage implements DataStorage { private String metadataTable; @Override - public boolean init() { + public boolean init(Storage storage) { FileConfiguration config = (FileConfiguration) plugin.getConfigFile().getConfiguration(); - ConfigurationSection settings = config.getConfigurationSection("storage.settings.mysql"); + ConfigurationSection settings = config.getConfigurationSection(storage == Storage.MYSQL ? "storage.settings.mysql" : "storage.settings.mariadb"); String address = settings.getString("address", "localhost"); String database = settings.getString("database", "minecraft"); @@ -43,11 +44,16 @@ public boolean init() { String password = settings.getString("password", "123"); HikariConfig hikariConfig = new HikariConfig(); - hikariConfig.setJdbcUrl(String.format("jdbc:mysql://%s/%s", address, database)); + hikariConfig.setJdbcUrl(String.format("jdbc:%s://%s/%s", storage == Storage.MYSQL ? "mysql" : "mariadb", address, database)); hikariConfig.setUsername(user); hikariConfig.setPassword(password); + if (storage == Storage.MARIADB) { + hikariConfig.setDriverClassName("org.mariadb.jdbc.Driver"); + hikariConfig.setAutoCommit(false); + } + ConfigurationSection properties = settings.getConfigurationSection("properties"); for (String key : properties.getKeys(false)) { hikariConfig.addDataSourceProperty(key, properties.getString(key)); diff --git a/bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/YamlStorage.java b/bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/YamlStorage.java index bf09ed4..4e95998 100644 --- a/bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/YamlStorage.java +++ b/bukkit/src/main/java/com/github/dig/endervaults/bukkit/storage/YamlStorage.java @@ -3,6 +3,7 @@ import com.github.dig.endervaults.api.PluginProvider; import com.github.dig.endervaults.api.lang.Lang; import com.github.dig.endervaults.api.storage.DataStorage; +import com.github.dig.endervaults.api.storage.Storage; import com.github.dig.endervaults.api.util.VaultSerializable; import com.github.dig.endervaults.api.vault.Vault; import com.github.dig.endervaults.api.vault.metadata.VaultMetadataRegistry; @@ -25,7 +26,7 @@ public class YamlStorage implements DataStorage { private final EVBukkitPlugin plugin = (EVBukkitPlugin) PluginProvider.getPlugin(); @Override - public boolean init() { + public boolean init(Storage storage) { return true; } diff --git a/bukkit/src/main/resources/config.yml b/bukkit/src/main/resources/config.yml index 88cdafb..567a007 100644 --- a/bukkit/src/main/resources/config.yml +++ b/bukkit/src/main/resources/config.yml @@ -113,15 +113,19 @@ storage: # All the different settings for the storage methods. settings: + + # Flat file settings flatfile: # Possible file formats: # - YAML type: YAML # Where the data will be saved directory: 'data' + + # MySQL settings mysql: # Address and port. Can specify as "host:port" or "host". - # Default port for MySQL and MariaDB is 3306. + # Default port for MySQL is 3306. address: 'localhost' # Database to create tables in @@ -144,3 +148,27 @@ storage: tables: vault: 'endervaults_vault' vault-metadata: 'endervaults_vault_metadata' + + # MariaDB settings + mariadb: + # Address and port. Can specify as "host:port" or "host". + # Default port for MariaDB is 3306. + address: 'localhost' + + # Database to create tables in + database: 'minecraft' + + # Credentials + user: 'minecraft' + password: '123' + + # Connection properties + properties: + cachePrepStmts: 'true' + prepStmtCacheSize: '250' + prepStmtCacheSqlLimit: '2048' + + # Table names + tables: + vault: 'endervaults_vault' + vault-metadata: 'endervaults_vault_metadata'