From 28018430e7573d3fb880069f30fe45d19fe004b8 Mon Sep 17 00:00:00 2001 From: Auxilor Date: Wed, 16 Feb 2022 18:01:41 +0000 Subject: [PATCH] Categorized keys are now saved completely serialized --- .../eco/core/data/keys/PersistentDataKey.java | 1 + .../eco/internal/spigot/data/KeyHelpers.kt | 42 +++++++++++++++++++ .../spigot/data/storage/MySQLDataHandler.kt | 24 ++++++----- 3 files changed, 56 insertions(+), 11 deletions(-) create mode 100644 eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/KeyHelpers.kt diff --git a/eco-api/src/main/java/com/willfp/eco/core/data/keys/PersistentDataKey.java b/eco-api/src/main/java/com/willfp/eco/core/data/keys/PersistentDataKey.java index e1937c3e..063f19d7 100644 --- a/eco-api/src/main/java/com/willfp/eco/core/data/keys/PersistentDataKey.java +++ b/eco-api/src/main/java/com/willfp/eco/core/data/keys/PersistentDataKey.java @@ -1,6 +1,7 @@ package com.willfp.eco.core.data.keys; import com.willfp.eco.core.Eco; +import com.willfp.eco.util.NamespacedKeyUtils; import org.bukkit.NamespacedKey; import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.Nullable; diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/KeyHelpers.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/KeyHelpers.kt new file mode 100644 index 00000000..95c4ee21 --- /dev/null +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/KeyHelpers.kt @@ -0,0 +1,42 @@ +package com.willfp.eco.internal.spigot.data + +import com.willfp.eco.core.data.keys.PersistentDataKey +import com.willfp.eco.core.data.keys.PersistentDataKeyType +import com.willfp.eco.util.NamespacedKeyUtils + +@Suppress("UNCHECKED_CAST") +object KeyHelpers { + fun deserializeFromString(serialized: String): PersistentDataKey<*>? { + val split = serialized.split("::").toTypedArray() + if (split.size != 3) { + return null + } + val key = NamespacedKeyUtils.fromStringOrNull(split[0]) ?: return null + val type = PersistentDataKeyType.valueOf(split[1]) ?: return null + return when (type.name()) { + "STRING" -> PersistentDataKey( + key, + type as PersistentDataKeyType, + split[2] + ) + "INT" -> PersistentDataKey( + key, + type as PersistentDataKeyType, split[2].toInt() + ) + "DOUBLE" -> PersistentDataKey( + key, + type as PersistentDataKeyType, split[2].toDouble() + ) + "BOOLEAN" -> PersistentDataKey( + key, + type as PersistentDataKeyType, + java.lang.Boolean.parseBoolean(split[2]) + ) + else -> null + } + } + + fun serializeToString(key: PersistentDataKey<*>): String { + return "${key.key}::${key.type.name()}::${key.defaultValue}" + } +} \ No newline at end of file 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 2500bda6..40fb1250 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 @@ -7,8 +7,8 @@ import com.willfp.eco.core.data.keys.PersistentDataKey import com.willfp.eco.core.data.keys.PersistentDataKeyType import com.willfp.eco.internal.spigot.EcoSpigotPlugin import com.willfp.eco.internal.spigot.data.EcoProfileHandler +import com.willfp.eco.internal.spigot.data.KeyHelpers import com.willfp.eco.internal.spigot.data.serverProfileUUID -import com.willfp.eco.util.NamespacedKeyUtils import com.zaxxer.hikari.HikariConfig import com.zaxxer.hikari.HikariDataSource import org.apache.logging.log4j.Level @@ -42,7 +42,7 @@ class MySQLDataHandler( UUIDTable("eco_players"), plugin, plugin.dataYml.getStrings("categorized-keys.player") - .mapNotNull { NamespacedKeyUtils.fromStringOrNull(it) } + .mapNotNull { KeyHelpers.deserializeFromString(it) } ) private val serverHandler = ImplementedMySQLHandler( @@ -50,7 +50,7 @@ class MySQLDataHandler( UUIDTable("eco_server"), plugin, plugin.dataYml.getStrings("categorized-keys.server") - .mapNotNull { NamespacedKeyUtils.fromStringOrNull(it) } + .mapNotNull { KeyHelpers.deserializeFromString(it) } ) override fun saveAll(uuids: Iterable) { @@ -87,11 +87,15 @@ class MySQLDataHandler( override fun save() { plugin.dataYml.set( "categorized-keys.player", - playerHandler.registeredKeys.map { it.toString() } + playerHandler.registeredKeys + .mapNotNull { Eco.getHandler().keyRegistry.getKeyFrom(it) } + .map { KeyHelpers.serializeToString(it) } ) plugin.dataYml.set( "categorized-keys.server", - serverHandler.registeredKeys.map { it.toString() } + serverHandler.registeredKeys + .mapNotNull { Eco.getHandler().keyRegistry.getKeyFrom(it) } + .map { KeyHelpers.serializeToString(it) } ) plugin.dataYml.save() } @@ -107,7 +111,7 @@ private class ImplementedMySQLHandler( private val handler: EcoProfileHandler, private val table: UUIDTable, private val plugin: EcoPlugin, - private val knownKeys: Collection + private val knownKeys: Collection> ) { private val columns = mutableMapOf>() private val threadFactory = ThreadFactoryBuilder().setNameFormat("eco-mysql-thread-%d").build() @@ -146,16 +150,13 @@ private class ImplementedMySQLHandler( fun runPostInit() { plugin.logger.info("Loading known keys: $knownKeys") - val persistentKeys = knownKeys - .mapNotNull { Eco.getHandler().keyRegistry.getKeyFrom(it) } - transaction { - for (key in persistentKeys) { + for (key in knownKeys) { registerColumn(key, table) } SchemaUtils.createMissingTablesAndColumns(table, withLogs = false) - for (key in persistentKeys) { + for (key in knownKeys) { registeredKeys.add(key.key) } } @@ -173,6 +174,7 @@ private class ImplementedMySQLHandler( return } + plugin.logger.info("Registering new key: $key...") transaction { registerColumn(persistentKey, table) SchemaUtils.createMissingTablesAndColumns(table, withLogs = false)