diff --git a/eco-api/src/main/java/com/willfp/eco/core/data/ProfileHandler.java b/eco-api/src/main/java/com/willfp/eco/core/data/ProfileHandler.java index a24787c5..63acb9a9 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/data/ProfileHandler.java +++ b/eco-api/src/main/java/com/willfp/eco/core/data/ProfileHandler.java @@ -68,7 +68,7 @@ public interface ProfileHandler { * @param async If the saving should be done asynchronously. * @deprecated async is now handled automatically depending on implementation. */ - @Deprecated + @Deprecated(forRemoval = true) default void saveAll(boolean async) { saveAll(); } @@ -77,8 +77,13 @@ public interface ProfileHandler { * Save all player data. *

* Can run async if using MySQL. + * + * @deprecated Never used. */ - void saveAll(); + @Deprecated(since = "6.36.0", forRemoval = true) + default void saveAll() { + // Do nothing. + } /** * Commit all changes to the file. diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt index 9053a0b5..071f22cc 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/EcoSpigotPlugin.kt @@ -239,7 +239,7 @@ abstract class EcoSpigotPlugin : EcoPlugin() { override fun handleReload() { CollatedRunnable(this) DropManager.update(this) - ProfileSaver(this) + ProfileSaver(this, Eco.getHandler().profileHandler) this.scheduler.runTimer( { clearFrames() }, this.configYml.getInt("display-frame-ttl").toLong(), diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/EcoProfileHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/EcoProfileHandler.kt index 459ce54c..266d8637 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/EcoProfileHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/EcoProfileHandler.kt @@ -63,10 +63,6 @@ class EcoProfileHandler( loaded.remove(uuid) } - override fun saveAll() { - handler.saveAll(loaded.keys.toList()) - } - override fun save() { handler.save() } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/DataHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/DataHandler.kt index 8009ec69..f9a1188a 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/DataHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/DataHandler.kt @@ -5,15 +5,30 @@ import com.willfp.eco.core.data.keys.PersistentDataKey import java.util.UUID interface DataHandler { - fun write(uuid: UUID, key: PersistentDataKey, value: Any) + /** + * Read value from a key. + */ fun read(uuid: UUID, key: PersistentDataKey): T? + /** + * Write value to a key. + * + * The value is set to the Any type rather than T because of generic casts + * with unknown types. + */ + fun write(uuid: UUID, key: PersistentDataKey, value: Any) + + /** + * Save a set of keys for a given UUID. + */ + fun saveKeysFor(uuid: UUID, keys: Set>) + + // Everything below this are methods that are only needed for certain implementations. + fun save() { } - fun saveAll(uuids: Iterable) - fun categorize(key: PersistentDataKey<*>, category: KeyRegistry.KeyCategory) { } @@ -21,10 +36,4 @@ interface DataHandler { fun initialize() { } - - fun savePlayer(uuid: UUID) { - saveKeysFor(uuid, PersistentDataKey.values()) - } - - fun saveKeysFor(uuid: UUID, keys: Set>) } \ No newline at end of file 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 41a36e75..af721f3b 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 @@ -39,9 +39,9 @@ class MongoDataHandler( collection = client.getDatabase("eco").getCollection() } - override fun saveAll(uuids: Iterable) { - for (uuid in uuids) { - saveKeysFor(uuid, PersistentDataKey.values()) + override fun read(uuid: UUID, key: PersistentDataKey): T? { + return runBlocking { + doRead(uuid, key) } } @@ -51,6 +51,16 @@ class MongoDataHandler( } } + override fun saveKeysFor(uuid: UUID, keys: Set>) { + val profile = handler.loadGenericProfile(uuid) + + scope.launch { + for (key in keys) { + doWrite(uuid, key.key, profile.read(key)) + } + } + } + private suspend fun doWrite(uuid: UUID, key: NamespacedKey, value: T) { val profile = getOrCreateDocument(uuid) @@ -65,22 +75,6 @@ class MongoDataHandler( collection.updateOne(UUIDProfile::uuid eq uuid.toString(), setValue(UUIDProfile::data, newData)) } - override fun saveKeysFor(uuid: UUID, keys: Set>) { - val profile = handler.loadGenericProfile(uuid) - - scope.launch { - for (key in keys) { - doWrite(uuid, key.key, profile.read(key)) - } - } - } - - override fun read(uuid: UUID, key: PersistentDataKey): T? { - return runBlocking { - doRead(uuid, key) - } - } - private suspend fun doRead(uuid: UUID, key: PersistentDataKey): T? { val profile = collection.findOne(UUIDProfile::uuid eq uuid.toString()) ?: return key.defaultValue return profile.data[key.key.toString()] as? T? diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/MySQLDataHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/MySQLDataHandler.kt index 71c350b7..8476d94f 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/MySQLDataHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/MySQLDataHandler.kt @@ -102,9 +102,10 @@ class MySQLDataHandler( ) } - override fun saveAll(uuids: Iterable) { - serverHandler.saveAll(uuids.filter { it == serverProfileUUID }) - playerHandler.saveAll(uuids.filter { it != serverProfileUUID }) + override fun read(uuid: UUID, key: PersistentDataKey): T? { + return applyFor(uuid) { + it.read(uuid, key) + } } override fun write(uuid: UUID, key: PersistentDataKey, value: Any) { @@ -119,12 +120,6 @@ class MySQLDataHandler( } } - override fun read(uuid: UUID, key: PersistentDataKey): T? { - return applyFor(uuid) { - it.read(uuid, key) - } - } - private inline fun applyFor(uuid: UUID, function: (ImplementedMySQLHandler) -> R): R { return if (uuid == serverProfileUUID) { function(serverHandler) diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/ProfileSaver.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/ProfileSaver.kt index a9d8404a..44f66c0a 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/ProfileSaver.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/ProfileSaver.kt @@ -1,16 +1,19 @@ package com.willfp.eco.internal.spigot.data.storage -import com.willfp.eco.core.Eco import com.willfp.eco.core.EcoPlugin +import com.willfp.eco.core.data.ProfileHandler import com.willfp.eco.internal.spigot.data.EcoProfile -class ProfileSaver(plugin: EcoPlugin) { +class ProfileSaver( + plugin: EcoPlugin, + handler: ProfileHandler +) { init { - plugin.scheduler.runTimer({ + plugin.scheduler.runTimer(1, 1) { for ((uuid, set) in EcoProfile.CHANGE_MAP) { - Eco.getHandler().profileHandler.saveKeysFor(uuid, set) + handler.saveKeysFor(uuid, set) } EcoProfile.CHANGE_MAP.clear() - }, 1, 1) + } } -} \ No newline at end of file +} diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/YamlDataHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/YamlDataHandler.kt index 73f82695..3959215e 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/YamlDataHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/storage/YamlDataHandler.kt @@ -18,30 +18,6 @@ class YamlDataHandler( dataYml.save() } - override fun saveAll(uuids: Iterable) { - for (uuid in uuids) { - savePlayer(uuid) - } - - save() - } - - override fun saveKeysFor(uuid: UUID, keys: Set>) { - val profile = handler.loadGenericProfile(uuid) - - for (key in keys) { - doWrite(uuid, key.key, profile.read(key)) - } - } - - override fun write(uuid: UUID, key: PersistentDataKey, value: Any) { - doWrite(uuid, key.key, value) - } - - private fun doWrite(uuid: UUID, key: NamespacedKey, value: Any) { - dataYml.set("player.$uuid.$key", value) - } - override fun read(uuid: UUID, key: PersistentDataKey): T? { // Separate `as T?` for each branch to prevent compiler warnings. val value = when (key.type) { @@ -55,4 +31,20 @@ class YamlDataHandler( return value } + + override fun write(uuid: UUID, key: PersistentDataKey, value: Any) { + doWrite(uuid, key.key, value) + } + + override fun saveKeysFor(uuid: UUID, keys: Set>) { + val profile = handler.loadGenericProfile(uuid) + + for (key in keys) { + doWrite(uuid, key.key, profile.read(key)) + } + } + + private fun doWrite(uuid: UUID, key: NamespacedKey, value: Any) { + dataYml.set("player.$uuid.$key", value) + } }