diff --git a/bukkit/src/main/resources/config.yml b/bukkit/src/main/resources/config.yml index 6fdd836ed..8fa99a81a 100644 --- a/bukkit/src/main/resources/config.yml +++ b/bukkit/src/main/resources/config.yml @@ -328,6 +328,10 @@ data: # The prefix to use for all LuckPerms collections. Change this if you want to use different # collections for different servers. The default is no prefix. mongodb_collection_prefix: '' + + # MongoDB ClientConnectionURI for use with replicasets and custom connection options + # See https://docs.mongodb.com/manual/reference/connection-string/ + mongodb_connection_URI: '' # This option controls how frequently LuckPerms will perform a sync task. # A sync task will refresh all data from the storage, and ensure that the most up-to-date data is @@ -456,4 +460,4 @@ default-assignments: # - group.default # give: # - group.member -# set-primary-group: member \ No newline at end of file +# set-primary-group: member diff --git a/bungee/src/main/resources/config.yml b/bungee/src/main/resources/config.yml index 67cd39d28..fe15c40e7 100644 --- a/bungee/src/main/resources/config.yml +++ b/bungee/src/main/resources/config.yml @@ -270,6 +270,10 @@ data: # The prefix to use for all LuckPerms collections. Change this if you want to use different # collections for different servers. The default is no prefix. mongodb_collection_prefix: '' + + # MongoDB ClientConnectionURI for use with replicasets and custom connection options + # See https://docs.mongodb.com/manual/reference/connection-string/ + mongodb_connection_URI: '' # This option controls how frequently LuckPerms will perform a sync task. # A sync task will refresh all data from the storage, and ensure that the most up-to-date data is @@ -399,4 +403,4 @@ default-assignments: # - group.default # give: # - group.member -# set-primary-group: member \ No newline at end of file +# set-primary-group: member diff --git a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java index f3c85a193..53f29b912 100644 --- a/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java +++ b/common/src/main/java/me/lucko/luckperms/common/config/ConfigKeys.java @@ -404,7 +404,12 @@ public class ConfigKeys { * The prefix for any MongoDB collections */ public static final ConfigKey MONGODB_COLLECTION_PREFIX = EnduringKey.wrap(StringKey.of("data.mongodb_collection_prefix", "")); - + + /** + * MongoDB ClientConnectionURI to override default connection options + */ + public static final ConfigKey MONGODB_CONNECTION_URI = EnduringKey.wrap(StringKey.of("data.mongodb_connection_URI", "")); + /** * The name of the storage method being used */ diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java b/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java index ab519eb3e..b132404fb 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/StorageFactory.java @@ -158,7 +158,8 @@ public class StorageFactory { return new MongoDao( this.plugin, this.plugin.getConfiguration().get(ConfigKeys.DATABASE_VALUES), - this.plugin.getConfiguration().get(ConfigKeys.MONGODB_COLLECTION_PREFIX) + this.plugin.getConfiguration().get(ConfigKeys.MONGODB_COLLECTION_PREFIX), + this.plugin.getConfiguration().get(ConfigKeys.MONGODB_CONNECTION_URI) ); case YAML: return new YamlDao(this.plugin, "yaml-storage"); diff --git a/common/src/main/java/me/lucko/luckperms/common/storage/dao/mongodb/MongoDao.java b/common/src/main/java/me/lucko/luckperms/common/storage/dao/mongodb/MongoDao.java index cbb773af9..ed6426f5e 100644 --- a/common/src/main/java/me/lucko/luckperms/common/storage/dao/mongodb/MongoDao.java +++ b/common/src/main/java/me/lucko/luckperms/common/storage/dao/mongodb/MongoDao.java @@ -28,6 +28,7 @@ package me.lucko.luckperms.common.storage.dao.mongodb; import com.google.common.base.Strings; import com.mongodb.MongoClient; import com.mongodb.MongoClientOptions; +import com.mongodb.MongoClientURI; import com.mongodb.MongoCredential; import com.mongodb.ServerAddress; import com.mongodb.client.MongoCollection; @@ -77,35 +78,41 @@ public class MongoDao extends AbstractDao { private MongoClient mongoClient; private MongoDatabase database; private final String prefix; + private final String connectionURI; - public MongoDao(LuckPermsPlugin plugin, StorageCredentials configuration, String prefix) { + public MongoDao(LuckPermsPlugin plugin, StorageCredentials configuration, String prefix, String connectionURI) { super(plugin, "MongoDB"); this.configuration = configuration; this.prefix = prefix; + this.connectionURI = connectionURI; } @Override public void init() { - MongoCredential credential = null; - if (!Strings.isNullOrEmpty(this.configuration.getUsername())) { - credential = MongoCredential.createCredential( - this.configuration.getUsername(), - this.configuration.getDatabase(), - Strings.isNullOrEmpty(this.configuration.getPassword()) ? null : this.configuration.getPassword().toCharArray() - ); - } - - String[] addressSplit = this.configuration.getAddress().split(":"); - String host = addressSplit[0]; - int port = addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : 27017; - ServerAddress address = new ServerAddress(host, port); - - if (credential == null) { - this.mongoClient = new MongoClient(address); + if (!Strings.isNullOrempty(this.connectionURI)) { + this.mongoClient = new MongoClient(new MongoClientURI(this.connectionURI)); } else { - this.mongoClient = new MongoClient(address, credential, MongoClientOptions.builder().build()); - } + MongoCredential credential = null; + if (!Strings.isNullOrEmpty(this.configuration.getUsername())) { + credential = MongoCredential.createCredential( + this.configuration.getUsername(), + this.configuration.getDatabase(), + Strings.isNullOrEmpty(this.configuration.getPassword()) ? null : this.configuration.getPassword().toCharArray() + ); + } + String[] addressSplit = this.configuration.getAddress().split(":"); + String host = addressSplit[0]; + int port = addressSplit.length > 1 ? Integer.parseInt(addressSplit[1]) : 27017; + ServerAddress address = new ServerAddress(host, port); + + if (credential == null) { + this.mongoClient = new MongoClient(address); + } else { + this.mongoClient = new MongoClient(address, credential, MongoClientOptions.builder().build()); + } + } + this.database = this.mongoClient.getDatabase(this.configuration.getDatabase()); } diff --git a/nukkit/src/main/resources/config.yml b/nukkit/src/main/resources/config.yml index 068844748..2b691e3e0 100644 --- a/nukkit/src/main/resources/config.yml +++ b/nukkit/src/main/resources/config.yml @@ -328,6 +328,10 @@ data: # The prefix to use for all LuckPerms collections. Change this if you want to use different # collections for different servers. The default is no prefix. mongodb_collection_prefix: '' + + # MongoDB ClientConnectionURI for use with replicasets and custom connection options + # See https://docs.mongodb.com/manual/reference/connection-string/ + mongodb_connection_URI: '' # This option controls how frequently LuckPerms will perform a sync task. # A sync task will refresh all data from the storage, and ensure that the most up-to-date data is @@ -456,4 +460,4 @@ default-assignments: # - group.default # give: # - group.member -# set-primary-group: member \ No newline at end of file +# set-primary-group: member diff --git a/sponge/src/main/resources/luckperms.conf b/sponge/src/main/resources/luckperms.conf index 127b8ab4f..2e495230b 100644 --- a/sponge/src/main/resources/luckperms.conf +++ b/sponge/src/main/resources/luckperms.conf @@ -277,6 +277,10 @@ data { # The prefix to use for all LuckPerms collections. Change this if you want to use different # collections for different servers. The default is no prefix. mongodb_collection_prefix="" + + # MongoDB ClientConnectionURI for use with replicasets and custom connection options + # See https://docs.mongodb.com/manual/reference/connection-string/ + mongodb_connection_URI="" # This option controls how frequently LuckPerms will perform a sync task. # A sync task will refresh all data from the storage, and ensure that the most up-to-date data is