From c2b8a8056071c8b715b9675cc649dcb258e3ed3d Mon Sep 17 00:00:00 2001 From: Auxilor Date: Wed, 25 May 2022 20:21:50 +0100 Subject: [PATCH] Improved MongoDB --- eco-core/core-plugin/build.gradle | 2 +- .../kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt | 6 +++++- .../eco/internal/spigot/data/storage/MongoDataHandler.kt | 9 +++++---- eco-core/core-plugin/src/main/resources/config.yml | 2 +- 4 files changed, 12 insertions(+), 7 deletions(-) diff --git a/eco-core/core-plugin/build.gradle b/eco-core/core-plugin/build.gradle index 89f7d3ee..4d1ab491 100644 --- a/eco-core/core-plugin/build.gradle +++ b/eco-core/core-plugin/build.gradle @@ -14,7 +14,7 @@ dependencies { implementation 'com.zaxxer:HikariCP:5.0.0' implementation 'net.kyori:adventure-platform-bukkit:4.1.0' implementation 'org.javassist:javassist:3.28.0-GA' - implementation 'org.mongodb:mongo-java-driver:2.12.3' + implementation 'org.mongodb:mongodb-driver-sync:4.6.0' implementation 'org.litote.kmongo:kmongo-coroutine:4.6.0' // Included in spigot jar diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt index 887f08a9..11c058b3 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoHandler.kt @@ -58,7 +58,11 @@ class EcoHandler : EcoSpigotPlugin(), Handler { private var adventure: BukkitAudiences? = null private val keyRegistry = EcoKeyRegistry() - private val playerProfileHandler = EcoProfileHandler(HandlerType.valueOf(this.configYml.getString("data-handler").uppercase()), this) + private val playerProfileHandler = EcoProfileHandler( + HandlerType.valueOf( + this.configYml.getString("data-handler").uppercase() + ), this + ) @Suppress("RedundantNullableReturnType") private val keyFactory: InternalNamespacedKeyFactory? = diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/MongoDataHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/MongoDataHandler.kt index 26954c35..57f97ba8 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/MongoDataHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/MongoDataHandler.kt @@ -47,9 +47,9 @@ class MongoDataHandler( val newData = profile.data.apply { if (value == null) { - this.remove(key) + this.remove(key.toString()) } else { - this[key] = value + this[key.toString()] = value } } @@ -74,7 +74,7 @@ class MongoDataHandler( private suspend fun doRead(uuid: UUID, key: PersistentDataKey): T? { val profile = collection.findOne(SerializableProfile::uuid eq uuid) ?: return key.defaultValue - return profile.data[key.key] as? T? + return profile.data[key.key.toString()] as? T? } private suspend fun getOrCreateDocument(uuid: UUID): SerializableProfile { @@ -97,5 +97,6 @@ class MongoDataHandler( private data class SerializableProfile( @BsonId val uuid: UUID, - val data: MutableMap + // Storing NamespacedKeys as strings for serialization + val data: MutableMap ) diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index 0c448cc1..4539cb4b 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -3,7 +3,7 @@ # by Auxilor # -handler-type: yaml # Pick from yaml/mongo/mysql - MongoDB is recommended over MySQL for networks. +data-handler: yaml # Pick from yaml/mongo/mysql - MongoDB is recommended over MySQL for networks. mongodb: # The full MongoDB connection URL.