From 413ae4e94df996f6b0acc6e49671f240a877f53f Mon Sep 17 00:00:00 2001 From: Auxilor Date: Sun, 25 Aug 2024 17:29:40 +0100 Subject: [PATCH] Persistent data tweaks and improvements --- .../eco/core/data/keys/PersistentDataKey.java | 17 +++----- .../eco/internal/spigot/data/KeyRegistry.kt | 39 +------------------ .../impl/MySQLPersistentDataHandler.kt | 8 ++-- .../core-plugin/src/main/resources/config.yml | 4 +- 4 files changed, 15 insertions(+), 53 deletions(-) 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 13f80cacf..3b6dbaebc 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 @@ -40,18 +40,11 @@ public final class PersistentDataKey { * @param key The key. * @param type The data type. * @param defaultValue The default value. - * @param isLocal If the key uses local storage. */ public PersistentDataKey(@NotNull final NamespacedKey key, @NotNull final PersistentDataKeyType type, - @NotNull final T defaultValue, - final boolean isLocal) { - this.key = key; - this.defaultValue = defaultValue; - this.type = type; - this.isLocal = isLocal; - - Eco.get().registerPersistentKey(this); + @NotNull final T defaultValue) { + this(key, type, defaultValue, false); } /** @@ -60,14 +53,16 @@ public PersistentDataKey(@NotNull final NamespacedKey key, * @param key The key. * @param type The data type. * @param defaultValue The default value. + * @param isLocal If the key uses local storage. */ public PersistentDataKey(@NotNull final NamespacedKey key, @NotNull final PersistentDataKeyType type, - @NotNull final T defaultValue) { + @NotNull final T defaultValue, + final boolean isLocal) { this.key = key; this.defaultValue = defaultValue; this.type = type; - this.isLocal = false; + this.isLocal = isLocal; Eco.get().registerPersistentKey(this); } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/KeyRegistry.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/KeyRegistry.kt index 129e22fde..2247a62a8 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/KeyRegistry.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/KeyRegistry.kt @@ -1,55 +1,20 @@ package com.willfp.eco.internal.spigot.data -import com.willfp.eco.core.config.interfaces.Config import com.willfp.eco.core.data.keys.PersistentDataKey -import com.willfp.eco.core.data.keys.PersistentDataKeyType import org.bukkit.NamespacedKey -import java.math.BigDecimal object KeyRegistry { private val registry = mutableMapOf>() fun registerKey(key: PersistentDataKey<*>) { - if (this.registry.containsKey(key.key)) { - this.registry.remove(key.key) + if (key.defaultValue == null) { + throw IllegalArgumentException("Default value cannot be null!") } - validateKey(key) - this.registry[key.key] = key } fun getRegisteredKeys(): Set> { return registry.values.toSet() } - - private fun validateKey(key: PersistentDataKey) { - val default = key.defaultValue - - when (key.type) { - PersistentDataKeyType.INT -> if (default !is Int) { - throw IllegalArgumentException("Invalid Data Type! Should be Int") - } - PersistentDataKeyType.DOUBLE -> if (default !is Double) { - throw IllegalArgumentException("Invalid Data Type! Should be Double") - } - PersistentDataKeyType.BOOLEAN -> if (default !is Boolean) { - throw IllegalArgumentException("Invalid Data Type! Should be Boolean") - } - PersistentDataKeyType.STRING -> if (default !is String) { - throw IllegalArgumentException("Invalid Data Type! Should be String") - } - PersistentDataKeyType.STRING_LIST -> if (default !is List<*> || default.firstOrNull() !is String?) { - throw IllegalArgumentException("Invalid Data Type! Should be String List") - } - PersistentDataKeyType.CONFIG -> if (default !is Config) { - throw IllegalArgumentException("Invalid Data Type! Should be Config") - } - PersistentDataKeyType.BIG_DECIMAL -> if (default !is BigDecimal) { - throw IllegalArgumentException("Invalid Data Type! Should be BigDecimal") - } - - else -> throw NullPointerException("Null value found!") - } - } } diff --git a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/handlers/impl/MySQLPersistentDataHandler.kt b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/handlers/impl/MySQLPersistentDataHandler.kt index 9a7425230..9a7c88651 100644 --- a/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/handlers/impl/MySQLPersistentDataHandler.kt +++ b/eco-core/core-plugin/src/main/kotlin/com/willfp/eco/internal/spigot/data/handlers/impl/MySQLPersistentDataHandler.kt @@ -26,6 +26,7 @@ import org.jetbrains.exposed.sql.transactions.transaction import org.jetbrains.exposed.sql.upsert import java.math.BigDecimal import java.util.UUID +import kotlin.math.pow class MySQLPersistentDataHandler( config: Config @@ -241,18 +242,19 @@ class MySQLPersistentDataHandler( } private inline fun withRetries(action: () -> T): T { - var retries = 0 + var retries = 1 while (true) { try { return action() } catch (e: Exception) { - if (retries >= 5) { + if (retries > 5) { throw e } retries++ + // Exponential backoff runBlocking { - delay(10) + delay(2.0.pow(retries.toDouble()).toLong()) } } } diff --git a/eco-core/core-plugin/src/main/resources/config.yml b/eco-core/core-plugin/src/main/resources/config.yml index 8c422be2a..80b9a619a 100644 --- a/eco-core/core-plugin/src/main/resources/config.yml +++ b/eco-core/core-plugin/src/main/resources/config.yml @@ -7,7 +7,7 @@ # How player/server data is saved: # yaml - Stored in data.yml: Good option for single-node servers (i.e. no BungeeCord/Velocity) # mysql - Standard database, great option for multi-node servers (i.e. BungeeCord/Velocity) -# mongo - Alternative database, may suit some servers better +# mongo - Alternative database, great option for multi-node servers (i.e. BungeeCord/Velocity) data-handler: yaml # If data should be migrated automatically when changing data handler. @@ -35,7 +35,7 @@ mysql: port: 3306 database: database user: username - password: passy + password: p4ssw0rd # How many ticks to wait between committing data to a database. This doesn't # affect yaml storage, only MySQL and MongoDB. By default, data is committed