Improved DataHandler
This commit is contained in:
@@ -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(),
|
||||
|
||||
@@ -63,10 +63,6 @@ class EcoProfileHandler(
|
||||
loaded.remove(uuid)
|
||||
}
|
||||
|
||||
override fun saveAll() {
|
||||
handler.saveAll(loaded.keys.toList())
|
||||
}
|
||||
|
||||
override fun save() {
|
||||
handler.save()
|
||||
}
|
||||
|
||||
@@ -5,15 +5,30 @@ import com.willfp.eco.core.data.keys.PersistentDataKey
|
||||
import java.util.UUID
|
||||
|
||||
interface DataHandler {
|
||||
fun <T : Any> write(uuid: UUID, key: PersistentDataKey<T>, value: Any)
|
||||
/**
|
||||
* Read value from a key.
|
||||
*/
|
||||
fun <T : Any> read(uuid: UUID, key: PersistentDataKey<T>): 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 <T : Any> write(uuid: UUID, key: PersistentDataKey<T>, value: Any)
|
||||
|
||||
/**
|
||||
* Save a set of keys for a given UUID.
|
||||
*/
|
||||
fun saveKeysFor(uuid: UUID, keys: Set<PersistentDataKey<*>>)
|
||||
|
||||
// Everything below this are methods that are only needed for certain implementations.
|
||||
|
||||
fun save() {
|
||||
|
||||
}
|
||||
|
||||
fun saveAll(uuids: Iterable<UUID>)
|
||||
|
||||
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<PersistentDataKey<*>>)
|
||||
}
|
||||
@@ -39,9 +39,9 @@ class MongoDataHandler(
|
||||
collection = client.getDatabase("eco").getCollection()
|
||||
}
|
||||
|
||||
override fun saveAll(uuids: Iterable<UUID>) {
|
||||
for (uuid in uuids) {
|
||||
saveKeysFor(uuid, PersistentDataKey.values())
|
||||
override fun <T : Any> read(uuid: UUID, key: PersistentDataKey<T>): T? {
|
||||
return runBlocking {
|
||||
doRead(uuid, key)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -51,6 +51,16 @@ class MongoDataHandler(
|
||||
}
|
||||
}
|
||||
|
||||
override fun saveKeysFor(uuid: UUID, keys: Set<PersistentDataKey<*>>) {
|
||||
val profile = handler.loadGenericProfile(uuid)
|
||||
|
||||
scope.launch {
|
||||
for (key in keys) {
|
||||
doWrite(uuid, key.key, profile.read(key))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun <T> 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<PersistentDataKey<*>>) {
|
||||
val profile = handler.loadGenericProfile(uuid)
|
||||
|
||||
scope.launch {
|
||||
for (key in keys) {
|
||||
doWrite(uuid, key.key, profile.read(key))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
override fun <T : Any> read(uuid: UUID, key: PersistentDataKey<T>): T? {
|
||||
return runBlocking {
|
||||
doRead(uuid, key)
|
||||
}
|
||||
}
|
||||
|
||||
private suspend fun <T> doRead(uuid: UUID, key: PersistentDataKey<T>): T? {
|
||||
val profile = collection.findOne(UUIDProfile::uuid eq uuid.toString()) ?: return key.defaultValue
|
||||
return profile.data[key.key.toString()] as? T?
|
||||
|
||||
@@ -102,9 +102,10 @@ class MySQLDataHandler(
|
||||
)
|
||||
}
|
||||
|
||||
override fun saveAll(uuids: Iterable<UUID>) {
|
||||
serverHandler.saveAll(uuids.filter { it == serverProfileUUID })
|
||||
playerHandler.saveAll(uuids.filter { it != serverProfileUUID })
|
||||
override fun <T : Any> read(uuid: UUID, key: PersistentDataKey<T>): T? {
|
||||
return applyFor(uuid) {
|
||||
it.read(uuid, key)
|
||||
}
|
||||
}
|
||||
|
||||
override fun <T : Any> write(uuid: UUID, key: PersistentDataKey<T>, value: Any) {
|
||||
@@ -119,12 +120,6 @@ class MySQLDataHandler(
|
||||
}
|
||||
}
|
||||
|
||||
override fun <T : Any> read(uuid: UUID, key: PersistentDataKey<T>): T? {
|
||||
return applyFor(uuid) {
|
||||
it.read(uuid, key)
|
||||
}
|
||||
}
|
||||
|
||||
private inline fun <R> applyFor(uuid: UUID, function: (ImplementedMySQLHandler) -> R): R {
|
||||
return if (uuid == serverProfileUUID) {
|
||||
function(serverHandler)
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -18,30 +18,6 @@ class YamlDataHandler(
|
||||
dataYml.save()
|
||||
}
|
||||
|
||||
override fun saveAll(uuids: Iterable<UUID>) {
|
||||
for (uuid in uuids) {
|
||||
savePlayer(uuid)
|
||||
}
|
||||
|
||||
save()
|
||||
}
|
||||
|
||||
override fun saveKeysFor(uuid: UUID, keys: Set<PersistentDataKey<*>>) {
|
||||
val profile = handler.loadGenericProfile(uuid)
|
||||
|
||||
for (key in keys) {
|
||||
doWrite(uuid, key.key, profile.read(key))
|
||||
}
|
||||
}
|
||||
|
||||
override fun <T : Any> write(uuid: UUID, key: PersistentDataKey<T>, value: Any) {
|
||||
doWrite(uuid, key.key, value)
|
||||
}
|
||||
|
||||
private fun doWrite(uuid: UUID, key: NamespacedKey, value: Any) {
|
||||
dataYml.set("player.$uuid.$key", value)
|
||||
}
|
||||
|
||||
override fun <T : Any> read(uuid: UUID, key: PersistentDataKey<T>): 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 <T : Any> write(uuid: UUID, key: PersistentDataKey<T>, value: Any) {
|
||||
doWrite(uuid, key.key, value)
|
||||
}
|
||||
|
||||
override fun saveKeysFor(uuid: UUID, keys: Set<PersistentDataKey<*>>) {
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user